Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

parse ordered lists

  • Loading branch information...
commit b6fa6f52b272f8f9098b06f0d223c99682eab4af 1 parent 5c085dc
@tychon authored
View
BIN  automatons/containerparser.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
84 compiler.co
@@ -54,9 +54,9 @@ CONTAINERPARSER = stackparser.createAutomaton {
initialStack: \#
initialOutput: '[{"type":"par","line":"0","column":"0","input":"'
transitions: [
- ## new line
- [\q1 \# /\s/ \q1] # trailing whitespaces
+ ## new line after normal paragraph (or 2 x \n)
[\q1 \# /\n/ \q0 null '"},{"type":"par","line":"{line}","column":"{column}","input":"']
+ [\q1 \# /\s/ \q1] # trailing whitespaces
[\q1 \# /\=/ \q3 \=#]
[\q1 \# /\-/ \q4 \-#]
[\q1 \# \# \q5 \*#]
@@ -80,20 +80,41 @@ CONTAINERPARSER = stackparser.createAutomaton {
## unordered list
# read '-'-symbols
- [\q4 /\-/ /\-/ \q4 \--]
- [\q4 /\-/ null \q4b '' '"},{"type":"unordered_list","line":"{line}","column":"{column}","depth":"{stack-length}","input":"{alpha}']
+ [\q4 /\-/ /\-/ \q4 \--]
+ [\q4 /\-/ null \q4b '' '"},{"type":"unordered_list","line":"{line}","column":"{column}","depth":"{stack-length}","input":"{alpha}']
# empty stack
[\q4b \# \epsilon \q4c null]
[\q4b null \epsilon \q4b '']
# read in item
- [\q4c \# /\n/ \q4d '{alpha}']
- [\q4c \# null \q4c null '{alpha}']
+ [\q4c \# /\n/ \q4d null '{alpha}']
+ [\q4c \# null \q4c null '{alpha}']
# test for next item
- [\q4d \# /\s/ \q4d] # trailing whitespaces
- [\q4d \# /\n/ \q0 null '"},{"type":"par","line":"{line}","column":"{column}","input":"']
- [\q4d \# null \q1 '']
+ [\q4d \# /\n/ \q1 null '"},{"type":"par","line":"{line}","column":"{column}","input":"']
+ [\q4d \# /\s/ \q4d ] # trailing whitespaces
+ [\q4d \# /\=/ \q3 \=#]
+ [\q4d \# /\-/ \q4 \-# ]
+ [\q4d \# \# \q5 \*#]
+ [\q4d \# /\\/ \q6 null '"},{"type":"command","line":"{line}","column":"{column}","input":"']
+ [\q4d \# null \q4c null '{alpha}']
## ordered list
+ # read '#'-symbols
+ [\q5 /\*/ /\*/ \q5 \**]
+ [\q5 /\*/ null \q5b '' '"},{"type":"ordered_list","line":"{line}","column":"{column}","depth":"{stack-length}","input":"{alpha}']
+ # empty stack
+ [\q5b \# \epsilon \q5c null]
+ [\q5b null \epsilon \q5b '']
+ # read in item
+ [\q5c \# /\n/ \q5d null '{alpha}']
+ [\q5c \# null \q5c null '{alpha}']
+ # test for next item
+ [\q5d \# /\n/ \q1 null '"},{"type":"par","line":"{line}","column":"{column}","input":"']
+ [\q5d \# /\s/ \q5d ] # trailing whitespaces
+ [\q5d \# /\=/ \q3 \=#]
+ [\q5d \# /\-/ \q4 \-# ]
+ [\q5d \# /#/ \q5 \*#]
+ [\q5d \# /\\/ \q6 null '"},{"type":"command","line":"{line}","column":"{column}","input":"']
+ [\q5d \# null \q5c null '{alpha}']
##command
[\q6 \# /\n/ \q1 null '{alpha}"},{"type":"par","line":"{line}","column":"{column}","input":"']
@@ -186,10 +207,10 @@ parse = (input) ->
return error: "JSON could not parse block parser output.\n\n#{stackparser.stringify config}"
for block of tree
- if block.type is not \container then continue
- result = parseContainer block
- if result.error then return error: result.error
- else block.subtree = result.subtree
+ if block.type is \container or \quote
+ result = parseContainer block
+ if result.error then return error: result.error
+ else block.subtree = result.subtree
# TODO
@@ -228,43 +249,8 @@ deleteEmptyBlocks = (tree) ->
# create html
generateBlocks = (tree) ->
- resulttext = ''
- for block, blockIndex of tree
- if block.type is \par
- resulttext += \\n + JSON.stringify tree + \\n
- #TODO
- else if block.type is \orderedlist or block.type is \unorderedlist
- resulttext += generateList block
- else if block.type is \math
- resulttext += generateMath block
- else if block.type is \quote
- resulttext += generateQuote block
-
- return html: resulttext
-
-generateList = (block) ->
- if block.type is \orderedlist
- res = "<ol>\n"
- res += generateSubList subItem for subItem of block.items
- res += "\n</ol>"
- else
- res = "<ul>\n"
- res += generateSubList subItem for subItem of block.items
- res += "\n</ul>"
+ return html: "nothing"
-generateSubList = (item) ->
- # TODO generate item text html
- if item.type
- res = "<li>#{item.text}\n#{if item.type is \orderedlist then '<ol>' else '<ul>'}\n"
- res += generateSubList subItem for subItem of item.items
- res += "#{if item.type is \orderedlist then '</ol>' else '</ul>'}\n</li>"
- return res
- else
- "<li>#{item.text}</li>"
-generateMath = (block) ->
- #TODO
-generateQuote = (block) ->
- "<blockquote>\n#{generateParagraph block.quote}\n</blockquote>"
################################################################################
# other
View
75 compiler.js
@@ -1,5 +1,5 @@
(function(){
- var request, htmlify, stackparser, BLOCK_PARSER, CONTAINERPARSER, config, couchdb, compile, parse, parseContainer, deleteEmptyBlocks, generateBlocks, generateList, generateSubList, generateMath, generateQuote, _parseOneLiners, _delChars, _peek;
+ var request, htmlify, stackparser, BLOCK_PARSER, CONTAINERPARSER, config, couchdb, compile, parse, parseContainer, deleteEmptyBlocks, generateBlocks, _parseOneLiners, _delChars, _peek;
request = require('request');
htmlify = require('./htmlify');
stackparser = require('./stackparser');
@@ -16,7 +16,7 @@
initialState: 'q1',
initialStack: '#',
initialOutput: '[{"type":"par","line":"0","column":"0","input":"',
- transitions: [['q1', '#', /\s/, 'q1'], ['q1', '#', /\n/, 'q0', null, '"},{"type":"par","line":"{line}","column":"{column}","input":"'], ['q1', '#', /\=/, 'q3', '=#'], ['q1', '#', /\-/, 'q4', '-#'], ['q1', '#', '#', 'q5', '*#'], ['q1', '#', /\\/, 'q6', null, '"},{"type":"command","line":"{line}","column":"{column}","input":"'], ['q1', '#', null, 'q0', null, '{alpha}'], ['q0', '#', /\n/, 'q1', null, '{alpha}'], ['q0', '#', null, 'q0', null, '{alpha}'], ['q3', /\=/, /\=/, 'q3', '=='], ['q3', /\=/, null, 'q3b', '', '"},{"type":"heading","line":"{line}","column":"{column}","depth":"{stack-length}","input":"{alpha}'], ['q3b', '#', 'epsilon', 'q3c', null], ['q3b', /\=/, 'epsilon', 'q3b', ''], ['q3c', '#', /\n/, 'q1', null, '{alpha}"},{"type":"par","line":"{line}","column":"{column}","input":"'], ['q3c', '#', null, 'q3c', null, '{alpha}'], ['q4', /\-/, /\-/, 'q4', '--'], ['q4', /\-/, null, 'q4b', '', '"},{"type":"unordered_list","line":"{line}","column":"{column}","depth":"{stack-length}","input":"{alpha}'], ['q4b', '#', 'epsilon', 'q4c', null], ['q4b', null, 'epsilon', 'q4b', ''], ['q4c', '#', /\n/, 'q4d', '{alpha}'], ['q4c', '#', null, 'q4c', null, '{alpha}'], ['q4d', '#', /\s/, 'q4d'], ['q4d', '#', /\n/, 'q0', null, '"},{"type":"par","line":"{line}","column":"{column}","input":"'], ['q4d', '#', null, 'q1', ''], ['q6', '#', /\n/, 'q1', null, '{alpha}"},{"type":"par","line":"{line}","column":"{column}","input":"'], ['q6', '#', null, 'q6', null, '{alpha}']],
+ transitions: [['q1', '#', /\n/, 'q0', null, '"},{"type":"par","line":"{line}","column":"{column}","input":"'], ['q1', '#', /\s/, 'q1'], ['q1', '#', /\=/, 'q3', '=#'], ['q1', '#', /\-/, 'q4', '-#'], ['q1', '#', '#', 'q5', '*#'], ['q1', '#', /\\/, 'q6', null, '"},{"type":"command","line":"{line}","column":"{column}","input":"'], ['q1', '#', null, 'q0', null, '{alpha}'], ['q0', '#', /\n/, 'q1', null, '{alpha}'], ['q0', '#', null, 'q0', null, '{alpha}'], ['q3', /\=/, /\=/, 'q3', '=='], ['q3', /\=/, null, 'q3b', '', '"},{"type":"heading","line":"{line}","column":"{column}","depth":"{stack-length}","input":"{alpha}'], ['q3b', '#', 'epsilon', 'q3c', null], ['q3b', /\=/, 'epsilon', 'q3b', ''], ['q3c', '#', /\n/, 'q1', null, '{alpha}"},{"type":"par","line":"{line}","column":"{column}","input":"'], ['q3c', '#', null, 'q3c', null, '{alpha}'], ['q4', /\-/, /\-/, 'q4', '--'], ['q4', /\-/, null, 'q4b', '', '"},{"type":"unordered_list","line":"{line}","column":"{column}","depth":"{stack-length}","input":"{alpha}'], ['q4b', '#', 'epsilon', 'q4c', null], ['q4b', null, 'epsilon', 'q4b', ''], ['q4c', '#', /\n/, 'q4d', null, '{alpha}'], ['q4c', '#', null, 'q4c', null, '{alpha}'], ['q4d', '#', /\n/, 'q1', null, '"},{"type":"par","line":"{line}","column":"{column}","input":"'], ['q4d', '#', /\s/, 'q4d'], ['q4d', '#', /\=/, 'q3', '=#'], ['q4d', '#', /\-/, 'q4', '-#'], ['q4d', '#', '#', 'q5', '*#'], ['q4d', '#', /\\/, 'q6', null, '"},{"type":"command","line":"{line}","column":"{column}","input":"'], ['q4d', '#', null, 'q4c', null, '{alpha}'], ['q5', /\*/, /\*/, 'q5', '**'], ['q5', /\*/, null, 'q5b', '', '"},{"type":"ordered_list","line":"{line}","column":"{column}","depth":"{stack-length}","input":"{alpha}'], ['q5b', '#', 'epsilon', 'q5c', null], ['q5b', null, 'epsilon', 'q5b', ''], ['q5c', '#', /\n/, 'q5d', null, '{alpha}'], ['q5c', '#', null, 'q5c', null, '{alpha}'], ['q5d', '#', /\n/, 'q1', null, '"},{"type":"par","line":"{line}","column":"{column}","input":"'], ['q5d', '#', /\s/, 'q5d'], ['q5d', '#', /\=/, 'q3', '=#'], ['q5d', '#', /\-/, 'q4', '-#'], ['q5d', '#', /#/, 'q5', '*#'], ['q5d', '#', /\\/, 'q6', null, '"},{"type":"command","line":"{line}","column":"{column}","input":"'], ['q5d', '#', null, 'q5c', null, '{alpha}'], ['q6', '#', /\n/, 'q1', null, '{alpha}"},{"type":"par","line":"{line}","column":"{column}","input":"'], ['q6', '#', null, 'q6', null, '{alpha}']],
replaceAlphas: [['\\', '\\\\'], ['"', '\\"'], ['\n', '\\n'], ['\t', '\\t'], ['\r', '']],
finalStates: [],
finalOutput: '"}]'
@@ -106,16 +106,15 @@
}
for (_i = 0, _len = tree.length; _i < _len; ++_i) {
block = tree[_i];
- if (block.type !== 'container') {
- continue;
- }
- result = parseContainer(block);
- if (result.error) {
- return {
- error: result.error
- };
- } else {
- block.subtree = result.subtree;
+ if (block.type === 'container' || 'quote') {
+ result = parseContainer(block);
+ if (result.error) {
+ return {
+ error: result.error
+ };
+ } else {
+ block.subtree = result.subtree;
+ }
}
}
return {
@@ -164,60 +163,10 @@
}
};
generateBlocks = function(tree){
- var resulttext, blockIndex, block, _len;
- resulttext = '';
- for (blockIndex = 0, _len = tree.length; blockIndex < _len; ++blockIndex) {
- block = tree[blockIndex];
- if (block.type === 'par') {
- resulttext += '\n' + JSON.stringify(tree + '\n');
- } else if (block.type === 'orderedlist' || block.type === 'unorderedlist') {
- resulttext += generateList(block);
- } else if (block.type === 'math') {
- resulttext += generateMath(block);
- } else if (block.type === 'quote') {
- resulttext += generateQuote(block);
- }
- }
return {
- html: resulttext
+ html: "nothing"
};
};
- generateList = function(block){
- var res, subItem, _i, _ref, _len;
- if (block.type === 'orderedlist') {
- res = "<ol>\n";
- for (_i = 0, _len = (_ref = block.items).length; _i < _len; ++_i) {
- subItem = _ref[_i];
- res += generateSubList(subItem);
- }
- return res += "\n</ol>";
- } else {
- res = "<ul>\n";
- for (_i = 0, _len = (_ref = block.items).length; _i < _len; ++_i) {
- subItem = _ref[_i];
- res += generateSubList(subItem);
- }
- return res += "\n</ul>";
- }
- };
- generateSubList = function(item){
- var res, subItem, _i, _ref, _len;
- if (item.type) {
- res = "<li>" + item.text + "\n" + (item.type === 'orderedlist' ? '<ol>' : '<ul>') + "\n";
- for (_i = 0, _len = (_ref = item.items).length; _i < _len; ++_i) {
- subItem = _ref[_i];
- res += generateSubList(subItem);
- }
- res += (item.type === 'orderedlist' ? '</ol>' : '</ul>') + "\n</li>";
- return res;
- } else {
- return "<li>" + item.text + "</li>";
- }
- };
- generateMath = function(block){};
- generateQuote = function(block){
- return "<blockquote>\n" + generateParagraph(block.quote) + "\n</blockquote>";
- };
_delChars = function(text, startIndex, length){
var firstPart, lastPart;
firstPart = text.substr(0, startIndex);
View
24 htmlify.co
@@ -1,14 +1,30 @@
htmlify = exports.htmlify = (object) ->
htmlifyObject object
-htmlifyObject = (object, attrName) ->
- ret = "<div style='margin-left:5px; margin-top:1ex; padding-bottom:2px; border-left: 3px solid red'>"
- if attrName then ret += "<span style='color:blue'>#{attrName}</span><br>"
+searchForMaxDepth = (object) ->
+ max = 1
+ for key in object
+ if typeof object[key] is \object
+ res = searchForMaxDepth object[key]
+ if res+1 > max then max = res+1
+ return max
+
+htmlifyObject = (object, attrName, depth, maxDepth) ->
+ if not depth then depth = 1
+ if not maxDepth then maxDepth = searchForMaxDepth(object)
+
+ # open div
+ ret = "<div style='background-color: rgba(0, 0, 0, #{depth/maxDepth/5}); margin-left:5px; margin-top:1ex; padding-bottom:2px; border-left: 3px solid red; border-top: 1px solid red; border-bottom:1px solid red; border-right: 1px solid red'>"
+ # attr name
+ if attrName then ret += "<span style='font-weight:bold'><span>key</span> : <span style='color:blue'>#{attrName}</span></span><br>"
+
for attr in object
if typeof object[attr] is \object
- ret += htmlifyObject object[attr], attr
+ ret += htmlifyObject object[attr], attr, depth+1, maxDepth
else
ret += htmlifyAttribute object, attr
+
+ # close div
ret += "</div>"
htmlifyAttribute = (object, attrName) ->
View
29 htmlify.js
@@ -1,17 +1,36 @@
(function(){
- var htmlify, htmlifyObject, htmlifyAttribute;
+ var htmlify, searchForMaxDepth, htmlifyObject, htmlifyAttribute;
htmlify = exports.htmlify = function(object){
return htmlifyObject(object);
};
- htmlifyObject = function(object, attrName){
+ searchForMaxDepth = function(object){
+ var max, key, res;
+ max = 1;
+ for (key in object) {
+ if (typeof object[key] === 'object') {
+ res = searchForMaxDepth(object[key]);
+ if (res + 1 > max) {
+ max = res + 1;
+ }
+ }
+ }
+ return max;
+ };
+ htmlifyObject = function(object, attrName, depth, maxDepth){
var ret, attr;
- ret = "<div style='margin-left:5px; margin-top:1ex; padding-bottom:2px; border-left: 3px solid red'>";
+ if (!depth) {
+ depth = 1;
+ }
+ if (!maxDepth) {
+ maxDepth = searchForMaxDepth(object);
+ }
+ ret = "<div style='background-color: rgba(0, 0, 0, " + depth / maxDepth / 5 + "); margin-left:5px; margin-top:1ex; padding-bottom:2px; border-left: 3px solid red; border-top: 1px solid red; border-bottom:1px solid red; border-right: 1px solid red'>";
if (attrName) {
- ret += "<span style='color:blue'>" + attrName + "</span><br>";
+ ret += "<span style='font-weight:bold'><span>key</span> : <span style='color:blue'>" + attrName + "</span></span><br>";
}
for (attr in object) {
if (typeof object[attr] === 'object') {
- ret += htmlifyObject(object[attr], attr);
+ ret += htmlifyObject(object[attr], attr, depth + 1, maxDepth);
} else {
ret += htmlifyAttribute(object, attr);
}
View
9 stackparser.co
@@ -38,6 +38,7 @@
# You can require these variables in the output: stack-length, gamma, alpha, column, line
# [\q0 /Z/ /a/ \q1 \AZ 'in line {line} column {column}']
### Replace Alpha
+# Replace {alpha} (after it was put on the stack and before it was appended to the output)
# This is a list of 2-tuples. The first Element in the tuple gives the
# match and the second the replacement.
@@ -102,15 +103,15 @@ class StackParser
doTransition = (transition, gamma, alpha) ~>
state := transition.dest
+ push = transition.push.replace /\{alpha\}/g, alpha
+ push = push.replace /\{gamma\}/g, gamma
+ stack := push + stack
+
# replace alpha
if alpha
for tuple of @description.replaceAlphas
alpha = alpha.replace tuple[0], tuple[1]
- push = transition.push.replace /\{alpha\}/g, alpha
- push = push.replace /\{gamma\}/g, gamma
- stack := push + stack
-
out = transition.output
out = out.replace /\{alpha\}/g, alpha or '' # alpha is undefined on epsilon-transitions
out = out.replace /\{gamma\}/g, gamma
View
8 stackparser.js
@@ -64,17 +64,17 @@
return conf;
};
doTransition = function(transition, gamma, alpha){
- var tuple, push, out, _i, _ref, _len;
+ var push, tuple, out, _i, _ref, _len;
state = transition.dest;
+ push = transition.push.replace(/\{alpha\}/g, alpha);
+ push = push.replace(/\{gamma\}/g, gamma);
+ stack = push + stack;
if (alpha) {
for (_i = 0, _len = (_ref = _this.description.replaceAlphas).length; _i < _len; ++_i) {
tuple = _ref[_i];
alpha = alpha.replace(tuple[0], tuple[1]);
}
}
- push = transition.push.replace(/\{alpha\}/g, alpha);
- push = push.replace(/\{gamma\}/g, gamma);
- stack = push + stack;
out = transition.output;
out = out.replace(/\{alpha\}/g, alpha) || '';
out = out.replace(/\{gamma\}/g, gamma);
Please sign in to comment.
Something went wrong with that request. Please try again.