asciidoc_highlight_rules.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Distributed under the BSD license:
  3. *
  4. * Copyright (c) 2010, Ajax.org B.V.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * * Neither the name of Ajax.org B.V. nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL AJAX.ORG B.V. BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * ***** END LICENSE BLOCK ***** */
  30. define(function(require, exports, module) {
  31. "use strict";
  32. var oop = require("../lib/oop");
  33. var TextHighlightRules = require("./text_highlight_rules").TextHighlightRules;
  34. var AsciidocHighlightRules = function() {
  35. var identifierRe = "[a-zA-Z\u00a1-\uffff]+\\b";
  36. this.$rules = {
  37. "start": [
  38. {token: "empty", regex: /$/},
  39. {token: "literal", regex: /^\.{4,}\s*$/, next: "listingBlock"},
  40. {token: "literal", regex: /^-{4,}\s*$/, next: "literalBlock"},
  41. {token: "string", regex: /^\+{4,}\s*$/, next: "passthroughBlock"},
  42. {token: "keyword", regex: /^={4,}\s*$/},
  43. {token: "text", regex: /^\s*$/},
  44. // immediately return to the start mode without matching anything
  45. {token: "empty", regex: "", next: "dissallowDelimitedBlock"}
  46. ],
  47. "dissallowDelimitedBlock": [
  48. {include: "paragraphEnd"},
  49. {token: "comment", regex: '^//.+$'},
  50. {token: "keyword", regex: "^(?:NOTE|TIP|IMPORTANT|WARNING|CAUTION):"},
  51. {include: "listStart"},
  52. {token: "literal", regex: /^\s+.+$/, next: "indentedBlock"},
  53. {token: "empty", regex: "", next: "text"}
  54. ],
  55. "paragraphEnd": [
  56. {token: "doc.comment", regex: /^\/{4,}\s*$/, next: "commentBlock"},
  57. {token: "tableBlock", regex: /^\s*[|!]=+\s*$/, next: "tableBlock"},
  58. // open block, ruller
  59. {token: "keyword", regex: /^(?:--|''')\s*$/, next: "start"},
  60. {token: "option", regex: /^\[.*\]\s*$/, next: "start"},
  61. {token: "pageBreak", regex: /^>{3,}$/, next: "start"},
  62. {token: "literal", regex: /^\.{4,}\s*$/, next: "listingBlock"},
  63. {token: "titleUnderline", regex: /^(?:={2,}|-{2,}|~{2,}|\^{2,}|\+{2,})\s*$/, next: "start"},
  64. {token: "singleLineTitle", regex: /^={1,5}\s+\S.*$/, next: "start"},
  65. {token: "otherBlock", regex: /^(?:\*{2,}|_{2,})\s*$/, next: "start"},
  66. // .optional title
  67. {token: "optionalTitle", regex: /^\.[^.\s].+$/, next: "start"}
  68. ],
  69. "listStart": [
  70. {token: "keyword", regex: /^\s*(?:\d+\.|[a-zA-Z]\.|[ixvmIXVM]+\)|\*{1,5}|-|\.{1,5})\s/, next: "listText"},
  71. {token: "meta.tag", regex: /^.+(?::{2,4}|;;)(?: |$)/, next: "listText"},
  72. {token: "support.function.list.callout", regex: /^(?:<\d+>|\d+>|>) /, next: "text"},
  73. // continuation
  74. {token: "keyword", regex: /^\+\s*$/, next: "start"}
  75. ],
  76. "text": [
  77. {token: ["link", "variable.language"], regex: /((?:https?:\/\/|ftp:\/\/|file:\/\/|mailto:|callto:)[^\s\[]+)(\[.*?\])/},
  78. {token: "link", regex: /(?:https?:\/\/|ftp:\/\/|file:\/\/|mailto:|callto:)[^\s\[]+/},
  79. {token: "link", regex: /\b[\w\.\/\-]+@[\w\.\/\-]+\b/},
  80. {include: "macros"},
  81. {include: "paragraphEnd"},
  82. {token: "literal", regex:/\+{3,}/, next:"smallPassthrough"},
  83. {token: "escape", regex: /\((?:C|TM|R)\)|\.{3}|->|<-|=>|<=|&#(?:\d+|x[a-fA-F\d]+);|(?: |^)--(?=\s+\S)/},
  84. {token: "escape", regex: /\\[_*'`+#]|\\{2}[_*'`+#]{2}/},
  85. {token: "keyword", regex: /\s\+$/},
  86. // any word
  87. {token: "text", regex: identifierRe},
  88. {token: ["keyword", "string", "keyword"],
  89. regex: /(<<[\w\d\-$]+,)(.*?)(>>|$)/},
  90. {token: "keyword", regex: /<<[\w\d\-$]+,?|>>/},
  91. {token: "constant.character", regex: /\({2,3}.*?\){2,3}/},
  92. // Anchor
  93. {token: "keyword", regex: /\[\[.+?\]\]/},
  94. // bibliography
  95. {token: "support", regex: /^\[{3}[\w\d =\-]+\]{3}/},
  96. {include: "quotes"},
  97. // text block end
  98. {token: "empty", regex: /^\s*$/, next: "start"}
  99. ],
  100. "listText": [
  101. {include: "listStart"},
  102. {include: "text"}
  103. ],
  104. "indentedBlock": [
  105. {token: "literal", regex: /^[\s\w].+$/, next: "indentedBlock"},
  106. {token: "literal", regex: "", next: "start"}
  107. ],
  108. "listingBlock": [
  109. {token: "literal", regex: /^\.{4,}\s*$/, next: "dissallowDelimitedBlock"},
  110. {token: "constant.numeric", regex: '<\\d+>'},
  111. {token: "literal", regex: '[^<]+'},
  112. {token: "literal", regex: '<'}
  113. ],
  114. "literalBlock": [
  115. {token: "literal", regex: /^-{4,}\s*$/, next: "dissallowDelimitedBlock"},
  116. {token: "constant.numeric", regex: '<\\d+>'},
  117. {token: "literal", regex: '[^<]+'},
  118. {token: "literal", regex: '<'}
  119. ],
  120. "passthroughBlock": [
  121. {token: "literal", regex: /^\+{4,}\s*$/, next: "dissallowDelimitedBlock"},
  122. {token: "literal", regex: identifierRe + "|\\d+"},
  123. {include: "macros"},
  124. {token: "literal", regex: "."}
  125. ],
  126. "smallPassthrough": [
  127. {token: "literal", regex: /[+]{3,}/, next: "dissallowDelimitedBlock"},
  128. {token: "literal", regex: /^\s*$/, next: "dissallowDelimitedBlock"},
  129. {token: "literal", regex: identifierRe + "|\\d+"},
  130. {include: "macros"}
  131. ],
  132. "commentBlock": [
  133. {token: "doc.comment", regex: /^\/{4,}\s*$/, next: "dissallowDelimitedBlock"},
  134. {token: "doc.comment", regex: '^.*$'}
  135. ],
  136. "tableBlock": [
  137. {token: "tableBlock", regex: /^\s*\|={3,}\s*$/, next: "dissallowDelimitedBlock"},
  138. {token: "tableBlock", regex: /^\s*!={3,}\s*$/, next: "innerTableBlock"},
  139. {token: "tableBlock", regex: /\|/},
  140. {include: "text", noEscape: true}
  141. ],
  142. "innerTableBlock": [
  143. {token: "tableBlock", regex: /^\s*!={3,}\s*$/, next: "tableBlock"},
  144. {token: "tableBlock", regex: /^\s*|={3,}\s*$/, next: "dissallowDelimitedBlock"},
  145. {token: "tableBlock", regex: /\!/}
  146. ],
  147. "macros": [
  148. {token: "macro", regex: /{[\w\-$]+}/},
  149. {token: ["text", "string", "text", "constant.character", "text"], regex: /({)([\w\-$]+)(:)?(.+)?(})/},
  150. {token: ["text", "markup.list.macro", "keyword", "string"], regex: /(\w+)(footnote(?:ref)?::?)([^\s\[]+)?(\[.*?\])?/},
  151. {token: ["markup.list.macro", "keyword", "string"], regex: /([a-zA-Z\-][\w\.\/\-]*::?)([^\s\[]+)(\[.*?\])?/},
  152. {token: ["markup.list.macro", "keyword"], regex: /([a-zA-Z\-][\w\.\/\-]+::?)(\[.*?\])/},
  153. {token: "keyword", regex: /^:.+?:(?= |$)/}
  154. ],
  155. "quotes": [
  156. {token: "string.italic", regex: /__[^_\s].*?__/},
  157. {token: "string.italic", regex: quoteRule("_")},
  158. {token: "keyword.bold", regex: /\*\*[^*\s].*?\*\*/},
  159. {token: "keyword.bold", regex: quoteRule("\\*")},
  160. {token: "literal", regex: quoteRule("\\+")},
  161. {token: "literal", regex: /\+\+[^+\s].*?\+\+/},
  162. {token: "literal", regex: /\$\$.+?\$\$/},
  163. {token: "literal", regex: quoteRule("`")},
  164. {token: "keyword", regex: quoteRule("^")},
  165. {token: "keyword", regex: quoteRule("~")},
  166. {token: "keyword", regex: /##?/},
  167. {token: "keyword", regex: /(?:\B|^)``|\b''/}
  168. ]
  169. };
  170. function quoteRule(ch) {
  171. var prefix = /\w/.test(ch) ? "\\b" : "(?:\\B|^)";
  172. return prefix + ch + "[^" + ch + "].*?" + ch + "(?![\\w*])";
  173. }
  174. //addQuoteBlock("text")
  175. var tokenMap = {
  176. macro: "constant.character",
  177. tableBlock: "doc.comment",
  178. titleUnderline: "markup.heading",
  179. singleLineTitle: "markup.heading",
  180. pageBreak: "string",
  181. option: "string.regexp",
  182. otherBlock: "markup.list",
  183. literal: "support.function",
  184. optionalTitle: "constant.numeric",
  185. escape: "constant.language.escape",
  186. link: "markup.underline.list"
  187. };
  188. for (var state in this.$rules) {
  189. var stateRules = this.$rules[state];
  190. for (var i = stateRules.length; i--; ) {
  191. var rule = stateRules[i];
  192. if (rule.include || typeof rule == "string") {
  193. var args = [i, 1].concat(this.$rules[rule.include || rule]);
  194. if (rule.noEscape) {
  195. args = args.filter(function(x) {
  196. return !x.next;
  197. });
  198. }
  199. stateRules.splice.apply(stateRules, args);
  200. } else if (rule.token in tokenMap) {
  201. rule.token = tokenMap[rule.token];
  202. }
  203. }
  204. }
  205. };
  206. oop.inherits(AsciidocHighlightRules, TextHighlightRules);
  207. exports.AsciidocHighlightRules = AsciidocHighlightRules;
  208. });