Commit 655be0e8 authored by iliia@google.com's avatar iliia@google.com

DevTools: [Documentation] Update parser for WikiText

BUG=391593

Review URL: https://codereview.chromium.org/539353004

git-svn-id: svn://svn.chromium.org/blink/trunk@181901 bbb929c8-8fbe-4397-9dbb-9b2b20218538
parent 76fc0b6d
Running: testWikiParser Running: testWikiParser
"{{A|B={{C|D=blablabla}}}}" is parsed "{{A|text=<code>it's not a code <code>}}" is parsed
{
A : {
text : {
_children : [
{
_children : [
{
_isHighlighted : false
_text : "<code>it's not a code <code>"
_type : "PlainText"
}
]
_hasBullet : false
_type : "Block"
}
]
_hasBullet : false
_type : "Block"
}
}
}
"{{A|Code={{=}} }}" is parsed
{
A : {
Code : {
_children : [
{
_children : [
{
_isHighlighted : false
_text : "="
_type : "PlainText"
}
]
_hasBullet : false
_type : "Block"
}
]
_hasBullet : false
_type : "Block"
}
}
}
"{{ABC|Array={{A|B=c}}{{X|y=Z}}|text=blabla}}" is parsed
{
ABC : {
Array : [
{
title : "A"
values : {
B : {
_children : [
{
_children : [
{
_isHighlighted : false
_text : "c"
_type : "PlainText"
}
]
_hasBullet : false
_type : "Block"
}
]
_hasBullet : false
_type : "Block"
}
}
}
{
title : "X"
values : {
y : {
_children : [
{
_children : [
{
_isHighlighted : false
_text : "Z"
_type : "PlainText"
}
]
_hasBullet : false
_type : "Block"
}
]
_hasBullet : false
_type : "Block"
}
}
}
]
text : {
_children : [
{
_children : [
{
_isHighlighted : false
_text : "blabla"
_type : "PlainText"
}
]
_hasBullet : false
_type : "Block"
}
]
_hasBullet : false
_type : "Block"
}
}
}
"{{A|B=C}}" is parsed
{ {
A : { A : {
B : { B : {
C : { _children : [
D : "blablabla" {
_children : [
{
_isHighlighted : false
_text : "C"
_type : "PlainText"
}
]
_hasBullet : false
_type : "Block"
}
]
_hasBullet : false
_type : "Block"
}
}
}
"{{A|B={{C|D=blablabla}}}}" is parsed
{
A : {
B : [
{
title : "C"
values : {
D : {
_children : [
{
_children : [
{
_isHighlighted : false
_text : "blablabla"
_type : "PlainText"
}
]
_hasBullet : false
_type : "Block"
}
]
_hasBullet : false
_type : "Block"
}
}
} }
]
}
}
"{{A|B={{C|D=bla}}|x=y}}" is parsed
{
A : {
B : [
{
title : "C"
values : {
D : {
_children : [
{
_children : [
{
_isHighlighted : false
_text : "bla"
_type : "PlainText"
}
]
_hasBullet : false
_type : "Block"
}
]
_hasBullet : false
_type : "Block"
}
}
}
]
x : {
_children : [
{
_children : [
{
_isHighlighted : false
_text : "y"
_type : "PlainText"
}
]
_hasBullet : false
_type : "Block"
}
]
_hasBullet : false
_type : "Block"
} }
} }
} }
"{{CS|Init=black|Values={{CSS Property Value "{{CS|Init=black|Values={{CSS Property Value
|Data Type=color |Data Type=color
|Description=[[css/color|CSS color value]]}} |Description= [[css/color|CSS color value]]}}
}}" is parsed }}" is parsed
{ {
CS : { CS : {
Init : "black" Init : {
Values : { _children : [
CSS Property Value : { {
Data Type : "color" _children : [
Description : "[[css/color|CSS color value]]" {
} _isHighlighted : false
_text : "black"
_type : "PlainText"
}
]
_hasBullet : false
_type : "Block"
}
]
_hasBullet : false
_type : "Block"
} }
Values : [
{
title : "CSS Property Value"
values : {
Data Type : {
_children : [
{
_children : [
{
_isHighlighted : false
_text : "color"
_type : "PlainText"
}
]
_hasBullet : false
_type : "Block"
}
]
_hasBullet : false
_type : "Block"
}
Description : {
_children : [
{
_children : [
{
_children : [
{
_children : [
{
_isHighlighted : false
_text : "CSS color value"
_type : "PlainText"
}
]
_type : "Inline"
}
]
_type : "Link"
_url : "css/color"
}
]
_hasBullet : false
_type : "Block"
}
]
_hasBullet : false
_type : "Block"
}
}
}
]
} }
} }
"{{A}}" is parsed "{{A}}" is parsed
{
A : undefined
}
"{{A|B= [[google.com{{!}}Google]]}}" is parsed
{ {
A : { A : {
B : {
_children : [
{
_children : [
{
_children : [
{
_children : [
{
_isHighlighted : false
_text : "Google"
_type : "PlainText"
}
]
_type : "Inline"
}
]
_type : "Link"
_url : "google.com"
}
]
_hasBullet : false
_type : "Block"
}
]
_hasBullet : false
_type : "Block"
}
} }
} }
"{{A|B=bla<a=c>bla}}" is parsed "{{AbC|xyz
\r}}" is parsed
{ {
A : { AbC : {
B : "bla<a=c>bla" _children : [
{
_children : [
{
_isHighlighted : false
_text : "xyz"
_type : "PlainText"
}
]
_hasBullet : false
_type : "Block"
}
]
_hasBullet : false
_type : "Block"
} }
} }
"{{A}}" is parsed "{{Table|there is a table {{{!}}
}}" is parsed
{ {
A : { Table : {
_children : [
{
_children : [
{
_isHighlighted : false
_text : "there is a table {{{!}}"
_type : "PlainText"
}
]
_hasBullet : false
_type : "Block"
}
]
_hasBullet : false
_type : "Block"
} }
} }
"{{AbC|xyz "{{External_Attribution|MSDN_link=[http://msdn.microsoft.com/en-us/library/ie/ms536365(v=vs.85).aspx cloneNode Method]|HTML5Rocks_link=}}" is parsed
\r}}" is parsed
{ {
AbC : "xyz External_Attribution : {
" HTML5Rocks_link : undefined
MSDN_link : {
_children : [
{
_children : [
{
_isHighlighted : false
_text : "[http://msdn.microsoft.com/en-us/library/ie/ms536365(v=vs.85).aspx cloneNode Method]"
_type : "PlainText"
}
]
_hasBullet : false
_type : "Block"
}
]
_hasBullet : false
_type : "Block"
}
}
}
"{{JS_Syntax|Formats={{JS Syntax Format|Format='''JSON.stringify(''' value [ , replacer] [ , space] ''')'''}}
}}" is parsed
{
JS_Syntax : {
Formats : [
{
title : "JS Syntax Format"
values : {
Format : {
_children : [
{
_children : [
{
_isHighlighted : true
_text : "JSON.stringify("
_type : "PlainText"
}
{
_isHighlighted : false
_text : " value"
_type : "PlainText"
}
{
_children : [
]
_type : "Link"
_url : " , replacer"
}
{
_children : [
]
_type : "Link"
_url : " , space"
}
{
_isHighlighted : false
_text : " "
_type : "PlainText"
}
{
_isHighlighted : true
_text : ")"
_type : "PlainText"
}
]
_hasBullet : false
_type : "Block"
}
]
_hasBullet : false
_type : "Block"
}
}
}
]
}
} }
Running: testJS Running: testJS
...@@ -123,6 +521,7 @@ Running: testJS ...@@ -123,6 +521,7 @@ Running: testJS
_hasBullet : false _hasBullet : false
_type : "Block" _type : "Block"
} }
summary : undefined
} }
Running: testSmall Running: testSmall
...@@ -131,6 +530,7 @@ Running: testSmall ...@@ -131,6 +530,7 @@ Running: testSmall
] ]
parameters : [ parameters : [
] ]
remarks : null
summary : { summary : {
_children : [ _children : [
{ {
...@@ -161,6 +561,7 @@ Running: testHighlightAndLink ...@@ -161,6 +561,7 @@ Running: testHighlightAndLink
] ]
parameters : [ parameters : [
] ]
remarks : null
summary : { summary : {
_children : [ _children : [
{ {
...@@ -269,6 +670,7 @@ Running: testNamedLink ...@@ -269,6 +670,7 @@ Running: testNamedLink
] ]
parameters : [ parameters : [
] ]
remarks : null
summary : { summary : {
_children : [ _children : [
{ {
...@@ -305,6 +707,7 @@ Running: testBullet ...@@ -305,6 +707,7 @@ Running: testBullet
] ]
parameters : [ parameters : [
] ]
remarks : null
summary : { summary : {
_children : [ _children : [
{ {
...@@ -444,6 +847,7 @@ console.log(Object.keys(my_array)); ...@@ -444,6 +847,7 @@ console.log(Object.keys(my_array));
_hasBullet : false _hasBullet : false
_type : "Block" _type : "Block"
} }
summary : undefined
} }
Running: testComplexLinkName Running: testComplexLinkName
...@@ -452,6 +856,7 @@ Running: testComplexLinkName ...@@ -452,6 +856,7 @@ Running: testComplexLinkName
] ]
parameters : [ parameters : [
] ]
remarks : null
summary : { summary : {
_children : [ _children : [
{ {
...@@ -498,6 +903,7 @@ Running: testCodeInsideBullet ...@@ -498,6 +903,7 @@ Running: testCodeInsideBullet
] ]
parameters : [ parameters : [
] ]
remarks : null
summary : { summary : {
_children : [ _children : [
{ {
...@@ -528,7 +934,6 @@ Running: testCodeInsideBullet ...@@ -528,7 +934,6 @@ Running: testCodeInsideBullet
} }
Running: testInvalidInput Running: testInvalidInput
error: Can't parse remarks
[object Object] [object Object]
Running: testLinkInsideCode Running: testLinkInsideCode
...@@ -537,6 +942,7 @@ Running: testLinkInsideCode ...@@ -537,6 +942,7 @@ Running: testLinkInsideCode
] ]
parameters : [ parameters : [
] ]
remarks : null
summary : { summary : {
_children : [ _children : [
{ {
...@@ -635,6 +1041,8 @@ Running: testExamples ...@@ -635,6 +1041,8 @@ Running: testExamples
] ]
parameters : [ parameters : [
] ]
remarks : null
summary : undefined
} }
Running: testJsMethod Running: testJsMethod
...@@ -691,15 +1099,10 @@ Running: testJsMethod ...@@ -691,15 +1099,10 @@ Running: testJsMethod
optional : true optional : true
} }
] ]
remarks : null
summary : undefined
} }
Running: testLinkWithoutSpace Running: testLinkWithoutSpace
error: Can't parse summary Expected error: Error: Two closing braces expected; found WRONG_LINK
{
examples : [
]
parameters : [
]
summary : null
}
...@@ -11,12 +11,19 @@ function test() ...@@ -11,12 +11,19 @@ function test()
function testWikiParser(next) function testWikiParser(next)
{ {
var testCases = [ var testCases = [
"{{A|text=&lt;code>it's not a code &lt;code>}}",
"{{A|Code={{=}} }}",
"{{ABC|Array={{A|B=c}}{{X|y=Z}}|text=blabla}}",
"{{A|B=C}}",
"{{A|B={{C|D=blablabla}}}}", "{{A|B={{C|D=blablabla}}}}",
"{{CS|Init=black|Values={{CSS Property Value\n|Data Type=color\n|Description=[[css/color|CSS color value]]}}\n}}", "{{A|B={{C|D=bla}}|x=y}}",
"{{CS|Init=black|Values={{CSS Property Value\n|Data Type=color\n|Description= [[css/color|CSS color value]]}}\n}}",
"{{A}}", "{{A}}",
"{{A|B=bla<a=c>bla}}", "{{A|B= [[google.com{{!}}Google]]}}",
"{{A}}", "{{AbC|xyz \n\t \r}}",
"{{AbC|xyz \n \t \r}}"]; "{{Table|there is a table {{{!}}\n}}",
"{{External_Attribution|MSDN_link=[http://msdn.microsoft.com/en-us/library/ie/ms536365(v=vs.85).aspx cloneNode Method]|HTML5Rocks_link=}}",
"{{JS_Syntax|Formats={{JS Syntax Format|Format='''JSON.stringify(''' value [ , replacer] [ , space] ''')'''}}\n}}"];
for (var i = 0; i < testCases.length; i++) { for (var i = 0; i < testCases.length; i++) {
var wikiParser = new WebInspector.WikiParser(testCases[i]); var wikiParser = new WebInspector.WikiParser(testCases[i]);
InspectorTest.addResult("\"" + testCases[i].replace("\r", "\\r") + "\" is parsed"); InspectorTest.addResult("\"" + testCases[i].replace("\r", "\\r") + "\" is parsed");
...@@ -99,7 +106,7 @@ function test() ...@@ -99,7 +106,7 @@ function test()
function testLinkInsideCode(next) function testLinkInsideCode(next)
{ {
var linkInsideCode = "{{Summary_Section|<code> [[google.com|GOOGLE!]] < /code>}}"; var linkInsideCode = "{{Summary_Section|<code> [[google.com|GOOGLE!]] </code >}}";
var article = WebInspector.JSArticle.parse(linkInsideCode); var article = WebInspector.JSArticle.parse(linkInsideCode);
InspectorTest.addObject(article); InspectorTest.addObject(article);
next(); next();
...@@ -124,13 +131,16 @@ function test() ...@@ -124,13 +131,16 @@ function test()
function testLinkWithoutSpace(next) function testLinkWithoutSpace(next)
{ {
var linkWithoutSpaceTest = "{{Summary_Section|[[http://wrong.com|WRONG_LINK]]}}"; var linkWithoutSpaceTest = "{{Summary_Section|[[http://wrong.com|WRONG_LINK]]}}";
var article = WebInspector.JSArticle.parse(linkWithoutSpaceTest); try {
InspectorTest.addObject(article); var article = WebInspector.JSArticle.parse(linkWithoutSpaceTest);
InspectorTest.addObject(article);
} catch (error) {
InspectorTest.addResult("Expected error: " + error);
}
next(); next();
} }
]); ]);
} }
</script> </script>
</head> </head>
......
...@@ -166,6 +166,20 @@ String.prototype.escapeHTML = function() ...@@ -166,6 +166,20 @@ String.prototype.escapeHTML = function()
return this.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;"); //" doublequotes just for editor return this.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;"); //" doublequotes just for editor
} }
/**
* @return {string}
*/
String.prototype.unescapeHTML = function()
{
return this.replace(/&lt;/g, "<")
.replace(/&gt;/g, ">")
.replace(/&#58;/g, ":")
.replace(/&quot;/g, "\"")
.replace(/&#60;/g, "<")
.replace(/&#62;/g, ">")
.replace(/&amp;/g, "&");
}
/** /**
* @return {string} * @return {string}
*/ */
......
...@@ -22,6 +22,7 @@ WebInspector.DocumentationView.showDocumentationURL = function(url, searchItem) ...@@ -22,6 +22,7 @@ WebInspector.DocumentationView.showDocumentationURL = function(url, searchItem)
if (!WebInspector.DocumentationView._view) if (!WebInspector.DocumentationView._view)
WebInspector.DocumentationView._view = new WebInspector.DocumentationView(); WebInspector.DocumentationView._view = new WebInspector.DocumentationView();
var view = WebInspector.DocumentationView._view; var view = WebInspector.DocumentationView._view;
view.element.removeChildren();
WebInspector.inspectorView.showCloseableViewInDrawer("documentation", WebInspector.UIString("Documentation"), view); WebInspector.inspectorView.showCloseableViewInDrawer("documentation", WebInspector.UIString("Documentation"), view);
view.showDocumentation(url, searchItem); view.showDocumentation(url, searchItem);
} }
...@@ -61,14 +62,19 @@ WebInspector.DocumentationView.prototype = { ...@@ -61,14 +62,19 @@ WebInspector.DocumentationView.prototype = {
return; return;
} }
var wikiMarkupText = pages[wikiKeys[0]]["revisions"]["0"]["*"]; var wikiMarkupText = pages[wikiKeys[0]]["revisions"]["0"]["*"];
var article = WebInspector.JSArticle.parse(wikiMarkupText); var article;
try {
article = WebInspector.JSArticle.parse(wikiMarkupText);
} catch (error) {
console.error("Article could not be parsed. " + error.message);
}
if (!article) { if (!article) {
this._createEmptyPage(); this._createEmptyPage();
return; return;
} }
var renderer = new WebInspector.DocumentationView.Renderer(article, searchItem);
this.element.removeChildren(); this.element.removeChildren();
var renderer = new WebInspector.DocumentationView.Renderer(article, searchItem);
this.element.appendChild(renderer.renderJSArticle()); this.element.appendChild(renderer.renderJSArticle());
}, },
...@@ -171,7 +177,7 @@ WebInspector.DocumentationView.Renderer.prototype = { ...@@ -171,7 +177,7 @@ WebInspector.DocumentationView.Renderer.prototype = {
if (i > 0) if (i > 0)
signature.createTextChild(", ") signature.createTextChild(", ")
var parameterType = signature.createChild("span", "documentation-parameter-data-type-value"); var parameterType = signature.createChild("span", "documentation-parameter-data-type-value");
parameterType.textContent = parameters[i].dataType + (parameters[i].optional ? "=" : "");; parameterType.textContent = parameters[i].dataType + (parameters[i].optional ? "=" : "");
} }
signature.createTextChild("): "); signature.createTextChild("): ");
var returnTypeElement = signature.createChild("span", "documentation-parameter-data-type-value"); var returnTypeElement = signature.createChild("span", "documentation-parameter-data-type-value");
...@@ -258,11 +264,11 @@ WebInspector.DocumentationView.Renderer.prototype = { ...@@ -258,11 +264,11 @@ WebInspector.DocumentationView.Renderer.prototype = {
/** /**
* @param {!WebInspector.WikiParser.ArticleElement} article * @param {!WebInspector.WikiParser.ArticleElement} article
* @return {!Element} * @return {?Element}
*/ */
_renderBlock: function(article) _renderBlock: function(article)
{ {
var element; var element = null;
var elementTypes = WebInspector.WikiParser.ArticleElement.Type; var elementTypes = WebInspector.WikiParser.ArticleElement.Type;
switch (article.type()) { switch (article.type()) {
...@@ -288,14 +294,17 @@ WebInspector.DocumentationView.Renderer.prototype = { ...@@ -288,14 +294,17 @@ WebInspector.DocumentationView.Renderer.prototype = {
if (article.isHighlighted()) if (article.isHighlighted())
element.classList.add("documentation-highlighted-text"); element.classList.add("documentation-highlighted-text");
break; break;
default:
console.error("Unknown ArticleElement type " + article.type());
case elementTypes.Block: case elementTypes.Block:
element = document.createElement(article.hasBullet() ? "li" : "p"); element = document.createElement(article.hasBullet() ? "li" : "p");
break; break;
default:
console.error("Unknown ArticleElement type " + article.type());
return null;
} }
if (article instanceof WebInspector.WikiParser.Block || article instanceof WebInspector.WikiParser.Inline) { if (article.type() === WebInspector.WikiParser.ArticleElement.Type.Block
|| article.type() === WebInspector.WikiParser.ArticleElement.Type.Code
|| article.type() === WebInspector.WikiParser.ArticleElement.Type.Inline) {
for (var i = 0; i < article.children().length; ++i) { for (var i = 0; i < article.children().length; ++i) {
var child = this._renderBlock(article.children()[i]); var child = this._renderBlock(article.children()[i]);
if (child) if (child)
......
...@@ -25,43 +25,55 @@ WebInspector.JSArticle = function() ...@@ -25,43 +25,55 @@ WebInspector.JSArticle = function()
/** /**
* @constructor * @constructor
* @param {string} name * @param {?WebInspector.WikiParser.Block} name
* @param {string} dataType * @param {?WebInspector.WikiParser.Block} dataType
* @param {string} optional * @param {?WebInspector.WikiParser.Block} optional
* @param {?WebInspector.WikiParser.Block} description * @param {?WebInspector.WikiParser.Block} description
*/ */
WebInspector.JSArticle.Parameter = function(name, dataType, optional, description) WebInspector.JSArticle.Parameter = function(name, dataType, optional, description)
{ {
this.name = name; this.name = WebInspector.JSArticle.unfoldStringValue(name);
this.dataType = dataType; this.dataType = WebInspector.JSArticle.unfoldStringValue(dataType);
this.optional = optional.toUpperCase() === "YES"; var textContent = WebInspector.JSArticle.unfoldStringValue(optional);
this.optional = textContent ? textContent.toUpperCase() === "YES" : false;
this.description = description; this.description = description;
} }
/** /**
* @constructor * @constructor
* @param {string} language * @param {?WebInspector.WikiParser.Block} language
* @param {string} code * @param {!WebInspector.WikiParser.Block} code
* @param {string} liveUrl * @param {?WebInspector.WikiParser.Block} liveUrl
* @param {?WebInspector.WikiParser.Block} description * @param {?WebInspector.WikiParser.Block} description
*/ */
WebInspector.JSArticle.Example = function(language, code, liveUrl, description) WebInspector.JSArticle.Example = function(language, code, liveUrl, description)
{ {
this.language = language; this.language = WebInspector.JSArticle.unfoldStringValue(language);
this.code = code; this.code = WebInspector.JSArticle.unfoldStringValue(code);
this.liveUrl = liveUrl; this.liveUrl = WebInspector.JSArticle.unfoldStringValue(liveUrl);
this.description = description; this.description = description;
} }
/** /**
* @constructor * @constructor
* @param {string} returnValueName * @param {?WebInspector.WikiParser.Block} returnValueName
* @param {string} returnValueDescription * @param {?WebInspector.WikiParser.Block} returnValueDescription
*/ */
WebInspector.JSArticle.Method = function(returnValueName, returnValueDescription) WebInspector.JSArticle.Method = function(returnValueName, returnValueDescription)
{ {
this.returnValueName = returnValueName; this.returnValueName = WebInspector.JSArticle.unfoldStringValue(returnValueName);
this.returnValueDescription = returnValueDescription; this.returnValueDescription = WebInspector.JSArticle.unfoldStringValue(returnValueDescription);
}
/**
* @param {?WebInspector.WikiParser.Block} block
* @return {?string}
*/
WebInspector.JSArticle.unfoldStringValue = function(block)
{
if (block && block.hasChildren() && block.children()[0].hasChildren())
return block.children()[0].children()[0].text();
return null;
} }
/** /**
...@@ -70,22 +82,8 @@ WebInspector.JSArticle.Method = function(returnValueName, returnValueDescription ...@@ -70,22 +82,8 @@ WebInspector.JSArticle.Method = function(returnValueName, returnValueDescription
*/ */
WebInspector.JSArticle.parse = function(wikiMarkupText) WebInspector.JSArticle.parse = function(wikiMarkupText)
{ {
/**
* @param {string} string
* @param {string} debugInfo
* @return {?WebInspector.WikiParser.Block}
*/
function parseString(string, debugInfo)
{
var result = wikiParser.parseString(string);
if (!result)
console.error("Can't parse " + debugInfo);
return result;
}
var wikiParser = new WebInspector.WikiParser(wikiMarkupText); var wikiParser = new WebInspector.WikiParser(wikiMarkupText);
var wikiDocument = wikiParser.document(); var wikiDocument = wikiParser.document();
var article = new WebInspector.JSArticle(); var article = new WebInspector.JSArticle();
article.pageTitle = wikiDocument["Page_Title"]; article.pageTitle = wikiDocument["Page_Title"];
if (typeof article.pageTitle !== "string") if (typeof article.pageTitle !== "string")
...@@ -101,23 +99,18 @@ WebInspector.JSArticle.parse = function(wikiMarkupText) ...@@ -101,23 +99,18 @@ WebInspector.JSArticle.parse = function(wikiMarkupText)
article.methods = new WebInspector.JSArticle.Method(returnValueName, returnValue); article.methods = new WebInspector.JSArticle.Method(returnValueName, returnValue);
} }
var remarks = wikiDocument["Remarks_Section"] ? wikiDocument["Remarks_Section"]["Remarks"] : null; article.remarks = wikiDocument["Remarks_Section"] ? wikiDocument["Remarks_Section"]["Remarks"] : null;
if (remarks) article.summary = wikiDocument["Summary_Section"];
article.remarks = parseString(remarks, "remarks");
var summary = wikiDocument["Summary_Section"];
if (summary)
article.summary = parseString(summary, "summary");
var examples = wikiDocument["Examples_Section"] ? wikiDocument["Examples_Section"]["Examples"] : []; var examples = wikiDocument["Examples_Section"] ? wikiDocument["Examples_Section"]["Examples"] : [];
if (!Array.isArray(examples) && typeof examples !== "undefined") if (!Array.isArray(examples) && typeof examples !== "undefined")
examples = [examples]; examples = [examples];
for (var i = 0; i < examples.length; ++i) { for (var i = 0; i < examples.length; ++i) {
var language = examples[i]["Single Example"]["Language"]; var language = examples[i].values["Language"];
var code = examples[i]["Single Example"]["Code"]; var code = examples[i].values["Code"];
var liveUrl = examples[i]["Single Example"]["LiveURL"]; var liveUrl = examples[i].values["LiveURL"];
var description = parseString(examples[i]["Single Example"]["Description"], "example description"); var description = examples[i].values["Description"];
article.examples.push(new WebInspector.JSArticle.Example(language, code, liveUrl, description)); article.examples.push(new WebInspector.JSArticle.Example(language, code, liveUrl, description));
} }
...@@ -126,10 +119,10 @@ WebInspector.JSArticle.parse = function(wikiMarkupText) ...@@ -126,10 +119,10 @@ WebInspector.JSArticle.parse = function(wikiMarkupText)
parameters = [parameters]; parameters = [parameters];
for (var i = 0; i < parameters.length; ++i) { for (var i = 0; i < parameters.length; ++i) {
var name = parameters[i]["Method Parameter"]["Name"]; var name = parameters[i].values["Name"];
var dataType = parameters[i]["Method Parameter"]["Data type"]; var dataType = parameters[i].values["Data type"];
var optional = parameters[i]["Method Parameter"]["Optional"]; var optional = parameters[i].values["Optional"];
var description = parseString(parameters[i]["Method Parameter"]["Description"], "method description"); var description = parameters[i].values["Description"];
article.parameters.push(new WebInspector.JSArticle.Parameter(name, dataType, optional, description)); article.parameters.push(new WebInspector.JSArticle.Parameter(name, dataType, optional, description));
} }
......
...@@ -8,72 +8,66 @@ ...@@ -8,72 +8,66 @@
*/ */
WebInspector.WikiParser = function(wikiMarkupText) WebInspector.WikiParser = function(wikiMarkupText)
{ {
this._position = 0; var text = wikiMarkupText;
this._wikiMarkupText = wikiMarkupText; this._tokenizer = new WebInspector.WikiParser.Tokenizer(text);
this._document = this._parse(); this._document = this._parse();
/** @type {?WebInspector.WikiParser.Tokenizer} */
this._tokenizer;
} }
/** /**
* @package * @constructor
* @enum {string}
*/ */
WebInspector.WikiParser.State = { WebInspector.WikiParser.Section = function()
Error: "Error", {
FirstOpen: "FirstOpen", /** @type {string} */
SecondOpen: "SecondOpen", this.title;
Title: "Title",
PropertyName: "PropertyName",
PropertyValue: "PropertyValue",
FirstClose: "FirstClose",
SecondClose: "SecondClose"
}
/** /** @type {?WebInspector.WikiParser.Values} */
* @package this.values;
* @enum {string}
*/
WebInspector.WikiParser.LinkStates = {
Error: "Error",
LinkUrl: "LinkUrl",
LinkName: "LinkName"
}
/** /** @type {?WebInspector.WikiParser.ArticleElement} */
* @package this.singleValue;
* @enum {string}
*/
WebInspector.WikiParser.HtmlStates = {
Error: "Error",
Entry: "Entry",
InsideTag: "InsideTag",
Exit: "Exit"
} }
/** /**
* @package * @constructor
* @enum {string}
*/ */
WebInspector.WikiParser.ValueState = { WebInspector.WikiParser.Field = function()
Error: "Error", {
Outside: "Outside", /** @type {string} */
InsideSquare: "InsideSquare" this.name;
/** @type {?WebInspector.WikiParser.FieldValue} */
this.value;
} }
/** @typedef {(?WebInspector.WikiParser.ArticleElement|!Array.<!WebInspector.WikiParser.Section>)} */
WebInspector.WikiParser.FieldValue;
/** @typedef {?Object.<string, !WebInspector.WikiParser.FieldValue>} */
WebInspector.WikiParser.Values;
/** @typedef {(?WebInspector.WikiParser.Value|?WebInspector.WikiParser.ArticleElement)} */
WebInspector.WikiParser.Value;
/** /**
* @package * @package
* @enum {string} * @enum {string}
*/ */
WebInspector.WikiParser.TokenType = { WebInspector.WikiParser.TokenType = {
TripleQuotes: "TripleQuotes", Text: "Text",
Table: "Table",
OpeningBraces: "OpeningBraces",
ClosingBraces: "ClosingBraces",
Exclamation: "Exclamation",
OpeningBrackets: "OpeningBrackets", OpeningBrackets: "OpeningBrackets",
OpeningCodeTag: "OpeningCodeTag",
ClosingBrackets: "ClosingBrackets", ClosingBrackets: "ClosingBrackets",
EqualSign: "EqualSign",
EqualSignInBraces: "EqualSignInBraces",
VerticalLine: "VerticalLine",
TripleQuotes: "TripleQuotes",
OpeningCodeTag: "OpeningCodeTag",
ClosingCodeTag: "ClosingCodeTag", ClosingCodeTag: "ClosingCodeTag",
Bullet: "Bullet", Bullet: "Bullet",
Text: "Text",
VerticalLine: "VerticalLine",
LineEnd: "LineEnd", LineEnd: "LineEnd",
CodeBlock: "CodeBlock" CodeBlock: "CodeBlock"
} }
...@@ -114,22 +108,41 @@ WebInspector.WikiParser.Token.prototype = { ...@@ -114,22 +108,41 @@ WebInspector.WikiParser.Token.prototype = {
WebInspector.WikiParser.Tokenizer = function(str) WebInspector.WikiParser.Tokenizer = function(str)
{ {
this._text = str; this._text = str;
this._token = this._internalNextToken();
} }
WebInspector.WikiParser.Tokenizer.prototype = { WebInspector.WikiParser.Tokenizer.prototype = {
/** /**
* @return {!WebInspector.WikiParser.Token} * @return {!WebInspector.WikiParser.Token}
*/ */
_nextToken: function() peekToken: function()
{
return this._token;
},
/**
* @return {!WebInspector.WikiParser.Token}
*/
nextToken: function()
{
var token = this._token;
this._token = this._internalNextToken();
return token;
},
/**
* @return {!WebInspector.WikiParser.Token}
*/
_internalNextToken: function()
{ {
if (WebInspector.WikiParser.newLineWithSpace.test(this._text)) { if (WebInspector.WikiParser.newLineWithSpace.test(this._text)) {
var result = WebInspector.WikiParser.newLineWithSpace.exec(this._text); var result = WebInspector.WikiParser.newLineWithSpace.exec(this._text);
var begin = result.index + result[0].length; var begin = result.index;
var end = this._text.length; var end = this._text.length;
var lineEnd = WebInspector.WikiParser.newLineWithoutSpace.exec(this._text); var lineEnd = WebInspector.WikiParser.newLineWithoutSpace.exec(this._text);
if (lineEnd) if (lineEnd)
end = lineEnd.index; end = lineEnd.index;
var token = this._text.substring(begin, end).replace(/\n */g, "\n"); var token = this._text.substring(begin, end).replace(/\n /g, "\n").replace(/{{=}}/g, "=");
this._text = this._text.substring(end + 1); this._text = this._text.substring(end + 1);
return new WebInspector.WikiParser.Token(token, WebInspector.WikiParser.TokenType.CodeBlock); return new WebInspector.WikiParser.Token(token, WebInspector.WikiParser.TokenType.CodeBlock);
} }
...@@ -158,15 +171,76 @@ WebInspector.WikiParser.Tokenizer.prototype = { ...@@ -158,15 +171,76 @@ WebInspector.WikiParser.Tokenizer.prototype = {
return new WebInspector.WikiParser.Token(token, WebInspector.WikiParser.TokenType.Text); return new WebInspector.WikiParser.Token(token, WebInspector.WikiParser.TokenType.Text);
}, },
/**
* @return {!WebInspector.WikiParser.Tokenizer}
*/
clone: function()
{
var tokenizer = new WebInspector.WikiParser.Tokenizer(this._text);
tokenizer._token = this._token;
tokenizer._text = this._text;
return tokenizer;
},
/** /**
* @return {boolean} * @return {boolean}
*/ */
_hasMoreTokens: function() hasMoreTokens: function()
{ {
return !!this._text.length; return !!this._text.length;
} }
} }
WebInspector.WikiParser.table = /^{{{!}}/;
WebInspector.WikiParser.exclamation = /^{{!}}/;
WebInspector.WikiParser.openingBraces = /^{{/;
WebInspector.WikiParser.equalSign = /^=/;
WebInspector.WikiParser.equalSignInBraces = /^{{=}}/;
WebInspector.WikiParser.closingBraces = /^\s*}}/;
WebInspector.WikiParser.oneOpeningBracketWithSpace = /^\n* \[/;
WebInspector.WikiParser.twoOpeningBracketsWithSpace = /^\n* \[\[/;
WebInspector.WikiParser.oneClosingBracket = /^\n*\]/;
WebInspector.WikiParser.twoClosingBrackets = /^\n*\]\]/;
WebInspector.WikiParser.tripleQuotes = /^\n*'''/;
WebInspector.WikiParser.openingCodeTag = /^<code\s*>/;
WebInspector.WikiParser.closingCodeTag = /^<\/code\s*>/;
WebInspector.WikiParser.closingBullet = /^\*/;
WebInspector.WikiParser.lineEnd = /^\n/;
WebInspector.WikiParser.verticalLine = /^\n*\|/;
WebInspector.WikiParser.newLineWithSpace = /^\n [^ ]/;
WebInspector.WikiParser.newLineWithoutSpace = /\n[^ ]/;
/**
* @constructor
* @param {!RegExp} regex
* @param {!WebInspector.WikiParser.TokenType} type
*/
WebInspector.WikiParser.TokenDescriptor = function(regex, type)
{
this.regex = regex;
this.type = type;
}
WebInspector.WikiParser._tokenDescriptors = [
new WebInspector.WikiParser.TokenDescriptor(WebInspector.WikiParser.exclamation, WebInspector.WikiParser.TokenType.Exclamation),
new WebInspector.WikiParser.TokenDescriptor(WebInspector.WikiParser.equalSignInBraces, WebInspector.WikiParser.TokenType.EqualSignInBraces),
new WebInspector.WikiParser.TokenDescriptor(WebInspector.WikiParser.equalSign, WebInspector.WikiParser.TokenType.EqualSign),
new WebInspector.WikiParser.TokenDescriptor(WebInspector.WikiParser.table, WebInspector.WikiParser.TokenType.Table),
new WebInspector.WikiParser.TokenDescriptor(WebInspector.WikiParser.openingBraces, WebInspector.WikiParser.TokenType.OpeningBraces),
new WebInspector.WikiParser.TokenDescriptor(WebInspector.WikiParser.verticalLine, WebInspector.WikiParser.TokenType.VerticalLine),
new WebInspector.WikiParser.TokenDescriptor(WebInspector.WikiParser.closingBraces, WebInspector.WikiParser.TokenType.ClosingBraces),
new WebInspector.WikiParser.TokenDescriptor(WebInspector.WikiParser.twoOpeningBracketsWithSpace, WebInspector.WikiParser.TokenType.OpeningBrackets),
new WebInspector.WikiParser.TokenDescriptor(WebInspector.WikiParser.twoClosingBrackets, WebInspector.WikiParser.TokenType.ClosingBrackets),
new WebInspector.WikiParser.TokenDescriptor(WebInspector.WikiParser.oneOpeningBracketWithSpace, WebInspector.WikiParser.TokenType.OpeningBrackets),
new WebInspector.WikiParser.TokenDescriptor(WebInspector.WikiParser.oneClosingBracket, WebInspector.WikiParser.TokenType.ClosingBrackets),
new WebInspector.WikiParser.TokenDescriptor(WebInspector.WikiParser.newLineWithSpace, WebInspector.WikiParser.TokenType.CodeBlock),
new WebInspector.WikiParser.TokenDescriptor(WebInspector.WikiParser.tripleQuotes, WebInspector.WikiParser.TokenType.TripleQuotes),
new WebInspector.WikiParser.TokenDescriptor(WebInspector.WikiParser.openingCodeTag, WebInspector.WikiParser.TokenType.OpeningCodeTag),
new WebInspector.WikiParser.TokenDescriptor(WebInspector.WikiParser.closingCodeTag, WebInspector.WikiParser.TokenType.ClosingCodeTag),
new WebInspector.WikiParser.TokenDescriptor(WebInspector.WikiParser.closingBullet, WebInspector.WikiParser.TokenType.Bullet),
new WebInspector.WikiParser.TokenDescriptor(WebInspector.WikiParser.lineEnd, WebInspector.WikiParser.TokenType.LineEnd)
]
WebInspector.WikiParser.prototype = { WebInspector.WikiParser.prototype = {
/** /**
* @return {!Object} * @return {!Object}
...@@ -177,199 +251,206 @@ WebInspector.WikiParser.prototype = { ...@@ -177,199 +251,206 @@ WebInspector.WikiParser.prototype = {
}, },
/** /**
* @return {!Object} * @return {?WebInspector.WikiParser.TokenType}
*/
_secondTokenType: function()
{
var tokenizer = this._tokenizer.clone();
if (!tokenizer.hasMoreTokens())
return null;
tokenizer.nextToken();
if (!tokenizer.hasMoreTokens())
return null;
return tokenizer.nextToken().type();
},
/**
* @return {!Object.<string, ?WebInspector.WikiParser.Value>}
*/ */
_parse: function() _parse: function()
{ {
var obj = {}; var obj = {};
this._wikiMarkupText = this._wikiMarkupText.replace(/&lt;/g, "<") while (this._tokenizer.hasMoreTokens()) {
.replace(/&gt;/g, ">") var section = this._parseSection();
.replace(/&#58;/g, ":") if (section.title)
.replace(/&quot;/g, "\"") obj[section.title] = section.singleValue || section.values;
.replace(/&#60;/g, "<")
.replace(/&#62;/g, ">")
.replace(/{{=}}/g, "=")
.replace(/{{!}}/g, "|")
.replace(/&amp;/g, "&");
while (this._position < this._wikiMarkupText.length) {
var field = this._parseField();
for (var key in field) {
console.assert(typeof obj[key] === "undefined", "Duplicate key: " + key);
obj[key] = field[key];
}
} }
return obj; return obj;
}, },
/** /**
* @return {string} * @return {!WebInspector.WikiParser.Section}
*/ */
_parseValue: function() { _parseSection: function()
var states = WebInspector.WikiParser.ValueState; {
var state = states.Outside; var section = new WebInspector.WikiParser.Section();
var value = ""; if (!this._tokenizer.hasMoreTokens() || this._tokenizer.nextToken().type() !== WebInspector.WikiParser.TokenType.OpeningBraces)
while (this._position < this._wikiMarkupText.length) { return section;
switch (state) {
case states.Outside: var title = this._deleteTrailingSpaces(this._parseSectionTitle());
if (this._wikiMarkupText[this._position] === "|" || (this._wikiMarkupText[this._position] === "}" && this._wikiMarkupText[this._position + 1] === "}")) if (!title.length)
return value; return section;
switch (this._wikiMarkupText[this._position]) { section.title = title;
case "<": if (this._tokenizer.peekToken().type() === WebInspector.WikiParser.TokenType.ClosingBraces) {
var indexClose = this._wikiMarkupText.indexOf(">", this._position); this._tokenizer.nextToken();
if (indexClose !== -1) { return section;
value += this._wikiMarkupText.substring(this._position, indexClose + 1); }
this._position = indexClose; var secondTokenType = this._secondTokenType();
} if (!secondTokenType || secondTokenType !== WebInspector.WikiParser.TokenType.EqualSign) {
break; section.singleValue = this._parseMarkupText();
case "[": } else {
state = states.InsideSquare; section.values = {};
value += this._wikiMarkupText[this._position]; while (this._tokenizer.hasMoreTokens()) {
break; var field = this._parseField();
default: section.values[field.name] = field.value;
value += this._wikiMarkupText[this._position]; if (this._tokenizer.peekToken().type() === WebInspector.WikiParser.TokenType.ClosingBraces) {
} this._tokenizer.nextToken();
break; return section;
case states.InsideSquare:
if (this._wikiMarkupText[this._position] === "[") {
var indexClose = this._wikiMarkupText.indexOf("]]", this._position);
if (indexClose !== -1) {
value += this._wikiMarkupText.substring(this._position, indexClose + 2);
this._position = indexClose + 1;
}
} else {
var indexClose = this._wikiMarkupText.indexOf("]", this._position);
if (indexClose !== -1) {
value += this._wikiMarkupText.substring(this._position, indexClose + 1);
this._position = indexClose;
}
} }
state = states.Outside;
break;
} }
this._position++;
} }
return value; var token = this._tokenizer.nextToken();
if (token.type() !== WebInspector.WikiParser.TokenType.ClosingBraces)
throw new Error("Two closing braces expected; found " + token.value());
return section;
}, },
/** /**
* @return {!Object} * @return {!WebInspector.WikiParser.Field}
*/ */
_parseField: function() _parseField: function()
{ {
var obj = {}; var field = new WebInspector.WikiParser.Field();
field.name = this._parseFieldName();
var token = this._tokenizer.peekToken();
switch (token.type()) {
case WebInspector.WikiParser.TokenType.OpeningBraces:
field.value = this._parseArray();
break;
case WebInspector.WikiParser.TokenType.LineEnd:
this._tokenizer.nextToken();
break;
case WebInspector.WikiParser.TokenType.ClosingBraces:
return field;
default:
if (field.name.toUpperCase() === "CODE")
field.value = this._parseExampleCode();
else
field.value = this._parseMarkupText();
}
return field;
},
/**
* @return {!Array.<!WebInspector.WikiParser.Section>}
*/
_parseArray: function()
{
var array = [];
while (this._tokenizer.peekToken().type() === WebInspector.WikiParser.TokenType.OpeningBraces)
array.push(this._parseSection());
if (this._tokenizer.peekToken().type() === WebInspector.WikiParser.TokenType.VerticalLine)
this._tokenizer.nextToken();
return array;
},
/**
* @return {string}
*/
_parseSectionTitle: function()
{
var title = ""; var title = "";
var propertyName = ""; while (this._tokenizer.hasMoreTokens()) {
var propertyValue = ""; var token = this._tokenizer.peekToken();
var states = WebInspector.WikiParser.State; switch (token.type()) {
var state = states.FirstOpen; case WebInspector.WikiParser.TokenType.ClosingBraces:
while (this._position < this._wikiMarkupText.length) { return title;
var skipIncrement = false; case WebInspector.WikiParser.TokenType.VerticalLine:
switch (state) { this._tokenizer.nextToken();
case states.FirstOpen: return title;
if (this._wikiMarkupText[this._position] === "{") case WebInspector.WikiParser.TokenType.Text:
state = states.SecondOpen; title += this._tokenizer.nextToken().value();
else
state = states.Error;
break;
case states.SecondOpen:
if (this._wikiMarkupText[this._position] === "{")
state = states.Title;
else
state = states.Error;
break;
case states.Title:
if (this._wikiMarkupText[this._position] === "|") {
title = this._deleteTrailingSpaces(title);
if (title !== "")
obj[title] = {};
state = states.PropertyName;
} else if (this._wikiMarkupText[this._position] === "}") {
title = this._deleteTrailingSpaces(title);
if (title !== "")
obj[title] = {};
state = states.FirstClose;
} else {
title += (this._wikiMarkupText[this._position] === "\n" ? "" : this._wikiMarkupText[this._position]);
}
break; break;
case states.PropertyName: default:
if (this._wikiMarkupText[this._position] === "=") { throw new Error("Title could not be parsed. Unexpected token " + token.value());
state = states.PropertyValue; }
this._deleteTrailingSpaces(propertyName); }
if (propertyName !== "") return title;
obj[title][propertyName] = []; },
} else {
if (this._wikiMarkupText[this._position] === "}") { /**
propertyName = this._deleteTrailingSpaces(propertyName); * @return {string}
obj[title] = propertyName; */
state = states.FirstClose; _parseFieldName: function()
} else { {
propertyName += this._wikiMarkupText[this._position]; var name = "";
} while (this._tokenizer.hasMoreTokens()) {
} var token = this._tokenizer.peekToken();
switch (token.type()) {
case WebInspector.WikiParser.TokenType.ClosingBraces:
return name;
case WebInspector.WikiParser.TokenType.EqualSign:
this._tokenizer.nextToken();
return name;
case WebInspector.WikiParser.TokenType.VerticalLine:
case WebInspector.WikiParser.TokenType.Text:
name += this._tokenizer.nextToken().value();
break; break;
case states.PropertyValue: default:
if (this._wikiMarkupText[this._position] === "{" && this._wikiMarkupText[this._position + 1] === "{") { throw new Error("Name could not be parsed. Unexpected token " + token.value());
propertyValue = this._parseField(); }
obj[title][propertyName].push(propertyValue); }
propertyValue = ""; return name;
skipIncrement = true; },
} else if (this._wikiMarkupText[this._position] === "|") {
propertyValue = this._deleteTrailingSpaces(propertyValue); /**
if (propertyValue !== "") * @return {!WebInspector.WikiParser.Block}
obj[title][propertyName] = propertyValue; */
_parseExampleCode: function()
state = states.PropertyName; {
if (Array.isArray(obj[title][propertyName]) && obj[title][propertyName].length === 1) { var code = "";
var newObj = obj[title][propertyName][0];
obj[title][propertyName] = newObj; /**
} * @return {!WebInspector.WikiParser.Block}
*/
propertyName = ""; function wrapIntoArticleElement()
propertyValue = ""; {
} else if (this._position + 1 < this._wikiMarkupText.length && this._wikiMarkupText[this._position] === "}" && this._wikiMarkupText[this._position + 1] === "}") { var plainText = new WebInspector.WikiParser.PlainText(code);
propertyValue = this._deleteTrailingSpaces(propertyValue); var block = new WebInspector.WikiParser.Block([plainText])
if (propertyValue !== "") var articleElement = new WebInspector.WikiParser.Block([block]);
obj[title][propertyName].push(propertyValue); return articleElement;
if (Array.isArray(obj[title][propertyName]) && obj[title][propertyName].length === 1) { }
var newObj = obj[title][propertyName][0];
obj[title][propertyName] = newObj; while (this._tokenizer.hasMoreTokens()) {
} var token = this._tokenizer.peekToken();
switch (token.type()) {
propertyValue = ""; case WebInspector.WikiParser.TokenType.ClosingBraces:
state = states.FirstClose; return wrapIntoArticleElement();
} else { case WebInspector.WikiParser.TokenType.VerticalLine:
propertyValue = this._parseValue(); this._tokenizer.nextToken();
skipIncrement = true; return wrapIntoArticleElement();
} case WebInspector.WikiParser.TokenType.Exclamation:
this._tokenizer.nextToken();
code += "|";
break; break;
case states.FirstClose: case WebInspector.WikiParser.TokenType.EqualSignInBraces:
if (this._wikiMarkupText[this._position] === "}") this._tokenizer.nextToken();
state = states.SecondClose; code += "=";
else
state = states.Error;
break; break;
case states.SecondClose: default:
while (this._position < this._wikiMarkupText.length && this._wikiMarkupText[this._position] === "\n") this._tokenizer.nextToken();
this._position++; code += token.value();
return obj;
case states.Error:
this._position = this._wikiMarkupText.length;
return {};
} }
if (!skipIncrement)
this._position++;
} }
return obj; return wrapIntoArticleElement();
}, },
/** /**
* @param {string} str
* @return {?WebInspector.WikiParser.Block} * @return {?WebInspector.WikiParser.Block}
*/ */
parseString: function(str) _parseMarkupText: function()
{ {
this._tokenizer = new WebInspector.WikiParser.Tokenizer(str);
var children = []; var children = [];
var blockChildren = []; var blockChildren = [];
var text = ""; var text = "";
...@@ -380,7 +461,7 @@ WebInspector.WikiParser.prototype = { ...@@ -380,7 +461,7 @@ WebInspector.WikiParser.prototype = {
var currentText = self._deleteTrailingSpaces(text); var currentText = self._deleteTrailingSpaces(text);
if (!currentText.length) if (!currentText.length)
return; return;
var simpleText = new WebInspector.WikiParser.PlainText(currentText, false); var simpleText = new WebInspector.WikiParser.PlainText(currentText);
blockChildren.push(simpleText); blockChildren.push(simpleText);
text = ""; text = "";
} }
...@@ -388,49 +469,75 @@ WebInspector.WikiParser.prototype = { ...@@ -388,49 +469,75 @@ WebInspector.WikiParser.prototype = {
function processBlock() function processBlock()
{ {
if (blockChildren.length) { if (blockChildren.length) {
children.push(new WebInspector.WikiParser.Block(blockChildren, false)); children.push(new WebInspector.WikiParser.Block(blockChildren));
blockChildren = []; blockChildren = [];
} }
} }
while (this._tokenizer._hasMoreTokens()) {
var token = this._tokenizer._nextToken(); while (this._tokenizer.hasMoreTokens()) {
var token = this._tokenizer.peekToken();
switch (token.type()) { switch (token.type()) {
case WebInspector.WikiParser.TokenType.VerticalLine:
case WebInspector.WikiParser.TokenType.ClosingBraces:
if (token.type() === WebInspector.WikiParser.TokenType.VerticalLine)
this._tokenizer.nextToken();
processSimpleText();
processBlock();
return new WebInspector.WikiParser.Block(children);
case WebInspector.WikiParser.TokenType.TripleQuotes: case WebInspector.WikiParser.TokenType.TripleQuotes:
this._tokenizer.nextToken();
processSimpleText(); processSimpleText();
var highlightText = this._parseHighlight(); var highlightText = this._parseHighlight();
blockChildren.push(highlightText) blockChildren.push(highlightText)
break; break;
case WebInspector.WikiParser.TokenType.OpeningBrackets: case WebInspector.WikiParser.TokenType.OpeningBrackets:
this._tokenizer.nextToken();
processSimpleText(); processSimpleText();
var link = this._parseLink(); var link = this._parseLink();
blockChildren.push(link); blockChildren.push(link);
break; break;
case WebInspector.WikiParser.TokenType.OpeningCodeTag: case WebInspector.WikiParser.TokenType.OpeningCodeTag:
this._tokenizer.nextToken();
processSimpleText(); processSimpleText();
var code = this._parseCode(); var code = this._parseCode();
blockChildren.push(code); blockChildren.push(code);
break; break;
case WebInspector.WikiParser.TokenType.Bullet: case WebInspector.WikiParser.TokenType.Bullet:
this._tokenizer.nextToken();
processSimpleText(); processSimpleText();
processBlock(); processBlock();
var bulletText = this._parseBullet(); var bulletText = this._parseBullet();
children.push(bulletText); children.push(bulletText);
break; break;
case WebInspector.WikiParser.TokenType.CodeBlock: case WebInspector.WikiParser.TokenType.CodeBlock:
this._tokenizer.nextToken();
processSimpleText(); processSimpleText();
processBlock(); processBlock();
var code = new WebInspector.WikiParser.CodeBlock(token.value()); var code = new WebInspector.WikiParser.CodeBlock(this._trimLeadingNewLines(token.value()));
children.push(code); children.push(code);
break; break;
case WebInspector.WikiParser.TokenType.LineEnd: case WebInspector.WikiParser.TokenType.LineEnd:
this._tokenizer.nextToken();
processSimpleText(); processSimpleText();
processBlock(); processBlock();
break; break;
case WebInspector.WikiParser.TokenType.VerticalLine: case WebInspector.WikiParser.TokenType.EqualSignInBraces:
this._tokenizer.nextToken();
text += "=";
break;
case WebInspector.WikiParser.TokenType.Exclamation:
this._tokenizer.nextToken();
text += "|";
break;
case WebInspector.WikiParser.TokenType.ClosingBrackets:
case WebInspector.WikiParser.TokenType.Text: case WebInspector.WikiParser.TokenType.Text:
case WebInspector.WikiParser.TokenType.EqualSign:
case WebInspector.WikiParser.TokenType.Table:
this._tokenizer.nextToken();
text += token.value(); text += token.value();
break; break;
default: default:
this._tokenizer.nextToken();
return null; return null;
} }
} }
...@@ -438,7 +545,7 @@ WebInspector.WikiParser.prototype = { ...@@ -438,7 +545,7 @@ WebInspector.WikiParser.prototype = {
processSimpleText(); processSimpleText();
processBlock(); processBlock();
return new WebInspector.WikiParser.Block(children, false); return new WebInspector.WikiParser.Block(children);
}, },
/** /**
...@@ -448,12 +555,13 @@ WebInspector.WikiParser.prototype = { ...@@ -448,12 +555,13 @@ WebInspector.WikiParser.prototype = {
{ {
var url = ""; var url = "";
var children = []; var children = [];
while (this._tokenizer._hasMoreTokens()) { while (this._tokenizer.hasMoreTokens()) {
var token = this._tokenizer._nextToken(); var token = this._tokenizer.nextToken();
switch (token.type()) { switch (token.type()) {
case WebInspector.WikiParser.TokenType.ClosingBrackets: case WebInspector.WikiParser.TokenType.ClosingBrackets:
return new WebInspector.WikiParser.Link(url, children); return new WebInspector.WikiParser.Link(url, children);
case WebInspector.WikiParser.TokenType.VerticalLine: case WebInspector.WikiParser.TokenType.VerticalLine:
case WebInspector.WikiParser.TokenType.Exclamation:
children.push(this._parseLinkName()); children.push(this._parseLinkName());
return new WebInspector.WikiParser.Link(url, children); return new WebInspector.WikiParser.Link(url, children);
default: default:
...@@ -470,16 +578,30 @@ WebInspector.WikiParser.prototype = { ...@@ -470,16 +578,30 @@ WebInspector.WikiParser.prototype = {
_parseLinkName: function() _parseLinkName: function()
{ {
var children = []; var children = [];
while (this._tokenizer._hasMoreTokens()) { var text = "";
var token = this._tokenizer._nextToken(); var self = this;
function processSimpleText()
{
text = self._deleteTrailingSpaces(text);
if (!text.length)
return;
var simpleText = new WebInspector.WikiParser.PlainText(text);
children.push(simpleText);
text = "";
}
while (this._tokenizer.hasMoreTokens()) {
var token = this._tokenizer.nextToken();
switch (token.type()) { switch (token.type()) {
case WebInspector.WikiParser.TokenType.ClosingBrackets: case WebInspector.WikiParser.TokenType.ClosingBrackets:
processSimpleText();
return new WebInspector.WikiParser.Inline(WebInspector.WikiParser.ArticleElement.Type.Inline, children); return new WebInspector.WikiParser.Inline(WebInspector.WikiParser.ArticleElement.Type.Inline, children);
case WebInspector.WikiParser.TokenType.OpeningCodeTag: case WebInspector.WikiParser.TokenType.OpeningCodeTag:
processSimpleText();
children.push(this._parseCode()); children.push(this._parseCode());
break; break;
default: default:
children.push(new WebInspector.WikiParser.PlainText(token.value(), false)); text += token.value();
break; break;
} }
} }
...@@ -494,13 +616,13 @@ WebInspector.WikiParser.prototype = { ...@@ -494,13 +616,13 @@ WebInspector.WikiParser.prototype = {
{ {
var children = []; var children = [];
var text = ""; var text = "";
while (this._tokenizer._hasMoreTokens()) { while (this._tokenizer.hasMoreTokens()) {
var token = this._tokenizer._nextToken(); var token = this._tokenizer.nextToken();
switch (token.type()) { switch (token.type()) {
case WebInspector.WikiParser.TokenType.ClosingCodeTag: case WebInspector.WikiParser.TokenType.ClosingCodeTag:
text = this._deleteTrailingSpaces(text); text = this._deleteTrailingSpaces(text);
if (text.length) { if (text.length) {
var simpleText = new WebInspector.WikiParser.PlainText(text, false); var simpleText = new WebInspector.WikiParser.PlainText(text);
children.push(simpleText); children.push(simpleText);
text = ""; text = "";
} }
...@@ -517,7 +639,7 @@ WebInspector.WikiParser.prototype = { ...@@ -517,7 +639,7 @@ WebInspector.WikiParser.prototype = {
text = this._deleteTrailingSpaces(text); text = this._deleteTrailingSpaces(text);
if (text.length) if (text.length)
children.push(new WebInspector.WikiParser.PlainText(text, false)); children.push(new WebInspector.WikiParser.PlainText(text));
return new WebInspector.WikiParser.Inline(WebInspector.WikiParser.ArticleElement.Type.Code, children); return new WebInspector.WikiParser.Inline(WebInspector.WikiParser.ArticleElement.Type.Code, children);
}, },
...@@ -528,8 +650,8 @@ WebInspector.WikiParser.prototype = { ...@@ -528,8 +650,8 @@ WebInspector.WikiParser.prototype = {
_parseBullet: function() _parseBullet: function()
{ {
var children = []; var children = [];
while (this._tokenizer._hasMoreTokens()) { while (this._tokenizer.hasMoreTokens()) {
var token = this._tokenizer._nextToken() var token = this._tokenizer.nextToken()
switch (token.type()) { switch (token.type()) {
case WebInspector.WikiParser.TokenType.OpeningBrackets: case WebInspector.WikiParser.TokenType.OpeningBrackets:
children.push(this._parseLink()); children.push(this._parseLink());
...@@ -542,7 +664,7 @@ WebInspector.WikiParser.prototype = { ...@@ -542,7 +664,7 @@ WebInspector.WikiParser.prototype = {
default: default:
var text = this._deleteTrailingSpaces(token.value()); var text = this._deleteTrailingSpaces(token.value());
if (text.length) { if (text.length) {
var simpleText = new WebInspector.WikiParser.PlainText(text, false); var simpleText = new WebInspector.WikiParser.PlainText(text);
children.push(simpleText); children.push(simpleText);
text = ""; text = "";
} }
...@@ -558,8 +680,8 @@ WebInspector.WikiParser.prototype = { ...@@ -558,8 +680,8 @@ WebInspector.WikiParser.prototype = {
_parseHighlight: function() _parseHighlight: function()
{ {
var text = ""; var text = "";
while (this._tokenizer._hasMoreTokens()) { while (this._tokenizer.hasMoreTokens()) {
var token = this._tokenizer._nextToken() var token = this._tokenizer.nextToken()
switch (token.type()) { switch (token.type()) {
case WebInspector.WikiParser.TokenType.TripleQuotes: case WebInspector.WikiParser.TokenType.TripleQuotes:
text = this._deleteTrailingSpaces(text); text = this._deleteTrailingSpaces(text);
...@@ -578,47 +700,18 @@ WebInspector.WikiParser.prototype = { ...@@ -578,47 +700,18 @@ WebInspector.WikiParser.prototype = {
_deleteTrailingSpaces: function(str) _deleteTrailingSpaces: function(str)
{ {
return str.replace(/[\n\r]*$/gm, ""); return str.replace(/[\n\r]*$/gm, "");
} },
}
WebInspector.WikiParser.oneOpeningBracket = /^\n* \[[^\[]/;
WebInspector.WikiParser.twoOpeningBrackets = /^\n* \[\[/;
WebInspector.WikiParser.oneClosingBracket = /^\n*\][^\]] /;
WebInspector.WikiParser.twoClosingBrackets = /^\n*\]\]/;
WebInspector.WikiParser.tripleQuotes = /^\n*'''/;
WebInspector.WikiParser.openingCodeTag = /^<\s*code\s*>/;
WebInspector.WikiParser.closingCodeTag = /^<\s*\/\s*code\s*>/;
WebInspector.WikiParser.closingBullet = /^\*/;
WebInspector.WikiParser.lineEnd = /^\n/;
WebInspector.WikiParser.verticalLine = /^\|/;
WebInspector.WikiParser.newLineWithSpace = /^\n /;
WebInspector.WikiParser.newLineWithoutSpace = /\n[^ ]/;
/** /**
* @constructor * @param {string} str
* @param {!RegExp} regex * @return {string}
* @param {!WebInspector.WikiParser.TokenType} type */
*/ _trimLeadingNewLines: function(str)
WebInspector.WikiParser.TokenDescriptor = function(regex, type) {
{ return str.replace(/^\n*/, "");
this.regex = regex; }
this.type = type;
} }
WebInspector.WikiParser._tokenDescriptors = [
new WebInspector.WikiParser.TokenDescriptor(WebInspector.WikiParser.newLineWithSpace, WebInspector.WikiParser.TokenType.CodeBlock),
new WebInspector.WikiParser.TokenDescriptor(WebInspector.WikiParser.tripleQuotes, WebInspector.WikiParser.TokenType.TripleQuotes),
new WebInspector.WikiParser.TokenDescriptor(WebInspector.WikiParser.oneOpeningBracket, WebInspector.WikiParser.TokenType.OpeningBrackets),
new WebInspector.WikiParser.TokenDescriptor(WebInspector.WikiParser.twoOpeningBrackets, WebInspector.WikiParser.TokenType.OpeningBrackets),
new WebInspector.WikiParser.TokenDescriptor(WebInspector.WikiParser.oneClosingBracket, WebInspector.WikiParser.TokenType.ClosingBrackets),
new WebInspector.WikiParser.TokenDescriptor(WebInspector.WikiParser.twoClosingBrackets, WebInspector.WikiParser.TokenType.ClosingBrackets),
new WebInspector.WikiParser.TokenDescriptor(WebInspector.WikiParser.openingCodeTag, WebInspector.WikiParser.TokenType.OpeningCodeTag),
new WebInspector.WikiParser.TokenDescriptor(WebInspector.WikiParser.closingCodeTag, WebInspector.WikiParser.TokenType.ClosingCodeTag),
new WebInspector.WikiParser.TokenDescriptor(WebInspector.WikiParser.closingBullet, WebInspector.WikiParser.TokenType.Bullet),
new WebInspector.WikiParser.TokenDescriptor(WebInspector.WikiParser.verticalLine, WebInspector.WikiParser.TokenType.VerticalLine),
new WebInspector.WikiParser.TokenDescriptor(WebInspector.WikiParser.lineEnd, WebInspector.WikiParser.TokenType.LineEnd)
];
/** /**
* @constructor * @constructor
* @param {!WebInspector.WikiParser.ArticleElement.Type} type * @param {!WebInspector.WikiParser.ArticleElement.Type} type
...@@ -654,20 +747,20 @@ WebInspector.WikiParser.ArticleElement.Type = { ...@@ -654,20 +747,20 @@ WebInspector.WikiParser.ArticleElement.Type = {
* @constructor * @constructor
* @extends {WebInspector.WikiParser.ArticleElement} * @extends {WebInspector.WikiParser.ArticleElement}
* @param {string} text * @param {string} text
* @param {boolean} highlight * @param {boolean=} highlight
*/ */
WebInspector.WikiParser.PlainText = function(text, highlight) WebInspector.WikiParser.PlainText = function(text, highlight)
{ {
WebInspector.WikiParser.ArticleElement.call(this, WebInspector.WikiParser.ArticleElement.Type.PlainText); WebInspector.WikiParser.ArticleElement.call(this, WebInspector.WikiParser.ArticleElement.Type.PlainText);
this._text = text; this._text = text.unescapeHTML();
this._isHighlighted = highlight; this._isHighlighted = highlight || false;
} }
WebInspector.WikiParser.PlainText.prototype = { WebInspector.WikiParser.PlainText.prototype = {
/** /**
* @return {string} * @return {string}
*/ */
text : function() text: function()
{ {
return this._text; return this._text;
}, },
...@@ -687,13 +780,13 @@ WebInspector.WikiParser.PlainText.prototype = { ...@@ -687,13 +780,13 @@ WebInspector.WikiParser.PlainText.prototype = {
* @constructor * @constructor
* @extends {WebInspector.WikiParser.ArticleElement} * @extends {WebInspector.WikiParser.ArticleElement}
* @param {!Array.<!WebInspector.WikiParser.ArticleElement>} children * @param {!Array.<!WebInspector.WikiParser.ArticleElement>} children
* @param {boolean} hasBullet * @param {boolean=} hasBullet
*/ */
WebInspector.WikiParser.Block = function(children, hasBullet) WebInspector.WikiParser.Block = function(children, hasBullet)
{ {
WebInspector.WikiParser.ArticleElement.call(this, WebInspector.WikiParser.ArticleElement.Type.Block); WebInspector.WikiParser.ArticleElement.call(this, WebInspector.WikiParser.ArticleElement.Type.Block);
this._children = children; this._children = children;
this._hasBullet = hasBullet this._hasBullet = hasBullet || false;
} }
WebInspector.WikiParser.Block.prototype = { WebInspector.WikiParser.Block.prototype = {
...@@ -705,6 +798,14 @@ WebInspector.WikiParser.Block.prototype = { ...@@ -705,6 +798,14 @@ WebInspector.WikiParser.Block.prototype = {
return this._children; return this._children;
}, },
/**
* @return {boolean}
*/
hasChildren: function()
{
return !!this._children && !!this._children.length;
},
/** /**
* @return {boolean} * @return {boolean}
*/ */
...@@ -724,7 +825,7 @@ WebInspector.WikiParser.Block.prototype = { ...@@ -724,7 +825,7 @@ WebInspector.WikiParser.Block.prototype = {
WebInspector.WikiParser.CodeBlock = function(text) WebInspector.WikiParser.CodeBlock = function(text)
{ {
WebInspector.WikiParser.ArticleElement.call(this, WebInspector.WikiParser.ArticleElement.Type.CodeBlock); WebInspector.WikiParser.ArticleElement.call(this, WebInspector.WikiParser.ArticleElement.Type.CodeBlock);
this._code = text; this._code = text.unescapeHTML();
} }
WebInspector.WikiParser.CodeBlock.prototype = { WebInspector.WikiParser.CodeBlock.prototype = {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment