better HTML rendering, more options for rendering, fixed command line option parsing
This commit is contained in:
parent
4c484f64fc
commit
e2d94cd249
10
bin/mmmdpp
10
bin/mmmdpp
|
@ -17,12 +17,9 @@ class OptionNavigator
|
|||
# Read a definition
|
||||
# @param define [String]
|
||||
def read_definition(define)
|
||||
define.split(";").each do |part|
|
||||
locstring, _, value = part.partition(":")
|
||||
locstring = deconstruct(locstring.strip)
|
||||
locstring, value = deconstruct(define)
|
||||
assign(locstring, JSON.parse(value))
|
||||
end
|
||||
end
|
||||
|
||||
attr_reader :options
|
||||
|
||||
|
@ -83,6 +80,9 @@ class OptionNavigator
|
|||
buffer = locstring[0..closepart]
|
||||
part = locstring[1..-2].to_i
|
||||
locstring = locstring.delete_prefix(buffer)
|
||||
when ':'
|
||||
locstring = locstring.delete_prefix(':')
|
||||
break
|
||||
else
|
||||
raise ParserError, 'separator missing' unless buffer.empty?
|
||||
|
||||
|
@ -92,7 +92,7 @@ class OptionNavigator
|
|||
end
|
||||
end
|
||||
parts.append(part) if part
|
||||
parts
|
||||
[parts, locstring]
|
||||
end
|
||||
|
||||
def assign(keys, value)
|
||||
|
|
|
@ -45,7 +45,8 @@ module MMMD
|
|||
tag: "ol"
|
||||
},
|
||||
"PointBlank::DOM::IndentBlock" => {
|
||||
tag: "pre"
|
||||
tag: "pre",
|
||||
codeblock: true
|
||||
},
|
||||
"PointBlank::DOM::ULListElement" => {
|
||||
tag: "li"
|
||||
|
@ -87,7 +88,8 @@ module MMMD
|
|||
tag: "pre",
|
||||
outer: {
|
||||
tag: "code"
|
||||
}
|
||||
},
|
||||
codeblock: true
|
||||
},
|
||||
"PointBlank::DOM::QuoteBlock" => {
|
||||
tag: "blockquote"
|
||||
|
@ -126,7 +128,12 @@ module MMMD
|
|||
|
||||
def initialize(overrides)
|
||||
@mapping = self.class.mapping
|
||||
@mapping = @mapping.merge(overrides["mapping"]) if overrides["mapping"]
|
||||
if overrides["mapping"]
|
||||
overrides["mapping"].each do |key, value|
|
||||
next unless @mapping[key]
|
||||
@mapping[key] = @mapping[key].merge(value)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
attr_reader :mapping
|
||||
|
@ -154,7 +161,7 @@ module MMMD
|
|||
text = _render(@document, @options, level: @options["init_level"])
|
||||
@options["init_level"].times { text = indent(text) }
|
||||
if @options["nowrap"]
|
||||
text
|
||||
remove_pre_spaces(text)
|
||||
else
|
||||
[
|
||||
preambule,
|
||||
|
@ -226,8 +233,7 @@ module MMMD
|
|||
end
|
||||
|
||||
def _render(element, options, inline: false, level: 0, literaltext: false)
|
||||
modeswitch = element.is_a?(::PointBlank::DOM::LeafBlock) ||
|
||||
element.is_a?(::PointBlank::DOM::Paragraph)
|
||||
modeswitch = figure_out_modeswitch(element)
|
||||
inline ||= modeswitch
|
||||
level += 1 unless inline
|
||||
text = if element.children.empty?
|
||||
|
@ -248,21 +254,29 @@ module MMMD
|
|||
literaltext: literaltext)
|
||||
end
|
||||
|
||||
def figure_out_modeswitch(element)
|
||||
element.is_a?(::PointBlank::DOM::LeafBlock) ||
|
||||
element.is_a?(::PointBlank::DOM::Paragraph)
|
||||
end
|
||||
|
||||
def run_filters(text, element, level:, inline:, modeswitch:,
|
||||
literaltext:)
|
||||
element_style = @mapping[element.class.name]
|
||||
return text unless element_style
|
||||
return text if literaltext
|
||||
|
||||
codeblock = element_style[:codeblock]
|
||||
hsize = @options["linewrap"] - (level * @options["indent"])
|
||||
text = wordwrap(text, hsize) if modeswitch
|
||||
text = wordwrap(text, hsize) if modeswitch && !codeblock
|
||||
|
||||
if element_style[:sanitize]
|
||||
text = MMMD::EntityUtils.encode_entities(text)
|
||||
end
|
||||
if element_style[:inline]
|
||||
innerclose(element, element_style, text)
|
||||
else
|
||||
openclose(text, element, element_style, inline)
|
||||
openclose(text, element, element_style,
|
||||
codeblock ? false : inline)
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -272,20 +286,25 @@ module MMMD
|
|||
opentag + text + closetag
|
||||
else
|
||||
[opentag,
|
||||
indent(text),
|
||||
indent(text.rstrip),
|
||||
closetag].join("\n")
|
||||
end
|
||||
end
|
||||
|
||||
def innerclose(element, style, text)
|
||||
props = element.properties
|
||||
tag = "<#{style[:tag]}"
|
||||
tag = ""
|
||||
tag += "<figure>" if style[:figcaption]
|
||||
tag += "<#{style[:tag]}"
|
||||
tag += " style=#{style[:style].inspect}" if style[:style]
|
||||
tag += " href=#{read_link(element)}" if style[:href]
|
||||
tag += " alt=#{text.inspect}" if style[:alt]
|
||||
tag += " src=#{read_link(element)}" if style[:src]
|
||||
tag += " title=#{read_title(element)}" if style[:title] && props[:title]
|
||||
tag += ">"
|
||||
if style[:figcaption]
|
||||
tag += "<figcaption>#{text}</figcaption></figure>"
|
||||
end
|
||||
if style[:outer]
|
||||
outeropen, outerclose = construct_tags(style[:outer], element)
|
||||
tag = outeropen + tag + outerclose
|
||||
|
@ -298,7 +317,11 @@ module MMMD
|
|||
|
||||
props = element.properties
|
||||
opentag = "<#{style[:tag]}"
|
||||
opentag += "<figure>#{opentag}" if style[:figcaption]
|
||||
closetag = "</#{style[:tag]}>"
|
||||
if style[:figcaption]
|
||||
closetag += "<figcaption>#{text}</figcaption></figure>"
|
||||
end
|
||||
opentag += " style=#{style[:style].inspect}" if style[:style]
|
||||
opentag += " href=#{read_link(element)}" if style[:href]
|
||||
opentag += " src=#{read_link(element)}" if style[:src]
|
||||
|
@ -326,7 +349,7 @@ module MMMD
|
|||
|
||||
def indent(text)
|
||||
text.lines.map do |line|
|
||||
"#{' ' * @options["indent"]}#{line}"
|
||||
"#{' ' * @options['indent']}#{line}"
|
||||
end.join('')
|
||||
end
|
||||
|
||||
|
@ -334,7 +357,7 @@ module MMMD
|
|||
head = @options['head']
|
||||
headinfo = "#{indent(<<~HEAD.rstrip)}\n " if head
|
||||
<head>
|
||||
#{head.is_a?(Array) ? head.join("\n") : head}
|
||||
#{indent(head.is_a?(Array) ? head.join("\n") : head)}
|
||||
</head>
|
||||
HEAD
|
||||
headinfo ||= " "
|
||||
|
|
Loading…
Reference in New Issue