prism-asciidoc.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. (function (Prism) {
  2. var attributes = {
  3. pattern: /(^[ \t]*)\[(?!\[)(?:(["'$`])(?:(?!\2)[^\\]|\\.)*\2|\[(?:[^\[\]\\]|\\.)*\]|[^\[\]\\"'$`]|\\.)*\]/m,
  4. lookbehind: true,
  5. inside: {
  6. 'quoted': {
  7. pattern: /([$`])(?:(?!\1)[^\\]|\\.)*\1/,
  8. inside: {
  9. 'punctuation': /^[$`]|[$`]$/
  10. }
  11. },
  12. 'interpreted': {
  13. pattern: /'(?:[^'\\]|\\.)*'/,
  14. inside: {
  15. 'punctuation': /^'|'$/
  16. // See rest below
  17. }
  18. },
  19. 'string': /"(?:[^"\\]|\\.)*"/,
  20. 'variable': /\w+(?==)/,
  21. 'punctuation': /^\[|\]$|,/,
  22. 'operator': /=/,
  23. // The negative look-ahead prevents blank matches
  24. 'attr-value': /(?!^\s+$).+/
  25. }
  26. };
  27. var asciidoc = Prism.languages.asciidoc = {
  28. 'comment-block': {
  29. pattern: /^(\/{4,})$[\s\S]*?^\1/m,
  30. alias: 'comment'
  31. },
  32. 'table': {
  33. pattern: /^\|={3,}(?:(?:\r?\n|\r(?!\n)).*)*?(?:\r?\n|\r)\|={3,}$/m,
  34. inside: {
  35. 'specifiers': {
  36. pattern: /(?:(?:(?:\d+(?:\.\d+)?|\.\d+)[+*](?:[<^>](?:\.[<^>])?|\.[<^>])?|[<^>](?:\.[<^>])?|\.[<^>])[a-z]*|[a-z]+)(?=\|)/,
  37. alias: 'attr-value'
  38. },
  39. 'punctuation': {
  40. pattern: /(^|[^\\])[|!]=*/,
  41. lookbehind: true
  42. }
  43. // See rest below
  44. }
  45. },
  46. 'passthrough-block': {
  47. pattern: /^(\+{4,})$[\s\S]*?^\1$/m,
  48. inside: {
  49. 'punctuation': /^\++|\++$/
  50. // See rest below
  51. }
  52. },
  53. // Literal blocks and listing blocks
  54. 'literal-block': {
  55. pattern: /^(-{4,}|\.{4,})$[\s\S]*?^\1$/m,
  56. inside: {
  57. 'punctuation': /^(?:-+|\.+)|(?:-+|\.+)$/
  58. // See rest below
  59. }
  60. },
  61. // Sidebar blocks, quote blocks, example blocks and open blocks
  62. 'other-block': {
  63. pattern: /^(--|\*{4,}|_{4,}|={4,})$[\s\S]*?^\1$/m,
  64. inside: {
  65. 'punctuation': /^(?:-+|\*+|_+|=+)|(?:-+|\*+|_+|=+)$/
  66. // See rest below
  67. }
  68. },
  69. // list-punctuation and list-label must appear before indented-block
  70. 'list-punctuation': {
  71. pattern: /(^[ \t]*)(?:-|\*{1,5}|\.{1,5}|(?:[a-z]|\d+)\.|[xvi]+\))(?= )/im,
  72. lookbehind: true,
  73. alias: 'punctuation'
  74. },
  75. 'list-label': {
  76. pattern: /(^[ \t]*)[a-z\d].+(?::{2,4}|;;)(?=\s)/im,
  77. lookbehind: true,
  78. alias: 'symbol'
  79. },
  80. 'indented-block': {
  81. pattern: /((\r?\n|\r)\2)([ \t]+)\S.*(?:(?:\r?\n|\r)\3.+)*(?=\2{2}|$)/,
  82. lookbehind: true
  83. },
  84. 'comment': /^\/\/.*/m,
  85. 'title': {
  86. pattern: /^.+(?:\r?\n|\r)(?:={3,}|-{3,}|~{3,}|\^{3,}|\+{3,})$|^={1,5} .+|^\.(?![\s.]).*/m,
  87. alias: 'important',
  88. inside: {
  89. 'punctuation': /^(?:\.|=+)|(?:=+|-+|~+|\^+|\++)$/
  90. // See rest below
  91. }
  92. },
  93. 'attribute-entry': {
  94. pattern: /^:[^:\r\n]+:(?: .*?(?: \+(?:\r?\n|\r).*?)*)?$/m,
  95. alias: 'tag'
  96. },
  97. 'attributes': attributes,
  98. 'hr': {
  99. pattern: /^'{3,}$/m,
  100. alias: 'punctuation'
  101. },
  102. 'page-break': {
  103. pattern: /^<{3,}$/m,
  104. alias: 'punctuation'
  105. },
  106. 'admonition': {
  107. pattern: /^(?:CAUTION|IMPORTANT|NOTE|TIP|WARNING):/m,
  108. alias: 'keyword'
  109. },
  110. 'callout': [
  111. {
  112. pattern: /(^[ \t]*)<?\d*>/m,
  113. lookbehind: true,
  114. alias: 'symbol'
  115. },
  116. {
  117. pattern: /<\d+>/,
  118. alias: 'symbol'
  119. }
  120. ],
  121. 'macro': {
  122. pattern: /\b[a-z\d][a-z\d-]*::?(?:[^\s\[\]]*\[(?:[^\]\\"']|(["'])(?:(?!\1)[^\\]|\\.)*\1|\\.)*\])/,
  123. inside: {
  124. 'function': /^[a-z\d-]+(?=:)/,
  125. 'punctuation': /^::?/,
  126. 'attributes': {
  127. pattern: /(?:\[(?:[^\]\\"']|(["'])(?:(?!\1)[^\\]|\\.)*\1|\\.)*\])/,
  128. inside: attributes.inside
  129. }
  130. }
  131. },
  132. 'inline': {
  133. /*
  134. The initial look-behind prevents the highlighting of escaped quoted text.
  135. Quoted text can be multi-line but cannot span an empty line.
  136. All quoted text can have attributes before [foobar, 'foobar', baz="bar"].
  137. First, we handle the constrained quotes.
  138. Those must be bounded by non-word chars and cannot have spaces between the delimiter and the first char.
  139. They are, in order: _emphasis_, ``double quotes'', `single quotes', `monospace`, 'emphasis', *strong*, +monospace+ and #unquoted#
  140. Then we handle the unconstrained quotes.
  141. Those do not have the restrictions of the constrained quotes.
  142. They are, in order: __emphasis__, **strong**, ++monospace++, +++passthrough+++, ##unquoted##, $$passthrough$$, ~subscript~, ^superscript^, {attribute-reference}, [[anchor]], [[[bibliography anchor]]], <<xref>>, (((indexes))) and ((indexes))
  143. */
  144. pattern: /(^|[^\\])(?:(?:\B\[(?:[^\]\\"']|(["'])(?:(?!\2)[^\\]|\\.)*\2|\\.)*\])?(?:\b_(?!\s)(?: _|[^_\\\r\n]|\\.)+(?:(?:\r?\n|\r)(?: _|[^_\\\r\n]|\\.)+)*_\b|\B``(?!\s).+?(?:(?:\r?\n|\r).+?)*''\B|\B`(?!\s)(?:[^`'\s]|\s+\S)+['`]\B|\B(['*+#])(?!\s)(?: \3|(?!\3)[^\\\r\n]|\\.)+(?:(?:\r?\n|\r)(?: \3|(?!\3)[^\\\r\n]|\\.)+)*\3\B)|(?:\[(?:[^\]\\"']|(["'])(?:(?!\4)[^\\]|\\.)*\4|\\.)*\])?(?:(__|\*\*|\+\+\+?|##|\$\$|[~^]).+?(?:(?:\r?\n|\r).+?)*\5|\{[^}\r\n]+\}|\[\[\[?.+?(?:(?:\r?\n|\r).+?)*\]?\]\]|<<.+?(?:(?:\r?\n|\r).+?)*>>|\(\(\(?.+?(?:(?:\r?\n|\r).+?)*\)?\)\)))/m,
  145. lookbehind: true,
  146. inside: {
  147. 'attributes': attributes,
  148. 'url': {
  149. pattern: /^(?:\[\[\[?.+?\]?\]\]|<<.+?>>)$/,
  150. inside: {
  151. 'punctuation': /^(?:\[\[\[?|<<)|(?:\]\]\]?|>>)$/
  152. }
  153. },
  154. 'attribute-ref': {
  155. pattern: /^\{.+\}$/,
  156. inside: {
  157. 'variable': {
  158. pattern: /(^\{)[a-z\d,+_-]+/,
  159. lookbehind: true
  160. },
  161. 'operator': /^[=?!#%@$]|!(?=[:}])/,
  162. 'punctuation': /^\{|\}$|::?/
  163. }
  164. },
  165. 'italic': {
  166. pattern: /^(['_])[\s\S]+\1$/,
  167. inside: {
  168. 'punctuation': /^(?:''?|__?)|(?:''?|__?)$/
  169. }
  170. },
  171. 'bold': {
  172. pattern: /^\*[\s\S]+\*$/,
  173. inside: {
  174. punctuation: /^\*\*?|\*\*?$/
  175. }
  176. },
  177. 'punctuation': /^(?:``?|\+{1,3}|##?|\$\$|[~^]|\(\(\(?)|(?:''?|\+{1,3}|##?|\$\$|[~^`]|\)?\)\))$/
  178. }
  179. },
  180. 'replacement': {
  181. pattern: /\((?:C|R|TM)\)/,
  182. alias: 'builtin'
  183. },
  184. 'entity': /&#?[\da-z]{1,8};/i,
  185. 'line-continuation': {
  186. pattern: /(^| )\+$/m,
  187. lookbehind: true,
  188. alias: 'punctuation'
  189. }
  190. };
  191. // Allow some nesting. There is no recursion though, so cloning should not be needed.
  192. function copyFromAsciiDoc(keys) {
  193. keys = keys.split(' ');
  194. var o = {};
  195. for (var i = 0, l = keys.length; i < l; i++) {
  196. o[keys[i]] = asciidoc[keys[i]];
  197. }
  198. return o;
  199. }
  200. attributes.inside['interpreted'].inside.rest = copyFromAsciiDoc('macro inline replacement entity');
  201. asciidoc['passthrough-block'].inside.rest = copyFromAsciiDoc('macro');
  202. asciidoc['literal-block'].inside.rest = copyFromAsciiDoc('callout');
  203. asciidoc['table'].inside.rest = copyFromAsciiDoc('comment-block passthrough-block literal-block other-block list-punctuation indented-block comment title attribute-entry attributes hr page-break admonition list-label callout macro inline replacement entity line-continuation');
  204. asciidoc['other-block'].inside.rest = copyFromAsciiDoc('table list-punctuation indented-block comment attribute-entry attributes hr page-break admonition list-label macro inline replacement entity line-continuation');
  205. asciidoc['title'].inside.rest = copyFromAsciiDoc('macro inline replacement entity');
  206. // Plugin to make entity title show the real entity, idea by Roman Komarov
  207. Prism.hooks.add('wrap', function (env) {
  208. if (env.type === 'entity') {
  209. env.attributes['title'] = env.content.replace(/&amp;/, '&');
  210. }
  211. });
  212. Prism.languages.adoc = Prism.languages.asciidoc;
  213. }(Prism));