| 1 |
var TOKEN_STYLES = { |
| 2 |
addition: "inserted", |
| 3 |
attributes: "propertyName", |
| 4 |
bold: "strong", |
| 5 |
cite: "keyword", |
| 6 |
code: "monospace", |
| 7 |
definitionList: "list", |
| 8 |
deletion: "deleted", |
| 9 |
div: "punctuation", |
| 10 |
em: "emphasis", |
| 11 |
footnote: "variable", |
| 12 |
footCite: "qualifier", |
| 13 |
header: "heading", |
| 14 |
html: "comment", |
| 15 |
image: "atom", |
| 16 |
italic: "emphasis", |
| 17 |
link: "link", |
| 18 |
linkDefinition: "link", |
| 19 |
list1: "list", |
| 20 |
list2: "list.special", |
| 21 |
list3: "list", |
| 22 |
notextile: "string.special", |
| 23 |
pre: "operator", |
| 24 |
p: "content", |
| 25 |
quote: "bracket", |
| 26 |
span: "quote", |
| 27 |
specialChar: "character", |
| 28 |
strong: "strong", |
| 29 |
sub: "content.special", |
| 30 |
sup: "content.special", |
| 31 |
table: "variableName.special", |
| 32 |
tableHeading: "operator" |
| 33 |
}; |
| 34 |
|
| 35 |
function startNewLine(stream, state) { |
| 36 |
state.mode = Modes.newLayout; |
| 37 |
state.tableHeading = false; |
| 38 |
|
| 39 |
if (state.layoutType === "definitionList" && state.spanningLayout && |
| 40 |
stream.match(RE("definitionListEnd"), false)) |
| 41 |
state.spanningLayout = false; |
| 42 |
} |
| 43 |
|
| 44 |
function handlePhraseModifier(stream, state, ch) { |
| 45 |
if (ch === "_") { |
| 46 |
if (stream.eat("_")) |
| 47 |
return togglePhraseModifier(stream, state, "italic", /__/, 2); |
| 48 |
else |
| 49 |
return togglePhraseModifier(stream, state, "em", /_/, 1); |
| 50 |
} |
| 51 |
|
| 52 |
if (ch === "*") { |
| 53 |
if (stream.eat("*")) { |
| 54 |
return togglePhraseModifier(stream, state, "bold", /\*\*/, 2); |
| 55 |
} |
| 56 |
return togglePhraseModifier(stream, state, "strong", /\*/, 1); |
| 57 |
} |
| 58 |
|
| 59 |
if (ch === "[") { |
| 60 |
if (stream.match(/\d+\]/)) state.footCite = true; |
| 61 |
return tokenStyles(state); |
| 62 |
} |
| 63 |
|
| 64 |
if (ch === "(") { |
| 65 |
var spec = stream.match(/^(r|tm|c)\)/); |
| 66 |
if (spec) |
| 67 |
return TOKEN_STYLES.specialChar |
| 68 |
} |
| 69 |
|
| 70 |
if (ch === "<" && stream.match(/(\w+)[^>]+>[^<]+<\/\1>/)) |
| 71 |
return TOKEN_STYLES.html |
| 72 |
|
| 73 |
if (ch === "?" && stream.eat("?")) |
| 74 |
return togglePhraseModifier(stream, state, "cite", /\?\?/, 2); |
| 75 |
|
| 76 |
if (ch === "=" && stream.eat("=")) |
| 77 |
return togglePhraseModifier(stream, state, "notextile", /==/, 2); |
| 78 |
|
| 79 |
if (ch === "-" && !stream.eat("-")) |
| 80 |
return togglePhraseModifier(stream, state, "deletion", /-/, 1); |
| 81 |
|
| 82 |
if (ch === "+") |
| 83 |
return togglePhraseModifier(stream, state, "addition", /\+/, 1); |
| 84 |
|
| 85 |
if (ch === "~") |
| 86 |
return togglePhraseModifier(stream, state, "sub", /~/, 1); |
| 87 |
|
| 88 |
if (ch === "^") |
| 89 |
return togglePhraseModifier(stream, state, "sup", /\^/, 1); |
| 90 |
|
| 91 |
if (ch === "%") |
| 92 |
return togglePhraseModifier(stream, state, "span", /%/, 1); |
| 93 |
|
| 94 |
if (ch === "@") |
| 95 |
return togglePhraseModifier(stream, state, "code", /@/, 1); |
| 96 |
|
| 97 |
if (ch === "!") { |
| 98 |
var type = togglePhraseModifier(stream, state, "image", /(?:\([^\)]+\))?!/, 1); |
| 99 |
stream.match(/^:\S+/); // optional Url portion |
| 100 |
return type; |
| 101 |
} |
| 102 |
return tokenStyles(state); |
| 103 |
} |
| 104 |
|
| 105 |
function togglePhraseModifier(stream, state, phraseModifier, closeRE, openSize) { |
| 106 |
var charBefore = stream.pos > openSize ? stream.string.charAt(stream.pos - openSize - 1) : null; |
| 107 |
var charAfter = stream.peek(); |
| 108 |
if (state[phraseModifier]) { |
| 109 |
if ((!charAfter || /\W/.test(charAfter)) && charBefore && /\S/.test(charBefore)) { |
| 110 |
var type = tokenStyles(state); |
| 111 |
state[phraseModifier] = false; |
| 112 |
return type; |
| 113 |
} |
| 114 |
} else if ((!charBefore || /\W/.test(charBefore)) && charAfter && /\S/.test(charAfter) && |
| 115 |
stream.match(new RegExp("^.*\\S" + closeRE.source + "(?:\\W|$)"), false)) { |
| 116 |
state[phraseModifier] = true; |
| 117 |
state.mode = Modes.attributes; |
| 118 |
} |
| 119 |
return tokenStyles(state); |
| 120 |
}; |
| 121 |
|
| 122 |
function tokenStyles(state) { |
| 123 |
var disabled = textileDisabled(state); |
| 124 |
if (disabled) return disabled; |
| 125 |
|
| 126 |
var styles = []; |
| 127 |
if (state.layoutType) styles.push(TOKEN_STYLES[state.layoutType]); |
| 128 |
|
| 129 |
styles = styles.concat(activeStyles( |
| 130 |
state, "addition", "bold", "cite", "code", "deletion", "em", "footCite", |
| 131 |
"image", "italic", "link", "span", "strong", "sub", "sup", "table", "tableHeading")); |
| 132 |
|
| 133 |
if (state.layoutType === "header") |
| 134 |
styles.push(TOKEN_STYLES.header + "-" + state.header); |
| 135 |
|
| 136 |
return styles.length ? styles.join(" ") : null; |
| 137 |
} |
| 138 |
|
| 139 |
function textileDisabled(state) { |
| 140 |
var type = state.layoutType; |
| 141 |
|
| 142 |
switch(type) { |
| 143 |
case "notextile": |
| 144 |
case "code": |
| 145 |
case "pre": |
| 146 |
return TOKEN_STYLES[type]; |
| 147 |
default: |
| 148 |
if (state.notextile) |
| 149 |
return TOKEN_STYLES.notextile + (type ? (" " + TOKEN_STYLES[type]) : ""); |
| 150 |
return null; |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
function activeStyles(state) { |
| 155 |
var styles = []; |
| 156 |
for (var i = 1; i < arguments.length; ++i) { |
| 157 |
if (state[arguments[i]]) |
| 158 |
styles.push(TOKEN_STYLES[arguments[i]]); |
| 159 |
} |
| 160 |
return styles; |
| 161 |
} |
| 162 |
|
| 163 |
function blankLine(state) { |
| 164 |
var spanningLayout = state.spanningLayout, type = state.layoutType; |
| 165 |
|
| 166 |
for (var key in state) if (state.hasOwnProperty(key)) |
| 167 |
delete state[key]; |
| 168 |
|
| 169 |
state.mode = Modes.newLayout; |
| 170 |
if (spanningLayout) { |
| 171 |
state.layoutType = type; |
| 172 |
state.spanningLayout = true; |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
var REs = { |
| 177 |
cache: {}, |
| 178 |
single: { |
| 179 |
bc: "bc", |
| 180 |
bq: "bq", |
| 181 |
definitionList: /- .*?:=+/, |
| 182 |
definitionListEnd: /.*=:\s*$/, |
| 183 |
div: "div", |
| 184 |
drawTable: /\|.*\|/, |
| 185 |
foot: /fn\d+/, |
| 186 |
header: /h[1-6]/, |
| 187 |
html: /\s*<(?:\/)?(\w+)(?:[^>]+)?>(?:[^<]+<\/\1>)?/, |
| 188 |
link: /[^"]+":\S/, |
| 189 |
linkDefinition: /\[[^\s\]]+\]\S+/, |
| 190 |
list: /(?:#+|\*+)/, |
| 191 |
notextile: "notextile", |
| 192 |
para: "p", |
| 193 |
pre: "pre", |
| 194 |
table: "table", |
| 195 |
tableCellAttributes: /[\/\\]\d+/, |
| 196 |
tableHeading: /\|_\./, |
| 197 |
tableText: /[^"_\*\[\(\?\+~\^%@|-]+/, |
| 198 |
text: /[^!"_=\*\[\(<\?\+~\^%@-]+/ |
| 199 |
}, |
| 200 |
attributes: { |
| 201 |
align: /(?:<>|<|>|=)/, |
| 202 |
selector: /\([^\(][^\)]+\)/, |
| 203 |
lang: /\[[^\[\]]+\]/, |
| 204 |
pad: /(?:\(+|\)+){1,2}/, |
| 205 |
css: /\{[^\}]+\}/ |
| 206 |
}, |
| 207 |
createRe: function(name) { |
| 208 |
switch (name) { |
| 209 |
case "drawTable": |
| 210 |
return REs.makeRe("^", REs.single.drawTable, "$"); |
| 211 |
case "html": |
| 212 |
return REs.makeRe("^", REs.single.html, "(?:", REs.single.html, ")*", "$"); |
| 213 |
case "linkDefinition": |
| 214 |
return REs.makeRe("^", REs.single.linkDefinition, "$"); |
| 215 |
case "listLayout": |
| 216 |
return REs.makeRe("^", REs.single.list, RE("allAttributes"), "*\\s+"); |
| 217 |
case "tableCellAttributes": |
| 218 |
return REs.makeRe("^", REs.choiceRe(REs.single.tableCellAttributes, |
| 219 |
RE("allAttributes")), "+\\."); |
| 220 |
case "type": |
| 221 |
return REs.makeRe("^", RE("allTypes")); |
| 222 |
case "typeLayout": |
| 223 |
return REs.makeRe("^", RE("allTypes"), RE("allAttributes"), |
| 224 |
"*\\.\\.?", "(\\s+|$)"); |
| 225 |
case "attributes": |
| 226 |
return REs.makeRe("^", RE("allAttributes"), "+"); |
| 227 |
|
| 228 |
case "allTypes": |
| 229 |
return REs.choiceRe(REs.single.div, REs.single.foot, |
| 230 |
REs.single.header, REs.single.bc, REs.single.bq, |
| 231 |
REs.single.notextile, REs.single.pre, REs.single.table, |
| 232 |
REs.single.para); |
| 233 |
|
| 234 |
case "allAttributes": |
| 235 |
return REs.choiceRe(REs.attributes.selector, REs.attributes.css, |
| 236 |
REs.attributes.lang, REs.attributes.align, REs.attributes.pad); |
| 237 |
|
| 238 |
default: |
| 239 |
return REs.makeRe("^", REs.single[name]); |
| 240 |
} |
| 241 |
}, |
| 242 |
makeRe: function() { |
| 243 |
var pattern = ""; |
| 244 |
for (var i = 0; i < arguments.length; ++i) { |
| 245 |
var arg = arguments[i]; |
| 246 |
pattern += (typeof arg === "string") ? arg : arg.source; |
| 247 |
} |
| 248 |
return new RegExp(pattern); |
| 249 |
}, |
| 250 |
choiceRe: function() { |
| 251 |
var parts = [arguments[0]]; |
| 252 |
for (var i = 1; i < arguments.length; ++i) { |
| 253 |
parts[i * 2 - 1] = "|"; |
| 254 |
parts[i * 2] = arguments[i]; |
| 255 |
} |
| 256 |
|
| 257 |
parts.unshift("(?:"); |
| 258 |
parts.push(")"); |
| 259 |
return REs.makeRe.apply(null, parts); |
| 260 |
} |
| 261 |
}; |
| 262 |
|
| 263 |
function RE(name) { |
| 264 |
return (REs.cache[name] || (REs.cache[name] = REs.createRe(name))); |
| 265 |
} |
| 266 |
|
| 267 |
var Modes = { |
| 268 |
newLayout: function(stream, state) { |
| 269 |
if (stream.match(RE("typeLayout"), false)) { |
| 270 |
state.spanningLayout = false; |
| 271 |
return (state.mode = Modes.blockType)(stream, state); |
| 272 |
} |
| 273 |
var newMode; |
| 274 |
if (!textileDisabled(state)) { |
| 275 |
if (stream.match(RE("listLayout"), false)) |
| 276 |
newMode = Modes.list; |
| 277 |
else if (stream.match(RE("drawTable"), false)) |
| 278 |
newMode = Modes.table; |
| 279 |
else if (stream.match(RE("linkDefinition"), false)) |
| 280 |
newMode = Modes.linkDefinition; |
| 281 |
else if (stream.match(RE("definitionList"))) |
| 282 |
newMode = Modes.definitionList; |
| 283 |
else if (stream.match(RE("html"), false)) |
| 284 |
newMode = Modes.html; |
| 285 |
} |
| 286 |
return (state.mode = (newMode || Modes.text))(stream, state); |
| 287 |
}, |
| 288 |
|
| 289 |
blockType: function(stream, state) { |
| 290 |
var match, type; |
| 291 |
state.layoutType = null; |
| 292 |
|
| 293 |
if (match = stream.match(RE("type"))) |
| 294 |
type = match[0]; |
| 295 |
else |
| 296 |
return (state.mode = Modes.text)(stream, state); |
| 297 |
|
| 298 |
if (match = type.match(RE("header"))) { |
| 299 |
state.layoutType = "header"; |
| 300 |
state.header = parseInt(match[0][1]); |
| 301 |
} else if (type.match(RE("bq"))) { |
| 302 |
state.layoutType = "quote"; |
| 303 |
} else if (type.match(RE("bc"))) { |
| 304 |
state.layoutType = "code"; |
| 305 |
} else if (type.match(RE("foot"))) { |
| 306 |
state.layoutType = "footnote"; |
| 307 |
} else if (type.match(RE("notextile"))) { |
| 308 |
state.layoutType = "notextile"; |
| 309 |
} else if (type.match(RE("pre"))) { |
| 310 |
state.layoutType = "pre"; |
| 311 |
} else if (type.match(RE("div"))) { |
| 312 |
state.layoutType = "div"; |
| 313 |
} else if (type.match(RE("table"))) { |
| 314 |
state.layoutType = "table"; |
| 315 |
} |
| 316 |
|
| 317 |
state.mode = Modes.attributes; |
| 318 |
return tokenStyles(state); |
| 319 |
}, |
| 320 |
|
| 321 |
text: function(stream, state) { |
| 322 |
if (stream.match(RE("text"))) return tokenStyles(state); |
| 323 |
|
| 324 |
var ch = stream.next(); |
| 325 |
if (ch === '"') |
| 326 |
return (state.mode = Modes.link)(stream, state); |
| 327 |
return handlePhraseModifier(stream, state, ch); |
| 328 |
}, |
| 329 |
|
| 330 |
attributes: function(stream, state) { |
| 331 |
state.mode = Modes.layoutLength; |
| 332 |
|
| 333 |
if (stream.match(RE("attributes"))) |
| 334 |
return TOKEN_STYLES.attributes |
| 335 |
else |
| 336 |
return tokenStyles(state); |
| 337 |
}, |
| 338 |
|
| 339 |
layoutLength: function(stream, state) { |
| 340 |
if (stream.eat(".") && stream.eat(".")) |
| 341 |
state.spanningLayout = true; |
| 342 |
|
| 343 |
state.mode = Modes.text; |
| 344 |
return tokenStyles(state); |
| 345 |
}, |
| 346 |
|
| 347 |
list: function(stream, state) { |
| 348 |
var match = stream.match(RE("list")); |
| 349 |
state.listDepth = match[0].length; |
| 350 |
var listMod = (state.listDepth - 1) % 3; |
| 351 |
if (!listMod) |
| 352 |
state.layoutType = "list1"; |
| 353 |
else if (listMod === 1) |
| 354 |
state.layoutType = "list2"; |
| 355 |
else |
| 356 |
state.layoutType = "list3"; |
| 357 |
|
| 358 |
state.mode = Modes.attributes; |
| 359 |
return tokenStyles(state); |
| 360 |
}, |
| 361 |
|
| 362 |
link: function(stream, state) { |
| 363 |
state.mode = Modes.text; |
| 364 |
if (stream.match(RE("link"))) { |
| 365 |
stream.match(/\S+/); |
| 366 |
return TOKEN_STYLES.link |
| 367 |
} |
| 368 |
return tokenStyles(state); |
| 369 |
}, |
| 370 |
|
| 371 |
linkDefinition: function(stream) { |
| 372 |
stream.skipToEnd(); |
| 373 |
return TOKEN_STYLES.linkDefinition |
| 374 |
}, |
| 375 |
|
| 376 |
definitionList: function(stream, state) { |
| 377 |
stream.match(RE("definitionList")); |
| 378 |
|
| 379 |
state.layoutType = "definitionList"; |
| 380 |
|
| 381 |
if (stream.match(/\s*$/)) |
| 382 |
state.spanningLayout = true; |
| 383 |
else |
| 384 |
state.mode = Modes.attributes; |
| 385 |
|
| 386 |
return tokenStyles(state); |
| 387 |
}, |
| 388 |
|
| 389 |
html: function(stream) { |
| 390 |
stream.skipToEnd(); |
| 391 |
return TOKEN_STYLES.html |
| 392 |
}, |
| 393 |
|
| 394 |
table: function(stream, state) { |
| 395 |
state.layoutType = "table"; |
| 396 |
return (state.mode = Modes.tableCell)(stream, state); |
| 397 |
}, |
| 398 |
|
| 399 |
tableCell: function(stream, state) { |
| 400 |
if (stream.match(RE("tableHeading"))) |
| 401 |
state.tableHeading = true; |
| 402 |
else |
| 403 |
stream.eat("|"); |
| 404 |
|
| 405 |
state.mode = Modes.tableCellAttributes; |
| 406 |
return tokenStyles(state); |
| 407 |
}, |
| 408 |
|
| 409 |
tableCellAttributes: function(stream, state) { |
| 410 |
state.mode = Modes.tableText; |
| 411 |
|
| 412 |
if (stream.match(RE("tableCellAttributes"))) |
| 413 |
return TOKEN_STYLES.attributes |
| 414 |
else |
| 415 |
return tokenStyles(state); |
| 416 |
}, |
| 417 |
|
| 418 |
tableText: function(stream, state) { |
| 419 |
if (stream.match(RE("tableText"))) |
| 420 |
return tokenStyles(state); |
| 421 |
|
| 422 |
if (stream.peek() === "|") { // end of cell |
| 423 |
state.mode = Modes.tableCell; |
| 424 |
return tokenStyles(state); |
| 425 |
} |
| 426 |
return handlePhraseModifier(stream, state, stream.next()); |
| 427 |
} |
| 428 |
}; |
| 429 |
|
| 430 |
export const textile = { |
| 431 |
name: "textile", |
| 432 |
startState: function() { |
| 433 |
return { mode: Modes.newLayout }; |
| 434 |
}, |
| 435 |
token: function(stream, state) { |
| 436 |
if (stream.sol()) startNewLine(stream, state); |
| 437 |
return state.mode(stream, state); |
| 438 |
}, |
| 439 |
blankLine: blankLine |
| 440 |
}; |