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
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}

View File

@ -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

View File

@ -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("")

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>'
'data': {'stories': {'title': 'AAA'}},
'description': "a conditional block"