reference.rst 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. Mode reference
  2. ==============
  3. Types
  4. -----
  5. Types of attributes values in this reference:
  6. +------------+-------------------------------------------------------------------------------------+
  7. | identifier | String suitable to be used as a Javascript variable and CSS class name |
  8. | | (i.e. mostly ``/[A-Za-z0-9_]+/``) |
  9. +------------+-------------------------------------------------------------------------------------+
  10. | regexp | String representing a Javascript regexp. |
  11. | | Note that since it's not a literal regexp all back-slashes should be repeated twice |
  12. +------------+-------------------------------------------------------------------------------------+
  13. | boolean | Javascript boolean: ``true`` or ``false`` |
  14. +------------+-------------------------------------------------------------------------------------+
  15. | number | Javascript number |
  16. +------------+-------------------------------------------------------------------------------------+
  17. | object | Javascript object: ``{ ... }`` |
  18. +------------+-------------------------------------------------------------------------------------+
  19. | array | Javascript array: ``[ ... ]`` |
  20. +------------+-------------------------------------------------------------------------------------+
  21. Attributes
  22. ----------
  23. case_insensitive
  24. ^^^^^^^^^^^^^^^^
  25. **type**: boolean
  26. Case insensitivity of language keywords and regexps. Used only on the top-level mode.
  27. aliases
  28. ^^^^^^^
  29. **type**: array
  30. A list of additional names (besides the canonical one given by the filename) that can be used to identify a language in HTML classes and in a call to :ref:`getLanguage <getLanguage>`.
  31. className
  32. ^^^^^^^^^
  33. **type**: identifier
  34. The name of the mode. It is used as a class name in HTML markup.
  35. Multiple modes can have the same name. This is useful when a language has multiple variants of syntax
  36. for one thing like string in single or double quotes.
  37. begin
  38. ^^^^^
  39. **type**: regexp
  40. Regular expression starting a mode. For example a single quote for strings or two forward slashes for C-style comments.
  41. If absent, ``begin`` defaults to a regexp that matches anything, so the mode starts immediately.
  42. end
  43. ^^^
  44. **type**: regexp
  45. Regular expression ending a mode. For example a single quote for strings or "$" (end of line) for one-line comments.
  46. It's often the case that a beginning regular expression defines the entire mode and doesn't need any special ending.
  47. For example a number can be defined with ``begin: "\\b\\d+"`` which spans all the digits.
  48. If absent, ``end`` defaults to a regexp that matches anything, so the mode ends immediately.
  49. Sometimes a mode can end not by itself but implicitly with its containing (parent) mode.
  50. This is achieved with :ref:`endsWithParent <endsWithParent>` attribute.
  51. beginKeywords
  52. ^^^^^^^^^^^^^^^^
  53. **type**: string
  54. Used instead of ``begin`` for modes starting with keywords to avoid needless repetition:
  55. ::
  56. {
  57. begin: '\\b(extends|implements) ',
  58. keywords: 'extends implements'
  59. }
  60. … becomes:
  61. ::
  62. {
  63. beginKeywords: 'extends implements'
  64. }
  65. Unlike the :ref:`keywords <keywords>` attribute, this one allows only a simple list of space separated keywords.
  66. If you do need additional features of ``keywords`` or you just need more keywords for this mode you may include ``keywords`` along with ``beginKeywords``.
  67. .. _endsWithParent:
  68. endsWithParent
  69. ^^^^^^^^^^^^^^
  70. **type**: boolean
  71. A flag showing that a mode ends when its parent ends.
  72. This is best demonstrated by example. In CSS syntax a selector has a set of rules contained within symbols "{" and "}".
  73. Individual rules separated by ";" but the last one in a set can omit the terminating semicolon:
  74. ::
  75. p {
  76. width: 100%; color: red
  77. }
  78. This is when ``endsWithParent`` comes into play:
  79. ::
  80. {
  81. className: 'rules', begin: '{', end: '}',
  82. contains: [
  83. {className: 'rule', /* ... */ end: ';', endsWithParent: true}
  84. ]
  85. }
  86. .. _endsParent:
  87. endsParent
  88. ^^^^^^^^^^^^^^
  89. **type**: boolean
  90. Forces closing of the parent mode right after the current mode is closed.
  91. This is used for modes that don't have an easily expressible ending lexeme but
  92. instead could be closed after the last interesting sub-mode is found.
  93. Here's an example with two ways of defining functions in Elixir, one using a
  94. keyword ``do`` and another using a comma:
  95. ::
  96. def foo :clear, list do
  97. :ok
  98. end
  99. def foo, do: IO.puts "hello world"
  100. Note that in the first case the parameter list after the function title may also
  101. include a comma. And iIf we're only interested in highlighting a title we can
  102. tell it to end the function definition after itself:
  103. ::
  104. {
  105. className: 'function',
  106. beginKeywords: 'def', end: /\B\b/,
  107. contains: [
  108. {
  109. className: 'title',
  110. begin: hljs.IDENT_RE, endsParent: true
  111. }
  112. ]
  113. }
  114. (The ``end: /\B\b/`` regex tells function to never end by itself.)
  115. .. _lexemes:
  116. lexemes
  117. ^^^^^^^
  118. **type**: regexp
  119. A regular expression that extracts individual lexemes from language text to find :ref:`keywords <keywords>` among them.
  120. Default value is ``hljs.IDENT_RE`` which works for most languages.
  121. .. _keywords:
  122. keywords
  123. ^^^^^^^^
  124. **type**: object
  125. Keyword definition comes in two forms:
  126. * ``'for while if else weird_voodoo|10 ... '`` -- a string of space-separated keywords with an optional relevance over a pipe
  127. * ``{'keyword': ' ... ', 'literal': ' ... '}`` -- an object whose keys are names of different kinds of keywords and values are keyword definition strings in the first form
  128. For detailed explanation see :doc:`Language definition guide </language-guide>`.
  129. illegal
  130. ^^^^^^^
  131. **type**: regexp
  132. A regular expression that defines symbols illegal for the mode.
  133. When the parser finds a match for illegal expression it immediately drops parsing the whole language altogether.
  134. excludeBegin, excludeEnd
  135. ^^^^^^^^^^^^^^^^^^^^^^^^
  136. **type**: boolean
  137. Exclude beginning or ending lexemes out of mode's generated markup. For example in CSS syntax a rule ends with a semicolon.
  138. However visually it's better not to color it as the rule contents. Having ``excludeEnd: true`` forces a ``<span>`` element for the rule to close before the semicolon.
  139. returnBegin
  140. ^^^^^^^^^^^
  141. **type**: boolean
  142. Returns just found beginning lexeme back into parser. This is used when beginning of a sub-mode is a complex expression
  143. that should not only be found within a parent mode but also parsed according to the rules of a sub-mode.
  144. Since the parser is effectively goes back it's quite possible to create a infinite loop here so use with caution!
  145. returnEnd
  146. ^^^^^^^^^
  147. **type**: boolean
  148. Returns just found ending lexeme back into parser. This is used for example to parse Javascript embedded into HTML.
  149. A Javascript block ends with the HTML closing tag ``</script>`` that cannot be parsed with Javascript rules.
  150. So it is returned back into its parent HTML mode that knows what to do with it.
  151. Since the parser is effectively goes back it's quite possible to create a infinite loop here so use with caution!
  152. contains
  153. ^^^^^^^^
  154. **type**: array
  155. The list of sub-modes that can be found inside the mode. For detailed explanation see :doc:`Language definition guide </language-guide>`.
  156. starts
  157. ^^^^^^
  158. **type**: identifier
  159. The name of the mode that will start right after the current mode ends. The new mode won't be contained within the current one.
  160. Currently this attribute is used to highlight Javascript and CSS contained within HTML.
  161. Tags ``<script>`` and ``<style>`` start sub-modes that use another language definition to parse their contents (see :ref:`subLanguage`).
  162. variants
  163. ^^^^^^^^
  164. **type**: array
  165. Modification to the main definitions of the mode, effectively expanding it into several similar modes
  166. each having all the attributes from the main definition augmented or overridden by the variants::
  167. {
  168. className: 'string',
  169. contains: [hljs.BACKSLASH_ESCAPE],
  170. relevance: 0,
  171. variants: [
  172. {begin: /"/, end: /"/},
  173. {begin: /'/, end: /'/, relevance: 1}
  174. ]
  175. }
  176. .. _subLanguage:
  177. subLanguage
  178. ^^^^^^^^^^^
  179. **type**: string or array
  180. Highlights the entire contents of the mode with another language.
  181. When using this attribute there's no point to define internal parsing rules like :ref:`lexemes` or :ref:`keywords`. Also it is recommended to skip ``className`` attribute since the sublanguage will wrap the text in its own ``<span class="language-name">``.
  182. The value of the attribute controls which language or languages will be used for highlighting:
  183. * language name: explicit highlighting with the specified language
  184. * empty array: auto detection with all the languages available
  185. * array of language names: auto detection constrained to the specified set
  186. skip
  187. ^^^^
  188. **type**: boolean
  189. Skips any markup processing for the mode ensuring that it remains a part of its
  190. parent buffer along with the starting and the ending lexemes. This works in
  191. conjunction with the parent's :ref:`subLanguage` when it requires complex
  192. parsing.
  193. Consider parsing PHP inside HTML::
  194. <p><? echo 'PHP'; /* ?> */ ?></p>
  195. The ``?>`` inside the comment should **not** end the PHP part, so we have to
  196. handle pairs of ``/* .. */`` to correctly find the ending ``?>``::
  197. {
  198. begin: /<\?/, end: /\?>/,
  199. subLanguage: 'php',
  200. contains: [{begin: '/\\*', end: '\\*/', skip: true}]
  201. }
  202. Without ``skip: true`` every comment would cause the parser to drop out back
  203. into the HTML mode.