support expressions and use them for links
This commit is contained in:
parent
b90362a6e8
commit
59b1d6c116
3 changed files with 75 additions and 26 deletions
63
lib/yamd.rb
63
lib/yamd.rb
|
|
@ -30,19 +30,20 @@ module YAMD
|
|||
class Output
|
||||
def initialize()
|
||||
@buf = String.new
|
||||
@buf << "Proc.new { |__renderer|\n"
|
||||
|
||||
@ind = 1
|
||||
#@buf << "Proc.new {\n"
|
||||
|
||||
@ind = 0
|
||||
end
|
||||
|
||||
def add_static(s)
|
||||
return if s.empty?
|
||||
|
||||
@buf << ("\t" * @ind) << "__renderer.str " << s.dump << "\n"
|
||||
@buf << ("\t" * @ind) << "str " << s.dump << "\n"
|
||||
end
|
||||
|
||||
def add_expr(e)
|
||||
@buf << ("\t" * @ind) << "__renderer.str((#{e}).to_s)"
|
||||
@buf << ("\t" * @ind) << "str((#{e}).to_s)"
|
||||
end
|
||||
|
||||
def add_code_nl(c)
|
||||
|
|
@ -60,7 +61,8 @@ module YAMD
|
|||
end
|
||||
|
||||
def finalize()
|
||||
@buf << "}"
|
||||
#@buf << "}"
|
||||
|
||||
@buf.freeze
|
||||
|
||||
@buf
|
||||
|
|
@ -218,7 +220,7 @@ module YAMD
|
|||
prevParagraph = true
|
||||
flushStrBuf.()
|
||||
|
||||
state.output.add_code_nl("__renderer.new_line")
|
||||
state.output.add_code_nl("new_line")
|
||||
end
|
||||
state.nextLine
|
||||
next
|
||||
|
|
@ -246,7 +248,7 @@ module YAMD
|
|||
flushStrBuf.()
|
||||
state.consume(2)
|
||||
|
||||
state.output.add_code_nl("__renderer." + state.line.strip + " {")
|
||||
state.output.add_code_nl("" + state.line.strip + " {")
|
||||
state.nextLine
|
||||
parseContent(state, ctx.indented)
|
||||
state.output.add_code_nl("}")
|
||||
|
|
@ -258,7 +260,7 @@ module YAMD
|
|||
|
||||
line = state.line.strip
|
||||
state.nextLine
|
||||
state.output.add_code_nl("__renderer." + line.strip + " {")
|
||||
state.output.add_code_nl("" + line.strip + " {")
|
||||
state.output.add_code_nl("next " + parseRaw(state, ctx.indented).dump)
|
||||
state.output.add_code_nl("}")
|
||||
|
||||
|
|
@ -276,7 +278,7 @@ module YAMD
|
|||
flushStrBuf.()
|
||||
state.consume(2)
|
||||
|
||||
state.output.add_code_nl("__renderer.list_item {")
|
||||
state.output.add_code_nl("list_item {")
|
||||
parseContent state, ctx.indented
|
||||
state.output.add_code_nl("}")
|
||||
|
||||
|
|
@ -292,6 +294,10 @@ module YAMD
|
|||
elsif state.line.start_with?('##')
|
||||
strBuf << '#'
|
||||
state.consume 2
|
||||
elsif state.line.start_with?('}#')
|
||||
flushStrBuf.()
|
||||
|
||||
return
|
||||
elsif state.line[0] == '#'
|
||||
state.output.add_static(strBuf)
|
||||
strBuf = String.new
|
||||
|
|
@ -318,7 +324,7 @@ module YAMD
|
|||
raise "unterminated \#` at #{state.errCtx}"
|
||||
end
|
||||
state.consume 1
|
||||
state.output.add_code_nl("__renderer.inline_code(#{code.dump})")
|
||||
state.output.add_code_nl("inline_code(#{code.dump})")
|
||||
elsif state.line.start_with?('#{')
|
||||
state.consume 2
|
||||
expr = read_balanced(state, ctx)
|
||||
|
|
@ -336,7 +342,42 @@ module YAMD
|
|||
end
|
||||
state.consume 1
|
||||
|
||||
state.output.add_code_nl "__renderer.inline_math #{expr.dump}"
|
||||
state.output.add_code_nl "inline_math #{expr.dump}"
|
||||
elsif state.line.start_with?(/^\#[a-zA-Z_]/)
|
||||
exprBuf = String.new
|
||||
state.consume 1
|
||||
|
||||
while not state.line.empty?
|
||||
if state.line[0] == '('
|
||||
exprBuf << '('
|
||||
state.consume 1
|
||||
exprBuf << read_balanced(state, ctx)
|
||||
raise "unbalanced () at #{state.errCtx}" if state.line[0] != ')'
|
||||
state.consume 1
|
||||
exprBuf << ')'
|
||||
elsif state.line.start_with?('#{')
|
||||
state.consume 2
|
||||
exprBuf << " {"
|
||||
state.output.add_code_nl(exprBuf)
|
||||
|
||||
while state.line[0] == ' '
|
||||
state.consume 1
|
||||
end
|
||||
|
||||
self.parseContent(state, ctx.indented)
|
||||
|
||||
raise "unmatched \#{ }\# at #{state.errCtx}" if not state.line.start_with?('}#')
|
||||
state.consume 2
|
||||
state.output.add_code_nl("}")
|
||||
|
||||
return
|
||||
else
|
||||
exprBuf << state.line[0]
|
||||
state.consume 1
|
||||
end
|
||||
end
|
||||
state.output.add_code_nl exprBuf
|
||||
# expression
|
||||
else
|
||||
raise "unexpected hash at #{state.errCtx}"
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue