lib/kramdown/converter/kramdown.rb in kramdown-0.11.0 vs lib/kramdown/converter/kramdown.rb in kramdown-0.12.0
- old
+ new
@@ -24,18 +24,18 @@
module Kramdown
module Converter
- # Converts a Kramdown::Document to the kramdown format.
+ # Converts an element tree to the kramdown format.
class Kramdown < Base
# :stopdoc:
- include ::Kramdown::Utils::HTML
+ include ::Kramdown::Utils::Html
- def initialize(doc)
+ def initialize(root, options)
super
@linkrefs = []
@footnotes = []
@abbrevs = []
@stack = []
@@ -43,16 +43,16 @@
def convert(el, opts = {:indent => 0})
res = send("convert_#{el.type}", el, opts)
if el.type != :html_element && el.type != :li && el.type != :dd && (ial = ial_for_element(el))
res << ial
- res << "\n\n" if el.options[:category] == :block
+ res << "\n\n" if Element.category(el) == :block
elsif [:ul, :dl, :ol, :codeblock].include?(el.type) && opts[:next] &&
([el.type, :codeblock].include?(opts[:next].type) ||
(opts[:next].type == :blank && opts[:nnext] && [el.type, :codeblock].include?(opts[:nnext].type)))
res << "^\n\n"
- elsif el.options[:category] == :block && ![:li, :dd, :dt, :td, :th, :tr, :thead, :tbody, :tfoot, :blank].include?(el.type) &&
+ elsif Element.category(el) == :block && ![:li, :dd, :dt, :td, :th, :tr, :thead, :tbody, :tfoot, :blank].include?(el.type) &&
(el.type != :p || !el.options[:transparent])
res << "\n"
end
res
end
@@ -75,11 +75,11 @@
def convert_blank(el, opts)
""
end
- ESCAPED_CHAR_RE = /(\$\$|[\\*_`\[\]\{"'])|^[ ]{0,3}(:)/
+ ESCAPED_CHAR_RE = /(\$\$|[\\*_`\[\]\{"'|])|^[ ]{0,3}(:)/
def convert_text(el, opts)
if opts[:raw_text]
el.value
else
@@ -88,14 +88,13 @@
end.gsub(/\s+/, ' ').gsub(ESCAPED_CHAR_RE) { "\\#{$1 || $2}" }
end
end
def convert_p(el, opts)
- w = @doc.options[:line_width] - opts[:indent].to_s.to_i
+ w = @options[:line_width] - opts[:indent].to_s.to_i
first, second, *rest = inner(el, opts).strip.gsub(/(.{1,#{w}})( +|$\n?)/, "\\1\n").split(/\n/)
- first.gsub!(/^(?:(#)|(\d+)\.|([+-]\s))/) { $1 || $3 ? "\\#{$1 || $3}" : "#{$2}\\."}
- first.gsub!(/\|/, '\\|')
+ first.gsub!(/^(?:(#|>)|(\d+)\.|([+-]\s))/) { $1 || $3 ? "\\#{$1 || $3}" : "#{$2}\\."} if first
second.gsub!(/^([=-]+\s*?)$/, "\\\1") if second
[first, second, *rest].compact.join("\n") + "\n"
end
@@ -130,11 +129,11 @@
['* ', el.children.first.type == :codeblock ? 4 : 2]
else
["#{opts[:index] + 1}.".ljust(4), 4]
end
if ial = ial_for_element(el)
- sym += ial + " "
+ sym << ial << " "
end
opts[:indent] += width
text = inner(el, opts)
newlines = text.scan(/\n*\Z/).first
@@ -154,21 +153,21 @@
end
def convert_dd(el, opts)
sym, width = ": ", (el.children.first.type == :codeblock ? 4 : 2)
if ial = ial_for_element(el)
- sym += ial + " "
+ sym << ial << " "
end
opts[:indent] += width
text = inner(el, opts)
newlines = text.scan(/\n*\Z/).first
first, *last = text.split(/\n/)
last = last.map {|l| " "*width + l}.join("\n")
text = first + (last.empty? ? "" : "\n") + last + newlines
text.chomp! if text =~ /\n\n\Z/ && opts[:next] && opts[:next].type == :dd
- text += "\n" if text !~ /\n\n\Z/ && opts[:next] && opts[:next].type == :dt
+ text << "\n" if text !~ /\n\n\Z/ && opts[:next] && opts[:next].type == :dt
if el.children.first.type == :p && !el.children.first.options[:transparent]
"\n#{sym}#{text}"
elsif el.children.first.type == :codeblock
"#{sym}\n #{text}"
else
@@ -182,45 +181,44 @@
HTML_TAGS_WITH_BODY=['div', 'script', 'iframe', 'textarea']
def convert_html_element(el, opts)
markdown_attr = el.options[:category] == :block && el.children.any? do |c|
- c.type != :html_element && (c.type != :p || !c.options[:transparent]) && c.options[:category] == :block
+ c.type != :html_element && (c.type != :p || !c.options[:transparent]) && Element.category(c) == :block
end
opts[:force_raw_text] = true if %w{script pre code}.include?(el.value)
opts[:raw_text] = opts[:force_raw_text] || opts[:block_raw_text] || (el.options[:category] != :span && !markdown_attr)
opts[:block_raw_text] = true if el.options[:category] == :block && opts[:raw_text]
res = inner(el, opts)
if el.options[:category] == :span
- "<#{el.value}#{html_attributes(el)}" << (!res.empty? || HTML_TAGS_WITH_BODY.include?(el.value) ? ">#{res}</#{el.value}>" : " />")
+ "<#{el.value}#{html_attributes(el.attr)}" << (!res.empty? || HTML_TAGS_WITH_BODY.include?(el.value) ? ">#{res}</#{el.value}>" : " />")
else
output = ''
- output << "<#{el.value}#{html_attributes(el)}"
+ output << "<#{el.value}#{html_attributes(el.attr)}"
output << " markdown=\"1\"" if markdown_attr
- if !res.empty? && el.options[:parse_type] != :block
+ if !res.empty? && el.options[:content_model] != :block
output << ">#{res}</#{el.value}>"
elsif !res.empty?
output << ">\n#{res}" << "</#{el.value}>"
elsif HTML_TAGS_WITH_BODY.include?(el.value)
output << "></#{el.value}>"
else
output << " />"
end
- output << "\n" if @stack.last.type != :html_element || @stack.last.options[:parse_type] != :raw
+ output << "\n" if @stack.last.type != :html_element || @stack.last.options[:content_model] != :raw
output
end
end
def convert_xml_comment(el, opts)
- if el.options[:category] == :block && (@stack.last.type != :html_element || @stack.last.options[:parse_type] != :raw)
+ if el.options[:category] == :block && (@stack.last.type != :html_element || @stack.last.options[:content_model] != :raw)
el.value + "\n"
else
el.value
end
end
alias :convert_xml_pi :convert_xml_comment
- alias :convert_html_doctype :convert_xml_comment
def convert_table(el, opts)
opts[:alignment] = el.options[:alignment]
inner(el, opts)
end
@@ -255,13 +253,12 @@
def convert_tr(el, opts)
"| " + el.children.map {|c| convert(c, opts)}.join(" | ") + " |\n"
end
def convert_td(el, opts)
- inner(el, opts).gsub(/\|/, '\\|')
+ inner(el, opts)
end
- alias :convert_th :convert_td
def convert_comment(el, opts)
if el.options[:category] == :block
"{::comment}\n#{el.value}\n{:/}\n"
else
@@ -308,11 +305,11 @@
delim = (el.value.scan(/`+/).max || '') + '`'
"#{delim}#{' ' if delim.size > 1}#{el.value}#{' ' if delim.size > 1}#{delim}"
end
def convert_footnote(el, opts)
- @footnotes << [el.options[:name], @doc.parse_infos[:footnotes][el.options[:name]]]
+ @footnotes << [el.options[:name], el.value]
"[^#{el.options[:name]}]"
end
def convert_raw(el, opts)
attr = (el.options[:type] || []).join(' ')
@@ -379,19 +376,19 @@
def create_footnote_defs
res = ''
@footnotes.each do |name, data|
res << "[^#{name}]:\n"
- res << inner(data[:content]).chomp.split(/\n/).map {|l| " #{l}"}.join("\n") + "\n\n"
+ res << inner(data).chomp.split(/\n/).map {|l| " #{l}"}.join("\n") + "\n\n"
end
res
end
def create_abbrev_defs
- return '' unless @doc.parse_infos[:abbrev_defs]
+ return '' unless @root.options[:abbrev_defs]
res = ''
- @doc.parse_infos[:abbrev_defs].each do |name, text|
+ @root.options[:abbrev_defs].each do |name, text|
res << "*[#{name}]: #{text}\n"
end
res
end
@@ -412,9 +409,11 @@
end.compact.join('')
res = "toc" + (res.strip.empty? ? '' : " #{res}") if (el.type == :ul || el.type == :ol) &&
(el.options[:ial][:refs].include?('toc') rescue nil)
res.strip.empty? ? nil : "{:#{res}}"
end
+
+ # :startdoc:
end
end
end