diff options
Diffstat (limited to 'js')
-rw-r--r-- | js/index.html | 13 | ||||
-rw-r--r-- | js/lib/blocks.js | 37 | ||||
-rwxr-xr-x | js/lib/index.js | 7 |
3 files changed, 46 insertions, 11 deletions
diff --git a/js/index.html b/js/index.html index 05fc216..fdd6a76 100644 --- a/js/index.html +++ b/js/index.html @@ -28,6 +28,7 @@ $(document).ready(function() { // $("#html").text(result); $("#preview").html(result); $("#html").text(result); + $("#ast").text(stmd.ASTRenderer(parsed)); $("#rendertime").text(renderTime); }; var parseAndRender = function () { @@ -78,15 +79,16 @@ $(document).ready(function() { <div class="col-md-6"> <h1>stmd.js dingus</h1> </div> - <div class="col-md-3"> + <div class="col-md-4"> <ul class="nav nav-tabs" role="tablist"> <li class="active"><a href="#preview" role="tab" data-toggle="tab">Preview</a></li> <li><a href="#result" role="tab" data-toggle="tab">HTML</a></li> + <li><a href="#result-ast" role="tab" data-toggle="tab">AST</a></li> </ul> </div> - <div class="col-md-3"> - <p class="timing">Parsed in <span class="timing" id="parsetime"></span> milliseconds.<br/> - Rendered in <span class="timing" id="rendertime"></span> milliseconds.</p> + <div class="col-md-2"> + <p class="timing">Parsed in <span class="timing" id="parsetime"></span> ms.<br> + Rendered in <span class="timing" id="rendertime"></span> ms.</p> </div> </div> <div class="row"> @@ -101,6 +103,9 @@ $(document).ready(function() { <div id="result" class="tab-pane"> <pre id="htmlpre"><code id="html"></code></pre> </div> + <div id="result-ast" class="tab-pane"> + <pre id="astpre"><code id="ast"></code></pre> + </div> </div> </div> </div> diff --git a/js/lib/blocks.js b/js/lib/blocks.js index 6cedb37..109661f 100644 --- a/js/lib/blocks.js +++ b/js/lib/blocks.js @@ -542,6 +542,7 @@ var finalize = function(block, line_number) { switch (block.t) { case 'Paragraph': block.string_content = block.strings.join('\n').replace(/^ */m,''); + // delete block.strings; // try parsing the beginning as link reference definitions: while (block.string_content.charCodeAt(0) === C_OPEN_BRACKET && @@ -613,26 +614,49 @@ var finalize = function(block, line_number) { }; // Walk through a block & children recursively, parsing string content -// into inline content where appropriate. +// into inline content where appropriate. Returns new object. var processInlines = function(block) { + var newblock = {}; + newblock.t = block.t; + newblock.start_line = block.start_line; + newblock.start_column = block.start_column; + newblock.end_line = block.end_line; + switch(block.t) { case 'Paragraph': + newblock.inline_content = + this.inlineParser.parse(block.string_content.trim(), this.refmap); + break; case 'SetextHeader': case 'ATXHeader': - block.inline_content = + newblock.inline_content = this.inlineParser.parse(block.string_content.trim(), this.refmap); - block.string_content = ""; + newblock.level = block.level; + break; + case 'List': + newblock.list_data = block.list_data; + newblock.tight = block.tight; + break; + case 'FencedCode': + newblock.string_content = block.string_content; + newblock.info = block.info; + break; + case 'IndentedCode': + case 'HtmlBlock': + newblock.string_content = block.string_content; break; default: break; } if (block.children) { + var newchildren = []; for (var i = 0; i < block.children.length; i++) { - this.processInlines(block.children[i]); + newchildren.push(this.processInlines(block.children[i])); } + newblock.children = newchildren; } - + return newblock; }; // The main parsing function. Returns a parsed document AST. @@ -648,8 +672,7 @@ var parse = function(input) { while (this.tip) { this.finalize(this.tip, len - 1); } - this.processInlines(this.doc); - return this.doc; + return this.processInlines(this.doc); }; diff --git a/js/lib/index.js b/js/lib/index.js index a8bf009..cfb8bf9 100755 --- a/js/lib/index.js +++ b/js/lib/index.js @@ -9,5 +9,12 @@ // var renderer = new stmd.HtmlRenderer(); // console.log(renderer.render(parser.parse('Hello *world*'))); +var util = require('util'); + +var renderAST = function(tree) { + return util.inspect(tree, {depth: null}); +} + module.exports.DocParser = require('./blocks'); module.exports.HtmlRenderer = require('./html-renderer'); +module.exports.ASTRenderer = renderAST; |