hooks.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. $bMarkdown = false;
  3. use League\HTMLToMarkdown\HtmlConverter;
  4. $oConverter = null;
  5. if ($bMarkdown) {
  6. require 'vendor/autoload.php';
  7. $oConverter = new HtmlConverter();
  8. }
  9. $sBaseDocUrl = 'https://ci.una.io/docs/';
  10. $sXml = file_get_contents('xml/hook.xml'); // xml folder should contain XML output from doxygen
  11. $oHooks = new SimpleXMLElement($sXml);
  12. $i = 0;
  13. $aOutput = [];
  14. $aUrls = [];
  15. foreach ($oHooks->xpath('//listitem/para/ref') as $oRef) {
  16. if (!$oRef->asXML())
  17. continue;
  18. // get hook reference id
  19. $sKey = dom_import_simplexml($oRef)->nodeValue;
  20. // generate URL with hook description and anchor
  21. $a = explode('_1', $oRef['refid'], 2);
  22. $sUrl = $sBaseDocUrl . "{$a[0]}.html";
  23. $sAnchor = $a[1];
  24. // get page content with hook desription
  25. $s = '';
  26. if (!isset($aUrls[$sUrl])) {
  27. $s = file_get_contents($sUrl);
  28. $aUrls[$sUrl] = $s;
  29. }
  30. else {
  31. $s = $aUrls[$sUrl];
  32. }
  33. // parse page content to get hook snippet
  34. $iStart = mb_strpos($s, $sAnchor);
  35. if ($iStart === false)
  36. continue;
  37. $iEnd = mb_strpos($s, '<dl class="hook"><dt><b><a class="el" href="hook.html', $iStart);
  38. $sSnippetHtml = mb_substr($s, $iStart - 26, $iEnd - $iStart + 26);
  39. if ($bMarkdown)
  40. $sSnippetMd = $oConverter->convert($sSnippetHtml);
  41. // fix broken HTML
  42. $oDoc = new DOMDocument();
  43. @$oDoc->loadHTML($sSnippetHtml);
  44. $sSnippetHtmlFixed = $oDoc->saveHTML();
  45. $iBodyStart = mb_strpos($sSnippetHtmlFixed, '<body>');
  46. $iBodyEnd = mb_strpos($sSnippetHtmlFixed, '</body>');
  47. if ($iBodyStart !== false && $iBodyEnd !== false)
  48. $sSnippetHtmlFixed = mb_substr($sSnippetHtmlFixed, $iBodyStart + 6, $iBodyEnd - $iBodyStart - 6);
  49. // fix links
  50. $sSnippetHtmlFixed = preg_replace_callback('/href="(.*?)"/', function ($m) use ($sBaseDocUrl) {
  51. if (false === ($i = mb_strpos($m[1], '#hook-')))
  52. return 'href="' . $sBaseDocUrl . $m[1] . '"';
  53. else
  54. return 'href="' . mb_substr($m[1], -mb_strlen($m[1]) + $i) . '"';
  55. }, $sSnippetHtmlFixed);
  56. if ($bMarkdown) {
  57. $sSnippetMd = preg_replace_callback('/\((.*?\.html.*?)\)/', function ($m) use ($sBaseDocUrl) {
  58. if (false === ($i = mb_strpos($m[1], '#hook-')))
  59. return '(' . $sBaseDocUrl . $m[1] . ')';
  60. else
  61. return '(' . mb_substr($m[1], -mb_strlen($m[1]) + $i) . ')';
  62. }, $sSnippetMd);
  63. }
  64. // add to output
  65. if (isset($aOutput[$sKey]))
  66. echo "\nERROR: duplicate hook detected - " . $sKey . " <br />\n";
  67. $aOutput[$sKey] = $bMarkdown ? $sSnippetMd : $sSnippetHtmlFixed;
  68. ++$i;
  69. // if ($i>10)
  70. // break;
  71. }
  72. // sort hooks
  73. asort($aOutput);
  74. // output
  75. echo "<h1>$i Hooks</h1><hr />\n\n";
  76. if ($bMarkdown) {
  77. echo '<pre>' . implode("\n\n", $aOutput) . '</pre>';
  78. } else {
  79. echo implode("\n\n", $aOutput);
  80. }