From 6e00e56e84e6f66df579de76120b6cead4cb72a4 Mon Sep 17 00:00:00 2001 From: "Elf M. Sternberg" Date: Mon, 1 Apr 2013 10:55:25 -0700 Subject: [PATCH] Working toward a dynamically defined language for the parse structure. --- README.md | 7 +++++++ src/parser.peg | 12 +++--------- src/tumble.coffee | 23 ++++++++++++++--------- test/01_basics_mocha.coffee | 2 +- 4 files changed, 25 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 10cb600..3b7cad7 100644 --- a/README.md +++ b/README.md @@ -84,6 +84,9 @@ AuthorsURL # > Handle these first +Minus the actual content of a template, the HTML that we use to build +every page, a document tends to look like this: + {URL} {SeriesTitle} {AuthorName} @@ -99,6 +102,10 @@ AuthorsURL The important trick here is that the TableOfContents will be recursed wherever the {Contents} block is seen, up to a maximum depth of four. +IfContents will be true if this is a subseries and there is content of +a subseries; the reason for this is to prevent the rendering of an +empty subseries. + {block:IfStory} {Title} diff --git a/src/parser.peg b/src/parser.peg index 6fae776..6287456 100644 --- a/src/parser.peg +++ b/src/parser.peg @@ -1,9 +1,3 @@ -{ - - - -} - document = p:part* { return p } @@ -12,11 +6,11 @@ part text = b:(!tag c:. {return c})+ - { return { type: "text", content: b.join('') }; } + { return { type: "text", text: b.join('') }; } variable "variable" = t:tag_start rd - { return { type: "variable", content: t }; } + { return { type: "variable", name: t }; } tag_start "tag_start" = ld n:tagname @@ -29,7 +23,7 @@ tagname "tagname" block "block" = t:block_tag_start p:part* n:block_end_tag &{ return t == n } - { return { type: "block", name: n, content: p }; } + { return { type: "block", name: n, data: p }; } block_tag_start "tag_start" = ld "block:" n:tagname rd diff --git a/src/tumble.coffee b/src/tumble.coffee index 48d47e5..62894f4 100644 --- a/src/tumble.coffee +++ b/src/tumble.coffee @@ -14,21 +14,26 @@ module.exports = (template) -> subtypes = (name) -> return 'cond' - handler = (obj) -> + handler = (content, ast) -> + isLegal = (name) -> true + { 'text': () -> - obj.content + ast.text + 'variable': () -> - return '' if not (isLegal(obj.content) and content.hasOwnProperty(obj.content)) - content[obj.content] + return '' if not (isLegal(ast.name) and content.hasOwnProperty(ast.name)) + content[ast.name] 'block': () -> - return '' if not (isLegal(obj.content) and content.hasOwnProperty(obj.content)) - { - 'cond': () -> if obj.content then handler(obj.content) else '' - 'loop': () -> (handler(o) for o in obj.content) - }[subtypes(obj.name)]() + return '' if not (isLegal(ast.name) and content.hasOwnProperty(ast.name)) + f = { + 'cond': () -> handler(content[ast.name], ast.data) + 'loop': () -> (handler(c, ast.data) for c in content[ast.name]) + }[subtypes(ast.name)] + console.log("F:", f) + f() }[obj.type]() (handler(i) for i in ast).join("") diff --git a/test/01_basics_mocha.coffee b/test/01_basics_mocha.coffee index 584b469..5acac02 100644 --- a/test/01_basics_mocha.coffee +++ b/test/01_basics_mocha.coffee @@ -31,7 +31,7 @@ test_data = [ } { - 'input': '