You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

111 lines
2.5 KiB

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