Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(979)

Side by Side Diff: Source/devtools/front_end/documentation/WikiParser.js

Issue 539353004: DevTools: [Documentation] Update parser for WikiText (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: add method secondToken() Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 /** 5 /**
6 * @constructor 6 * @constructor
7 * @param {string} wikiMarkupText 7 * @param {string} wikiMarkupText
8 */ 8 */
9 WebInspector.WikiParser = function(wikiMarkupText) 9 WebInspector.WikiParser = function(wikiMarkupText)
10 { 10 {
11 this._position = 0; 11 var text = wikiMarkupText.replace(/&lt;/g, "<")
lushnikov 2014/09/08 14:08:30 lets extact this into utilities.js and call it une
12 this._wikiMarkupText = wikiMarkupText; 12 .replace(/&gt;/g, ">")
13 .replace(/&#58;/g, ":")
14 .replace(/&quot;/g, "\"")
15 .replace(/&#60;/g, "<")
16 .replace(/&#62;/g, ">")
17 .replace(/&amp;/g, "&");
18 this._tokenizer = new WebInspector.WikiParser.Tokenizer(text);
13 this._document = this._parse(); 19 this._document = this._parse();
14 /** @type {?WebInspector.WikiParser.Tokenizer} */
15 this._tokenizer;
16 } 20 }
17 21
18 /** 22 /**
19 * @package
20 * @enum {string}
21 */
22 WebInspector.WikiParser.State = {
23 Error: "Error",
24 FirstOpen: "FirstOpen",
25 SecondOpen: "SecondOpen",
26 Title: "Title",
27 PropertyName: "PropertyName",
28 PropertyValue: "PropertyValue",
29 FirstClose: "FirstClose",
30 SecondClose: "SecondClose"
31 }
32
33 /**
34 * @package
35 * @enum {string}
36 */
37 WebInspector.WikiParser.LinkStates = {
38 Error: "Error",
39 LinkUrl: "LinkUrl",
40 LinkName: "LinkName"
41 }
42
43 /**
44 * @package
45 * @enum {string}
46 */
47 WebInspector.WikiParser.HtmlStates = {
48 Error: "Error",
49 Entry: "Entry",
50 InsideTag: "InsideTag",
51 Exit: "Exit"
52 }
53
54 /**
55 * @package
56 * @enum {string}
57 */
58 WebInspector.WikiParser.ValueState = {
59 Error: "Error",
60 Outside: "Outside",
61 InsideSquare: "InsideSquare"
62 }
63
64 /**
65 * @package 23 * @package
66 * @enum {string} 24 * @enum {string}
67 */ 25 */
68 WebInspector.WikiParser.TokenType = { 26 WebInspector.WikiParser.TokenType = {
27 Text: "Text",
28 Table: "Table",
lushnikov 2014/09/08 14:08:31 do we have any tests for table?
iliia 2014/09/08 15:20:50 we don't support tables at the moment. On 2014/09/
29 OpeningBraces: "OpeningBraces",
30 ClosingBraces: "ClosingBraces",
31 Exclamation: "Exclamation",
32 OpeningBrackets: "OpeningBrackets",
33 ClosingBrackets: "ClosingBrackets",
34 EqualSign: "EqualSign",
35 EqualSignInBraces: "EqualSignInBraces",
36 VerticalLine: "VerticalLine",
69 TripleQuotes: "TripleQuotes", 37 TripleQuotes: "TripleQuotes",
70 OpeningBrackets: "OpeningBrackets",
71 OpeningCodeTag: "OpeningCodeTag", 38 OpeningCodeTag: "OpeningCodeTag",
72 ClosingBrackets: "ClosingBrackets",
73 ClosingCodeTag: "ClosingCodeTag", 39 ClosingCodeTag: "ClosingCodeTag",
74 Bullet: "Bullet", 40 Bullet: "Bullet",
75 Text: "Text",
76 VerticalLine: "VerticalLine",
77 LineEnd: "LineEnd", 41 LineEnd: "LineEnd",
78 CodeBlock: "CodeBlock" 42 CodeBlock: "CodeBlock"
79 } 43 }
80 44
81 /** 45 /**
82 * @constructor 46 * @constructor
83 * @param {string} result 47 * @param {string} result
84 * @param {!WebInspector.WikiParser.TokenType} type 48 * @param {!WebInspector.WikiParser.TokenType} type
85 */ 49 */
86 WebInspector.WikiParser.Token = function(result, type) 50 WebInspector.WikiParser.Token = function(result, type)
(...skipping 12 matching lines...) Expand all
99 }, 63 },
100 64
101 /** 65 /**
102 * @return {!WebInspector.WikiParser.TokenType} 66 * @return {!WebInspector.WikiParser.TokenType}
103 */ 67 */
104 type: function() 68 type: function()
105 { 69 {
106 return this._type; 70 return this._type;
107 } 71 }
108 } 72 }
109
110 /** 73 /**
111 * @constructor 74 * @constructor
112 * @param {string} str 75 * @param {string} str
113 */ 76 */
114 WebInspector.WikiParser.Tokenizer = function(str) 77 WebInspector.WikiParser.Tokenizer = function(str)
115 { 78 {
116 this._text = str; 79 this._text = str;
80 this._token = this._internalNextToken();
117 } 81 }
118 82
119 WebInspector.WikiParser.Tokenizer.prototype = { 83 WebInspector.WikiParser.Tokenizer.prototype = {
120 /** 84 /**
121 * @return {!WebInspector.WikiParser.Token} 85 * @return {!WebInspector.WikiParser.Token}
122 */ 86 */
123 _nextToken: function() 87 peekToken: function()
88 {
89 return this._token;
90 },
91
92 /**
93 * @return {!WebInspector.WikiParser.Token}
94 */
95 nextToken: function()
96 {
97 var token = this._token;
98 this._token = this._internalNextToken();
99 return token;
100 },
101
102 /**
103 * @return {!WebInspector.WikiParser.Token}
104 */
105 _internalNextToken: function()
124 { 106 {
125 if (WebInspector.WikiParser.newLineWithSpace.test(this._text)) { 107 if (WebInspector.WikiParser.newLineWithSpace.test(this._text)) {
126 var result = WebInspector.WikiParser.newLineWithSpace.exec(this._tex t); 108 var result = WebInspector.WikiParser.newLineWithSpace.exec(this._tex t);
127 var begin = result.index + result[0].length; 109 var begin = result.index + result[0].length - 1;
128 var end = this._text.length; 110 var end = this._text.length;
129 var lineEnd = WebInspector.WikiParser.newLineWithoutSpace.exec(this. _text); 111 var lineEnd = WebInspector.WikiParser.newLineWithoutSpace.exec(this. _text);
130 if (lineEnd) 112 if (lineEnd)
131 end = lineEnd.index; 113 end = lineEnd.index;
132 var token = this._text.substring(begin, end).replace(/\n */g, "\n"); 114 var token = this._text.substring(begin, end).replace(/\n /g, "\n").r eplace(/{{=}}/g, "=");
133 this._text = this._text.substring(end + 1); 115 this._text = this._text.substring(end + 1);
134 return new WebInspector.WikiParser.Token(token, WebInspector.WikiPar ser.TokenType.CodeBlock); 116 return new WebInspector.WikiParser.Token(token, WebInspector.WikiPar ser.TokenType.CodeBlock);
135 } 117 }
136 118
137 for (var i = 0; i < WebInspector.WikiParser._tokenDescriptors.length; ++ i) { 119 for (var i = 0; i < WebInspector.WikiParser._tokenDescriptors.length; ++ i) {
138 var result = WebInspector.WikiParser._tokenDescriptors[i].regex.exec (this._text); 120 var result = WebInspector.WikiParser._tokenDescriptors[i].regex.exec (this._text);
139 if (result) { 121 if (result) {
140 this._text = this._text.substring(result.index + result[0].lengt h); 122 this._text = this._text.substring(result.index + result[0].lengt h);
141 return new WebInspector.WikiParser.Token(result[0], WebInspector .WikiParser._tokenDescriptors[i].type); 123 return new WebInspector.WikiParser.Token(result[0], WebInspector .WikiParser._tokenDescriptors[i].type);
142 } 124 }
143 } 125 }
144 126
145 for (var lastIndex = 0; lastIndex < this._text.length; ++lastIndex) { 127 for (var lastIndex = 0; lastIndex < this._text.length; ++lastIndex) {
146 var testString = this._text.substring(lastIndex); 128 var testString = this._text.substring(lastIndex);
147 for (var i = 0; i < WebInspector.WikiParser._tokenDescriptors.length ; ++i) { 129 for (var i = 0; i < WebInspector.WikiParser._tokenDescriptors.length ; ++i) {
148 if (WebInspector.WikiParser._tokenDescriptors[i].regex.test(test String)) { 130 if (WebInspector.WikiParser._tokenDescriptors[i].regex.test(test String)) {
149 var token = this._text.substring(0, lastIndex); 131 var token = this._text.substring(0, lastIndex);
150 this._text = this._text.substring(lastIndex); 132 this._text = this._text.substring(lastIndex);
151 return new WebInspector.WikiParser.Token(token, WebInspector .WikiParser.TokenType.Text); 133 return new WebInspector.WikiParser.Token(token, WebInspector .WikiParser.TokenType.Text);
152 } 134 }
153 } 135 }
154 } 136 }
155 137
156 var token = this._text; 138 var token = this._text;
157 this._text = ""; 139 this._text = "";
158 return new WebInspector.WikiParser.Token(token, WebInspector.WikiParser. TokenType.Text); 140 return new WebInspector.WikiParser.Token(token, WebInspector.WikiParser. TokenType.Text);
159 }, 141 },
160 142
161 /** 143 /**
144 * @return {?WebInspector.WikiParser.TokenType}
145 */
146 secondToken: function()
147 {
148 var tokenizer = new WebInspector.WikiParser.Tokenizer(this._text);
149 if (tokenizer.hasMoreTokens())
150 return tokenizer.nextToken().type();
151 return null;
152 },
153
154 /**
162 * @return {boolean} 155 * @return {boolean}
163 */ 156 */
164 _hasMoreTokens: function() 157 hasMoreTokens: function()
165 { 158 {
166 return !!this._text.length; 159 return !!this._text.length;
167 } 160 }
168 } 161 }
169 162
163 WebInspector.WikiParser.table = /^{{{!}}/;
164 WebInspector.WikiParser.exclamation = /^{{!}}/;
165 WebInspector.WikiParser.openingBraces = /^{{/;
166 WebInspector.WikiParser.equalSign = /^=/;
167 WebInspector.WikiParser.equalSignInBraces = /^{{=}}/;
168 WebInspector.WikiParser.closingBraces = /^\s*}}/;
169 WebInspector.WikiParser.oneOpeningBracket = /^\n* \[[^\[]/;
170 WebInspector.WikiParser.twoOpeningBrackets = /^\n* \[\[/;
171 WebInspector.WikiParser.oneClosingBracket = /^\n*\][^\]]/;
172 WebInspector.WikiParser.twoClosingBrackets = /^\n*\]\]/;
173 WebInspector.WikiParser.tripleQuotes = /^\n*'''/;
174 WebInspector.WikiParser.openingCodeTag = /^<\s*code\s*>/;
175 WebInspector.WikiParser.closingCodeTag = /^<\s*\/\s*code\s*>/;
176 WebInspector.WikiParser.closingBullet = /^\*/;
177 WebInspector.WikiParser.lineEnd = /^\n/;
178 WebInspector.WikiParser.verticalLine = /^\n*\|/;
179 WebInspector.WikiParser.newLineWithSpace = /^\n [^ ]/;
180 WebInspector.WikiParser.newLineWithoutSpace = /\n[^ ]/;
181
182 /**
183 * @constructor
184 * @param {!RegExp} regex
185 * @param {!WebInspector.WikiParser.TokenType} type
186 */
187 WebInspector.WikiParser.TokenDescriptor = function(regex, type)
188 {
189 this.regex = regex;
190 this.type = type;
191 }
192
193 WebInspector.WikiParser._tokenDescriptors = [
194 new WebInspector.WikiParser.TokenDescriptor(WebInspector.WikiParser.exclamat ion, WebInspector.WikiParser.TokenType.Exclamation),
195 new WebInspector.WikiParser.TokenDescriptor(WebInspector.WikiParser.equalSig nInBraces, WebInspector.WikiParser.TokenType.EqualSignInBraces),
196 new WebInspector.WikiParser.TokenDescriptor(WebInspector.WikiParser.equalSig n, WebInspector.WikiParser.TokenType.EqualSign),
197 new WebInspector.WikiParser.TokenDescriptor(WebInspector.WikiParser.table, W ebInspector.WikiParser.TokenType.Table),
198 new WebInspector.WikiParser.TokenDescriptor(WebInspector.WikiParser.openingB races, WebInspector.WikiParser.TokenType.OpeningBraces),
199 new WebInspector.WikiParser.TokenDescriptor(WebInspector.WikiParser.vertical Line, WebInspector.WikiParser.TokenType.VerticalLine),
200 new WebInspector.WikiParser.TokenDescriptor(WebInspector.WikiParser.closingB races, WebInspector.WikiParser.TokenType.ClosingBraces),
201 new WebInspector.WikiParser.TokenDescriptor(WebInspector.WikiParser.oneOpeni ngBracket, WebInspector.WikiParser.TokenType.OpeningBrackets),
202 new WebInspector.WikiParser.TokenDescriptor(WebInspector.WikiParser.twoOpeni ngBrackets, WebInspector.WikiParser.TokenType.OpeningBrackets),
203 new WebInspector.WikiParser.TokenDescriptor(WebInspector.WikiParser.oneClosi ngBracket, WebInspector.WikiParser.TokenType.ClosingBrackets),
204 new WebInspector.WikiParser.TokenDescriptor(WebInspector.WikiParser.twoClosi ngBrackets, WebInspector.WikiParser.TokenType.ClosingBrackets),
205 new WebInspector.WikiParser.TokenDescriptor(WebInspector.WikiParser.newLineW ithSpace, WebInspector.WikiParser.TokenType.CodeBlock),
206 new WebInspector.WikiParser.TokenDescriptor(WebInspector.WikiParser.tripleQu otes, WebInspector.WikiParser.TokenType.TripleQuotes),
207 new WebInspector.WikiParser.TokenDescriptor(WebInspector.WikiParser.openingC odeTag, WebInspector.WikiParser.TokenType.OpeningCodeTag),
208 new WebInspector.WikiParser.TokenDescriptor(WebInspector.WikiParser.closingC odeTag, WebInspector.WikiParser.TokenType.ClosingCodeTag),
209 new WebInspector.WikiParser.TokenDescriptor(WebInspector.WikiParser.closingB ullet, WebInspector.WikiParser.TokenType.Bullet),
210 new WebInspector.WikiParser.TokenDescriptor(WebInspector.WikiParser.lineEnd, WebInspector.WikiParser.TokenType.LineEnd)
211 ]
212
213 /**
214 * @package
215 * @enum {string}
216 */
217 WebInspector.WikiParser.State = {
lushnikov 2014/09/08 14:08:30 looks like you don't need this
iliia 2014/09/08 15:20:50 Done.
218 Error: "Error",
219 FirstOpen: "FirstOpen",
220 SecondOpen: "SecondOpen",
221 Title: "Title",
222 PropertyName: "PropertyName",
223 PropertyValue: "PropertyValue",
224 FirstClose: "FirstClose",
225 SecondClose: "SecondClose"
226 }
227
228 /**
229 * @package
230 * @enum {string}
231 */
232 WebInspector.WikiParser.HtmlStates = {
lushnikov 2014/09/08 14:08:30 and this
iliia 2014/09/08 15:20:51 Done.
233 Error: "Error",
234 Entry: "Entry",
235 InsideTag: "InsideTag",
236 Exit: "Exit"
237 }
238
239 /**
240 * @package
241 * @enum {string}
242 */
243 WebInspector.WikiParser.ValueState = {
lushnikov 2014/09/08 14:08:31 and even this
iliia 2014/09/08 15:20:50 Done.
244 Error: "Error",
245 Outside: "Outside",
246 InsideSquare: "InsideSquare"
247 }
248
170 WebInspector.WikiParser.prototype = { 249 WebInspector.WikiParser.prototype = {
171 /** 250 /**
172 * @return {!Object} 251 * @return {!Object}
173 */ 252 */
174 document: function() 253 document: function()
175 { 254 {
176 return this._document; 255 return this._document;
177 }, 256 },
178 257
179 /** 258 /**
180 * @return {!Object} 259 * @return {!Object}
181 */ 260 */
182 _parse: function() 261 _parse: function()
183 { 262 {
184 var obj = {}; 263 var obj = {};
185 this._wikiMarkupText = this._wikiMarkupText.replace(/&lt;/g, "<") 264 while (this._tokenizer.hasMoreTokens()) {
186 .replace(/&gt;/g, ">")
187 .replace(/&#58;/g, ":")
188 .replace(/&quot;/g, "\"")
189 .replace(/&#60;/g, "<")
190 .replace(/&#62;/g, ">")
191 .replace(/{{=}}/g, "=")
192 .replace(/{{!}}/g, "|")
193 .replace(/&amp;/g, "&");
194 while (this._position < this._wikiMarkupText.length) {
195 var field = this._parseField(); 265 var field = this._parseField();
196 for (var key in field) { 266 for (var key in field) {
197 console.assert(typeof obj[key] === "undefined", "Duplicate key: " + key); 267 console.assert(typeof obj[key] === "undefined", "Duplicate key: " + key);
198 obj[key] = field[key]; 268 obj[key] = field[key];
199 } 269 }
200 } 270 }
201 return obj; 271 return obj;
202 }, 272 },
203 273
204 /** 274 /**
275 * @return {?Object}
276 */
277 _parseField: function()
278 {
279 var obj = {};
lushnikov 2014/09/08 14:08:30 obj is not a good name here as it doesn't give any
iliia 2014/09/08 15:20:50 Done.
280 if (!this._tokenizer.hasMoreTokens() || this._tokenizer.nextToken().type () !== WebInspector.WikiParser.TokenType.OpeningBraces)
281 return obj;
282
283 var title = this._deleteTrailingSpaces(this._parseTitle());
284 if (!title.length)
285 return obj;
286
287 obj[title] = {};
288 if (this._tokenizer.peekToken().type() == WebInspector.WikiParser.TokenT ype.ClosingBraces) {
lushnikov 2014/09/08 14:08:31 ===
iliia 2014/09/08 15:20:50 Done.
289 this._tokenizer.nextToken();
290 return obj;
291 }
292 if (!this._tokenizer.secondToken() || this._tokenizer.secondToken() !== WebInspector.WikiParser.TokenType.EqualSign) {
293 var propertyValue = this._parseString();
294 obj[title] = propertyValue;
295 } else {
296 while (this._tokenizer.hasMoreTokens()) {
297 var propertyName = this._parseName();
298 var token = this._tokenizer.peekToken();
299 switch (token.type()) {
300 case WebInspector.WikiParser.TokenType.OpeningBraces:
301 while (this._tokenizer.peekToken().type() === WebInspector.W ikiParser.TokenType.OpeningBraces) {
lushnikov 2014/09/08 14:08:30 this is while, inside case, inside while, inside i
iliia 2014/09/08 15:20:50 Done.
302 if (!Array.isArray(obj[title][propertyName]))
303 obj[title][propertyName] = [];
304 obj[title][propertyName].push(this._parseField());
305 }
306 if (this._tokenizer.peekToken().type() === WebInspector.Wiki Parser.TokenType.VerticalLine)
307 this._tokenizer.nextToken();
308 break;
309 case WebInspector.WikiParser.TokenType.LineEnd:
310 this._tokenizer.nextToken();
311 break;
312 case WebInspector.WikiParser.TokenType.ClosingBraces:
313 this._tokenizer.nextToken();
314 return obj;
315 break;
316 default:
317 if (propertyName === "Code")
318 obj[title][propertyName] = this._parseExampleCode();
319 else
320 obj[title][propertyName] = this._parseString();
321 }
322 }
323 }
324 var token = this._tokenizer.nextToken();
325 if (token.type() != WebInspector.WikiParser.TokenType.ClosingBraces)
lushnikov 2014/09/08 14:08:30 !==
iliia 2014/09/08 15:20:50 Done.
326 throw new Error("Two closing braces expected; found " + token.value( ));
327
328 return obj;
329 },
330
331 /**
205 * @return {string} 332 * @return {string}
206 */ 333 */
207 _parseValue: function() { 334 _parseTitle: function()
208 var states = WebInspector.WikiParser.ValueState; 335 {
209 var state = states.Outside; 336 var title = "";
210 var value = ""; 337 while (this._tokenizer.hasMoreTokens()) {
211 while (this._position < this._wikiMarkupText.length) { 338 var token = this._tokenizer.peekToken();
212 switch (state) { 339 switch (token.type()) {
213 case states.Outside: 340 case WebInspector.WikiParser.TokenType.ClosingBraces:
214 if (this._wikiMarkupText[this._position] === "|" || (this._wikiM arkupText[this._position] === "}" && this._wikiMarkupText[this._position + 1] == = "}")) 341 return title;
215 return value; 342 case WebInspector.WikiParser.TokenType.VerticalLine:
216 switch (this._wikiMarkupText[this._position]) { 343 this._tokenizer.nextToken();
217 case "<": 344 return title;
218 var indexClose = this._wikiMarkupText.indexOf(">", this._pos ition); 345 case WebInspector.WikiParser.TokenType.Text:
219 if (indexClose !== -1) { 346 title += this._tokenizer.nextToken().value();
220 value += this._wikiMarkupText.substring(this._position, indexClose + 1);
221 this._position = indexClose;
222 }
223 break;
224 case "[":
225 state = states.InsideSquare;
226 value += this._wikiMarkupText[this._position];
227 break;
228 default:
229 value += this._wikiMarkupText[this._position];
230 }
231 break; 347 break;
232 case states.InsideSquare: 348 default:
233 if (this._wikiMarkupText[this._position] === "[") { 349 return "Wrong title";
lushnikov 2014/09/08 14:08:30 throw exception
iliia 2014/09/08 15:20:50 Done.
iliia 2014/09/08 15:20:50 Done.
234 var indexClose = this._wikiMarkupText.indexOf("]]", this._po sition);
235 if (indexClose !== -1) {
236 value += this._wikiMarkupText.substring(this._position, indexClose + 2);
237 this._position = indexClose + 1;
238 }
239 } else {
240 var indexClose = this._wikiMarkupText.indexOf("]", this._pos ition);
241 if (indexClose !== -1) {
242 value += this._wikiMarkupText.substring(this._position, indexClose + 1);
243 this._position = indexClose;
244 }
245 }
246 state = states.Outside;
247 break;
248 } 350 }
249 this._position++;
250 } 351 }
251 return value; 352 return title;
252 }, 353 },
253 354
254 /** 355 /**
255 * @return {!Object} 356 * @return {string}
256 */ 357 */
257 _parseField: function() 358 _parseName: function()
258 { 359 {
259 var obj = {}; 360 var name = "";
260 var title = ""; 361 while (this._tokenizer.hasMoreTokens()) {
261 var propertyName = ""; 362 var token = this._tokenizer.peekToken();
262 var propertyValue = ""; 363 switch (token.type()) {
263 var states = WebInspector.WikiParser.State; 364 case WebInspector.WikiParser.TokenType.ClosingBraces:
264 var state = states.FirstOpen; 365 return name;
265 while (this._position < this._wikiMarkupText.length) { 366 case WebInspector.WikiParser.TokenType.EqualSign:
266 var skipIncrement = false; 367 this._tokenizer.nextToken();
267 switch (state) { 368 return name;
268 case states.FirstOpen: 369 case WebInspector.WikiParser.TokenType.VerticalLine:
269 if (this._wikiMarkupText[this._position] === "{") 370 case WebInspector.WikiParser.TokenType.Text:
270 state = states.SecondOpen; 371 name += this._tokenizer.nextToken().value();
271 else
272 state = states.Error;
273 break; 372 break;
274 case states.SecondOpen: 373 default:
275 if (this._wikiMarkupText[this._position] === "{") 374 return "Wrong name";
lushnikov 2014/09/08 14:08:31 nope, you want to throw here
iliia 2014/09/08 15:20:50 Done.
276 state = states.Title;
277 else
278 state = states.Error;
279 break;
280 case states.Title:
281 if (this._wikiMarkupText[this._position] === "|") {
282 title = this._deleteTrailingSpaces(title);
283 if (title !== "")
284 obj[title] = {};
285 state = states.PropertyName;
286 } else if (this._wikiMarkupText[this._position] === "}") {
287 title = this._deleteTrailingSpaces(title);
288 if (title !== "")
289 obj[title] = {};
290 state = states.FirstClose;
291 } else {
292 title += (this._wikiMarkupText[this._position] === "\n" ? "" : this._wikiMarkupText[this._position]);
293 }
294 break;
295 case states.PropertyName:
296 if (this._wikiMarkupText[this._position] === "=") {
297 state = states.PropertyValue;
298 this._deleteTrailingSpaces(propertyName);
299 if (propertyName !== "")
300 obj[title][propertyName] = [];
301 } else {
302 if (this._wikiMarkupText[this._position] === "}") {
303 propertyName = this._deleteTrailingSpaces(propertyName);
304 obj[title] = propertyName;
305 state = states.FirstClose;
306 } else {
307 propertyName += this._wikiMarkupText[this._position];
308 }
309 }
310 break;
311 case states.PropertyValue:
312 if (this._wikiMarkupText[this._position] === "{" && this._wikiMa rkupText[this._position + 1] === "{") {
313 propertyValue = this._parseField();
314 obj[title][propertyName].push(propertyValue);
315 propertyValue = "";
316 skipIncrement = true;
317 } else if (this._wikiMarkupText[this._position] === "|") {
318 propertyValue = this._deleteTrailingSpaces(propertyValue);
319 if (propertyValue !== "")
320 obj[title][propertyName] = propertyValue;
321
322 state = states.PropertyName;
323 if (Array.isArray(obj[title][propertyName]) && obj[title][pr opertyName].length === 1) {
324 var newObj = obj[title][propertyName][0];
325 obj[title][propertyName] = newObj;
326 }
327
328 propertyName = "";
329 propertyValue = "";
330 } else if (this._position + 1 < this._wikiMarkupText.length && t his._wikiMarkupText[this._position] === "}" && this._wikiMarkupText[this._positi on + 1] === "}") {
331 propertyValue = this._deleteTrailingSpaces(propertyValue);
332 if (propertyValue !== "")
333 obj[title][propertyName].push(propertyValue);
334 if (Array.isArray(obj[title][propertyName]) && obj[title][pr opertyName].length === 1) {
335 var newObj = obj[title][propertyName][0];
336 obj[title][propertyName] = newObj;
337 }
338
339 propertyValue = "";
340 state = states.FirstClose;
341 } else {
342 propertyValue = this._parseValue();
343 skipIncrement = true;
344 }
345 break;
346 case states.FirstClose:
347 if (this._wikiMarkupText[this._position] === "}")
348 state = states.SecondClose;
349 else
350 state = states.Error;
351 break;
352 case states.SecondClose:
353 while (this._position < this._wikiMarkupText.length && this._wik iMarkupText[this._position] === "\n")
354 this._position++;
355 return obj;
356 case states.Error:
357 this._position = this._wikiMarkupText.length;
358 return {};
359 } 375 }
360 if (!skipIncrement)
361 this._position++;
362 } 376 }
363 return obj; 377 return name;
364 }, 378 },
365 379
366 /** 380 /**
367 * @param {string} str 381 * @return {string}
382 */
383 _parseExampleCode: function()
384 {
385 var code = "";
386 while (this._tokenizer.hasMoreTokens()) {
387 var token = this._tokenizer.peekToken();
388 switch (token.type()) {
389 case WebInspector.WikiParser.TokenType.ClosingBraces:
390 return code;
391 case WebInspector.WikiParser.TokenType.VerticalLine:
392 this._tokenizer.nextToken();
393 return code;
394 case WebInspector.WikiParser.TokenType.Exclamation:
395 this._tokenizer.nextToken();
396 code += '|';
397 break;
398 case WebInspector.WikiParser.TokenType.EqualSignInBraces:
lushnikov 2014/09/08 14:08:30 looks like this branch might be omitted, as Defaul
iliia 2014/09/08 15:20:51 in this case we should add "=", but not "{{=}}". O
399 this._tokenizer.nextToken();
400 code += "=";
401 break;
402 default:
403 this._tokenizer.nextToken();
404 code += token.value();
405 }
406 }
407 return code;
408 },
409
410 /**
368 * @return {?WebInspector.WikiParser.Block} 411 * @return {?WebInspector.WikiParser.Block}
369 */ 412 */
370 parseString: function(str) 413 _parseString: function()
371 { 414 {
372 this._tokenizer = new WebInspector.WikiParser.Tokenizer(str);
373 var children = []; 415 var children = [];
374 var blockChildren = []; 416 var blockChildren = [];
375 var text = ""; 417 var text = "";
376 var self = this; 418 var self = this;
377 419
378 function processSimpleText() 420 function processSimpleText()
379 { 421 {
380 var currentText = self._deleteTrailingSpaces(text); 422 var currentText = self._deleteTrailingSpaces(text);
381 if (!currentText.length) 423 if (!currentText.length)
382 return; 424 return;
383 var simpleText = new WebInspector.WikiParser.PlainText(currentText, false); 425 var simpleText = new WebInspector.WikiParser.PlainText(currentText, false);
384 blockChildren.push(simpleText); 426 blockChildren.push(simpleText);
385 text = ""; 427 text = "";
386 } 428 }
387 429
388 function processBlock() 430 function processBlock()
389 { 431 {
390 if (blockChildren.length) { 432 if (blockChildren.length) {
391 children.push(new WebInspector.WikiParser.Block(blockChildren, f alse)); 433 children.push(new WebInspector.WikiParser.Block(blockChildren, f alse));
392 blockChildren = []; 434 blockChildren = [];
393 } 435 }
394 } 436 }
395 while (this._tokenizer._hasMoreTokens()) { 437 while (this._tokenizer.hasMoreTokens()) {
396 var token = this._tokenizer._nextToken(); 438 var token = this._tokenizer.peekToken();
397 switch (token.type()) { 439 switch (token.type()) {
440 case WebInspector.WikiParser.TokenType.VerticalLine:
441 this._tokenizer.nextToken();
lushnikov 2014/09/08 14:08:30 why missing break here?
iliia 2014/09/08 15:20:51 because we must do exactly the same as in case of
442 case WebInspector.WikiParser.TokenType.ClosingBraces:
443 processSimpleText();
444 processBlock();
445 return new WebInspector.WikiParser.Block(children, false);
398 case WebInspector.WikiParser.TokenType.TripleQuotes: 446 case WebInspector.WikiParser.TokenType.TripleQuotes:
447 this._tokenizer.nextToken();
399 processSimpleText(); 448 processSimpleText();
400 var highlightText = this._parseHighlight(); 449 var highlightText = this._parseHighlight();
401 blockChildren.push(highlightText) 450 blockChildren.push(highlightText)
402 break; 451 break;
403 case WebInspector.WikiParser.TokenType.OpeningBrackets: 452 case WebInspector.WikiParser.TokenType.OpeningBrackets:
453 this._tokenizer.nextToken();
404 processSimpleText(); 454 processSimpleText();
405 var link = this._parseLink(); 455 var link = this._parseLink();
406 blockChildren.push(link); 456 blockChildren.push(link);
407 break; 457 break;
408 case WebInspector.WikiParser.TokenType.OpeningCodeTag: 458 case WebInspector.WikiParser.TokenType.OpeningCodeTag:
459 this._tokenizer.nextToken();
409 processSimpleText(); 460 processSimpleText();
410 var code = this._parseCode(); 461 var code = this._parseCode();
411 blockChildren.push(code); 462 blockChildren.push(code);
412 break; 463 break;
413 case WebInspector.WikiParser.TokenType.Bullet: 464 case WebInspector.WikiParser.TokenType.Bullet:
465 this._tokenizer.nextToken();
414 processSimpleText(); 466 processSimpleText();
415 processBlock(); 467 processBlock();
416 var bulletText = this._parseBullet(); 468 var bulletText = this._parseBullet();
417 children.push(bulletText); 469 children.push(bulletText);
418 break; 470 break;
419 case WebInspector.WikiParser.TokenType.CodeBlock: 471 case WebInspector.WikiParser.TokenType.CodeBlock:
472 this._tokenizer.nextToken();
420 processSimpleText(); 473 processSimpleText();
421 processBlock(); 474 processBlock();
422 var code = new WebInspector.WikiParser.CodeBlock(token.value()); 475 var code = new WebInspector.WikiParser.CodeBlock(token.value());
423 children.push(code); 476 children.push(code);
424 break; 477 break;
425 case WebInspector.WikiParser.TokenType.LineEnd: 478 case WebInspector.WikiParser.TokenType.LineEnd:
479 this._tokenizer.nextToken();
426 processSimpleText(); 480 processSimpleText();
427 processBlock(); 481 processBlock();
428 break; 482 break;
483 case WebInspector.WikiParser.TokenType.EqualSignInBraces:
484 this._tokenizer.nextToken();
485 text += "=";
486 break;
487 case WebInspector.WikiParser.TokenType.Exclamation:
488 this._tokenizer.nextToken();
489 text += "|";
490 break;
429 case WebInspector.WikiParser.TokenType.VerticalLine: 491 case WebInspector.WikiParser.TokenType.VerticalLine:
430 case WebInspector.WikiParser.TokenType.Text: 492 case WebInspector.WikiParser.TokenType.Text:
493 this._tokenizer.nextToken();
431 text += token.value(); 494 text += token.value();
432 break; 495 break;
433 default: 496 default:
497 this._tokenizer.nextToken();
434 return null; 498 return null;
435 } 499 }
436 } 500 }
437 501
438 processSimpleText(); 502 processSimpleText();
439 processBlock(); 503 processBlock();
440 504
441 return new WebInspector.WikiParser.Block(children, false); 505 return new WebInspector.WikiParser.Block(children, false);
442 }, 506 },
443 507
444 /** 508 /**
445 * @return {!WebInspector.WikiParser.Link} 509 * @return {!WebInspector.WikiParser.Link}
446 */ 510 */
447 _parseLink: function() 511 _parseLink: function()
448 { 512 {
449 var url = ""; 513 var url = "";
450 var children = []; 514 var children = [];
451 while (this._tokenizer._hasMoreTokens()) { 515 while (this._tokenizer.hasMoreTokens()) {
452 var token = this._tokenizer._nextToken(); 516 var token = this._tokenizer.nextToken();
453 switch (token.type()) { 517 switch (token.type()) {
454 case WebInspector.WikiParser.TokenType.ClosingBrackets: 518 case WebInspector.WikiParser.TokenType.ClosingBrackets:
455 return new WebInspector.WikiParser.Link(url, children); 519 return new WebInspector.WikiParser.Link(url, children);
456 case WebInspector.WikiParser.TokenType.VerticalLine: 520 case WebInspector.WikiParser.TokenType.VerticalLine:
521 case WebInspector.WikiParser.TokenType.Exclamation:
457 children.push(this._parseLinkName()); 522 children.push(this._parseLinkName());
458 return new WebInspector.WikiParser.Link(url, children); 523 return new WebInspector.WikiParser.Link(url, children);
459 default: 524 default:
460 url += token.value(); 525 url += token.value();
461 } 526 }
462 } 527 }
463 528
464 return new WebInspector.WikiParser.Link(url, children); 529 return new WebInspector.WikiParser.Link(url, children);
465 }, 530 },
466 531
467 /** 532 /**
468 * @return {!WebInspector.WikiParser.Inline} 533 * @return {!WebInspector.WikiParser.Inline}
469 */ 534 */
470 _parseLinkName: function() 535 _parseLinkName: function()
471 { 536 {
472 var children = []; 537 var children = [];
473 while (this._tokenizer._hasMoreTokens()) { 538 while (this._tokenizer.hasMoreTokens()) {
474 var token = this._tokenizer._nextToken(); 539 var token = this._tokenizer.nextToken();
475 switch (token.type()) { 540 switch (token.type()) {
476 case WebInspector.WikiParser.TokenType.ClosingBrackets: 541 case WebInspector.WikiParser.TokenType.ClosingBrackets:
477 return new WebInspector.WikiParser.Inline(WebInspector.WikiParse r.ArticleElement.Type.Inline, children); 542 return new WebInspector.WikiParser.Inline(WebInspector.WikiParse r.ArticleElement.Type.Inline, children);
478 case WebInspector.WikiParser.TokenType.OpeningCodeTag: 543 case WebInspector.WikiParser.TokenType.OpeningCodeTag:
479 children.push(this._parseCode()); 544 children.push(this._parseCode());
480 break; 545 break;
481 default: 546 default:
482 children.push(new WebInspector.WikiParser.PlainText(token.value( ), false)); 547 children.push(new WebInspector.WikiParser.PlainText(token.value( ), false));
483 break; 548 break;
484 } 549 }
485 } 550 }
486 551
487 return new WebInspector.WikiParser.Inline(WebInspector.WikiParser.Articl eElement.Type.Inline, children); 552 return new WebInspector.WikiParser.Inline(WebInspector.WikiParser.Articl eElement.Type.Inline, children);
488 }, 553 },
489 554
490 /** 555 /**
491 * @return {!WebInspector.WikiParser.Inline} 556 * @return {!WebInspector.WikiParser.Inline}
492 */ 557 */
493 _parseCode : function() 558 _parseCode : function()
494 { 559 {
495 var children = []; 560 var children = [];
496 var text = ""; 561 var text = "";
497 while (this._tokenizer._hasMoreTokens()) { 562 while (this._tokenizer.hasMoreTokens()) {
498 var token = this._tokenizer._nextToken(); 563 var token = this._tokenizer.nextToken();
499 switch (token.type()) { 564 switch (token.type()) {
500 case WebInspector.WikiParser.TokenType.ClosingCodeTag: 565 case WebInspector.WikiParser.TokenType.ClosingCodeTag:
501 text = this._deleteTrailingSpaces(text); 566 text = this._deleteTrailingSpaces(text);
502 if (text.length) { 567 if (text.length) {
503 var simpleText = new WebInspector.WikiParser.PlainText(text, false); 568 var simpleText = new WebInspector.WikiParser.PlainText(text, false);
504 children.push(simpleText); 569 children.push(simpleText);
505 text = ""; 570 text = "";
506 } 571 }
507 var code = new WebInspector.WikiParser.Inline(WebInspector.WikiP arser.ArticleElement.Type.Code, children); 572 var code = new WebInspector.WikiParser.Inline(WebInspector.WikiP arser.ArticleElement.Type.Code, children);
508 return code; 573 return code;
(...skipping 12 matching lines...) Expand all
521 586
522 return new WebInspector.WikiParser.Inline(WebInspector.WikiParser.Articl eElement.Type.Code, children); 587 return new WebInspector.WikiParser.Inline(WebInspector.WikiParser.Articl eElement.Type.Code, children);
523 }, 588 },
524 589
525 /** 590 /**
526 * @return {!WebInspector.WikiParser.Block} 591 * @return {!WebInspector.WikiParser.Block}
527 */ 592 */
528 _parseBullet: function() 593 _parseBullet: function()
529 { 594 {
530 var children = []; 595 var children = [];
531 while (this._tokenizer._hasMoreTokens()) { 596 while (this._tokenizer.hasMoreTokens()) {
532 var token = this._tokenizer._nextToken() 597 var token = this._tokenizer.nextToken()
533 switch (token.type()) { 598 switch (token.type()) {
534 case WebInspector.WikiParser.TokenType.OpeningBrackets: 599 case WebInspector.WikiParser.TokenType.OpeningBrackets:
535 children.push(this._parseLink()); 600 children.push(this._parseLink());
536 break; 601 break;
537 case WebInspector.WikiParser.TokenType.OpeningCodeTag: 602 case WebInspector.WikiParser.TokenType.OpeningCodeTag:
538 children.push(this._parseCode()); 603 children.push(this._parseCode());
539 break; 604 break;
540 case WebInspector.WikiParser.TokenType.LineEnd: 605 case WebInspector.WikiParser.TokenType.LineEnd:
541 return new WebInspector.WikiParser.Block(children, true); 606 return new WebInspector.WikiParser.Block(children, true);
542 default: 607 default:
543 var text = this._deleteTrailingSpaces(token.value()); 608 var text = this._deleteTrailingSpaces(token.value());
544 if (text.length) { 609 if (text.length) {
545 var simpleText = new WebInspector.WikiParser.PlainText(text, false); 610 var simpleText = new WebInspector.WikiParser.PlainText(text, false);
546 children.push(simpleText); 611 children.push(simpleText);
547 text = ""; 612 text = "";
548 } 613 }
549 } 614 }
550 } 615 }
551 616
552 return new WebInspector.WikiParser.Block(children, true); 617 return new WebInspector.WikiParser.Block(children, true);
553 }, 618 },
554 619
555 /** 620 /**
556 * @return {!WebInspector.WikiParser.PlainText} 621 * @return {!WebInspector.WikiParser.PlainText}
557 */ 622 */
558 _parseHighlight: function() 623 _parseHighlight: function()
559 { 624 {
560 var text = ""; 625 var text = "";
561 while (this._tokenizer._hasMoreTokens()) { 626 while (this._tokenizer.hasMoreTokens()) {
562 var token = this._tokenizer._nextToken() 627 var token = this._tokenizer.nextToken()
563 switch (token.type()) { 628 switch (token.type()) {
564 case WebInspector.WikiParser.TokenType.TripleQuotes: 629 case WebInspector.WikiParser.TokenType.TripleQuotes:
565 text = this._deleteTrailingSpaces(text); 630 text = this._deleteTrailingSpaces(text);
566 return new WebInspector.WikiParser.PlainText(text, true); 631 return new WebInspector.WikiParser.PlainText(text, true);
567 default: 632 default:
568 text += token.value(); 633 text += token.value();
569 } 634 }
570 } 635 }
571 return new WebInspector.WikiParser.PlainText(text, true); 636 return new WebInspector.WikiParser.PlainText(text, true);
572 }, 637 },
573 638
574 /** 639 /**
575 * @param {string} str 640 * @param {string} str
576 * @return {string} 641 * @return {string}
577 */ 642 */
578 _deleteTrailingSpaces: function(str) 643 _deleteTrailingSpaces: function(str)
579 { 644 {
580 return str.replace(/[\n\r]*$/gm, ""); 645 return str.replace(/[\n\r]*$/gm, "");
581 } 646 }
582 } 647 }
583 648
584 WebInspector.WikiParser.oneOpeningBracket = /^\n* \[[^\[]/;
585 WebInspector.WikiParser.twoOpeningBrackets = /^\n* \[\[/;
586 WebInspector.WikiParser.oneClosingBracket = /^\n*\][^\]] /;
587 WebInspector.WikiParser.twoClosingBrackets = /^\n*\]\]/;
588 WebInspector.WikiParser.tripleQuotes = /^\n*'''/;
589 WebInspector.WikiParser.openingCodeTag = /^<\s*code\s*>/;
590 WebInspector.WikiParser.closingCodeTag = /^<\s*\/\s*code\s*>/;
591 WebInspector.WikiParser.closingBullet = /^\*/;
592 WebInspector.WikiParser.lineEnd = /^\n/;
593 WebInspector.WikiParser.verticalLine = /^\|/;
594 WebInspector.WikiParser.newLineWithSpace = /^\n /;
595 WebInspector.WikiParser.newLineWithoutSpace = /\n[^ ]/;
596
597 /**
598 * @constructor
599 * @param {!RegExp} regex
600 * @param {!WebInspector.WikiParser.TokenType} type
601 */
602 WebInspector.WikiParser.TokenDescriptor = function(regex, type)
603 {
604 this.regex = regex;
605 this.type = type;
606 }
607
608 WebInspector.WikiParser._tokenDescriptors = [
609 new WebInspector.WikiParser.TokenDescriptor(WebInspector.WikiParser.newLineW ithSpace, WebInspector.WikiParser.TokenType.CodeBlock),
610 new WebInspector.WikiParser.TokenDescriptor(WebInspector.WikiParser.tripleQu otes, WebInspector.WikiParser.TokenType.TripleQuotes),
611 new WebInspector.WikiParser.TokenDescriptor(WebInspector.WikiParser.oneOpeni ngBracket, WebInspector.WikiParser.TokenType.OpeningBrackets),
612 new WebInspector.WikiParser.TokenDescriptor(WebInspector.WikiParser.twoOpeni ngBrackets, WebInspector.WikiParser.TokenType.OpeningBrackets),
613 new WebInspector.WikiParser.TokenDescriptor(WebInspector.WikiParser.oneClosi ngBracket, WebInspector.WikiParser.TokenType.ClosingBrackets),
614 new WebInspector.WikiParser.TokenDescriptor(WebInspector.WikiParser.twoClosi ngBrackets, WebInspector.WikiParser.TokenType.ClosingBrackets),
615 new WebInspector.WikiParser.TokenDescriptor(WebInspector.WikiParser.openingC odeTag, WebInspector.WikiParser.TokenType.OpeningCodeTag),
616 new WebInspector.WikiParser.TokenDescriptor(WebInspector.WikiParser.closingC odeTag, WebInspector.WikiParser.TokenType.ClosingCodeTag),
617 new WebInspector.WikiParser.TokenDescriptor(WebInspector.WikiParser.closingB ullet, WebInspector.WikiParser.TokenType.Bullet),
618 new WebInspector.WikiParser.TokenDescriptor(WebInspector.WikiParser.vertical Line, WebInspector.WikiParser.TokenType.VerticalLine),
619 new WebInspector.WikiParser.TokenDescriptor(WebInspector.WikiParser.lineEnd, WebInspector.WikiParser.TokenType.LineEnd)
620 ];
621
622 /** 649 /**
623 * @constructor 650 * @constructor
624 * @param {!WebInspector.WikiParser.ArticleElement.Type} type 651 * @param {!WebInspector.WikiParser.ArticleElement.Type} type
625 */ 652 */
626 WebInspector.WikiParser.ArticleElement = function(type) 653 WebInspector.WikiParser.ArticleElement = function(type)
627 { 654 {
628 this._type = type; 655 this._type = type;
629 } 656 }
630 657
631 WebInspector.WikiParser.ArticleElement.prototype = { 658 WebInspector.WikiParser.ArticleElement.prototype = {
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after
779 /** 806 /**
780 * @return {string} 807 * @return {string}
781 */ 808 */
782 url : function() 809 url : function()
783 { 810 {
784 return this._url; 811 return this._url;
785 }, 812 },
786 813
787 __proto__: WebInspector.WikiParser.Inline.prototype 814 __proto__: WebInspector.WikiParser.Inline.prototype
788 } 815 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698