initial commit
This commit is contained in:
commit
46af3c6fa9
18 changed files with 1363 additions and 0 deletions
137
exe/yamd
Executable file
137
exe/yamd
Executable file
|
|
@ -0,0 +1,137 @@
|
|||
#!/usr/bin/env ruby
|
||||
# frozen_string_literal: true
|
||||
|
||||
# YAMD
|
||||
# Copyright (C) 2025 kp2pml30
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
require 'bundler/setup'
|
||||
|
||||
require 'pathname'
|
||||
|
||||
require 'yamd'
|
||||
require 'yamd/code'
|
||||
require 'erb'
|
||||
|
||||
require 'asciimath'
|
||||
|
||||
class HTMLRenderer < YAMD::Renderer
|
||||
def str(s)
|
||||
@buf << ERB::Util.html_escape(s)
|
||||
end
|
||||
|
||||
def new_line
|
||||
@buf << "</br>"
|
||||
end
|
||||
|
||||
def _list_marker(chr, &blk)
|
||||
t, attrs = case chr
|
||||
when "1", "a", "A", "i", "I"
|
||||
["ol", {"type" => chr}]
|
||||
when "-"
|
||||
["ul", {}]
|
||||
else
|
||||
raise "unknown tag #{chr}"
|
||||
end
|
||||
tag(t, **attrs, &blk)
|
||||
end
|
||||
|
||||
def list(numbering, &blk)
|
||||
old_create_sublist = @create_sublist
|
||||
old_list_stack = @list_stack.clone
|
||||
begin
|
||||
@create_sublist = false
|
||||
@list_stack = numbering.split('.').reverse
|
||||
_list_marker(@list_stack.pop, &blk)
|
||||
ensure
|
||||
@create_sublist = old_create_sublist
|
||||
@list_stack = old_list_stack
|
||||
end
|
||||
end
|
||||
|
||||
def list_item(&blk)
|
||||
if @create_sublist
|
||||
begin
|
||||
@create_sublist = false
|
||||
old_list_stack = @list_stack.clone
|
||||
|
||||
_list_marker(@list_stack.pop) {
|
||||
list_item(&blk)
|
||||
}
|
||||
ensure
|
||||
@create_sublist = true
|
||||
@list_stack = old_list_stack
|
||||
end
|
||||
|
||||
return
|
||||
end
|
||||
|
||||
tag("li") {
|
||||
oldVal = @create_sublist
|
||||
begin
|
||||
@create_sublist = true
|
||||
|
||||
blk.()
|
||||
ensure
|
||||
@create_sublist = oldVal
|
||||
end
|
||||
}
|
||||
end
|
||||
|
||||
def tag(name, **attrs)
|
||||
@buf << "<#{name}"
|
||||
attrs.each { |k, v|
|
||||
@buf << " " << k << "=" << v.dump
|
||||
}
|
||||
@buf << ">"
|
||||
yield
|
||||
@buf << "</#{name}>"
|
||||
end
|
||||
|
||||
def code(**attrs, &blk)
|
||||
tag('pre') {
|
||||
tag('code', **attrs) {
|
||||
@buf << YAMD::Code::highlight(blk.())
|
||||
}
|
||||
}
|
||||
end
|
||||
|
||||
def inline_code(text)
|
||||
tag('code') {
|
||||
@buf << YAMD::Code::highlight(text)
|
||||
}
|
||||
end
|
||||
|
||||
def inline_math(text)
|
||||
parsed = AsciiMath.parse(text)
|
||||
@buf << parsed.to_mathml
|
||||
end
|
||||
end
|
||||
|
||||
File.open(ARGV[0]) { |f|
|
||||
state = YAMD::State.new f
|
||||
ctx = YAMD::Context.new 0
|
||||
|
||||
YAMD::parseContent state, ctx
|
||||
|
||||
code = state.output.finalize
|
||||
puts code
|
||||
|
||||
renderer = HTMLRenderer.new
|
||||
|
||||
eval(code).(renderer)
|
||||
puts renderer.finalize
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue