reference.rst 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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 (after possibly
  49. matching any ``contains`` sub-modes).
  50. Sometimes a mode can end not by itself but implicitly with its containing (parent) mode.
  51. This is achieved with :ref:`endsWithParent <endsWithParent>` attribute.
  52. beginKeywords
  53. ^^^^^^^^^^^^^^^^
  54. **type**: string
  55. Used instead of ``begin`` for modes starting with keywords to avoid needless repetition:
  56. ::
  57. {
  58. begin: '\\b(extends|implements) ',
  59. keywords: 'extends implements'
  60. }
  61. … becomes:
  62. ::
  63. {
  64. beginKeywords: 'extends implements'
  65. }
  66. Unlike the :ref:`keywords <keywords>` attribute, this one allows only a simple list of space separated keywords.
  67. If you do need additional features of ``keywords`` or you just need more keywords for this mode you may include ``keywords`` along with ``beginKeywords``.
  68. .. _endsWithParent:
  69. endsWithParent
  70. ^^^^^^^^^^^^^^
  71. **type**: boolean
  72. A flag showing that a mode ends when its parent ends.
  73. This is best demonstrated by example. In CSS syntax a selector has a set of rules contained within symbols "{" and "}".
  74. Individual rules separated by ";" but the last one in a set can omit the terminating semicolon:
  75. ::
  76. p {
  77. width: 100%; color: red
  78. }
  79. This is when ``endsWithParent`` comes into play:
  80. ::
  81. {
  82. className: 'rules', begin: '{', end: '}',
  83. contains: [
  84. {className: 'rule', /* ... */ end: ';', endsWithParent: true}
  85. ]
  86. }
  87. .. _endsParent:
  88. endsParent
  89. ^^^^^^^^^^^^^^
  90. **type**: boolean
  91. Forces closing of the parent mode right after the current mode is closed.
  92. This is used for modes that don't have an easily expressible ending lexeme but
  93. instead could be closed after the last interesting sub-mode is found.
  94. Here's an example with two ways of defining functions in Elixir, one using a
  95. keyword ``do`` and another using a comma:
  96. ::
  97. def foo :clear, list do
  98. :ok
  99. end
  100. def foo, do: IO.puts "hello world"
  101. Note that in the first case the parameter list after the function title may also
  102. include a comma. And if we're only interested in highlighting a title we can
  103. tell it to end the function definition after itself:
  104. ::
  105. {
  106. className: 'function',
  107. beginKeywords: 'def', end: /\B\b/,
  108. contains: [
  109. {
  110. className: 'title',
  111. begin: hljs.IDENT_RE, endsParent: true
  112. }
  113. ]
  114. }
  115. (The ``end: /\B\b/`` regex tells function to never end by itself.)
  116. .. _endSameAsBegin:
  117. endSameAsBegin
  118. ^^^^^^^^^^^^^^
  119. **type**: boolean
  120. Acts as ``end`` matching exactly the same string that was found by the
  121. corresponding ``begin`` regexp.
  122. For example, in PostgreSQL string constants can uee "dollar quotes",
  123. consisting of a dollar sign, an optional tag of zero or more characters,
  124. and another dollar sign. String constant must be ended with the same
  125. construct using the same tag. It is possible to nest dollar-quoted string
  126. constants by choosing different tags at each nesting level:
  127. ::
  128. $foo$
  129. ...
  130. $bar$ nested $bar$
  131. ...
  132. $foo$
  133. In this case you can't simply specify the same regexp for ``begin`` and
  134. ``end`` (say, ``"\\$[a-z]\\$"``), but you can use ``begin: "\\$[a-z]\\$"``
  135. and ``endSameAsBegin: true``.
  136. .. _lexemes:
  137. lexemes
  138. ^^^^^^^
  139. **type**: regexp
  140. A regular expression that extracts individual lexemes from language text to find :ref:`keywords <keywords>` among them.
  141. Default value is ``hljs.IDENT_RE`` which works for most languages.
  142. .. _keywords:
  143. keywords
  144. ^^^^^^^^
  145. **type**: object
  146. Keyword definition comes in two forms:
  147. * ``'for while if else weird_voodoo|10 ... '`` -- a string of space-separated keywords with an optional relevance over a pipe
  148. * ``{'keyword': ' ... ', 'literal': ' ... '}`` -- an object whose keys are names of different kinds of keywords and values are keyword definition strings in the first form
  149. For detailed explanation see :doc:`Language definition guide </language-guide>`.
  150. illegal
  151. ^^^^^^^
  152. **type**: regexp
  153. A regular expression that defines symbols illegal for the mode.
  154. When the parser finds a match for illegal expression it immediately drops parsing the whole language altogether.
  155. excludeBegin, excludeEnd
  156. ^^^^^^^^^^^^^^^^^^^^^^^^
  157. **type**: boolean
  158. Exclude beginning or ending lexemes out of mode's generated markup. For example in CSS syntax a rule ends with a semicolon.
  159. 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.
  160. returnBegin
  161. ^^^^^^^^^^^
  162. **type**: boolean
  163. Returns just found beginning lexeme back into parser. This is used when beginning of a sub-mode is a complex expression
  164. that should not only be found within a parent mode but also parsed according to the rules of a sub-mode.
  165. Since the parser is effectively goes back it's quite possible to create a infinite loop here so use with caution!
  166. returnEnd
  167. ^^^^^^^^^
  168. **type**: boolean
  169. Returns just found ending lexeme back into parser. This is used for example to parse Javascript embedded into HTML.
  170. A Javascript block ends with the HTML closing tag ``</script>`` that cannot be parsed with Javascript rules.
  171. So it is returned back into its parent HTML mode that knows what to do with it.
  172. Since the parser is effectively goes back it's quite possible to create a infinite loop here so use with caution!
  173. contains
  174. ^^^^^^^^
  175. **type**: array
  176. The list of sub-modes that can be found inside the mode. For detailed explanation see :doc:`Language definition guide </language-guide>`.
  177. starts
  178. ^^^^^^
  179. **type**: identifier
  180. 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.
  181. Currently this attribute is used to highlight Javascript and CSS contained within HTML.
  182. Tags ``<script>`` and ``<style>`` start sub-modes that use another language definition to parse their contents (see :ref:`subLanguage`).
  183. variants
  184. ^^^^^^^^
  185. **type**: array
  186. Modification to the main definitions of the mode, effectively expanding it into several similar modes
  187. each having all the attributes from the main definition augmented or overridden by the variants::
  188. {
  189. className: 'string',
  190. contains: [hljs.BACKSLASH_ESCAPE],
  191. relevance: 0,
  192. variants: [
  193. {begin: /"/, end: /"/},
  194. {begin: /'/, end: /'/, relevance: 1}
  195. ]
  196. }
  197. .. _subLanguage:
  198. subLanguage
  199. ^^^^^^^^^^^
  200. **type**: string or array
  201. Highlights the entire contents of the mode with another language.
  202. 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">``.
  203. The value of the attribute controls which language or languages will be used for highlighting:
  204. * language name: explicit highlighting with the specified language
  205. * empty array: auto detection with all the languages available
  206. * array of language names: auto detection constrained to the specified set
  207. skip
  208. ^^^^
  209. **type**: boolean
  210. Skips any markup processing for the mode ensuring that it remains a part of its
  211. parent buffer along with the starting and the ending lexemes. This works in
  212. conjunction with the parent's :ref:`subLanguage` when it requires complex
  213. parsing.
  214. Consider parsing PHP inside HTML::
  215. <p><? echo 'PHP'; /* ?> */ ?></p>
  216. The ``?>`` inside the comment should **not** end the PHP part, so we have to
  217. handle pairs of ``/* .. */`` to correctly find the ending ``?>``::
  218. {
  219. begin: /<\?/, end: /\?>/,
  220. subLanguage: 'php',
  221. contains: [{begin: '/\\*', end: '\\*/', skip: true}]
  222. }
  223. Without ``skip: true`` every comment would cause the parser to drop out back
  224. into the HTML mode.
  225. disableAutodetect
  226. ^^^^^^^^^^^^^^^^^
  227. **type**: boolean
  228. Disables autodetection for this language.