source: node_modules/refractor/lang/markup.js

main
Last change on this file was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

  • Property mode set to 100644
File size: 5.6 KB
Line 
1'use strict'
2
3module.exports = markup
4markup.displayName = 'markup'
5markup.aliases = ['html', 'mathml', 'svg', 'xml', 'ssml', 'atom', 'rss']
6function markup(Prism) {
7 Prism.languages.markup = {
8 comment: {
9 pattern: /<!--(?:(?!<!--)[\s\S])*?-->/,
10 greedy: true
11 },
12 prolog: {
13 pattern: /<\?[\s\S]+?\?>/,
14 greedy: true
15 },
16 doctype: {
17 // https://www.w3.org/TR/xml/#NT-doctypedecl
18 pattern:
19 /<!DOCTYPE(?:[^>"'[\]]|"[^"]*"|'[^']*')+(?:\[(?:[^<"'\]]|"[^"]*"|'[^']*'|<(?!!--)|<!--(?:[^-]|-(?!->))*-->)*\]\s*)?>/i,
20 greedy: true,
21 inside: {
22 'internal-subset': {
23 pattern: /(^[^\[]*\[)[\s\S]+(?=\]>$)/,
24 lookbehind: true,
25 greedy: true,
26 inside: null // see below
27 },
28 string: {
29 pattern: /"[^"]*"|'[^']*'/,
30 greedy: true
31 },
32 punctuation: /^<!|>$|[[\]]/,
33 'doctype-tag': /^DOCTYPE/i,
34 name: /[^\s<>'"]+/
35 }
36 },
37 cdata: {
38 pattern: /<!\[CDATA\[[\s\S]*?\]\]>/i,
39 greedy: true
40 },
41 tag: {
42 pattern:
43 /<\/?(?!\d)[^\s>\/=$<%]+(?:\s(?:\s*[^\s>\/=]+(?:\s*=\s*(?:"[^"]*"|'[^']*'|[^\s'">=]+(?=[\s>]))|(?=[\s/>])))+)?\s*\/?>/,
44 greedy: true,
45 inside: {
46 tag: {
47 pattern: /^<\/?[^\s>\/]+/,
48 inside: {
49 punctuation: /^<\/?/,
50 namespace: /^[^\s>\/:]+:/
51 }
52 },
53 'special-attr': [],
54 'attr-value': {
55 pattern: /=\s*(?:"[^"]*"|'[^']*'|[^\s'">=]+)/,
56 inside: {
57 punctuation: [
58 {
59 pattern: /^=/,
60 alias: 'attr-equals'
61 },
62 /"|'/
63 ]
64 }
65 },
66 punctuation: /\/?>/,
67 'attr-name': {
68 pattern: /[^\s>\/]+/,
69 inside: {
70 namespace: /^[^\s>\/:]+:/
71 }
72 }
73 }
74 },
75 entity: [
76 {
77 pattern: /&[\da-z]{1,8};/i,
78 alias: 'named-entity'
79 },
80 /&#x?[\da-f]{1,8};/i
81 ]
82 }
83 Prism.languages.markup['tag'].inside['attr-value'].inside['entity'] =
84 Prism.languages.markup['entity']
85 Prism.languages.markup['doctype'].inside['internal-subset'].inside =
86 Prism.languages.markup // Plugin to make entity title show the real entity, idea by Roman Komarov
87 Prism.hooks.add('wrap', function (env) {
88 if (env.type === 'entity') {
89 env.attributes['title'] = env.content.value.replace(/&amp;/, '&')
90 }
91 })
92 Object.defineProperty(Prism.languages.markup.tag, 'addInlined', {
93 /**
94 * Adds an inlined language to markup.
95 *
96 * An example of an inlined language is CSS with `<style>` tags.
97 *
98 * @param {string} tagName The name of the tag that contains the inlined language. This name will be treated as
99 * case insensitive.
100 * @param {string} lang The language key.
101 * @example
102 * addInlined('style', 'css');
103 */
104 value: function addInlined(tagName, lang) {
105 var includedCdataInside = {}
106 includedCdataInside['language-' + lang] = {
107 pattern: /(^<!\[CDATA\[)[\s\S]+?(?=\]\]>$)/i,
108 lookbehind: true,
109 inside: Prism.languages[lang]
110 }
111 includedCdataInside['cdata'] = /^<!\[CDATA\[|\]\]>$/i
112 var inside = {
113 'included-cdata': {
114 pattern: /<!\[CDATA\[[\s\S]*?\]\]>/i,
115 inside: includedCdataInside
116 }
117 }
118 inside['language-' + lang] = {
119 pattern: /[\s\S]+/,
120 inside: Prism.languages[lang]
121 }
122 var def = {}
123 def[tagName] = {
124 pattern: RegExp(
125 /(<__[^>]*>)(?:<!\[CDATA\[(?:[^\]]|\](?!\]>))*\]\]>|(?!<!\[CDATA\[)[\s\S])*?(?=<\/__>)/.source.replace(
126 /__/g,
127 function () {
128 return tagName
129 }
130 ),
131 'i'
132 ),
133 lookbehind: true,
134 greedy: true,
135 inside: inside
136 }
137 Prism.languages.insertBefore('markup', 'cdata', def)
138 }
139 })
140 Object.defineProperty(Prism.languages.markup.tag, 'addAttribute', {
141 /**
142 * Adds an pattern to highlight languages embedded in HTML attributes.
143 *
144 * An example of an inlined language is CSS with `style` attributes.
145 *
146 * @param {string} attrName The name of the tag that contains the inlined language. This name will be treated as
147 * case insensitive.
148 * @param {string} lang The language key.
149 * @example
150 * addAttribute('style', 'css');
151 */
152 value: function (attrName, lang) {
153 Prism.languages.markup.tag.inside['special-attr'].push({
154 pattern: RegExp(
155 /(^|["'\s])/.source +
156 '(?:' +
157 attrName +
158 ')' +
159 /\s*=\s*(?:"[^"]*"|'[^']*'|[^\s'">=]+(?=[\s>]))/.source,
160 'i'
161 ),
162 lookbehind: true,
163 inside: {
164 'attr-name': /^[^\s=]+/,
165 'attr-value': {
166 pattern: /=[\s\S]+/,
167 inside: {
168 value: {
169 pattern: /(^=\s*(["']|(?!["'])))\S[\s\S]*(?=\2$)/,
170 lookbehind: true,
171 alias: [lang, 'language-' + lang],
172 inside: Prism.languages[lang]
173 },
174 punctuation: [
175 {
176 pattern: /^=/,
177 alias: 'attr-equals'
178 },
179 /"|'/
180 ]
181 }
182 }
183 }
184 })
185 }
186 })
187 Prism.languages.html = Prism.languages.markup
188 Prism.languages.mathml = Prism.languages.markup
189 Prism.languages.svg = Prism.languages.markup
190 Prism.languages.xml = Prism.languages.extend('markup', {})
191 Prism.languages.ssml = Prism.languages.xml
192 Prism.languages.atom = Prism.languages.xml
193 Prism.languages.rss = Prism.languages.xml
194}
Note: See TracBrowser for help on using the repository browser.