hooks.php 3.3 KB

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