Initial commit.

This commit is contained in:
yan
2011-11-17 15:45:33 -06:00
commit 882015bc6d
1819 changed files with 111625 additions and 0 deletions

View File

@@ -0,0 +1,111 @@
# -*- coding: utf-8 -*-
#
#--
# Copyright (C) 2009-2010 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
# 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 'erb'
module Kramdown
module Converter
# == Base class for converters
#
# This class serves as base class for all converters. It provides methods that can/should be
# used by all converters (like #generate_id) as well as common functionality that is
# automatically applied to the result (for example, embedding the output into a template).
#
# == Implementing a converter
#
# Implementing a new converter is rather easy: just create a new sub class from this class and
# put it in the Kramdown::Converter module (the latter is only needed if auto-detection should
# work properly). Then you need to implement the #convert(tree) method which takes a document
# tree and should return the converted output.
#
# The document instance is automatically set as @doc in Base#initialize. Furthermore, the
# document instance provides a hash called `conversion_infos` that is also automatically cleared
# and can be used to store information about the conversion process.
#
# The actual transformation of the document tree can be done in any way. However, writing one
# method per tree element type is a straight forward way to do it - this is how the Html and
# Latex converters do the transformation.
class Base
# Initialize the converter with the given Kramdown document +doc+.
def initialize(doc)
@doc = doc
@doc.conversion_infos.clear
end
private_class_method(:new, :allocate)
# Convert the Kramdown document +doc+ to the output format implemented by a subclass.
#
# Initializes a new instance of the calling class and then calls the #convert method that must
# be implemented by each subclass. If the +template+ option is specified and non-empty, the
# result is rendered into the specified template.
def self.convert(doc)
result = new(doc).convert(doc.tree)
result = apply_template(doc, result) if !doc.options[:template].empty?
result
end
# Apply the template specified in the +doc+ options, using +body+ as the body string.
def self.apply_template(doc, body)
erb = ERB.new(get_template(doc.options[:template]))
obj = Object.new
obj.instance_variable_set(:@doc, doc)
obj.instance_variable_set(:@body, body)
erb.result(obj.instance_eval{binding})
end
# Return the template specified by +template+.
def self.get_template(template)
format_ext = '.' + self.name.split(/::/).last.downcase
shipped = File.join(::Kramdown.data_dir, template + format_ext)
if File.exist?(template)
File.read(template)
elsif File.exist?(template + format_ext)
File.read(template + format_ext)
elsif File.exist?(shipped)
File.read(shipped)
else
raise "The specified template file #{template} does not exist"
end
end
# Generate an unique alpha-numeric ID from the the string +str+ for use as header ID.
def generate_id(str)
gen_id = str.gsub(/[^a-zA-Z0-9 -]/, '').gsub(/^[^a-zA-Z]*/, '').gsub(' ', '-').downcase
gen_id = 'section' if gen_id.length == 0
@used_ids ||= {}
if @used_ids.has_key?(gen_id)
gen_id += '-' + (@used_ids[gen_id] += 1).to_s
else
@used_ids[gen_id] = 0
end
@doc.options[:auto_id_prefix] + gen_id
end
end
end
end

View File

@@ -0,0 +1,391 @@
# -*- coding: utf-8 -*-
#
#--
# Copyright (C) 2009-2010 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
# 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 'rexml/parsers/baseparser'
module Kramdown
module Converter
# Converts a Kramdown::Document to HTML.
class Html < Base
include ::Kramdown::Utils::HTML
# DEPRECATED: use #html_attributes
def options_for_element(el)
warn("DEPRECATION WARNING: this method will be deprecated in the next release, use #html_attributes instead")
html_attributes(el)
end
# :stopdoc:
# Defines the amount of indentation used when nesting HTML tags.
INDENTATION = 2
begin
require 'coderay'
# Highlighting via coderay is available if this constant is +true+.
HIGHLIGHTING_AVAILABLE = true
rescue LoadError => e
HIGHLIGHTING_AVAILABLE = false
end
# Initialize the HTML converter with the given Kramdown document +doc+.
def initialize(doc)
super
@footnote_counter = @footnote_start = @doc.options[:footnote_nr]
@footnotes = []
@toc = []
@toc_code = nil
end
def convert(el, indent = -INDENTATION, opts = {})
send("convert_#{el.type}", el, indent, opts)
end
def inner(el, indent, opts)
result = ''
indent += INDENTATION
el.children.each do |inner_el|
result << send("convert_#{inner_el.type}", inner_el, indent, opts)
end
result
end
def convert_blank(el, indent, opts)
"\n"
end
def convert_text(el, indent, opts)
escape_html(el.value, :text)
end
def convert_p(el, indent, opts)
if el.options[:transparent]
"#{inner(el, indent, opts)}"
else
"#{' '*indent}<p#{html_attributes(el)}>#{inner(el, indent, opts)}</p>\n"
end
end
def convert_codeblock(el, indent, opts)
if el.options[:attr] && el.options[:attr]['lang'] && HIGHLIGHTING_AVAILABLE
el = Marshal.load(Marshal.dump(el)) # so that the original is not changed
opts = {:wrap => @doc.options[:coderay_wrap], :line_numbers => @doc.options[:coderay_line_numbers],
:line_number_start => @doc.options[:coderay_line_number_start], :tab_width => @doc.options[:coderay_tab_width],
:bold_every => @doc.options[:coderay_bold_every], :css => @doc.options[:coderay_css]}
result = CodeRay.scan(el.value, el.options[:attr].delete('lang').to_sym).html(opts).chomp + "\n"
"#{' '*indent}<div#{html_attributes(el)}>#{result}#{' '*indent}</div>\n"
else
result = escape_html(el.value)
if el.options[:attr] && el.options[:attr].has_key?('class') && el.options[:attr]['class'] =~ /\bshow-whitespaces\b/
result.gsub!(/(?:(^[ \t]+)|([ \t]+$)|([ \t]+))/) do |m|
suffix = ($1 ? '-l' : ($2 ? '-r' : ''))
m.scan(/./).map do |c|
case c
when "\t" then "<span class=\"ws-tab#{suffix}\">\t</span>"
when " " then "<span class=\"ws-space#{suffix}\">&#8901;</span>"
end
end.join('')
end
end
"#{' '*indent}<pre#{html_attributes(el)}><code>#{result}#{result =~ /\n\Z/ ? '' : "\n"}</code></pre>\n"
end
end
def convert_blockquote(el, indent, opts)
"#{' '*indent}<blockquote#{html_attributes(el)}>\n#{inner(el, indent, opts)}#{' '*indent}</blockquote>\n"
end
def convert_header(el, indent, opts)
el = Marshal.load(Marshal.dump(el)) # so that the original is not changed
if @doc.options[:auto_ids] && !(el.options[:attr] && el.options[:attr]['id'])
(el.options[:attr] ||= {})['id'] = generate_id(el.options[:raw_text])
end
@toc << [el.options[:level], el.options[:attr]['id'], el.children] if el.options[:attr] && el.options[:attr]['id'] && within_toc_depth?(el)
"#{' '*indent}<h#{el.options[:level]}#{html_attributes(el)}>#{inner(el, indent, opts)}</h#{el.options[:level]}>\n"
end
def within_toc_depth?(el)
@doc.options[:toc_depth] <= 0 || el.options[:level] <= @doc.options[:toc_depth]
end
def convert_hr(el, indent, opts)
"#{' '*indent}<hr />\n"
end
def convert_ul(el, indent, opts)
if !@toc_code && (el.options[:ial][:refs].include?('toc') rescue nil) && (el.type == :ul || el.type == :ol)
@toc_code = [el.type, el.options[:attr], (0..128).to_a.map{|a| rand(36).to_s(36)}.join]
@toc_code.last
else
"#{' '*indent}<#{el.type}#{html_attributes(el)}>\n#{inner(el, indent, opts)}#{' '*indent}</#{el.type}>\n"
end
end
alias :convert_ol :convert_ul
alias :convert_dl :convert_ul
def convert_li(el, indent, opts)
output = ' '*indent << "<#{el.type}" << html_attributes(el) << ">"
res = inner(el, indent, opts)
if el.children.empty? || (el.children.first.type == :p && el.children.first.options[:transparent])
output << res << (res =~ /\n\Z/ ? ' '*indent : '')
else
output << "\n" << res << ' '*indent
end
output << "</#{el.type}>\n"
end
alias :convert_dd :convert_li
def convert_dt(el, indent, opts)
"#{' '*indent}<dt#{html_attributes(el)}>#{inner(el, indent, opts)}</dt>\n"
end
HTML_TAGS_WITH_BODY=['div', 'script']
def convert_html_element(el, indent, opts)
res = inner(el, indent, opts)
if el.options[:category] == :span
"<#{el.value}#{html_attributes(el)}" << (!res.empty? ? ">#{res}</#{el.value}>" : " />")
else
output = ''
output << ' '*indent if !el.options[:parent_is_raw]
output << "<#{el.value}#{html_attributes(el)}"
if !res.empty? && el.options[:parse_type] != :block
output << ">#{res}</#{el.value}>"
elsif !res.empty?
output << ">\n#{res}" << ' '*indent << "</#{el.value}>"
elsif HTML_TAGS_WITH_BODY.include?(el.value)
output << "></#{el.value}>"
else
output << " />"
end
output << "\n" if el.options[:outer_element] || !el.options[:parent_is_raw]
output
end
end
def convert_xml_comment(el, indent, opts)
if el.options[:category] == :block && !el.options[:parent_is_raw]
' '*indent + 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, indent, opts)
if el.options[:alignment].all? {|a| a == :default}
alignment = ''
else
alignment = el.options[:alignment].map do |a|
"#{' '*(indent + INDENTATION)}" + (a == :default ? "<col />" : "<col align=\"#{a}\" />") + "\n"
end.join('')
end
"#{' '*indent}<table#{html_attributes(el)}>\n#{alignment}#{inner(el, indent, opts)}#{' '*indent}</table>\n"
end
def convert_thead(el, indent, opts)
"#{' '*indent}<#{el.type}#{html_attributes(el)}>\n#{inner(el, indent, opts)}#{' '*indent}</#{el.type}>\n"
end
alias :convert_tbody :convert_thead
alias :convert_tfoot :convert_thead
alias :convert_tr :convert_thead
def convert_td(el, indent, opts)
res = inner(el, indent, opts)
"#{' '*indent}<#{el.type}#{html_attributes(el)}>#{res.empty? ? "&nbsp;" : res}</#{el.type}>\n"
end
alias :convert_th :convert_td
def convert_comment(el, indent, opts)
if el.options[:category] == :block
"#{' '*indent}<!-- #{el.value} -->\n"
else
"<!-- #{el.value} -->"
end
end
def convert_br(el, indent, opts)
"<br />"
end
def convert_a(el, indent, opts)
do_obfuscation = el.options[:attr]['href'] =~ /^mailto:/
if do_obfuscation
el = Marshal.load(Marshal.dump(el)) # so that the original is not changed
href = obfuscate(el.options[:attr]['href'].sub(/^mailto:/, ''))
mailto = obfuscate('mailto')
el.options[:attr]['href'] = "#{mailto}:#{href}"
end
res = inner(el, indent, opts)
res = obfuscate(res) if do_obfuscation
"<a#{html_attributes(el)}>#{res}</a>"
end
def convert_img(el, indent, opts)
"<img#{html_attributes(el)} />"
end
def convert_codespan(el, indent, opts)
"<code#{html_attributes(el)}>#{escape_html(el.value)}</code>"
end
def convert_footnote(el, indent, opts)
number = @footnote_counter
@footnote_counter += 1
@footnotes << [el.options[:name], @doc.parse_infos[:footnotes][el.options[:name]]]
"<sup id=\"fnref:#{el.options[:name]}\"><a href=\"#fn:#{el.options[:name]}\" rel=\"footnote\">#{number}</a></sup>"
end
def convert_raw(el, indent, opts)
el.value + (el.options[:category] == :block ? "\n" : '')
end
def convert_em(el, indent, opts)
"<#{el.type}#{html_attributes(el)}>#{inner(el, indent, opts)}</#{el.type}>"
end
alias :convert_strong :convert_em
def convert_entity(el, indent, opts)
entity_to_str(el.value)
end
TYPOGRAPHIC_SYMS = {
:mdash => [::Kramdown::Utils::Entities.entity('mdash')],
:ndash => [::Kramdown::Utils::Entities.entity('ndash')],
:hellip => [::Kramdown::Utils::Entities.entity('hellip')],
:laquo_space => [::Kramdown::Utils::Entities.entity('laquo'), ::Kramdown::Utils::Entities.entity('nbsp')],
:raquo_space => [::Kramdown::Utils::Entities.entity('nbsp'), ::Kramdown::Utils::Entities.entity('raquo')],
:laquo => [::Kramdown::Utils::Entities.entity('laquo')],
:raquo => [::Kramdown::Utils::Entities.entity('raquo')]
}
def convert_typographic_sym(el, indent, opts)
TYPOGRAPHIC_SYMS[el.value].map {|e| entity_to_str(e)}.join('')
end
def convert_smart_quote(el, indent, opts)
entity_to_str(::Kramdown::Utils::Entities.entity(el.value.to_s))
end
def convert_math(el, indent, opts)
el = Marshal.load(Marshal.dump(el)) # so that the original is not changed
el.options[:attr] ||= {}
el.options[:attr]['class'] ||= ''
el.options[:attr]['class'] += (el.options[:attr]['class'].empty? ? '' : ' ') + 'math'
type = 'span'
type = 'div' if el.options[:category] == :block
"<#{type}#{html_attributes(el)}>#{escape_html(el.value)}</#{type}>#{type == 'div' ? "\n" : ''}"
end
def convert_abbreviation(el, indent, opts)
title = @doc.parse_infos[:abbrev_defs][el.value]
title = nil if title.empty?
"<abbr#{title ? " title=\"#{title}\"" : ''}>#{el.value}</abbr>"
end
def convert_root(el, indent, opts)
result = inner(el, indent, opts)
result << footnote_content
if @toc_code
toc_tree = generate_toc_tree(@toc, @toc_code[0], @toc_code[1] || {})
text = if toc_tree.children.size > 0
convert(toc_tree, 0)
else
''
end
result.sub!(/#{@toc_code.last}/, text)
end
result
end
def generate_toc_tree(toc, type, attr)
sections = Element.new(type, nil, {:attr => {'id' => 'markdown-toc'}.merge(attr)})
stack = []
toc.each do |level, id, children|
li = Element.new(:li, nil, {:level => level})
li.children << Element.new(:p, nil, {:transparent => true})
a = Element.new(:a, nil, {:attr => {:href => "##{id}"}})
a.children += children
li.children.last.children << a
li.children << Element.new(type)
success = false
while !success
if stack.empty?
sections.children << li
stack << li
success = true
elsif stack.last.options[:level] < li.options[:level]
stack.last.children.last.children << li
stack << li
success = true
else
item = stack.pop
item.children.pop unless item.children.last.children.size > 0
end
end
end
while !stack.empty?
item = stack.pop
item.children.pop unless item.children.last.children.size > 0
end
sections
end
# Helper method for obfuscating the +text+ by using HTML entities.
def obfuscate(text)
result = ""
text.each_byte do |b|
result += (b > 128 ? b.chr : "&#%03d;" % b)
end
result.force_encoding(text.encoding) if RUBY_VERSION >= '1.9'
result
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_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
para = li.children.last
else
li.children << (para = Element.new(:p))
end
para.children << ref
end
(ol.children.empty? ? '' : "<div class=\"footnotes\">\n#{convert(ol, 2)}</div>\n")
end
end
end
end

View File

@@ -0,0 +1,398 @@
# -*- coding: utf-8 -*-
#
#--
# Copyright (C) 2009-2010 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
# 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 'rexml/parsers/baseparser'
module Kramdown
module Converter
# Converts a Kramdown::Document to the kramdown format.
class Kramdown < Base
# :stopdoc:
include ::Kramdown::Utils::HTML
def initialize(doc)
super
@linkrefs = []
@footnotes = []
@abbrevs = []
@stack = []
end
def convert(el, opts = {})
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
end
res
end
def inner(el, opts = {})
@stack.push([el, opts])
result = ''
el.children.each_with_index do |inner_el, index|
options = opts.dup
#p [index, inner_el]
options[:index] = index
options[:prev] = (index == 0 ? nil : el.children[index-1])
options[:next] = (index == el.children.length - 1 ? nil : el.children[index+1])
result << convert(inner_el, options)
end
@stack.pop
result
end
def convert_blank(el, opts)
"\n"
end
ESCAPED_CHAR_RE = /(\$\$|[\\*_`\[\]\{\}"'])|^[ ]{0,3}(:)/
def convert_text(el, opts)
if opts[:raw_text]
el.value
else
nl = (el.value =~ /\n$/)
el.value.gsub(/\s+/, ' ').gsub(ESCAPED_CHAR_RE) { "\\#{$1 || $2}" } + (nl ? "\n" : '')
end
end
def convert_p(el, opts)
res = inner(el, opts).strip.gsub(/\A(?:([#|])|(\d+)\.|([+-]\s))/) do
$1 || $3 ? "\\#{$1 || $3}" : "#{$2}\\."
end + "\n"
if opts[:next] && opts[:next].type == :p && !ial_for_element(el)
res += "\n"
end
res
end
CODEBLOCK_PREV_EL = [:ul, :ol, :dl, :codeblock]
def convert_codeblock(el, opts)
res = ''
res << "^\n" if opts[:prev] && ((CODEBLOCK_PREV_EL.include?(opts[:prev].type) && !ial_for_element(opts[:prev])) ||
(opts[:prev].type == :blank &&
opts[:index]-2 >= 0 &&
(tmp = @stack.last.first.children[opts[:index]-2]) &&
CODEBLOCK_PREV_EL.include?(tmp.type) && !ial_for_element(tmp)))
res << el.value.split(/\n/).map {|l| l.empty? ? " " : " #{l}"}.join("\n") + "\n"
end
def convert_blockquote(el, opts)
res = ''
res << "\n" if opts[:prev] && opts[:prev].type == :blockquote
res << inner(el, opts).chomp.split(/\n/).map {|l| "> #{l}"}.join("\n") << "\n"
end
def convert_header(el, opts)
res = ''
res << "\n" if opts[:prev] && opts[:prev].type != :blank
res << "#{'#' * el.options[:level]} #{inner(el, opts)}"
res << " {##{el.options[:attr]['id']}}" if el.options[:attr] && el.options[:attr]['id']
res << "\n" if opts[:next] && opts[:next].type != :blank
res << "\n"
end
def convert_hr(el, opts)
"* * *\n"
end
def convert_ul(el, opts)
res = ''
res << "\n" if opts[:prev] && (opts[:prev].type == :p && !opts[:prev].options[:transparent])
res << "^\n" if opts[:prev] && ((opts[:prev].type == el.type && !ial_for_element(opts[:prev])) ||
(opts[:prev].type == :blank && opts[:index]-2 >= 0 &&
(tmp = @stack.last.first.children[opts[:index]-2]) &&
tmp.type == el.type && !ial_for_element(tmp)))
res + inner(el, opts).sub(/\n+\Z/, "\n")
end
alias :convert_ol :convert_ul
alias :convert_dl :convert_ul
def convert_li(el, opts)
sym, width = if @stack.last.first.type == :ul
['* ', el.children.first.type == :codeblock ? 4 : 2]
else
["#{opts[:index] + 1}.".ljust(4), 4]
end
if ial = ial_for_element(el)
sym += ial + " "
end
first, *last = inner(el, opts).chomp.split(/\n/)
last = last.map {|l| " "*width + l}.join("\n")
last = last.empty? ? "\n" : "\n#{last}\n"
if el.children.first.type == :p && !el.children.first.options[:transparent]
res = "#{sym}#{first}\n#{last}"
res << "^\n" if el.children.size == 1 && @stack.last.first.children.last == el &&
(@stack.last.first.children.any? {|c| c.children.first.type != :p} || @stack.last.first.children.size == 1)
res
elsif el.children.first.type == :codeblock
"#{sym}\n #{first}#{last}"
else
"#{sym}#{first}#{last}"
end
end
def convert_dd(el, opts)
sym, width = ": ", (el.children.first.type == :codeblock ? 4 : 2)
if ial = ial_for_element(el)
sym += ial + " "
end
first, *last = inner(el, opts).chomp.split(/\n/)
last = last.map {|l| " "*width + l}.join("\n")
text = first + (last.empty? ? '' : "\n" + last)
if el.children.first.type == :p && !el.children.first.options[:transparent]
"\n#{sym}#{text}\n"
elsif el.children.first.type == :codeblock
"#{sym}\n #{text}\n"
else
"#{sym}#{text}\n"
end
end
def convert_dt(el, opts)
res = ''
res << inner(el, opts) << "\n"
end
HTML_TAGS_WITH_BODY=['div', 'script']
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
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? ? ">#{res}</#{el.value}>" : " />")
else
output = ''
output << "<#{el.value}#{html_attributes(el)}"
output << " markdown=\"1\"" if markdown_attr
if !res.empty? && el.options[:parse_type] != :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 el.options[:outer_element] || !el.options[:parent_is_raw]
output
end
end
def convert_xml_comment(el, opts)
if el.options[:category] == :block && !el.options[:parent_is_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
def convert_thead(el, opts)
rows = inner(el, opts)
if opts[:alignment].all? {|a| a == :default}
"#{rows}|" + "-"*10 + "\n"
else
"#{rows}| " + opts[:alignment].map do |a|
case a
when :left then ":-"
when :right then "-:"
when :center then ":-:"
when :default then "-"
end
end.join(' ') + "\n"
end
end
def convert_tbody(el, opts)
res = ''
res << inner(el, opts)
res << '|' << '-'*10 << "\n" if opts[:next] && opts[:next].type == :tbody
res
end
def convert_tfoot(el, opts)
"|" + "="*10 + "\n#{inner(el, opts)}"
end
def convert_tr(el, opts)
"| " + el.children.map {|c| convert(c, opts)}.join(" | ") + " |\n"
end
def convert_td(el, opts)
inner(el, opts).gsub(/\|/, '\\|')
end
alias :convert_th :convert_td
def convert_comment(el, opts)
if el.options[:category] == :block
"{::comment}\n#{el.value}\n{:/}\n"
else
"{::comment}#{el.value}{:/}"
end
end
def convert_br(el, opts)
" \n"
end
def convert_a(el, opts)
if el.options[:attr]['href'].empty?
"[#{inner(el, opts)}]()"
else
@linkrefs << el
"[#{inner(el, opts)}][#{@linkrefs.size}]"
end
end
def convert_img(el, opts)
title = (el.options[:attr]['title'] ? ' "' + el.options[:attr]['title'].gsub(/"/, "&quot;") + '"' : '')
"![#{el.options[:attr]['alt']}](<#{el.options[:attr]['src']}>#{title})"
end
def convert_codespan(el, opts)
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]]]
"[^#{el.options[:name]}]"
end
def convert_raw(el, opts)
if @stack.last.first.type == :html_element
el.value
elsif el.options[:category] == :block
"{::nomarkdown}\n#{el.value}\n{:/}\n"
else
"{::nomarkdown}#{el.value}{:/}"
end
end
def convert_em(el, opts)
"*#{inner(el, opts)}*"
end
def convert_strong(el, opts)
"**#{inner(el, opts)}**"
end
def convert_entity(el, opts)
entity_to_str(el.value)
end
TYPOGRAPHIC_SYMS = {
:mdash => '---', :ndash => '--', :hellip => '...',
:laquo_space => '<< ', :raquo_space => ' >>',
:laquo => '<<', :raquo => '>>'
}
def convert_typographic_sym(el, opts)
TYPOGRAPHIC_SYMS[el.value]
end
def convert_smart_quote(el, opts)
el.value.to_s =~ /[rl]dquo/ ? "\"" : "'"
end
def convert_math(el, opts)
(@stack.last.first.type == :p && opts[:prev].nil? ? "\\" : '') + "$$#{el.value}$$" + (el.options[:category] == :block ? "\n" : '')
end
def convert_abbreviation(el, opts)
el.value
end
def convert_root(el, opts)
res = inner(el, opts)
res << create_link_defs
res << create_footnote_defs
res << create_abbrev_defs
res
end
def create_link_defs
res = ''
res << "\n\n" if @linkrefs.size > 0
@linkrefs.each_with_index do |el, i|
link = (el.type == :a ? el.options[:attr]['href'] : el.options[:attr]['src'])
link = "<#{link}>" if link =~ / /
title = el.options[:attr]['title']
res << "[#{i+1}]: #{link} #{title ? '"' + title.gsub(/"/, "&quot;") + '"' : ''}\n"
end
res
end
def create_footnote_defs
res = ''
res = "\n" if @footnotes.size > 0
@footnotes.each do |name, data|
res << "\n[^#{name}]:\n"
res << inner(data[:content]).chomp.split(/\n/).map {|l| " #{l}"}.join("\n")
end
res
end
def create_abbrev_defs
return '' unless @doc.parse_infos[:abbrev_defs]
res = ''
@doc.parse_infos[:abbrev_defs].each do |name, text|
res << "*[#{name}]: #{text}\n"
end
res
end
# Return the IAL containing the attributes of the element +el+.
def ial_for_element(el)
res = (el.options[: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'
v.nil? ? '' : " #{k}=\"#{v.to_s}\""
end.compact.sort.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
end
end
end

View File

@@ -0,0 +1,553 @@
# -*- coding: utf-8 -*-
#
#--
# Copyright (C) 2009-2010 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
# 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 'set'
module Kramdown
module Converter
# Converts a Kramdown::Document to LaTeX. This converter uses ideas from other Markdown-to-LaTeX
# converters like Pandoc and Maruku.
class Latex < Base
# :stopdoc:
# Initialize the LaTeX converter with the given Kramdown document +doc+.
def initialize(doc)
super
#TODO: set the footnote counter at the beginning of the document
@doc.options[:footnote_nr]
@doc.conversion_infos[:packages] = Set.new
end
def convert(el, opts = {})
send("convert_#{el.type}", el, opts)
end
def inner(el, opts)
result = ''
el.children.each do |inner_el|
result << send("convert_#{inner_el.type}", inner_el, opts)
end
result
end
def convert_root(el, opts)
inner(el, opts)
end
def convert_blank(el, opts)
""
end
def convert_text(el, opts)
escape(el.value)
end
def convert_p(el, opts)
"#{inner(el, opts)}\n\n"
end
def convert_codeblock(el, opts)
show_whitespace = el.options[:attr] && el.options[:attr]['class'].to_s =~ /\bshow-whitespaces\b/
lang = el.options[:attr] && el.options[:attr]['lang']
if show_whitespace || lang
result = "\\lstset{showspaces=%s,showtabs=%s}\n" % (show_whitespace ? ['true', 'true'] : ['false', 'false'])
result += "\\lstset{language=#{lang}}\n" if lang
result += "\\lstset{basicstyle=\\ttfamily\\footnotesize}\\lstset{columns=fixed,frame=tlbr}\n"
"#{result}\\begin{lstlisting}#{attribute_list(el)}\n#{el.value}\n\\end{lstlisting}\n"
else
"\\begin{verbatim}#{el.value}\\end{verbatim}\n"
end
end
def latex_environment(type, el, text)
"\\begin{#{type}}#{attribute_list(el)}\n#{text}\n\\end{#{type}}\n"
end
def convert_blockquote(el, opts)
latex_environment('quote', el, inner(el, opts))
end
HEADER_TYPES = {
1 => 'section',
2 => 'subsection',
3 => 'subsubsection',
4 => 'paragraph',
5 => 'subparagraph',
6 => 'subparagraph'
}
def convert_header(el, opts)
type = HEADER_TYPES[el.options[:level]]
if ((el.options[:attr] && (id = el.options[:attr]['id'])) ||
(@doc.options[:auto_ids] && (id = generate_id(el.options[:raw_text])))) &&
(@doc.options[:toc_depth] <= 0 || el.options[:level] <= @doc.options[:toc_depth])
"\\hypertarget{#{id}}{}\\#{type}{#{inner(el, opts)}}\\label{#{id}}\n\n"
else
"\\#{type}*{#{inner(el, opts)}}\n\n"
end
end
def convert_hr(el, opts)
"\\begin{center}#{attribute_list(el)}\n\\rule{3in}{0.4pt}\n\\end{center}\n"
end
def convert_ul(el, opts)
if !@doc.conversion_infos[:has_toc] && (el.options[:ial][:refs].include?('toc') rescue nil)
@doc.conversion_infos[:has_toc] = true
'\tableofcontents'
else
latex_environment(el.type == :ul ? 'itemize' : 'enumerate', el, inner(el, opts))
end
end
alias :convert_ol :convert_ul
def convert_dl(el, opts)
latex_environment('description', el, inner(el, opts))
end
def convert_li(el, opts)
"\\item #{inner(el, opts).sub(/\n+\Z/, '')}\n"
end
def convert_dt(el, opts)
"\\item[#{inner(el, opts)}] "
end
def convert_dd(el, opts)
"#{inner(el, opts)}\n\n"
end
def convert_html_element(el, opts)
if el.value == 'i'
"\\emph{#{inner(el, opts)}}"
elsif el.value == 'b'
"\\emph{#{inner(el, opts)}}"
else
@doc.warnings << "Can't convert HTML element"
''
end
end
def convert_xml_comment(el, opts)
el.value.split(/\n/).map {|l| "% #{l}"}.join("\n") + "\n"
end
def convert_xml_pi(el, opts)
@doc.warnings << "Can't convert XML PI/HTML document type"
''
end
alias :convert_html_doctype :convert_xml_pi
TABLE_ALIGNMENT_CHAR = {:default => 'l', :left => 'l', :center => 'c', :right => 'r'}
def convert_table(el, opts)
align = el.options[:alignment].map {|a| TABLE_ALIGNMENT_CHAR[a]}.join('|')
"\\begin{tabular}{|#{align}|}#{attribute_list(el)}\n\\hline\n#{inner(el, opts)}\\hline\n\\end{tabular}\n\n"
end
def convert_thead(el, opts)
"#{inner(el, opts)}\\hline\n"
end
def convert_tbody(el, opts)
inner(el, opts)
end
def convert_tfoot(el, opts)
"\\hline \\hline \n#{inner(el, opts)}"
end
def convert_tr(el, opts)
el.children.map {|c| send("convert_#{c.type}", c, opts)}.join(' & ') + "\\\\\n"
end
def convert_td(el, opts)
inner(el, opts)
end
alias :convert_th :convert_td
def convert_comment(el, opts)
el.value.split(/\n/).map {|l| "% #{l}"}.join("\n") + "\n"
end
def convert_br(el, opts)
"\\newline\n"
end
def convert_a(el, opts)
url = el.options[:attr]['href']
if url =~ /^#/
"\\hyperlink{#{url[1..-1]}}{#{inner(el, opts)}}"
else
"\\href{#{url}}{#{inner(el, opts)}}"
end
end
def convert_img(el, opts)
if el.options[:attr]['src'] =~ /^(https?|ftps?):\/\//
@doc.warnings << "Cannot include non-local image"
''
elsif !el.options[:attr]['src'].empty?
@doc.conversion_infos[:packages] << 'graphicx'
"\\includegraphics{#{el.options[:attr]['src']}}"
else
@doc.warnings << "Cannot include image with empty path"
''
end
end
def convert_codespan(el, opts)
"{\\tt #{escape(el.value)}}"
end
def convert_footnote(el, opts)
@doc.conversion_infos[:packages] << 'fancyvrb'
"\\footnote{#{inner(@doc.parse_infos[:footnotes][el.options[:name]][:content], opts)}}"
end
def convert_raw(el, opts)
escape(el.value)
end
def convert_em(el, opts)
"\\emph{#{inner(el, opts)}}"
end
def convert_strong(el, opts)
"\\textbf{#{inner(el, opts)}}"
end
# Inspired by Maruku: entity conversion table based on the one from htmltolatex
# (http://sourceforge.net/projects/htmltolatex/), with some small adjustments/additions
ENTITY_CONV_TABLE = {
913 => ['$A$'],
914 => ['$B$'],
915 => ['$\Gamma$'],
916 => ['$\Delta$'],
917 => ['$E$'],
918 => ['$Z$'],
919 => ['$H$'],
920 => ['$\Theta$'],
921 => ['$I$'],
922 => ['$K$'],
923 => ['$\Lambda$'],
924 => ['$M$'],
925 => ['$N$'],
926 => ['$\Xi$'],
927 => ['$O$'],
928 => ['$\Pi$'],
929 => ['$P$'],
931 => ['$\Sigma$'],
932 => ['$T$'],
933 => ['$Y$'],
934 => ['$\Phi$'],
935 => ['$X$'],
936 => ['$\Psi$'],
937 => ['$\Omega$'],
945 => ['$\alpha$'],
946 => ['$\beta$'],
947 => ['$\gamma$'],
948 => ['$\delta$'],
949 => ['$\epsilon$'],
950 => ['$\zeta$'],
951 => ['$\eta$'],
952 => ['$\theta$'],
953 => ['$\iota$'],
954 => ['$\kappa$'],
955 => ['$\lambda$'],
956 => ['$\mu$'],
957 => ['$\nu$'],
958 => ['$\xi$'],
959 => ['$o$'],
960 => ['$\pi$'],
961 => ['$\rho$'],
963 => ['$\sigma$'],
964 => ['$\tau$'],
965 => ['$\upsilon$'],
966 => ['$\phi$'],
967 => ['$\chi$'],
968 => ['$\psi$'],
969 => ['$\omega$'],
962 => ['$\varsigma$'],
977 => ['$\vartheta$'],
982 => ['$\varpi$'],
8230 => ['\ldots'],
8242 => ['$\prime$'],
8254 => ['-'],
8260 => ['/'],
8472 => ['$\wp$'],
8465 => ['$\Im$'],
8476 => ['$\Re$'],
8501 => ['$\aleph$'],
8226 => ['$\bullet$'],
8482 => ['$^{\rm TM}$'],
8592 => ['$\leftarrow$'],
8594 => ['$\rightarrow$'],
8593 => ['$\uparrow$'],
8595 => ['$\downarrow$'],
8596 => ['$\leftrightarrow$'],
8629 => ['$\hookleftarrow$'],
8657 => ['$\Uparrow$'],
8659 => ['$\Downarrow$'],
8656 => ['$\Leftarrow$'],
8658 => ['$\Rightarrow$'],
8660 => ['$\Leftrightarrow$'],
8704 => ['$\forall$'],
8706 => ['$\partial$'],
8707 => ['$\exists$'],
8709 => ['$\emptyset$'],
8711 => ['$\nabla$'],
8712 => ['$\in$'],
8715 => ['$\ni$'],
8713 => ['$\notin$'],
8721 => ['$\sum$'],
8719 => ['$\prod$'],
8722 => ['$-$'],
8727 => ['$\ast$'],
8730 => ['$\surd$'],
8733 => ['$\propto$'],
8734 => ['$\infty$'],
8736 => ['$\angle$'],
8743 => ['$\wedge$'],
8744 => ['$\vee$'],
8745 => ['$\cup$'],
8746 => ['$\cap$'],
8747 => ['$\int$'],
8756 => ['$\therefore$', 'amssymb'],
8764 => ['$\sim$'],
8776 => ['$\approx$'],
8773 => ['$\cong$'],
8800 => ['$\neq$'],
8801 => ['$\equiv$'],
8804 => ['$\leq$'],
8805 => ['$\geq$'],
8834 => ['$\subset$'],
8835 => ['$\supset$'],
8838 => ['$\subseteq$'],
8839 => ['$\supseteq$'],
8836 => ['$\nsubset$', 'amssymb'],
8853 => ['$\oplus$'],
8855 => ['$\otimes$'],
8869 => ['$\perp$'],
8901 => ['$\cdot$'],
8968 => ['$\rceil$'],
8969 => ['$\lceil$'],
8970 => ['$\lfloor$'],
8971 => ['$\rfloor$'],
9001 => ['$\rangle$'],
9002 => ['$\langle$'],
9674 => ['$\lozenge$', 'amssymb'],
9824 => ['$\spadesuit$'],
9827 => ['$\clubsuit$'],
9829 => ['$\heartsuit$'],
9830 => ['$\diamondsuit$'],
38 => ['\&'],
34 => ['"'],
39 => ['\''],
169 => ['\copyright'],
60 => ['\textless{}'],
62 => ['\textgreater{}'],
338 => ['\OE'],
339 => ['\oe'],
352 => ['\v{S}'],
353 => ['\v{s}'],
376 => ['\"Y'],
710 => ['\textasciicircum'],
732 => ['\textasciitilde'],
8211 => ['--'],
8212 => ['---'],
8216 => ['`'],
8217 => ['\''],
8220 => ['``'],
8221 => ['\'\''],
8224 => ['\dag'],
8225 => ['\ddag'],
8240 => ['\permil', 'wasysym'],
8364 => ['\euro', 'eurosym'],
8249 => ['\guilsinglleft'],
8250 => ['\guilsinglright'],
8218 => ['\quotesinglbase', 'mathcomp'],
8222 => ['\quotedblbase', 'mathcomp'],
402 => ['\textflorin', 'mathcomp'],
381 => ['\v{Z}'],
382 => ['\v{z}'],
160 => ['\nolinebreak'],
161 => ['\textexclamdown'],
163 => ['\pounds'],
164 => ['\currency', 'wasysym'],
165 => ['\textyen', 'textcomp'],
166 => ['\brokenvert', 'wasysym'],
167 => ['\S'],
171 => ['\guillemotleft'],
187 => ['\guillemotright'],
174 => ['\textregistered'],
170 => ['\textordfeminine'],
172 => ['$\neg$'],
176 => ['$\degree$', 'mathabx'],
177 => ['$\pm$'],
180 => ['\''],
181 => ['$\mu$'],
182 => ['\P'],
183 => ['$\cdot$'],
186 => ['\textordmasculine'],
162 => ['\cent', 'wasysym'],
185 => ['$^1$'],
178 => ['$^2$'],
179 => ['$^3$'],
189 => ['$\frac{1}{2}$'],
188 => ['$\frac{1}{4}$'],
190 => ['$\frac{3}{4}'],
192 => ['\`A'],
193 => ['\\\'A'],
194 => ['\^A'],
195 => ['\~A'],
196 => ['\"A'],
197 => ['\AA'],
198 => ['\AE'],
199 => ['\cC'],
200 => ['\`E'],
201 => ['\\\'E'],
202 => ['\^E'],
203 => ['\"E'],
204 => ['\`I'],
205 => ['\\\'I'],
206 => ['\^I'],
207 => ['\"I'],
208 => ['$\eth$', 'amssymb'],
209 => ['\~N'],
210 => ['\`O'],
211 => ['\\\'O'],
212 => ['\^O'],
213 => ['\~O'],
214 => ['\"O'],
215 => ['$\times$'],
216 => ['\O'],
217 => ['\`U'],
218 => ['\\\'U'],
219 => ['\^U'],
220 => ['\"U'],
221 => ['\\\'Y'],
222 => ['\Thorn', 'wasysym'],
223 => ['\ss'],
224 => ['\`a'],
225 => ['\\\'a'],
226 => ['\^a'],
227 => ['\~a'],
228 => ['\"a'],
229 => ['\aa'],
230 => ['\ae'],
231 => ['\cc'],
232 => ['\`e'],
233 => ['\\\'e'],
234 => ['\^e'],
235 => ['\"e'],
236 => ['\`i'],
237 => ['\\\'i'],
238 => ['\^i'],
239 => ['\"i'],
240 => ['$\eth$'],
241 => ['\~n'],
242 => ['\`o'],
243 => ['\\\'o'],
244 => ['\^o'],
245 => ['\~o'],
246 => ['\"o'],
247 => ['$\divide$'],
248 => ['\o'],
249 => ['\`u'],
250 => ['\\\'u'],
251 => ['\^u'],
252 => ['\"u'],
253 => ['\\\'y'],
254 => ['\thorn', 'wasysym'],
255 => ['\"y'],
}
ENTITY_CONV_TABLE.each {|k,v| ENTITY_CONV_TABLE[k] = v.unshift(v.shift + '{}')}
def convert_entity(el, opts)
text, package = ENTITY_CONV_TABLE[el.value.code_point]
if text
@doc.conversion_infos[:packages] << package if package
text
else
@doc.warnings << "Couldn't find entity in substitution table!"
''
end
end
TYPOGRAPHIC_SYMS = {
:mdash => '---', :ndash => '--', :hellip => '\ldots{}',
:laquo_space => '\guillemotleft{}~', :raquo_space => '~\guillemotright{}',
:laquo => '\guillemotleft{}', :raquo => '\guillemotright{}'
}
def convert_typographic_sym(el, opts)
TYPOGRAPHIC_SYMS[el.value]
end
SMART_QUOTE_SYMS = {:lsquo => '`', :rsquo => '\'', :ldquo => '``', :rdquo => '\'\''}
def convert_smart_quote(el, opts)
SMART_QUOTE_SYMS[el.value]
end
def convert_math(el, opts)
@doc.conversion_infos[:packages] += %w[amssymb amsmath amsthm amsfonts]
if el.options[:category] == :block
if el.value =~ /\A\s*\\begin\{/
el.value
else
latex_environment('displaymath', el, el.value)
end
else
"$#{el.value}$"
end
end
def convert_abbreviation(el, opts)
el.value
end
ESCAPE_MAP = {
"^" => "\\^{}",
"\\" => "\\textbackslash{}",
"~" => "\\ensuremath{\\sim}",
"|" => "\\textbar{}",
"<" => "\\textless{}",
">" => "\\textgreater{}"
}.merge(Hash[*("{}$%&_#".scan(/./).map {|c| [c, "\\#{c}"]}.flatten)])
ESCAPE_RE = Regexp.union(*ESCAPE_MAP.collect {|k,v| k})
def escape(str)
str.gsub(ESCAPE_RE) {|m| ESCAPE_MAP[m]}
end
def attribute_list(el)
attrs = (el.options[:attr] || {}).map {|k,v| v.nil? ? '' : " #{k}=\"#{v.to_s}\""}.compact.sort.join('')
attrs = " % #{attrs}" if !attrs.empty?
attrs
end
end
end
end