r93862 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r93861‎ | r93862 | r93863 >
Date:23:14, 3 August 2011
Author:inez
Status:deferred
Tags:
Comment:
Add JSON view to the new demo
Modified paths:
  • /trunk/parsers/wikidom/demos/es/index2.html (modified) (history)
  • /trunk/parsers/wikidom/lib/FormatJSON.js (added) (history)
  • /trunk/parsers/wikidom/lib/es/es.ParagraphBlock.js (modified) (history)

Diff [purge]

Index: trunk/parsers/wikidom/lib/es/es.ParagraphBlock.js
@@ -181,8 +181,8 @@
182182
183183 es.ParagraphBlock.prototype.getWikiDom = function() {
184184 return {
185 - lines: this.content.getWikiDomLines(),
186 - type: 'paragraph'
 185+ type: 'paragraph',
 186+ lines: this.content.getWikiDomLines()
187187 };
188188 };
189189
Index: trunk/parsers/wikidom/lib/FormatJSON.js
@@ -0,0 +1,81 @@
 2+function RealTypeOf(v) {
 3+ if (typeof (v) == "object") {
 4+ if (v === null) return "null";
 5+ if (v.constructor == (new Array).constructor) return "array";
 6+ if (v.constructor == (new Date).constructor) return "date";
 7+ if (v.constructor == (new RegExp).constructor) return "regex";
 8+ return "object";
 9+ }
 10+ return typeof (v);
 11+}
 12+function FormatJSON(oData, sIndent) {
 13+ if (arguments.length < 2) {
 14+ var sIndent = "";
 15+ }
 16+ var sIndentStyle = " ";
 17+ var sDataType = RealTypeOf(oData);
 18+
 19+ // open object
 20+ if (sDataType == "array") {
 21+ if (oData.length == 0) {
 22+ return "[]";
 23+ }
 24+ var sHTML = "[";
 25+ } else {
 26+ var iCount = 0;
 27+ $.each(oData, function () {
 28+ iCount++;
 29+ return;
 30+ });
 31+ if (iCount == 0) { // object is empty
 32+ return "{}";
 33+ }
 34+ var sHTML = "{";
 35+ }
 36+
 37+ // loop through items
 38+ var iCount = 0;
 39+ $.each(oData, function (sKey, vValue) {
 40+ if (iCount > 0) {
 41+ sHTML += ",";
 42+ }
 43+ if (sDataType == "array") {
 44+ sHTML += ("\n" + sIndent + sIndentStyle);
 45+ } else {
 46+ sHTML += ("\n" + sIndent + sIndentStyle + "\"" + sKey + "\"" + ": ");
 47+ }
 48+
 49+ // display relevant data type
 50+ switch (RealTypeOf(vValue)) {
 51+ case "array":
 52+ case "object":
 53+ sHTML += FormatJSON(vValue, (sIndent + sIndentStyle));
 54+ break;
 55+ case "boolean":
 56+ case "number":
 57+ sHTML += vValue.toString();
 58+ break;
 59+ case "null":
 60+ sHTML += "null";
 61+ break;
 62+ case "string":
 63+ sHTML += ("\"" + vValue + "\"");
 64+ break;
 65+ default:
 66+ sHTML += ("TYPEOF: " + typeof (vValue));
 67+ }
 68+
 69+ // loop
 70+ iCount++;
 71+ });
 72+
 73+ // close object
 74+ if (sDataType == "array") {
 75+ sHTML += ("\n" + sIndent + "]");
 76+ } else {
 77+ sHTML += ("\n" + sIndent + "}");
 78+ }
 79+
 80+ // return
 81+ return sHTML;
 82+}
\ No newline at end of file
Index: trunk/parsers/wikidom/demos/es/index2.html
@@ -39,7 +39,7 @@
4040 width: 95%;
4141 margin: auto;
4242 }
43 - #wikitext-source {
 43+ .source {
4444 min-height: 150px;
4545 margin: 0;
4646 padding: 1em;
@@ -69,13 +69,16 @@
7070 <td width="50%">
7171 <div id="source-wrapper">
7272 <h3>Wikitext Source</h3>
73 - <div id="wikitext-source"></div>
 73+ <div id="wikitext-source" class="source"></div>
 74+ <h3>JSON Source</h3>
 75+ <div id="json-source" class="source"></div>
7476 </div>
7577 </td>
7678 </tr>
7779 </table>
7880
7981 <script type="text/javascript" src="../../lib/jquery.js"></script>
 82+ <script type="text/javascript" src="../../lib/jquery.json.js"></script>
8083 <script type="text/javascript" src="../../lib/es/es.js"></script>
8184 <script type="text/javascript" src="../../lib/es/es.EventEmitter.js"></script>
8285 <script type="text/javascript" src="../../lib/es/es.Position.js"></script>
@@ -93,7 +96,7 @@
9497 <script type="text/javascript" src="../../lib/es/es.ListBlockItem.js"></script>
9598 <script type="text/javascript" src="../../lib/es/es.ListBlock.js"></script>
9699 <script type="text/javascript" src="../../lib/es/es.Cursor.js"></script>
97 -
 100+ <script type="text/javascript" src="../../lib/FormatJSON.js"></script>
98101 <script src="../../lib/wiki.js" type="text/javascript"></script>
99102 <script src="../../lib/wiki.util.js" type="text/javascript"></script>
100103 <script src="../../lib/wiki.AnnotationRenderer.js" type="text/javascript"></script>
@@ -317,8 +320,9 @@
318321 clearTimeout( previewTimeout );
319322 }
320323 previewTimeout = setTimeout( function () {
321 - console.log(doc.getWikiDomDocument());
322 - $( '#wikitext-source' ).text( wikitextRenderer.render( doc.getWikiDomDocument() ) );
 324+ var data = doc.getWikiDomDocument();
 325+ $( '#wikitext-source' ).text( wikitextRenderer.render( data ) );
 326+ $( '#json-source' ).text( FormatJSON( data ) );
323327 }, 100 );
324328 } );
325329

Status & tagging log