| 1 |
function inBlock(style, terminator, returnTokenizer) { |
| 2 |
return function(stream, state) { |
| 3 |
while (!stream.eol()) { |
| 4 |
if (stream.match(terminator)) { |
| 5 |
state.tokenize = inText; |
| 6 |
break; |
| 7 |
} |
| 8 |
stream.next(); |
| 9 |
} |
| 10 |
|
| 11 |
if (returnTokenizer) state.tokenize = returnTokenizer; |
| 12 |
|
| 13 |
return style; |
| 14 |
}; |
| 15 |
} |
| 16 |
|
| 17 |
function inLine(style) { |
| 18 |
return function(stream, state) { |
| 19 |
while(!stream.eol()) { |
| 20 |
stream.next(); |
| 21 |
} |
| 22 |
state.tokenize = inText; |
| 23 |
return style; |
| 24 |
}; |
| 25 |
} |
| 26 |
|
| 27 |
function inText(stream, state) { |
| 28 |
function chain(parser) { |
| 29 |
state.tokenize = parser; |
| 30 |
return parser(stream, state); |
| 31 |
} |
| 32 |
|
| 33 |
var sol = stream.sol(); |
| 34 |
var ch = stream.next(); |
| 35 |
|
| 36 |
//non start of line |
| 37 |
switch (ch) { //switch is generally much faster than if, so it is used here |
| 38 |
case "{": //plugin |
| 39 |
stream.eat("/"); |
| 40 |
stream.eatSpace(); |
| 41 |
stream.eatWhile(/[^\s\u00a0=\"\'\/?(}]/); |
| 42 |
state.tokenize = inPlugin; |
| 43 |
return "tag"; |
| 44 |
case "_": //bold |
| 45 |
if (stream.eat("_")) |
| 46 |
return chain(inBlock("strong", "__", inText)); |
| 47 |
break; |
| 48 |
case "'": //italics |
| 49 |
if (stream.eat("'")) |
| 50 |
return chain(inBlock("em", "''", inText)); |
| 51 |
break; |
| 52 |
case "(":// Wiki Link |
| 53 |
if (stream.eat("(")) |
| 54 |
return chain(inBlock("link", "))", inText)); |
| 55 |
break; |
| 56 |
case "[":// Weblink |
| 57 |
return chain(inBlock("url", "]", inText)); |
| 58 |
break; |
| 59 |
case "|": //table |
| 60 |
if (stream.eat("|")) |
| 61 |
return chain(inBlock("comment", "||")); |
| 62 |
break; |
| 63 |
case "-": |
| 64 |
if (stream.eat("=")) {//titleBar |
| 65 |
return chain(inBlock("header string", "=-", inText)); |
| 66 |
} else if (stream.eat("-")) {//deleted |
| 67 |
return chain(inBlock("error tw-deleted", "--", inText)); |
| 68 |
} |
| 69 |
break; |
| 70 |
case "=": //underline |
| 71 |
if (stream.match("==")) |
| 72 |
return chain(inBlock("tw-underline", "===", inText)); |
| 73 |
break; |
| 74 |
case ":": |
| 75 |
if (stream.eat(":")) |
| 76 |
return chain(inBlock("comment", "::")); |
| 77 |
break; |
| 78 |
case "^": //box |
| 79 |
return chain(inBlock("tw-box", "^")); |
| 80 |
break; |
| 81 |
case "~": //np |
| 82 |
if (stream.match("np~")) |
| 83 |
return chain(inBlock("meta", "~/np~")); |
| 84 |
break; |
| 85 |
} |
| 86 |
|
| 87 |
//start of line types |
| 88 |
if (sol) { |
| 89 |
switch (ch) { |
| 90 |
case "!": //header at start of line |
| 91 |
if (stream.match('!!!!!')) { |
| 92 |
return chain(inLine("header string")); |
| 93 |
} else if (stream.match('!!!!')) { |
| 94 |
return chain(inLine("header string")); |
| 95 |
} else if (stream.match('!!!')) { |
| 96 |
return chain(inLine("header string")); |
| 97 |
} else if (stream.match('!!')) { |
| 98 |
return chain(inLine("header string")); |
| 99 |
} else { |
| 100 |
return chain(inLine("header string")); |
| 101 |
} |
| 102 |
break; |
| 103 |
case "*": //unordered list line item, or <li /> at start of line |
| 104 |
case "#": //ordered list line item, or <li /> at start of line |
| 105 |
case "+": //ordered list line item, or <li /> at start of line |
| 106 |
return chain(inLine("tw-listitem bracket")); |
| 107 |
break; |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
//stream.eatWhile(/[&{]/); was eating up plugins, turned off to act less like html and more like tiki |
| 112 |
return null; |
| 113 |
} |
| 114 |
|
| 115 |
// Return variables for tokenizers |
| 116 |
var pluginName, type; |
| 117 |
function inPlugin(stream, state) { |
| 118 |
var ch = stream.next(); |
| 119 |
var peek = stream.peek(); |
| 120 |
|
| 121 |
if (ch == "}") { |
| 122 |
state.tokenize = inText; |
| 123 |
//type = ch == ")" ? "endPlugin" : "selfclosePlugin"; inPlugin |
| 124 |
return "tag"; |
| 125 |
} else if (ch == "(" || ch == ")") { |
| 126 |
return "bracket"; |
| 127 |
} else if (ch == "=") { |
| 128 |
type = "equals"; |
| 129 |
|
| 130 |
if (peek == ">") { |
| 131 |
stream.next(); |
| 132 |
peek = stream.peek(); |
| 133 |
} |
| 134 |
|
| 135 |
//here we detect values directly after equal character with no quotes |
| 136 |
if (!/[\'\"]/.test(peek)) { |
| 137 |
state.tokenize = inAttributeNoQuote(); |
| 138 |
} |
| 139 |
//end detect values |
| 140 |
|
| 141 |
return "operator"; |
| 142 |
} else if (/[\'\"]/.test(ch)) { |
| 143 |
state.tokenize = inAttribute(ch); |
| 144 |
return state.tokenize(stream, state); |
| 145 |
} else { |
| 146 |
stream.eatWhile(/[^\s\u00a0=\"\'\/?]/); |
| 147 |
return "keyword"; |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
function inAttribute(quote) { |
| 152 |
return function(stream, state) { |
| 153 |
while (!stream.eol()) { |
| 154 |
if (stream.next() == quote) { |
| 155 |
state.tokenize = inPlugin; |
| 156 |
break; |
| 157 |
} |
| 158 |
} |
| 159 |
return "string"; |
| 160 |
}; |
| 161 |
} |
| 162 |
|
| 163 |
function inAttributeNoQuote() { |
| 164 |
return function(stream, state) { |
| 165 |
while (!stream.eol()) { |
| 166 |
var ch = stream.next(); |
| 167 |
var peek = stream.peek(); |
| 168 |
if (ch == " " || ch == "," || /[ )}]/.test(peek)) { |
| 169 |
state.tokenize = inPlugin; |
| 170 |
break; |
| 171 |
} |
| 172 |
} |
| 173 |
return "string"; |
| 174 |
}; |
| 175 |
} |
| 176 |
|
| 177 |
var curState, setStyle; |
| 178 |
function pass() { |
| 179 |
for (var i = arguments.length - 1; i >= 0; i--) curState.cc.push(arguments[i]); |
| 180 |
} |
| 181 |
|
| 182 |
function cont() { |
| 183 |
pass.apply(null, arguments); |
| 184 |
return true; |
| 185 |
} |
| 186 |
|
| 187 |
function pushContext(pluginName, startOfLine) { |
| 188 |
var noIndent = curState.context && curState.context.noIndent; |
| 189 |
curState.context = { |
| 190 |
prev: curState.context, |
| 191 |
pluginName: pluginName, |
| 192 |
indent: curState.indented, |
| 193 |
startOfLine: startOfLine, |
| 194 |
noIndent: noIndent |
| 195 |
}; |
| 196 |
} |
| 197 |
|
| 198 |
function popContext() { |
| 199 |
if (curState.context) curState.context = curState.context.prev; |
| 200 |
} |
| 201 |
|
| 202 |
function element(type) { |
| 203 |
if (type == "openPlugin") {curState.pluginName = pluginName; return cont(attributes, endplugin(curState.startOfLine));} |
| 204 |
else if (type == "closePlugin") { |
| 205 |
var err = false; |
| 206 |
if (curState.context) { |
| 207 |
err = curState.context.pluginName != pluginName; |
| 208 |
popContext(); |
| 209 |
} else { |
| 210 |
err = true; |
| 211 |
} |
| 212 |
if (err) setStyle = "error"; |
| 213 |
return cont(endcloseplugin(err)); |
| 214 |
} |
| 215 |
else if (type == "string") { |
| 216 |
if (!curState.context || curState.context.name != "!cdata") pushContext("!cdata"); |
| 217 |
if (curState.tokenize == inText) popContext(); |
| 218 |
return cont(); |
| 219 |
} |
| 220 |
else return cont(); |
| 221 |
} |
| 222 |
|
| 223 |
function endplugin(startOfLine) { |
| 224 |
return function(type) { |
| 225 |
if ( |
| 226 |
type == "selfclosePlugin" || |
| 227 |
type == "endPlugin" |
| 228 |
) |
| 229 |
return cont(); |
| 230 |
if (type == "endPlugin") {pushContext(curState.pluginName, startOfLine); return cont();} |
| 231 |
return cont(); |
| 232 |
}; |
| 233 |
} |
| 234 |
|
| 235 |
function endcloseplugin(err) { |
| 236 |
return function(type) { |
| 237 |
if (err) setStyle = "error"; |
| 238 |
if (type == "endPlugin") return cont(); |
| 239 |
return pass(); |
| 240 |
}; |
| 241 |
} |
| 242 |
|
| 243 |
function attributes(type) { |
| 244 |
if (type == "keyword") {setStyle = "attribute"; return cont(attributes);} |
| 245 |
if (type == "equals") return cont(attvalue, attributes); |
| 246 |
return pass(); |
| 247 |
} |
| 248 |
function attvalue(type) { |
| 249 |
if (type == "keyword") {setStyle = "string"; return cont();} |
| 250 |
if (type == "string") return cont(attvaluemaybe); |
| 251 |
return pass(); |
| 252 |
} |
| 253 |
function attvaluemaybe(type) { |
| 254 |
if (type == "string") return cont(attvaluemaybe); |
| 255 |
else return pass(); |
| 256 |
} |
| 257 |
export const tiki = { |
| 258 |
name: "tiki", |
| 259 |
startState: function() { |
| 260 |
return {tokenize: inText, cc: [], indented: 0, startOfLine: true, pluginName: null, context: null}; |
| 261 |
}, |
| 262 |
token: function(stream, state) { |
| 263 |
if (stream.sol()) { |
| 264 |
state.startOfLine = true; |
| 265 |
state.indented = stream.indentation(); |
| 266 |
} |
| 267 |
if (stream.eatSpace()) return null; |
| 268 |
|
| 269 |
setStyle = type = pluginName = null; |
| 270 |
var style = state.tokenize(stream, state); |
| 271 |
if ((style || type) && style != "comment") { |
| 272 |
curState = state; |
| 273 |
while (true) { |
| 274 |
var comb = state.cc.pop() || element; |
| 275 |
if (comb(type || style)) break; |
| 276 |
} |
| 277 |
} |
| 278 |
state.startOfLine = false; |
| 279 |
return setStyle || style; |
| 280 |
}, |
| 281 |
indent: function(state, textAfter, cx) { |
| 282 |
var context = state.context; |
| 283 |
if (context && context.noIndent) return 0; |
| 284 |
if (context && /^{\//.test(textAfter)) |
| 285 |
context = context.prev; |
| 286 |
while (context && !context.startOfLine) |
| 287 |
context = context.prev; |
| 288 |
if (context) return context.indent + cx.unit; |
| 289 |
else return 0; |
| 290 |
} |
| 291 |
}; |