Working toward a dynamically defined language for the parse structure.

This commit is contained in:
Elf M. Sternberg 2013-04-01 10:55:25 -07:00
parent 70149bcf1e
commit 6e00e56e84
4 changed files with 25 additions and 19 deletions

View File

@ -84,6 +84,9 @@ AuthorsURL
# > Handle these first # > 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} {URL}
{SeriesTitle} {SeriesTitle}
{AuthorName} {AuthorName}
@ -99,6 +102,10 @@ AuthorsURL
The important trick here is that the TableOfContents will be recursed The important trick here is that the TableOfContents will be recursed
wherever the {Contents} block is seen, up to a maximum depth of four. 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} {block:IfStory}
{Title} {Title}

View File

@ -1,9 +1,3 @@
{
}
document document
= p:part* { return p } = p:part* { return p }
@ -12,11 +6,11 @@ part
text text
= b:(!tag c:. {return c})+ = b:(!tag c:. {return c})+
{ return { type: "text", content: b.join('') }; } { return { type: "text", text: b.join('') }; }
variable "variable" variable "variable"
= t:tag_start rd = t:tag_start rd
{ return { type: "variable", content: t }; } { return { type: "variable", name: t }; }
tag_start "tag_start" tag_start "tag_start"
= ld n:tagname = ld n:tagname
@ -29,7 +23,7 @@ tagname "tagname"
block "block" block "block"
= t:block_tag_start p:part* n:block_end_tag = t:block_tag_start p:part* n:block_end_tag
&{ return t == n } &{ return t == n }
{ return { type: "block", name: n, content: p }; } { return { type: "block", name: n, data: p }; }
block_tag_start "tag_start" block_tag_start "tag_start"
= ld "block:" n:tagname rd = ld "block:" n:tagname rd

View File

@ -14,21 +14,26 @@ module.exports = (template) ->
subtypes = (name) -> subtypes = (name) ->
return 'cond' return 'cond'
handler = (obj) -> handler = (content, ast) ->
isLegal = (name) -> true isLegal = (name) -> true
{ {
'text': () -> 'text': () ->
obj.content ast.text
'variable': () -> 'variable': () ->
return '' if not (isLegal(obj.content) and content.hasOwnProperty(obj.content)) return '' if not (isLegal(ast.name) and content.hasOwnProperty(ast.name))
content[obj.content] content[ast.name]
'block': () -> 'block': () ->
return '' if not (isLegal(obj.content) and content.hasOwnProperty(obj.content)) return '' if not (isLegal(ast.name) and content.hasOwnProperty(ast.name))
{ f = {
'cond': () -> if obj.content then handler(obj.content) else '' 'cond': () -> handler(content[ast.name], ast.data)
'loop': () -> (handler(o) for o in obj.content) 'loop': () -> (handler(c, ast.data) for c in content[ast.name])
}[subtypes(obj.name)]() }[subtypes(ast.name)]
console.log("F:", f)
f()
}[obj.type]() }[obj.type]()
(handler(i) for i in ast).join("") (handler(i) for i in ast).join("")

View File

@ -31,7 +31,7 @@ test_data = [
} }
{ {
'input': '<ul>{block:Stories}{Title}{/block:Stories}' 'input': '<ul>{block:Stories}{Title}{/block:Stories}</ul>'
'output': '<ul>AAABBB</ul>' 'output': '<ul>AAABBB</ul>'
'data': {'stories': {'title': 'AAA'}}, 'data': {'stories': {'title': 'AAA'}},
'description': "a conditional block" 'description': "a conditional block"