preventive.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. /**
  3. * Comment Syntax support for DokuWiki; preventive syntax component
  4. * preventive macro comment in Wiki source text.
  5. *
  6. * Assume invalid macro directive caused by a typo error as a source comment
  7. * in the page, eg. "~~NO CACHE~~" (correct sytax is "~~NOCACHE~~").
  8. * You may disable the directive temporary by intentional typo.
  9. *
  10. * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
  11. * @author Satoshi Sahara <sahara.satoshi@gmail.com>
  12. */
  13. class syntax_plugin_commentsyntax_preventive extends DokuWiki_Syntax_Plugin
  14. {
  15. /** syntax type */
  16. public function getType()
  17. {
  18. return 'substition';
  19. }
  20. /** sort number used to determine priority of this mode */
  21. public function getSort()
  22. {
  23. return 9999; // very low priority
  24. }
  25. /**
  26. * Connect lookup pattern to lexer
  27. */
  28. protected $mode, $pattern;
  29. public function preConnect()
  30. {
  31. // syntax mode, drop 'syntax_' from class name
  32. $this->mode = substr(__CLASS__, 7);
  33. // syntax pattern
  34. $this->pattern = [
  35. 5 => '~~[^\n~]+~~',
  36. ];
  37. }
  38. public function connectTo($mode)
  39. {
  40. $this->Lexer->addSpecialPattern($this->pattern[5], $mode, $this->mode);
  41. }
  42. /**
  43. * Handle the match
  44. */
  45. public function handle($match, $state, $pos, Doku_Handler $handler)
  46. {
  47. global $ID, $ACT;
  48. if ($ACT == 'preview') {
  49. return $data = $match;
  50. } else if ($this->getConf('log_invalid_macro')) {
  51. error_log($this->mode.': match='.$match.' |'.$ID);
  52. }
  53. return false;
  54. }
  55. /**
  56. * Create output
  57. */
  58. public function render($format, Doku_Renderer $renderer, $data)
  59. {
  60. global $ACT;
  61. if ($format == 'xhtml' && $ACT == 'preview') {
  62. $renderer->doc .= $renderer->_xmlEntities($data);
  63. }
  64. return true;
  65. }
  66. }