Formatting.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. namespace dokuwiki\Parsing\ParserMode;
  3. /**
  4. * This class sets the markup for bold (=strong),
  5. * italic (=emphasis), underline etc.
  6. */
  7. class Formatting extends AbstractMode
  8. {
  9. protected $type;
  10. protected $formatting = array(
  11. 'strong' => array(
  12. 'entry' => '\*\*(?=.*\*\*)',
  13. 'exit' => '\*\*',
  14. 'sort' => 70
  15. ),
  16. 'emphasis' => array(
  17. 'entry' => '//(?=[^\x00]*[^:])', //hack for bugs #384 #763 #1468
  18. 'exit' => '//',
  19. 'sort' => 80
  20. ),
  21. 'underline' => array(
  22. 'entry' => '__(?=.*__)',
  23. 'exit' => '__',
  24. 'sort' => 90
  25. ),
  26. 'monospace' => array(
  27. 'entry' => '\x27\x27(?=.*\x27\x27)',
  28. 'exit' => '\x27\x27',
  29. 'sort' => 100
  30. ),
  31. 'subscript' => array(
  32. 'entry' => '<sub>(?=.*</sub>)',
  33. 'exit' => '</sub>',
  34. 'sort' => 110
  35. ),
  36. 'superscript' => array(
  37. 'entry' => '<sup>(?=.*</sup>)',
  38. 'exit' => '</sup>',
  39. 'sort' => 120
  40. ),
  41. 'deleted' => array(
  42. 'entry' => '<del>(?=.*</del>)',
  43. 'exit' => '</del>',
  44. 'sort' => 130
  45. ),
  46. );
  47. /**
  48. * @param string $type
  49. */
  50. public function __construct($type)
  51. {
  52. global $PARSER_MODES;
  53. if (!array_key_exists($type, $this->formatting)) {
  54. trigger_error('Invalid formatting type ' . $type, E_USER_WARNING);
  55. }
  56. $this->type = $type;
  57. // formatting may contain other formatting but not it self
  58. $modes = $PARSER_MODES['formatting'];
  59. $key = array_search($type, $modes);
  60. if (is_int($key)) {
  61. unset($modes[$key]);
  62. }
  63. $this->allowedModes = array_merge(
  64. $modes,
  65. $PARSER_MODES['substition'],
  66. $PARSER_MODES['disabled']
  67. );
  68. }
  69. /** @inheritdoc */
  70. public function connectTo($mode)
  71. {
  72. // Can't nest formatting in itself
  73. if ($mode == $this->type) {
  74. return;
  75. }
  76. $this->Lexer->addEntryPattern(
  77. $this->formatting[$this->type]['entry'],
  78. $mode,
  79. $this->type
  80. );
  81. }
  82. /** @inheritdoc */
  83. public function postConnect()
  84. {
  85. $this->Lexer->addExitPattern(
  86. $this->formatting[$this->type]['exit'],
  87. $this->type
  88. );
  89. }
  90. /** @inheritdoc */
  91. public function getSort()
  92. {
  93. return $this->formatting[$this->type]['sort'];
  94. }
  95. }