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

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: comments addressed 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, "<")
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",
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",
43 Space: "Space"
79 } 44 }
80 45
81 /** 46 /**
82 * @constructor 47 * @constructor
83 * @param {string} result 48 * @param {string} result
84 * @param {!WebInspector.WikiParser.TokenType} type 49 * @param {!WebInspector.WikiParser.TokenType} type
85 */ 50 */
86 WebInspector.WikiParser.Token = function(result, type) 51 WebInspector.WikiParser.Token = function(result, type)
87 { 52 {
88 this._value = result; 53 this._value = result;
(...skipping 10 matching lines...) Expand all
99 }, 64 },
100 65
101 /** 66 /**
102 * @return {!WebInspector.WikiParser.TokenType} 67 * @return {!WebInspector.WikiParser.TokenType}
103 */ 68 */
104 type: function() 69 type: function()
105 { 70 {
106 return this._type; 71 return this._type;
107 } 72 }
108 } 73 }
109
loislo 2014/09/09 08:05:05 please return back this empty line
iliia 2014/09/09 08:19:29 Done.
110 /** 74 /**
111 * @constructor 75 * @constructor
112 * @param {string} str 76 * @param {string} str
113 */ 77 */
114 WebInspector.WikiParser.Tokenizer = function(str) 78 WebInspector.WikiParser.Tokenizer = function(str)
115 { 79 {
116 this._text = str; 80 this._text = str;
81 this._token = this._internalNextToken();
117 } 82 }
118 83
119 WebInspector.WikiParser.Tokenizer.prototype = { 84 WebInspector.WikiParser.Tokenizer.prototype = {
120 /** 85 /**
121 * @return {!WebInspector.WikiParser.Token} 86 * @return {!WebInspector.WikiParser.Token}
122 */ 87 */
123 _nextToken: function() 88 peekToken: function()
89 {
90 return this._token;
91 },
92
93 /**
94 * @return {!WebInspector.WikiParser.Token}
95 */
96 nextToken: function()
97 {
98 var token = this._token;
99 this._token = this._internalNextToken();
100 return token;
101 },
102
103 /**
104 * @return {!WebInspector.WikiParser.Token}
105 */
106 _internalNextToken: function()
124 { 107 {
125 if (WebInspector.WikiParser.newLineWithSpace.test(this._text)) { 108 if (WebInspector.WikiParser.newLineWithSpace.test(this._text)) {
126 var result = WebInspector.WikiParser.newLineWithSpace.exec(this._tex t); 109 var result = WebInspector.WikiParser.newLineWithSpace.exec(this._tex t);
127 var begin = result.index + result[0].length; 110 var begin = result.index + result[0].length - 1;
128 var end = this._text.length; 111 var end = this._text.length;
129 var lineEnd = WebInspector.WikiParser.newLineWithoutSpace.exec(this. _text); 112 var lineEnd = WebInspector.WikiParser.newLineWithoutSpace.exec(this. _text);
130 if (lineEnd) 113 if (lineEnd)
131 end = lineEnd.index; 114 end = lineEnd.index;
132 var token = this._text.substring(begin, end).replace(/\n */g, "\n"); 115 var token = this._text.substring(begin, end).replace(/\n /g, "\n").r eplace(/{{=}}/g, "=");
133 this._text = this._text.substring(end + 1); 116 this._text = this._text.substring(end + 1);
134 return new WebInspector.WikiParser.Token(token, WebInspector.WikiPar ser.TokenType.CodeBlock); 117 return new WebInspector.WikiParser.Token(token, WebInspector.WikiPar ser.TokenType.CodeBlock);
135 } 118 }
136 119
137 for (var i = 0; i < WebInspector.WikiParser._tokenDescriptors.length; ++ i) { 120 for (var i = 0; i < WebInspector.WikiParser._tokenDescriptors.length; ++ i) {
138 var result = WebInspector.WikiParser._tokenDescriptors[i].regex.exec (this._text); 121 var result = WebInspector.WikiParser._tokenDescriptors[i].regex.exec (this._text);
139 if (result) { 122 if (result) {
140 this._text = this._text.substring(result.index + result[0].lengt h); 123 this._text = this._text.substring(result.index + result[0].lengt h);
141 return new WebInspector.WikiParser.Token(result[0], WebInspector .WikiParser._tokenDescriptors[i].type); 124 return new WebInspector.WikiParser.Token(result[0], WebInspector .WikiParser._tokenDescriptors[i].type);
142 } 125 }
143 } 126 }
144 127
145 for (var lastIndex = 0; lastIndex < this._text.length; ++lastIndex) { 128 for (var lastIndex = 0; lastIndex < this._text.length; ++lastIndex) {
146 var testString = this._text.substring(lastIndex); 129 var testString = this._text.substring(lastIndex);
147 for (var i = 0; i < WebInspector.WikiParser._tokenDescriptors.length ; ++i) { 130 for (var i = 0; i < WebInspector.WikiParser._tokenDescriptors.length ; ++i) {
148 if (WebInspector.WikiParser._tokenDescriptors[i].regex.test(test String)) { 131 if (WebInspector.WikiParser._tokenDescriptors[i].regex.test(test String)) {
149 var token = this._text.substring(0, lastIndex); 132 var token = this._text.substring(0, lastIndex);
150 this._text = this._text.substring(lastIndex); 133 this._text = this._text.substring(lastIndex);
151 return new WebInspector.WikiParser.Token(token, WebInspector .WikiParser.TokenType.Text); 134 return new WebInspector.WikiParser.Token(token, WebInspector .WikiParser.TokenType.Text);
152 } 135 }
153 } 136 }
154 } 137 }
155 138
156 var token = this._text; 139 var token = this._text;
157 this._text = ""; 140 this._text = "";
158 return new WebInspector.WikiParser.Token(token, WebInspector.WikiParser. TokenType.Text); 141 return new WebInspector.WikiParser.Token(token, WebInspector.WikiParser. TokenType.Text);
159 }, 142 },
160 143
161 /** 144 /**
145 * @return {?WebInspector.WikiParser.TokenType}
146 */
147 secondToken: function()
148 {
149 var tokenizer = new WebInspector.WikiParser.Tokenizer(this._text);
150 if (tokenizer.hasMoreTokens())
151 return tokenizer.nextToken().type();
152 return null;
153 },
154
155 /**
162 * @return {boolean} 156 * @return {boolean}
163 */ 157 */
164 _hasMoreTokens: function() 158 hasMoreTokens: function()
165 { 159 {
166 return !!this._text.length; 160 return !!this._text.length;
167 } 161 }
168 } 162 }
169 163
164 WebInspector.WikiParser.table = /^{{{!}}/;
165 WebInspector.WikiParser.exclamation = /^{{!}}/;
166 WebInspector.WikiParser.openingBraces = /^{{/;
167 WebInspector.WikiParser.equalSign = /^=/;
168 WebInspector.WikiParser.equalSignInBraces = /^{{=}}/;
169 WebInspector.WikiParser.closingBraces = /^\s*}}/;
170 WebInspector.WikiParser.oneOpeningBracketWithSpace = /^\n* \[/;
171 WebInspector.WikiParser.twoOpeningBracketsWithSpace = /^\n* \[\[/;
172 WebInspector.WikiParser.oneClosingBracket = /^\n*\]/;
173 WebInspector.WikiParser.twoClosingBrackets = /^\n*\]\]/;
174 WebInspector.WikiParser.tripleQuotes = /^\n*'''/;
175 WebInspector.WikiParser.openingCodeTag = /^<\s*code\s*>/;
176 WebInspector.WikiParser.closingCodeTag = /^<\s*\/\s*code\s*>/;
177 WebInspector.WikiParser.closingBullet = /^\*/;
178 WebInspector.WikiParser.lineEnd = /^\n/;
179 WebInspector.WikiParser.verticalLine = /^\n*\|/;
180 WebInspector.WikiParser.newLineWithSpace = /^\n [^ ]/;
181 WebInspector.WikiParser.newLineWithoutSpace = /\n[^ ]/;
182 WebInspector.WikiParser.space = /^ /;
183
184 /**
185 * @constructor
186 * @param {!RegExp} regex
187 * @param {!WebInspector.WikiParser.TokenType} type
188 */
189 WebInspector.WikiParser.TokenDescriptor = function(regex, type)
190 {
191 this.regex = regex;
192 this.type = type;
193 }
194
195 WebInspector.WikiParser._tokenDescriptors = [
196 new WebInspector.WikiParser.TokenDescriptor(WebInspector.WikiParser.exclamat ion, WebInspector.WikiParser.TokenType.Exclamation),
197 new WebInspector.WikiParser.TokenDescriptor(WebInspector.WikiParser.equalSig nInBraces, WebInspector.WikiParser.TokenType.EqualSignInBraces),
198 new WebInspector.WikiParser.TokenDescriptor(WebInspector.WikiParser.equalSig n, WebInspector.WikiParser.TokenType.EqualSign),
199 new WebInspector.WikiParser.TokenDescriptor(WebInspector.WikiParser.table, W ebInspector.WikiParser.TokenType.Table),
200 new WebInspector.WikiParser.TokenDescriptor(WebInspector.WikiParser.openingB races, WebInspector.WikiParser.TokenType.OpeningBraces),
201 new WebInspector.WikiParser.TokenDescriptor(WebInspector.WikiParser.vertical Line, WebInspector.WikiParser.TokenType.VerticalLine),
202 new WebInspector.WikiParser.TokenDescriptor(WebInspector.WikiParser.closingB races, WebInspector.WikiParser.TokenType.ClosingBraces),
203 new WebInspector.WikiParser.TokenDescriptor(WebInspector.WikiParser.twoOpeni ngBracketsWithSpace, WebInspector.WikiParser.TokenType.OpeningBrackets),
204 new WebInspector.WikiParser.TokenDescriptor(WebInspector.WikiParser.twoClosi ngBrackets, WebInspector.WikiParser.TokenType.ClosingBrackets),
205 new WebInspector.WikiParser.TokenDescriptor(WebInspector.WikiParser.oneOpeni ngBracketWithSpace, WebInspector.WikiParser.TokenType.OpeningBrackets),
206 new WebInspector.WikiParser.TokenDescriptor(WebInspector.WikiParser.oneClosi ngBracket, WebInspector.WikiParser.TokenType.ClosingBrackets),
207 new WebInspector.WikiParser.TokenDescriptor(WebInspector.WikiParser.newLineW ithSpace, WebInspector.WikiParser.TokenType.CodeBlock),
208 new WebInspector.WikiParser.TokenDescriptor(WebInspector.WikiParser.tripleQu otes, WebInspector.WikiParser.TokenType.TripleQuotes),
209 new WebInspector.WikiParser.TokenDescriptor(WebInspector.WikiParser.openingC odeTag, WebInspector.WikiParser.TokenType.OpeningCodeTag),
210 new WebInspector.WikiParser.TokenDescriptor(WebInspector.WikiParser.closingC odeTag, WebInspector.WikiParser.TokenType.ClosingCodeTag),
211 new WebInspector.WikiParser.TokenDescriptor(WebInspector.WikiParser.closingB ullet, WebInspector.WikiParser.TokenType.Bullet),
212 new WebInspector.WikiParser.TokenDescriptor(WebInspector.WikiParser.lineEnd, WebInspector.WikiParser.TokenType.LineEnd)//,
213 //new WebInspector.WikiParser.TokenDescriptor(WebInspector.WikiParser.space, WebInspector.WikiParser.TokenType.Space)
214 ]
215
170 WebInspector.WikiParser.prototype = { 216 WebInspector.WikiParser.prototype = {
171 /** 217 /**
172 * @return {!Object} 218 * @return {!Object}
173 */ 219 */
174 document: function() 220 document: function()
175 { 221 {
176 return this._document; 222 return this._document;
177 }, 223 },
178 224
179 /** 225 /**
180 * @return {!Object} 226 * @return {!Object}
181 */ 227 */
182 _parse: function() 228 _parse: function()
183 { 229 {
184 var obj = {}; 230 var obj = {};
185 this._wikiMarkupText = this._wikiMarkupText.replace(/&lt;/g, "<") 231 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(); 232 var field = this._parseField();
196 for (var key in field) { 233 for (var key in field) {
197 console.assert(typeof obj[key] === "undefined", "Duplicate key: " + key); 234 console.assert(typeof obj[key] === "undefined", "Duplicate key: " + key);
198 obj[key] = field[key]; 235 obj[key] = field[key];
199 } 236 }
200 } 237 }
201 return obj; 238 return obj;
202 }, 239 },
203 240
204 /** 241 /**
242 * @return {?Object}
243 */
244 _parseField: function()
245 {
246 var field = {};
247 if (!this._tokenizer.hasMoreTokens() || this._tokenizer.nextToken().type () !== WebInspector.WikiParser.TokenType.OpeningBraces)
248 return field;
249
250 var title = this._deleteTrailingSpaces(this._parseTitle());
251 if (!title.length)
252 return field;
253 field[title] = {};
254 if (this._tokenizer.peekToken().type() === WebInspector.WikiParser.Token Type.ClosingBraces) {
255 this._tokenizer.nextToken();
256 return field;
257 }
258 if (!this._tokenizer.secondToken() || this._tokenizer.secondToken() !== WebInspector.WikiParser.TokenType.EqualSign) {
259 var propertyValue = this._parseString();
260 field[title] = propertyValue;
261 } else {
262 while (this._tokenizer.hasMoreTokens()) {
263 var propertyName = this._parseName();
264 var token = this._tokenizer.peekToken();
265 switch (token.type()) {
266 case WebInspector.WikiParser.TokenType.OpeningBraces:
267 field[title][propertyName] = this._parseArray();
268 break;
269 case WebInspector.WikiParser.TokenType.LineEnd:
270 this._tokenizer.nextToken();
271 break;
272 case WebInspector.WikiParser.TokenType.ClosingBraces:
273 this._tokenizer.nextToken();
274 return field;
275 break;
276 default:
277 if (propertyName === "Code")
278 field[title][propertyName] = this._parseExampleCode();
279 else
280 field[title][propertyName] = this._parseString();
281 }
282 }
283 }
284 var token = this._tokenizer.nextToken();
285 if (token.type() !== WebInspector.WikiParser.TokenType.ClosingBraces)
286 throw new Error("Two closing braces expected; found " + token.value( ));
287
288 return field;
289 },
290
291 /**
292 * @return {!Array.<?Object>}
293 */
294 _parseArray: function()
295 {
296 var array = [];
297 while (this._tokenizer.peekToken().type() === WebInspector.WikiParser.To kenType.OpeningBraces)
298 array.push(this._parseField());
299 if (this._tokenizer.peekToken().type() === WebInspector.WikiParser.Token Type.VerticalLine)
300 this._tokenizer.nextToken();
301 return array;
302 },
303
304 /**
205 * @return {string} 305 * @return {string}
206 */ 306 */
207 _parseValue: function() { 307 _parseTitle: function()
208 var states = WebInspector.WikiParser.ValueState; 308 {
209 var state = states.Outside; 309 var title = "";
210 var value = ""; 310 while (this._tokenizer.hasMoreTokens()) {
211 while (this._position < this._wikiMarkupText.length) { 311 var token = this._tokenizer.peekToken();
212 switch (state) { 312 switch (token.type()) {
213 case states.Outside: 313 case WebInspector.WikiParser.TokenType.ClosingBraces:
214 if (this._wikiMarkupText[this._position] === "|" || (this._wikiM arkupText[this._position] === "}" && this._wikiMarkupText[this._position + 1] == = "}")) 314 return title;
215 return value; 315 case WebInspector.WikiParser.TokenType.VerticalLine:
216 switch (this._wikiMarkupText[this._position]) { 316 this._tokenizer.nextToken();
217 case "<": 317 return title;
218 var indexClose = this._wikiMarkupText.indexOf(">", this._pos ition); 318 case WebInspector.WikiParser.TokenType.Text:
219 if (indexClose !== -1) { 319 case WebInspector.WikiParser.TokenType.Space:
220 value += this._wikiMarkupText.substring(this._position, indexClose + 1); 320 title += this._tokenizer.nextToken().value();
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; 321 break;
232 case states.InsideSquare: 322 default:
233 if (this._wikiMarkupText[this._position] === "[") { 323 throw new Error("Can't parse Title, unexpected token " + token.v alue());
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 } 324 }
249 this._position++;
250 } 325 }
251 return value; 326 return title;
252 }, 327 },
253 328
254 /** 329 /**
255 * @return {!Object} 330 * @return {string}
256 */ 331 */
257 _parseField: function() 332 _parseName: function()
258 { 333 {
259 var obj = {}; 334 var name = "";
260 var title = ""; 335 while (this._tokenizer.hasMoreTokens()) {
261 var propertyName = ""; 336 var token = this._tokenizer.peekToken();
262 var propertyValue = ""; 337 switch (token.type()) {
263 var states = WebInspector.WikiParser.State; 338 case WebInspector.WikiParser.TokenType.ClosingBraces:
264 var state = states.FirstOpen; 339 return name;
265 while (this._position < this._wikiMarkupText.length) { 340 case WebInspector.WikiParser.TokenType.EqualSign:
266 var skipIncrement = false; 341 this._tokenizer.nextToken();
267 switch (state) { 342 return name;
268 case states.FirstOpen: 343 case WebInspector.WikiParser.TokenType.VerticalLine:
269 if (this._wikiMarkupText[this._position] === "{") 344 case WebInspector.WikiParser.TokenType.Space:
270 state = states.SecondOpen; 345 case WebInspector.WikiParser.TokenType.Text:
271 else 346 name += this._tokenizer.nextToken().value();
272 state = states.Error;
273 break; 347 break;
274 case states.SecondOpen: 348 default:
275 if (this._wikiMarkupText[this._position] === "{") 349 throw new Error("Can't parse name, unexpected token " + token.va lue());
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 } 350 }
360 if (!skipIncrement)
361 this._position++;
362 } 351 }
363 return obj; 352 return name;
364 }, 353 },
365 354
366 /** 355 /**
367 * @param {string} str 356 * @return {string}
357 */
358 _parseExampleCode: function()
359 {
360 var code = "";
361 while (this._tokenizer.hasMoreTokens()) {
362 var token = this._tokenizer.peekToken();
363 switch (token.type()) {
364 case WebInspector.WikiParser.TokenType.ClosingBraces:
365 return code;
366 case WebInspector.WikiParser.TokenType.VerticalLine:
367 this._tokenizer.nextToken();
368 return code;
369 case WebInspector.WikiParser.TokenType.Exclamation:
370 this._tokenizer.nextToken();
371 code += '|';
372 break;
373 case WebInspector.WikiParser.TokenType.EqualSignInBraces:
374 this._tokenizer.nextToken();
375 code += "=";
376 break;
377 default:
378 this._tokenizer.nextToken();
379 code += token.value();
380 }
381 }
382 return code;
383 },
384
385 /**
368 * @return {?WebInspector.WikiParser.Block} 386 * @return {?WebInspector.WikiParser.Block}
369 */ 387 */
370 parseString: function(str) 388 _parseString: function()
371 { 389 {
372 this._tokenizer = new WebInspector.WikiParser.Tokenizer(str);
373 var children = []; 390 var children = [];
374 var blockChildren = []; 391 var blockChildren = [];
375 var text = ""; 392 var text = "";
376 var self = this; 393 var self = this;
377 394
378 function processSimpleText() 395 function processSimpleText()
379 { 396 {
380 var currentText = self._deleteTrailingSpaces(text); 397 var currentText = self._deleteTrailingSpaces(text);
381 if (!currentText.length) 398 if (!currentText.length)
382 return; 399 return;
383 var simpleText = new WebInspector.WikiParser.PlainText(currentText, false); 400 var simpleText = new WebInspector.WikiParser.PlainText(currentText, false);
384 blockChildren.push(simpleText); 401 blockChildren.push(simpleText);
385 text = ""; 402 text = "";
386 } 403 }
387 404
388 function processBlock() 405 function processBlock()
389 { 406 {
390 if (blockChildren.length) { 407 if (blockChildren.length) {
391 children.push(new WebInspector.WikiParser.Block(blockChildren, f alse)); 408 children.push(new WebInspector.WikiParser.Block(blockChildren, f alse));
392 blockChildren = []; 409 blockChildren = [];
393 } 410 }
394 } 411 }
395 while (this._tokenizer._hasMoreTokens()) { 412
396 var token = this._tokenizer._nextToken(); 413 while (this._tokenizer.hasMoreTokens()) {
414 var token = this._tokenizer.peekToken();
397 switch (token.type()) { 415 switch (token.type()) {
416 case WebInspector.WikiParser.TokenType.VerticalLine:
417 this._tokenizer.nextToken();
418 case WebInspector.WikiParser.TokenType.ClosingBraces:
419 processSimpleText();
420 processBlock();
421 return new WebInspector.WikiParser.Block(children, false);
398 case WebInspector.WikiParser.TokenType.TripleQuotes: 422 case WebInspector.WikiParser.TokenType.TripleQuotes:
423 this._tokenizer.nextToken();
399 processSimpleText(); 424 processSimpleText();
400 var highlightText = this._parseHighlight(); 425 var highlightText = this._parseHighlight();
401 blockChildren.push(highlightText) 426 blockChildren.push(highlightText)
402 break; 427 break;
403 case WebInspector.WikiParser.TokenType.OpeningBrackets: 428 case WebInspector.WikiParser.TokenType.OpeningBrackets:
429 this._tokenizer.nextToken();
404 processSimpleText(); 430 processSimpleText();
405 var link = this._parseLink(); 431 var link = this._parseLink();
406 blockChildren.push(link); 432 blockChildren.push(link);
407 break; 433 break;
408 case WebInspector.WikiParser.TokenType.OpeningCodeTag: 434 case WebInspector.WikiParser.TokenType.OpeningCodeTag:
435 this._tokenizer.nextToken();
409 processSimpleText(); 436 processSimpleText();
410 var code = this._parseCode(); 437 var code = this._parseCode();
411 blockChildren.push(code); 438 blockChildren.push(code);
412 break; 439 break;
413 case WebInspector.WikiParser.TokenType.Bullet: 440 case WebInspector.WikiParser.TokenType.Bullet:
441 this._tokenizer.nextToken();
414 processSimpleText(); 442 processSimpleText();
415 processBlock(); 443 processBlock();
416 var bulletText = this._parseBullet(); 444 var bulletText = this._parseBullet();
417 children.push(bulletText); 445 children.push(bulletText);
418 break; 446 break;
419 case WebInspector.WikiParser.TokenType.CodeBlock: 447 case WebInspector.WikiParser.TokenType.CodeBlock:
448 this._tokenizer.nextToken();
420 processSimpleText(); 449 processSimpleText();
421 processBlock(); 450 processBlock();
422 var code = new WebInspector.WikiParser.CodeBlock(token.value()); 451 var code = new WebInspector.WikiParser.CodeBlock(token.value());
423 children.push(code); 452 children.push(code);
424 break; 453 break;
425 case WebInspector.WikiParser.TokenType.LineEnd: 454 case WebInspector.WikiParser.TokenType.LineEnd:
455 this._tokenizer.nextToken();
426 processSimpleText(); 456 processSimpleText();
427 processBlock(); 457 processBlock();
428 break; 458 break;
429 case WebInspector.WikiParser.TokenType.VerticalLine: 459 case WebInspector.WikiParser.TokenType.EqualSignInBraces:
460 this._tokenizer.nextToken();
461 text += "=";
462 break;
463 case WebInspector.WikiParser.TokenType.Exclamation:
464 this._tokenizer.nextToken();
465 text += "|";
466 break;
467 case WebInspector.WikiParser.TokenType.ClosingBrackets:
430 case WebInspector.WikiParser.TokenType.Text: 468 case WebInspector.WikiParser.TokenType.Text:
469 case WebInspector.WikiParser.TokenType.Space:
470 case WebInspector.WikiParser.TokenType.EqualSign:
471 this._tokenizer.nextToken();
431 text += token.value(); 472 text += token.value();
432 break; 473 break;
433 default: 474 default:
475 this._tokenizer.nextToken();
434 return null; 476 return null;
435 } 477 }
436 } 478 }
437 479
438 processSimpleText(); 480 processSimpleText();
439 processBlock(); 481 processBlock();
440 482
441 return new WebInspector.WikiParser.Block(children, false); 483 return new WebInspector.WikiParser.Block(children, false);
442 }, 484 },
443 485
444 /** 486 /**
445 * @return {!WebInspector.WikiParser.Link} 487 * @return {!WebInspector.WikiParser.Link}
446 */ 488 */
447 _parseLink: function() 489 _parseLink: function()
448 { 490 {
449 var url = ""; 491 var url = "";
450 var children = []; 492 var children = [];
451 while (this._tokenizer._hasMoreTokens()) { 493 while (this._tokenizer.hasMoreTokens()) {
452 var token = this._tokenizer._nextToken(); 494 var token = this._tokenizer.nextToken();
453 switch (token.type()) { 495 switch (token.type()) {
454 case WebInspector.WikiParser.TokenType.ClosingBrackets: 496 case WebInspector.WikiParser.TokenType.ClosingBrackets:
455 return new WebInspector.WikiParser.Link(url, children); 497 return new WebInspector.WikiParser.Link(url, children);
456 case WebInspector.WikiParser.TokenType.VerticalLine: 498 case WebInspector.WikiParser.TokenType.VerticalLine:
499 case WebInspector.WikiParser.TokenType.Exclamation:
457 children.push(this._parseLinkName()); 500 children.push(this._parseLinkName());
458 return new WebInspector.WikiParser.Link(url, children); 501 return new WebInspector.WikiParser.Link(url, children);
459 default: 502 default:
460 url += token.value(); 503 url += token.value();
461 } 504 }
462 } 505 }
463 506
464 return new WebInspector.WikiParser.Link(url, children); 507 return new WebInspector.WikiParser.Link(url, children);
465 }, 508 },
466 509
467 /** 510 /**
468 * @return {!WebInspector.WikiParser.Inline} 511 * @return {!WebInspector.WikiParser.Inline}
469 */ 512 */
470 _parseLinkName: function() 513 _parseLinkName: function()
471 { 514 {
472 var children = []; 515 var children = [];
473 while (this._tokenizer._hasMoreTokens()) { 516 var text = "";
474 var token = this._tokenizer._nextToken(); 517 var self = this;
518 function processSimpleText()
519 {
520 text = self._deleteTrailingSpaces(text);
521 if (!text.length)
522 return;
523 var simpleText = new WebInspector.WikiParser.PlainText(text, false);
524 children.push(simpleText);
525 text = "";
526 }
527
528 while (this._tokenizer.hasMoreTokens()) {
529 var token = this._tokenizer.nextToken();
475 switch (token.type()) { 530 switch (token.type()) {
476 case WebInspector.WikiParser.TokenType.ClosingBrackets: 531 case WebInspector.WikiParser.TokenType.ClosingBrackets:
532 processSimpleText();
477 return new WebInspector.WikiParser.Inline(WebInspector.WikiParse r.ArticleElement.Type.Inline, children); 533 return new WebInspector.WikiParser.Inline(WebInspector.WikiParse r.ArticleElement.Type.Inline, children);
478 case WebInspector.WikiParser.TokenType.OpeningCodeTag: 534 case WebInspector.WikiParser.TokenType.OpeningCodeTag:
535 processSimpleText();
479 children.push(this._parseCode()); 536 children.push(this._parseCode());
480 break; 537 break;
481 default: 538 default:
482 children.push(new WebInspector.WikiParser.PlainText(token.value( ), false)); 539 text += token.value();
483 break; 540 break;
484 } 541 }
485 } 542 }
486 543
487 return new WebInspector.WikiParser.Inline(WebInspector.WikiParser.Articl eElement.Type.Inline, children); 544 return new WebInspector.WikiParser.Inline(WebInspector.WikiParser.Articl eElement.Type.Inline, children);
488 }, 545 },
489 546
490 /** 547 /**
491 * @return {!WebInspector.WikiParser.Inline} 548 * @return {!WebInspector.WikiParser.Inline}
492 */ 549 */
493 _parseCode : function() 550 _parseCode : function()
494 { 551 {
495 var children = []; 552 var children = [];
496 var text = ""; 553 var text = "";
497 while (this._tokenizer._hasMoreTokens()) { 554 while (this._tokenizer.hasMoreTokens()) {
498 var token = this._tokenizer._nextToken(); 555 var token = this._tokenizer.nextToken();
499 switch (token.type()) { 556 switch (token.type()) {
500 case WebInspector.WikiParser.TokenType.ClosingCodeTag: 557 case WebInspector.WikiParser.TokenType.ClosingCodeTag:
501 text = this._deleteTrailingSpaces(text); 558 text = this._deleteTrailingSpaces(text);
502 if (text.length) { 559 if (text.length) {
503 var simpleText = new WebInspector.WikiParser.PlainText(text, false); 560 var simpleText = new WebInspector.WikiParser.PlainText(text, false);
504 children.push(simpleText); 561 children.push(simpleText);
505 text = ""; 562 text = "";
506 } 563 }
507 var code = new WebInspector.WikiParser.Inline(WebInspector.WikiP arser.ArticleElement.Type.Code, children); 564 var code = new WebInspector.WikiParser.Inline(WebInspector.WikiP arser.ArticleElement.Type.Code, children);
508 return code; 565 return code;
(...skipping 12 matching lines...) Expand all
521 578
522 return new WebInspector.WikiParser.Inline(WebInspector.WikiParser.Articl eElement.Type.Code, children); 579 return new WebInspector.WikiParser.Inline(WebInspector.WikiParser.Articl eElement.Type.Code, children);
523 }, 580 },
524 581
525 /** 582 /**
526 * @return {!WebInspector.WikiParser.Block} 583 * @return {!WebInspector.WikiParser.Block}
527 */ 584 */
528 _parseBullet: function() 585 _parseBullet: function()
529 { 586 {
530 var children = []; 587 var children = [];
531 while (this._tokenizer._hasMoreTokens()) { 588 while (this._tokenizer.hasMoreTokens()) {
532 var token = this._tokenizer._nextToken() 589 var token = this._tokenizer.nextToken()
533 switch (token.type()) { 590 switch (token.type()) {
534 case WebInspector.WikiParser.TokenType.OpeningBrackets: 591 case WebInspector.WikiParser.TokenType.OpeningBrackets:
535 children.push(this._parseLink()); 592 children.push(this._parseLink());
536 break; 593 break;
537 case WebInspector.WikiParser.TokenType.OpeningCodeTag: 594 case WebInspector.WikiParser.TokenType.OpeningCodeTag:
538 children.push(this._parseCode()); 595 children.push(this._parseCode());
539 break; 596 break;
540 case WebInspector.WikiParser.TokenType.LineEnd: 597 case WebInspector.WikiParser.TokenType.LineEnd:
541 return new WebInspector.WikiParser.Block(children, true); 598 return new WebInspector.WikiParser.Block(children, true);
542 default: 599 default:
543 var text = this._deleteTrailingSpaces(token.value()); 600 var text = this._deleteTrailingSpaces(token.value());
544 if (text.length) { 601 if (text.length) {
545 var simpleText = new WebInspector.WikiParser.PlainText(text, false); 602 var simpleText = new WebInspector.WikiParser.PlainText(text, false);
546 children.push(simpleText); 603 children.push(simpleText);
547 text = ""; 604 text = "";
548 } 605 }
549 } 606 }
550 } 607 }
551 608
552 return new WebInspector.WikiParser.Block(children, true); 609 return new WebInspector.WikiParser.Block(children, true);
553 }, 610 },
554 611
555 /** 612 /**
556 * @return {!WebInspector.WikiParser.PlainText} 613 * @return {!WebInspector.WikiParser.PlainText}
557 */ 614 */
558 _parseHighlight: function() 615 _parseHighlight: function()
559 { 616 {
560 var text = ""; 617 var text = "";
561 while (this._tokenizer._hasMoreTokens()) { 618 while (this._tokenizer.hasMoreTokens()) {
562 var token = this._tokenizer._nextToken() 619 var token = this._tokenizer.nextToken()
563 switch (token.type()) { 620 switch (token.type()) {
564 case WebInspector.WikiParser.TokenType.TripleQuotes: 621 case WebInspector.WikiParser.TokenType.TripleQuotes:
565 text = this._deleteTrailingSpaces(text); 622 text = this._deleteTrailingSpaces(text);
566 return new WebInspector.WikiParser.PlainText(text, true); 623 return new WebInspector.WikiParser.PlainText(text, true);
567 default: 624 default:
568 text += token.value(); 625 text += token.value();
569 } 626 }
570 } 627 }
571 return new WebInspector.WikiParser.PlainText(text, true); 628 return new WebInspector.WikiParser.PlainText(text, true);
572 }, 629 },
573 630
574 /** 631 /**
575 * @param {string} str 632 * @param {string} str
576 * @return {string} 633 * @return {string}
577 */ 634 */
578 _deleteTrailingSpaces: function(str) 635 _deleteTrailingSpaces: function(str)
579 { 636 {
580 return str.replace(/[\n\r]*$/gm, ""); 637 return str.replace(/[\n\r]*$/gm, "");
581 } 638 }
582 } 639 }
583 640
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 /** 641 /**
623 * @constructor 642 * @constructor
624 * @param {!WebInspector.WikiParser.ArticleElement.Type} type 643 * @param {!WebInspector.WikiParser.ArticleElement.Type} type
625 */ 644 */
626 WebInspector.WikiParser.ArticleElement = function(type) 645 WebInspector.WikiParser.ArticleElement = function(type)
627 { 646 {
628 this._type = type; 647 this._type = type;
629 } 648 }
630 649
631 WebInspector.WikiParser.ArticleElement.prototype = { 650 WebInspector.WikiParser.ArticleElement.prototype = {
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after
779 /** 798 /**
780 * @return {string} 799 * @return {string}
781 */ 800 */
782 url : function() 801 url : function()
783 { 802 {
784 return this._url; 803 return this._url;
785 }, 804 },
786 805
787 __proto__: WebInspector.WikiParser.Inline.prototype 806 __proto__: WebInspector.WikiParser.Inline.prototype
788 } 807 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698