Class SM::ToLaTeX
In: rdoc/markup/simple_markup/to_latex.rb
Parent: Object

Convert SimpleMarkup to basic LaTeX report format

Methods

Constants

BS = "\020"
CB = "\022"
DL = "\023"
BACKSLASH = "#{BS}symbol#{OB}92#{CB}"
HAT = "#{BS}symbol#{OB}94#{CB}"
BACKQUOTE = "#{BS}symbol#{OB}0#{CB}"
TILDE = "#{DL}#{BS}sim#{DL}"
LESSTHAN = "#{DL}<#{DL}"
GREATERTHAN = "#{DL}>#{DL}"
LIST_TYPE_TO_LATEX = { ListBase::BULLET => [ l("\\begin{itemize}"), l("\\end{itemize}") ], ListBase::NUMBER => [ l("\\begin{enumerate}"), l("\\end{enumerate}"), "\\arabic" ], ListBase::UPPERALPHA => [ l("\\begin{enumerate}"), l("\\end{enumerate}"), "\\Alph" ], ListBase::LOWERALPHA => [ l("\\begin{enumerate}"), l("\\end{enumerate}"), "\\alph" ], ListBase::LABELED => [ l("\\begin{description}"), l("\\end{description}") ], ListBase::NOTE => [ l("\\begin{tabularx}{\\linewidth}{@{} l X @{}}"), l("\\end{tabularx}") ], }
InlineTag = Struct.new(:bit, :on, :off)

Public Class methods

[Source]

# File rdoc/markup/simple_markup/to_latex.rb, line 24
    def self.l(str)
      str.tr('\\', BS).tr('{', OB).tr('}', CB).tr('$', DL)
    end

[Source]

# File rdoc/markup/simple_markup/to_latex.rb, line 45
    def initialize
      init_tags
      @list_depth = 0
      @prev_list_types = []
    end

Public Instance methods

[Source]

# File rdoc/markup/simple_markup/to_latex.rb, line 139
    def accept_blank_line(am, fragment)
      # @res << "\n"
    end

[Source]

# File rdoc/markup/simple_markup/to_latex.rb, line 143
    def accept_heading(am, fragment)
      @res << convert_heading(fragment.head_level, am.flow(fragment.txt))
    end

[Source]

# File rdoc/markup/simple_markup/to_latex.rb, line 123
    def accept_list_end(am, fragment)
      if tag = @in_list_entry.pop
        @res << tag << "\n"
      end
      @res << list_name(fragment.type, false) <<"\n"
    end

[Source]

# File rdoc/markup/simple_markup/to_latex.rb, line 130
    def accept_list_item(am, fragment)
      if tag = @in_list_entry.last
        @res << tag << "\n"
      end
      @res << list_item_start(am, fragment)
      @res << wrap(convert_flow(am.flow(fragment.txt))) << "\n"
      @in_list_entry[-1] = list_end_for(fragment.type)
    end

[Source]

# File rdoc/markup/simple_markup/to_latex.rb, line 118
    def accept_list_start(am, fragment)
      @res << list_name(fragment.type, true) <<"\n"
      @in_list_entry.push false
    end

[Source]

# File rdoc/markup/simple_markup/to_latex.rb, line 101
    def accept_paragraph(am, fragment)
      @res << wrap(convert_flow(am.flow(fragment.txt)))
      @res << "\n"
    end

[Source]

# File rdoc/markup/simple_markup/to_latex.rb, line 112
    def accept_rule(am, fragment)
      size = fragment.param
      size = 10 if size > 10
      @res << "\n\n\\rule{\\linewidth}{#{size}pt}\n\n"
    end

[Source]

# File rdoc/markup/simple_markup/to_latex.rb, line 106
    def accept_verbatim(am, fragment)
      @res << "\n\\begin{code}\n"
      @res << fragment.txt.sub(/[\n\s]+\Z/, '')
      @res << "\n\\end{code}\n\n"
    end

Add a new set of LaTeX tags for an attribute. We allow separate start and end tags for flexibility

[Source]

# File rdoc/markup/simple_markup/to_latex.rb, line 84
    def add_tag(name, start, stop)
      @attr_tags << InlineTag.new(SM::Attribute.bitmap_for(name), start, stop)
    end

[Source]

# File rdoc/markup/simple_markup/to_latex.rb, line 97
    def end_accepting
      @res.tr(BS, '\\').tr(OB, '{').tr(CB, '}').tr(DL, '$')
    end

Escape a LaTeX string

[Source]

# File rdoc/markup/simple_markup/to_latex.rb, line 64
    def escape(str)
# $stderr.print "FE: ", str
      s = str.
#        sub(/\s+$/, '').
        gsub(/([_\${}&%#])/, "#{BS}\\1").
        gsub(/\\/, BACKSLASH).
        gsub(/\^/, HAT).
        gsub(/~/,  TILDE).
        gsub(/</,  LESSTHAN).
        gsub(/>/,  GREATERTHAN).
        gsub(/,,/, ",{},").
        gsub(/\`/,  BACKQUOTE)
# $stderr.print "-> ", s, "\n"
      s
    end

Set up the standard mapping of attributes to LaTeX

[Source]

# File rdoc/markup/simple_markup/to_latex.rb, line 54
    def init_tags
      @attr_tags = [
        InlineTag.new(SM::Attribute.bitmap_for(:BOLD), l("\\textbf{"), l("}")),
        InlineTag.new(SM::Attribute.bitmap_for(:TT),   l("\\texttt{"), l("}")),
        InlineTag.new(SM::Attribute.bitmap_for(:EM),   l("\\emph{"), l("}")),
      ]
    end

[Source]

# File rdoc/markup/simple_markup/to_latex.rb, line 28
    def l(arg)
      SM::ToLaTeX.l(arg)
    end

Here‘s the client side of the visitor pattern

[Source]

# File rdoc/markup/simple_markup/to_latex.rb, line 92
    def start_accepting
      @res = ""
      @in_list_entry = []
    end

This is a higher speed (if messier) version of wrap

[Source]

# File rdoc/markup/simple_markup/to_latex.rb, line 149
    def wrap(txt, line_len = 76)
      res = ""
      sp = 0
      ep = txt.length
      while sp < ep
        # scan back for a space
        p = sp + line_len - 1
        if p >= ep
          p = ep
        else
          while p > sp and txt[p] != ?\s
            p -= 1
          end
          if p <= sp
            p = sp + line_len
            while p < ep and txt[p] != ?\s
              p += 1
            end
          end
        end
        res << txt[sp...p] << "\n"
        sp = p
        sp += 1 while sp < ep and txt[sp] == ?\s
      end
      res
    end

[Validate]

ruby-doc.org is a community service provided by Happy Camper Studios, a Phoenix, Arizona, Ruby application development company.

Documentation content on ruby-doc.org is provided by remarkable members of the Ruby community.

For more information on the Ruby programming language, visit ruby-lang.org.

Want to help improve Ruby's API docs? See Ruby Documentation Guidelines.