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
...@@ -11,12 +11,19 @@ function test() ...@@ -11,12 +11,19 @@ function test()
function testWikiParser(next) function testWikiParser(next)
{ {
var testCases = [ var testCases = [
"{{A|text=<code>it's not a code <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));
} }
......
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