lib/kramdown/converter.rb in kramdown-0.1.0 vs lib/kramdown/converter.rb in kramdown-0.2.0

- old
+ new

@@ -59,10 +59,14 @@ def convert_text(el, inner, indent) escape_html(el.value, false) end + def convert_eob(el, inner, indent) + '' + end + def convert_p(el, inner, indent) "#{' '*indent}<p#{options_for_element(el)}>#{inner}</p>\n" end def convert_codeblock(el, inner, indent) @@ -95,42 +99,55 @@ def convert_ul(el, inner, indent) "#{' '*indent}<#{el.type}#{options_for_element(el)}>\n#{inner}#{' '*indent}</#{el.type}>\n" end alias :convert_ol :convert_ul + alias :convert_dl :convert_ul def convert_li(el, inner, indent) - output = ' '*indent << "<li" << options_for_element(el) << ">" - if el.options[:first_as_block] + output = ' '*indent << "<#{el.type}" << options_for_element(el) << ">" + if el.options[:first_is_block] output << "\n" << inner << ' '*indent else output << inner << (inner =~ /\n\Z/ ? ' '*indent : '') end - output << "</li>\n" + output << "</#{el.type}>\n" end + alias :convert_dd :convert_li + def convert_dt(el, inner, indent) + "#{' '*indent}<dt#{options_for_element(el)}>#{inner}</dt>\n" + end + def convert_html_raw(el, inner, indent) el.value + (el.options[:type] == :block ? "\n" : '') end - HTML_TAGS_WITH_BODY=['div'] + HTML_TAGS_WITH_BODY=['div', 'script'] def convert_html_element(el, inner, indent) if @doc.options[:filter_html].include?(el.value) inner.chomp + (el.options[:type] == :block ? "\n" : '') elsif el.options[:type] == :span "<#{el.value}#{options_for_element(el)}" << (!inner.empty? ? ">#{inner}</#{el.value}>" : " />") else - output = ' '*indent << "<#{el.value}#{options_for_element(el)}" - if !inner.empty? - output << ">\n#{inner.chomp}\n" << ' '*indent << "</#{el.value}>" + output = '' + output << ' '*indent if !el.options[:no_start_indent] && el.options[:parse_type] != :raw && !el.options[:parent_is_raw] + output << "<#{el.value}#{options_for_element(el)}" + if !inner.empty? && (el.options[:compact] || el.options[:parse_type] != :block) + output << ">#{inner}</#{el.value}>" + elsif !inner.empty? && (el.children.first.type == :text || el.children.first.options[:no_start_indent]) + output << ">#{inner}" << ' '*indent << "</#{el.value}>" + elsif !inner.empty? + output << ">\n#{inner}" << ' '*indent << "</#{el.value}>" elsif HTML_TAGS_WITH_BODY.include?(el.value) output << "></#{el.value}>" else output << " />" end - output << "\n" + output << "\n" if el.options[:outer_element] || (el.options[:parse_type] != :raw && !el.options[:parent_is_raw]) + output end end def convert_br(el, inner, indent) "<br />" @@ -162,21 +179,34 @@ def convert_em(el, inner, indent) "<#{el.type}#{options_for_element(el)}>#{inner}</#{el.type}>" end alias :convert_strong :convert_em + def convert_entity(el, inner, indent) + el.value + end + + TYPOGRAPHIC_SYMS = { + :mdash => '&mdash;', :ndash => '&ndash;', :ellipsis => '&hellip;', + :laquo_space => '&laquo;&nbsp;', :raquo_space => '&nbsp;&raquo;', + :laquo => '&laquo;', :raquo => '&raquo;' + } + def convert_typographic_sym(el, inner, indent) + TYPOGRAPHIC_SYMS[el.value] + end + def convert_root(el, inner, indent) inner << footnote_content end # Return a HTML list with the footnote content for the used footnotes. def footnote_content ol = Element.new(:ol) ol.options[:attr] = {'start' => @footnote_start} if @footnote_start != 1 @footnotes.each do |name, data| - li = Element.new(:li, nil, {:attr => {:id => "fn:#{name}"}, :first_as_block => true}) + li = Element.new(:li, nil, {:attr => {:id => "fn:#{name}"}, :first_is_block => true}) li.children = Marshal.load(Marshal.dump(data[:content].children)) #TODO: probably remove this!!!! ol.children << li ref = Element.new(:raw, "<a href=\"#fnref:#{name}\" rev=\"footnote\">&#8617;</a>") if li.children.last.type == :p @@ -202,10 +232,11 @@ } ESCAPE_ALL_RE = Regexp.union(*ESCAPE_MAP.collect {|k,v| Regexp.escape(k)}) ESCAPE_ALL_NOT_ENTITIES_RE = Regexp.union(REXML::Parsers::BaseParser::REFERENCE_RE, ESCAPE_ALL_RE) # Escape the special HTML characters in the string +str+. If +all+ is +true+ then all - # characters are escaped, if +all+ is +false+ + # characters are escaped, if +all+ is +false+ then only those characters are escaped that are + # not part on an HTML entity. def escape_html(str, all = true) str.gsub(all ? ESCAPE_ALL_RE : ESCAPE_ALL_NOT_ENTITIES_RE) {|m| ESCAPE_MAP[m] || m} end end