support expressions and use them for links

This commit is contained in:
kp2pml30 2025-03-21 13:40:31 +04:00
parent b90362a6e8
commit 59b1d6c116
3 changed files with 75 additions and 26 deletions

View file

@ -106,9 +106,13 @@ class HTMLRenderer < YAMD::Renderer
@buf << "</#{name}>" @buf << "</#{name}>"
end end
def code(**attrs, &blk) def link(to, &blk)
tag('pre') { tag('a', href: to, &blk)
tag('code', **attrs) { end
def code(&blk)
tag('pre', class: "language-any") {
tag('code', class: "language-any") {
@buf << YAMD::Code::highlight(blk.()) @buf << YAMD::Code::highlight(blk.())
} }
} }
@ -151,7 +155,7 @@ File.open(options[:in]) { |f|
renderer = HTMLRenderer.new renderer = HTMLRenderer.new
eval(code).(renderer) renderer.instance_eval(code)
Pathname.new(options[:out]).write(renderer.finalize) Pathname.new(options[:out]).write(renderer.finalize)
} }

View file

@ -30,19 +30,20 @@ module YAMD
class Output class Output
def initialize() def initialize()
@buf = String.new @buf = String.new
@buf << "Proc.new { |__renderer|\n"
@ind = 1 #@buf << "Proc.new {\n"
@ind = 0
end end
def add_static(s) def add_static(s)
return if s.empty? return if s.empty?
@buf << ("\t" * @ind) << "__renderer.str " << s.dump << "\n" @buf << ("\t" * @ind) << "str " << s.dump << "\n"
end end
def add_expr(e) def add_expr(e)
@buf << ("\t" * @ind) << "__renderer.str((#{e}).to_s)" @buf << ("\t" * @ind) << "str((#{e}).to_s)"
end end
def add_code_nl(c) def add_code_nl(c)
@ -60,7 +61,8 @@ module YAMD
end end
def finalize() def finalize()
@buf << "}" #@buf << "}"
@buf.freeze @buf.freeze
@buf @buf
@ -218,7 +220,7 @@ module YAMD
prevParagraph = true prevParagraph = true
flushStrBuf.() flushStrBuf.()
state.output.add_code_nl("__renderer.new_line") state.output.add_code_nl("new_line")
end end
state.nextLine state.nextLine
next next
@ -246,7 +248,7 @@ module YAMD
flushStrBuf.() flushStrBuf.()
state.consume(2) state.consume(2)
state.output.add_code_nl("__renderer." + state.line.strip + " {") state.output.add_code_nl("" + state.line.strip + " {")
state.nextLine state.nextLine
parseContent(state, ctx.indented) parseContent(state, ctx.indented)
state.output.add_code_nl("}") state.output.add_code_nl("}")
@ -258,7 +260,7 @@ module YAMD
line = state.line.strip line = state.line.strip
state.nextLine 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("next " + parseRaw(state, ctx.indented).dump)
state.output.add_code_nl("}") state.output.add_code_nl("}")
@ -276,7 +278,7 @@ module YAMD
flushStrBuf.() flushStrBuf.()
state.consume(2) state.consume(2)
state.output.add_code_nl("__renderer.list_item {") state.output.add_code_nl("list_item {")
parseContent state, ctx.indented parseContent state, ctx.indented
state.output.add_code_nl("}") state.output.add_code_nl("}")
@ -292,6 +294,10 @@ module YAMD
elsif state.line.start_with?('##') elsif state.line.start_with?('##')
strBuf << '#' strBuf << '#'
state.consume 2 state.consume 2
elsif state.line.start_with?('}#')
flushStrBuf.()
return
elsif state.line[0] == '#' elsif state.line[0] == '#'
state.output.add_static(strBuf) state.output.add_static(strBuf)
strBuf = String.new strBuf = String.new
@ -318,7 +324,7 @@ module YAMD
raise "unterminated \#` at #{state.errCtx}" raise "unterminated \#` at #{state.errCtx}"
end end
state.consume 1 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?('#{') elsif state.line.start_with?('#{')
state.consume 2 state.consume 2
expr = read_balanced(state, ctx) expr = read_balanced(state, ctx)
@ -336,7 +342,42 @@ module YAMD
end end
state.consume 1 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 else
raise "unexpected hash at #{state.errCtx}" raise "unexpected hash at #{state.errCtx}"
end end

View file

@ -17,18 +17,22 @@
require 'erb' require 'erb'
module YAMD::Code module YAMD::Code
HIGHLIGHTS = { module Lang
/\b(?:[A-Z][a-zA-Z\-_0-9]*)\b*/ => 'type', DEFAULT = {
/#(\s|$)[^\n]*/ => 'comment', /\b(?:[A-Z][a-zA-Z\-_0-9]*)\b*/ => 'type',
/\b(?:open|final|class|fn|let|var|if|else|while|loop|return|namespace|new)\b/ => 'kw', /#(\s|$)[^\n]*/ => 'comment',
/\b(?:null|undefined|true|false|this|self)\b/i => 'const', /\b(?:open|final|class|fn|let|var|if|else|while|loop|return|namespace|new)\b/ => 'kw',
/[:,;()\[\]{}<>]/ => 'punct', /\b(?:def|import|from)\b/ => 'kw',
/0|(0x[0-9a-fA-F]+)|([1-9][0-9]*(\.[0-9]+)?)/ => 'number', /\b(?:null|undefined|true|false|this|self)\b/i => 'const',
/"(?:\\.|[^\\"])*"/ => 'str', /\b(?:__[a-zA-Z0-9_]+__)\b/i => 'const',
/'(?:\\.|[^\\'])*'/ => 'str', /[:,;()\[\]{}<>]/ => 'punct',
} /0|(0x[0-9a-fA-F]+)|([1-9][0-9]*(\.[0-9]+)?)/ => 'number',
/"(?:\\.|[^\\"])*"/ => 'str',
/'(?:\\.|[^\\'])*'/ => 'str',
}
end
def self.highlight(txt, highlights=HIGHLIGHTS) def self.highlight(txt, highlights=Lang::DEFAULT)
idx = 0 idx = 0
res = String.new res = String.new
old_idx = 0 old_idx = 0