Skip to content

Commit

Permalink
Draft YAML renderer.
Browse files Browse the repository at this point in the history
  • Loading branch information
tmpfs committed Mar 21, 2016
1 parent b96e173 commit 8231986
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ var deserialize = require('mkast').deserialize
, through = require('through3')
, types = {
markdown: './lib/render/markdown',
yaml: './lib/render/yaml',
xml: 'commonmark/lib/xml',
html: 'commonmark/lib/render/html'
};
Expand Down
97 changes: 97 additions & 0 deletions lib/render/yaml.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
"use strict";

var Renderer = require('commonmark/lib/render/renderer')
, Node = require('mkast').Node
, repeat = require('string-repeater');

function YamlRendererer(options) {
options = options || {};
Renderer.call(this);
this.indent = ' ';
}

function properties(node, depth) {

var k, v;

for(k in node) {
v = node[k];
k = k.replace(/^_/, '');
if(k === 'type'
|| k === 'firstChild'
|| k === 'lastChild'
|| k === 'next'
|| k === 'prev'
|| k === 'parent'
|| v === undefined
|| v === null
|| typeof v === 'function') {
continue;
}

this.out(repeat(this.indent, depth + 2));
if(k === 'literal' && ~v.indexOf('\n')) {
this.out(k + ': |' + v);
}else{
this.out(k + ': ' + v);
}
this.cr();
}
}

function render(ast) {
var walker = ast.walker()
, event
, entering
, node
, type
, depth
, p;

this.buffer = '';
this.lastOut = '\n';

while((event = walker.next())) {
entering = event.entering;
node = event.node;
type = node.type;
depth = 0;
p = node.parent;

while(p) {
depth++;
p = p.parent;
}

if(Node.is(node, Node.DOCUMENT) && entering) {
this.out('---');
this.cr();
}

//console.error(depth);


this.out(repeat(this.indent, depth));
if(!Node.is(node, Node.DOCUMENT)) {
this.out('- ');
}
this.out(type + ':');
this.cr();
this.properties(node, depth);

if(Node.is(node, Node.DOCUMENT) && !entering) {
this.out('---');
this.cr();
}

}

return this.buffer;
}

YamlRendererer.prototype = Object.create(Renderer.prototype);

YamlRendererer.prototype.render = render;
YamlRendererer.prototype.properties = properties;

module.exports = YamlRendererer;

0 comments on commit 8231986

Please sign in to comment.