Mode.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. /* Copyright (c) 2019 Geert Bergman (geert@scrivo.nl), highlight.php
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * 1. Redistributions of source code must retain the above copyright notice,
  8. * this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright notice,
  10. * this list of conditions and the following disclaimer in the documentation
  11. * and/or other materials provided with the distribution.
  12. * 3. Neither the name of "highlight.js", "highlight.php", nor the names of its
  13. * contributors may be used to endorse or promote products derived from this
  14. * software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  20. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  21. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  22. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  23. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  24. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  25. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  26. * POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. namespace Highlight;
  29. /**
  30. * A PHP representation of a Mode in the JS library.
  31. *
  32. * @internal
  33. *
  34. * @since 9.16.0.0
  35. * @mixin ModeDeprecations
  36. *
  37. * Language definition set via language definition JSON files
  38. *
  39. * @property bool $case_insensitive = false
  40. * @property string[] $aliases = array()
  41. * @property string|null $className = null
  42. * @property string|null $begin = null
  43. * @property RegEx|null $beginRe = null
  44. * @property string|null $end = null
  45. * @property RegEx|null $endRe = null
  46. * @property string|null $beginKeywords = null
  47. * @property bool $endsWithParent = false
  48. * @property bool $endsParent = false
  49. * @property bool $endSameAsBegin = false
  50. * @property string|null $lexemes = null
  51. * @property RegEx|null $lexemesRe = null
  52. * @property array<string, array<int, string|int>> $keywords = array()
  53. * @property string|null $illegal = null
  54. * @property RegEx|null $illegalRe = null
  55. * @property bool $excludeBegin = false
  56. * @property bool $excludeEnd = false
  57. * @property bool $returnBegin = false
  58. * @property bool $returnEnd = false
  59. * @property Mode[] $contains = array()
  60. * @property Mode|null $starts = null
  61. * @property Mode[] $variants = array()
  62. * @property int|null $relevance = null
  63. * @property string|string[]|null $subLanguage = null
  64. * @property bool $skip = false
  65. * @property bool $disableAutodetect = false
  66. *
  67. * Properties set at runtime by the language compilation process
  68. * @property array<int, Mode> $cachedVariants = array()
  69. * @property Terminators|null $terminators = null
  70. * @property string $terminator_end = ""
  71. * @property bool $compiled = false
  72. * @property Mode|null $parent = null
  73. * @property string $type = ''
  74. *
  75. * @see https://highlightjs.readthedocs.io/en/latest/reference.html
  76. */
  77. abstract class Mode extends \stdClass
  78. {
  79. /**
  80. * Fill in the missing properties that this Mode does not have.
  81. *
  82. * @internal
  83. *
  84. * @param \stdClass|null $obj
  85. *
  86. * @since 9.16.0.0
  87. *
  88. * @return void
  89. */
  90. public static function _normalize(&$obj)
  91. {
  92. // Don't overload our Modes if we've already normalized it
  93. if (isset($obj->__IS_COMPLETE)) {
  94. return;
  95. }
  96. if ($obj === null) {
  97. $obj = new \stdClass();
  98. }
  99. $patch = array(
  100. "begin" => true,
  101. "end" => true,
  102. "lexemes" => true,
  103. "illegal" => true,
  104. );
  105. // These values come in from JSON definition files
  106. $defaultValues = array(
  107. "case_insensitive" => false,
  108. "aliases" => array(),
  109. "className" => null,
  110. "begin" => null,
  111. "beginRe" => null,
  112. "end" => null,
  113. "endRe" => null,
  114. "beginKeywords" => null,
  115. "endsWithParent" => false,
  116. "endsParent" => false,
  117. "endSameAsBegin" => false,
  118. "lexemes" => null,
  119. "lexemesRe" => null,
  120. "keywords" => array(),
  121. "illegal" => null,
  122. "illegalRe" => null,
  123. "excludeBegin" => false,
  124. "excludeEnd" => false,
  125. "returnBegin" => false,
  126. "returnEnd" => false,
  127. "contains" => array(),
  128. "starts" => null,
  129. "variants" => array(),
  130. "relevance" => null,
  131. "subLanguage" => null,
  132. "skip" => false,
  133. "disableAutodetect" => false,
  134. );
  135. // These values are set at runtime
  136. $runTimeValues = array(
  137. "cachedVariants" => array(),
  138. "terminators" => null,
  139. "terminator_end" => "",
  140. "compiled" => false,
  141. "parent" => null,
  142. // This value is unique to highlight.php Modes
  143. "__IS_COMPLETE" => true,
  144. );
  145. foreach ($patch as $k => $v) {
  146. if (isset($obj->{$k})) {
  147. $obj->{$k} = str_replace("\\/", "/", $obj->{$k});
  148. $obj->{$k} = str_replace("/", "\\/", $obj->{$k});
  149. }
  150. }
  151. foreach ($defaultValues as $k => $v) {
  152. if (!isset($obj->{$k}) && is_object($obj)) {
  153. $obj->{$k} = $v;
  154. }
  155. }
  156. foreach ($runTimeValues as $k => $v) {
  157. if (is_object($obj)) {
  158. $obj->{$k} = $v;
  159. }
  160. }
  161. }
  162. /**
  163. * Set any deprecated properties values to their replacement values.
  164. *
  165. * @internal
  166. *
  167. * @param \stdClass $obj
  168. *
  169. * @return void
  170. */
  171. public static function _handleDeprecations(&$obj)
  172. {
  173. $deprecations = array(
  174. // @TODO Deprecated since 9.16.0.0; remove at 10.x
  175. 'caseInsensitive' => 'case_insensitive',
  176. 'terminatorEnd' => 'terminator_end',
  177. );
  178. foreach ($deprecations as $deprecated => $new) {
  179. $obj->{$deprecated} = &$obj->{$new};
  180. }
  181. }
  182. }