markdown_highlight_rules.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 lang = require("../lib/lang");
  34. var TextHighlightRules = require("./text_highlight_rules").TextHighlightRules;
  35. var JavaScriptHighlightRules = require("./javascript_highlight_rules").JavaScriptHighlightRules;
  36. var XmlHighlightRules = require("./xml_highlight_rules").XmlHighlightRules;
  37. var HtmlHighlightRules = require("./html_highlight_rules").HtmlHighlightRules;
  38. var CssHighlightRules = require("./css_highlight_rules").CssHighlightRules;
  39. var escaped = function(ch) {
  40. return "(?:[^" + lang.escapeRegExp(ch) + "\\\\]|\\\\.)*";
  41. }
  42. function github_embed(tag, prefix) {
  43. return { // Github style block
  44. token : "support.function",
  45. regex : "^\\s*```" + tag + "\\s*$",
  46. push : prefix + "start"
  47. };
  48. }
  49. var MarkdownHighlightRules = function() {
  50. HtmlHighlightRules.call(this);
  51. // regexp must not have capturing parentheses
  52. // regexps are ordered -> the first match is used
  53. this.$rules["start"].unshift({
  54. token : "empty_line",
  55. regex : '^$',
  56. next: "allowBlock"
  57. }, { // h1
  58. token: "markup.heading.1",
  59. regex: "^=+(?=\\s*$)"
  60. }, { // h2
  61. token: "markup.heading.2",
  62. regex: "^\\-+(?=\\s*$)"
  63. }, {
  64. token : function(value) {
  65. return "markup.heading." + value.length;
  66. },
  67. regex : /^#{1,6}(?=\s*[^ #]|\s+#.)/,
  68. next : "header"
  69. },
  70. github_embed("(?:javascript|js)", "jscode-"),
  71. github_embed("xml", "xmlcode-"),
  72. github_embed("html", "htmlcode-"),
  73. github_embed("css", "csscode-"),
  74. { // Github style block
  75. token : "support.function",
  76. regex : "^\\s*```\\s*\\S*(?:{.*?\\})?\\s*$",
  77. next : "githubblock"
  78. }, { // block quote
  79. token : "string.blockquote",
  80. regex : "^\\s*>\\s*(?:[*+-]|\\d+\\.)?\\s+",
  81. next : "blockquote"
  82. }, { // HR * - _
  83. token : "constant",
  84. regex : "^ {0,2}(?:(?: ?\\* ?){3,}|(?: ?\\- ?){3,}|(?: ?\\_ ?){3,})\\s*$",
  85. next: "allowBlock"
  86. }, { // list
  87. token : "markup.list",
  88. regex : "^\\s{0,3}(?:[*+-]|\\d+\\.)\\s+",
  89. next : "listblock-start"
  90. }, {
  91. include : "basic"
  92. });
  93. this.addRules({
  94. "basic" : [{
  95. token : "constant.language.escape",
  96. regex : /\\[\\`*_{}\[\]()#+\-.!]/
  97. }, { // code span `
  98. token : "support.function",
  99. regex : "(`+)(.*?[^`])(\\1)"
  100. }, { // reference
  101. token : ["text", "constant", "text", "url", "string", "text"],
  102. regex : "^([ ]{0,3}\\[)([^\\]]+)(\\]:\\s*)([^ ]+)(\\s*(?:[\"][^\"]+[\"])?(\\s*))$"
  103. }, { // link by reference
  104. token : ["text", "string", "text", "constant", "text"],
  105. regex : "(\\[)(" + escaped("]") + ")(\\]\s*\\[)("+ escaped("]") + ")(\\])"
  106. }, { // link by url
  107. token : ["text", "string", "text", "markup.underline", "string", "text"],
  108. regex : "(\\[)(" + // [
  109. escaped("]") + // link text
  110. ")(\\]\\()"+ // ](
  111. '((?:[^\\)\\s\\\\]|\\\\.|\\s(?=[^"]))*)' + // href
  112. '(\\s*"' + escaped('"') + '"\\s*)?' + // "title"
  113. "(\\))" // )
  114. }, { // strong ** __
  115. token : "string.strong",
  116. regex : "([*]{2}|[_]{2}(?=\\S))(.*?\\S[*_]*)(\\1)"
  117. }, { // emphasis * _
  118. token : "string.emphasis",
  119. regex : "([*]|[_](?=\\S))(.*?\\S[*_]*)(\\1)"
  120. }, { //
  121. token : ["text", "url", "text"],
  122. regex : "(<)("+
  123. "(?:https?|ftp|dict):[^'\">\\s]+"+
  124. "|"+
  125. "(?:mailto:)?[-.\\w]+\\@[-a-z0-9]+(?:\\.[-a-z0-9]+)*\\.[a-z]+"+
  126. ")(>)"
  127. }],
  128. // code block
  129. "allowBlock": [
  130. {token : "support.function", regex : "^ {4}.+", next : "allowBlock"},
  131. {token : "empty_line", regex : '^$', next: "allowBlock"},
  132. {token : "empty", regex : "", next : "start"}
  133. ],
  134. "header" : [{
  135. regex: "$",
  136. next : "start"
  137. }, {
  138. include: "basic"
  139. }, {
  140. defaultToken : "heading"
  141. } ],
  142. "listblock-start" : [{
  143. token : "support.variable",
  144. regex : /(?:\[[ x]\])?/,
  145. next : "listblock"
  146. }],
  147. "listblock" : [ { // Lists only escape on completely blank lines.
  148. token : "empty_line",
  149. regex : "^$",
  150. next : "start"
  151. }, { // list
  152. token : "markup.list",
  153. regex : "^\\s{0,3}(?:[*+-]|\\d+\\.)\\s+",
  154. next : "listblock-start"
  155. }, {
  156. include : "basic", noEscape: true
  157. }, { // Github style block
  158. token : "support.function",
  159. regex : "^\\s*```\\s*[a-zA-Z]*(?:{.*?\\})?\\s*$",
  160. next : "githubblock"
  161. }, {
  162. defaultToken : "list" //do not use markup.list to allow stling leading `*` differntly
  163. } ],
  164. "blockquote" : [ { // Blockquotes only escape on blank lines.
  165. token : "empty_line",
  166. regex : "^\\s*$",
  167. next : "start"
  168. }, { // block quote
  169. token : "string.blockquote",
  170. regex : "^\\s*>\\s*(?:[*+-]|\\d+\\.)?\\s+",
  171. next : "blockquote"
  172. }, {
  173. include : "basic", noEscape: true
  174. }, {
  175. defaultToken : "string.blockquote"
  176. } ],
  177. "githubblock" : [ {
  178. token : "support.function",
  179. regex : "^\\s*```",
  180. next : "start"
  181. }, {
  182. token : "support.function",
  183. regex : ".+"
  184. } ]
  185. });
  186. this.embedRules(JavaScriptHighlightRules, "jscode-", [{
  187. token : "support.function",
  188. regex : "^\\s*```",
  189. next : "pop"
  190. }]);
  191. this.embedRules(HtmlHighlightRules, "htmlcode-", [{
  192. token : "support.function",
  193. regex : "^\\s*```",
  194. next : "pop"
  195. }]);
  196. this.embedRules(CssHighlightRules, "csscode-", [{
  197. token : "support.function",
  198. regex : "^\\s*```",
  199. next : "pop"
  200. }]);
  201. this.embedRules(XmlHighlightRules, "xmlcode-", [{
  202. token : "support.function",
  203. regex : "^\\s*```",
  204. next : "pop"
  205. }]);
  206. this.normalizeRules();
  207. };
  208. oop.inherits(MarkdownHighlightRules, TextHighlightRules);
  209. exports.MarkdownHighlightRules = MarkdownHighlightRules;
  210. });