htmlcomment.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. /**
  3. * Comment Syntax support for DokuWiki; html comment syntax component
  4. * allows HTML comments to be retained in the output. The HTML comment will not
  5. * rendered by the browser, but can be viewed with “View source code” command.
  6. *
  7. * Note: adopted original HTML Comment Plugin by Christopher Arndt
  8. * https://www.dokuwiki.org/plugin:htmlcomment
  9. *
  10. * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
  11. * @author Christopher Arndt <chris@chrisarndt.de>
  12. * @author Danny Lin <danny0838@gmail.com>
  13. * @author Satoshi Sahara <sahara.satoshi@gmail.com>
  14. */
  15. class syntax_plugin_commentsyntax_htmlcomment extends DokuWiki_Syntax_Plugin
  16. {
  17. /** syntax type */
  18. public function getType()
  19. {
  20. return 'substition';
  21. }
  22. /** sort number used to determine priority of this mode */
  23. public function getSort()
  24. {
  25. return 325;
  26. }
  27. /**
  28. * Connect lookup pattern to lexer
  29. */
  30. protected $mode, $pattern;
  31. public function preConnect()
  32. {
  33. // syntax mode, drop 'syntax_' from class name
  34. $this->mode = substr(__CLASS__, 7);
  35. // syntax pattern
  36. $this->pattern = [
  37. 5 => '<\!--.*?-->',
  38. ];
  39. }
  40. public function connectTo($mode)
  41. {
  42. $this->Lexer->addSpecialPattern($this->pattern[5], $mode, $this->mode);
  43. }
  44. /**
  45. * Handle the match
  46. */
  47. public function handle($match, $state, $pos, Doku_Handler $handler)
  48. {
  49. if ($state == DOKU_LEXER_SPECIAL) {
  50. // strip <!-- from start and --> from end
  51. return array($state, substr($match, 4, -3));
  52. }
  53. return false;
  54. }
  55. /**
  56. * Create output
  57. */
  58. public function render($format, Doku_Renderer $renderer, $data)
  59. {
  60. if ($format == 'xhtml') {
  61. list($state, $comment) = $data;
  62. if ($state == DOKU_LEXER_SPECIAL) {
  63. $renderer->doc .= '<!--'.$comment.'-->';
  64. }
  65. return true;
  66. }
  67. return true;
  68. }
  69. }