lib/kramdown/converter/kramdown.rb in kramdown-0.13.4 vs lib/kramdown/converter/kramdown.rb in kramdown-0.13.5

- old
+ new

@@ -1,9 +1,9 @@ # -*- coding: utf-8 -*- # #-- -# Copyright (C) 2009-2010 Thomas Leitner <t_leitner@gmx.at> +# Copyright (C) 2009-2012 Thomas Leitner <t_leitner@gmx.at> # # This file is part of kramdown. # # kramdown is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -94,11 +94,17 @@ def convert_p(el, opts) 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}\\."} if first second.gsub!(/^([=-]+\s*?)$/, "\\\1") if second - [first, second, *rest].compact.join("\n") + "\n" + res = [first, second, *rest].compact.join("\n") + "\n" + if el.children.length == 1 && el.children.first.type == :math + res = "\\#{res}" + elsif res.start_with?('\$$') && res.end_with?("\\$$\n") + res.sub!(/^\\\$\$/, '\$\$') + end + res end def convert_codeblock(el, opts) el.value.split(/\n/).map {|l| l.empty? ? " " : " #{l}"}.join("\n") + "\n" @@ -110,11 +116,11 @@ end def convert_header(el, opts) res = '' res << "#{'#' * el.options[:level]} #{inner(el, opts)}" - res << " {##{el.attr['id']}}" if el.attr['id'] + res << " {##{el.attr['id']}}" if el.attr['id'] && !el.attr['id'].strip.empty? res << "\n" end def convert_hr(el, opts) "* * *\n" @@ -349,11 +355,11 @@ def convert_smart_quote(el, opts) el.value.to_s =~ /[rl]dquo/ ? "\"" : "'" end def convert_math(el, opts) - (@stack.last.type == :p && opts[:prev].nil? ? "\\" : '') + "$$#{el.value}$$" + (el.options[:category] == :block ? "\n" : '') + "$$#{el.value}$$" + (el.options[:category] == :block ? "\n" : '') end def convert_abbreviation(el, opts) el.value end @@ -396,15 +402,15 @@ # Return the IAL containing the attributes of the element +el+. def ial_for_element(el) res = el.attr.map do |k,v| next if [:img, :a].include?(el.type) && ['href', 'src', 'alt', 'title'].include?(k) - next if el.type == :header && k == 'id' + next if el.type == :header && k == 'id' && !v.strip.empty? if v.nil? '' - elsif k == 'class' + elsif k == 'class' && !v.empty? " " + v.split(/\s+/).map {|w| ".#{w}"}.join(" ") - elsif k == 'id' + elsif k == 'id' && !v.strip.empty? " ##{v}" else " #{k}=\"#{v.to_s}\"" end end.compact.join('')