class BayeuxLatexGen

Public Class Methods

new(syntax_tree) click to toggle source

Default Constructor

# File lib/bayeux/latex_gen.rb, line 4
def initialize(syntax_tree)
  @syntax_tree = syntax_tree
  @lines = Array.new
  @text = String.new
end

Public Instance Methods

generate() click to toggle source

Return the AST as an HTML string (stored in @lines)

# File lib/bayeux/latex_gen.rb, line 11
def generate
  
  # Create a walker for the trees in the forest
  walker = TreeWalker.new
  
  walker.on_before_down = self.method(:generate_node_start)
  walker.on_after_up = self.method(:generate_node_end)
  
  walker.on_no_siblings = self.method(:generate_full_node)
  walker.on_no_children = self.method(:generate_full_node)
      
  # Clear the internal LaTeX replacement
  @lines.clear
  
  # Walk the forest
  @syntax_tree.block_forest.each{|tree|
    walker.walk_tree(tree)
  }

  #puts @syntax_tree.to_s
  #puts @lines
  
  latex_string = String.new
  @lines.each{|line|
    unless (line.empty? or (/\S/ !~ line)) then
    #unless line.empty? then
      latex_string << line + "\n" 
    end
  }
          
  return latex_string
end
generate_full_node(block) click to toggle source

Output the full contents of the node, properly bracketed as an LaTeX expression. This is only called if we have no sub-nodes to deal with

# File lib/bayeux/latex_gen.rb, line 52
def generate_full_node(block)
  
  # Check if we need to add a space
  # Check if we need to add a space
  if not block.content[0] =~ /\s|[:;.,?!]/
    @lines << " "
  end
              
  case block.type
    
    # Headers
    when :h1
      @lines << "\\chapter{#{block.content}}\n"
    when :h2
      @lines << "\\section{#{block.content}}\n"
    when :h3
      @lines << "\\subsection{#{block.content}}\n"
    when :h4
      @lines << "\\subsubsection{#{block.content}}\n"
    when :h5
      @lines << "\\paragraph{#{block.content}}\n"
    when :h5
      @lines << "\\subparagraph{#{block.content}}\n"
    
    # Ordinary paragraphs
    when :paragraph
      @lines << "#{typeset(block.content)}\n"

    when :none
      @text << "#{block.content}"
      
    # Special paragraphs
    when :block_quote
      @lines << "\\begin{quotation}" << "#{typeset(block.content)}" << "\\end{quotation}\n"  
    when :single_quote
      @lines << "'#{typeset(block.content)}'"
    when :double_quote
      @lines << "\"#{typeset(block.content)}\""
      
    when :note
      @lines << "\\textbf{Note:}~#{typeset(block.content)}\n"
      
    when :command
      @lines << "\\begin{Verbatim}[frame=lines,rulecolor=\\color{highlight}]" << "#{block.content.strip.indent(3)}" << "\\end{Verbatim}\n"
      
    when :code_language
      @code_language = block.content
    when :code_start_number
      @code_start_number = block.content  
      
    when :file
      @lines << "<pre class=\"file\">#{block.content}</pre>"
    when :output
      @lines << "\\crule" << "\\begin{verbatim}" << "#{block.content.strip}" << "\\end{verbatim}" << "\\crule\n"
      
    # Special Characters
    when :em_dash
      @text << "---"
    when :en_dash
      @text << "--"
    when :elipses
      @text << "\\ldots"
    when :elipses_stop
      @text << "\\dots."
      
    # Links
    when :link_target
      @link_target = block.content
    when :link_text
      @link_text = block.content
      
    # Lists
    when :item
      @lines << "\\item #{typeset(block.content)}"
    
    when :dl_header
      @lines << "\\item[#{block.content}]"
    when :dl_text
      @lines << "#{block.content}"
            
    # Tags
    when :ac
      @text << "\\ac{#{block.content}}"
          
    when :emph 
      @text << "\\emph{#{block.content}}"
    
    when :tt
      @text << "\\texttt{#{block.content}}"
      
  end
end
generate_node_end(block) click to toggle source
# File lib/bayeux/latex_gen.rb, line 240
def generate_node_end(block)
      
  case block.type
    
    # Headers
    when :h1, :h2, :h3, :h4, :h5, :h6
      @lines << "#{typeset(@text)}}\n"
      @text = ""
    
    # Ordinary paragraphs
    when :paragraph
      @lines << "#{typeset(@text)}\n"
      @text = ""
    
    # Special paragraphs
    when :block_quote
      @lines << "\\end{quotation}\n"
    when :single_quote
      @lines << "'"
    when :double_quote
      @lines << "\""
      
    when :note
      @lines << ""
      
    when :command
      @lines << "\\end{Verbatim}\n"
      
    when :code
      # Assemble the code block from the sub-tree nodes
      begin
        pretty_code = Uv.parse(block.content, "xhtml", @code_language, @code_start_number.to_i, "dawn")
        @lines << pretty_code
      rescue
        pretty_code = Uv.parse(block.content, "xhtml", "plain_text", @code_start_number.to_i, "dawn")
        @lines << pretty_code          
      end
      
    # Links
    when :link
      # Assemble the link from the sub-tree nodes
      @lines << "\\href{#{@link_target}\"}{#{@link_text}}"
    when :link_target
      @link_target = block.content
    when :link_text
      @link_text = block.content
        
    # Lists
    when :ol
      @lines << "\\end{enumerate}" 
    when :ul
      @lines << "\\end{itemize}" 
    when :item
      @lines << "\\item #{typeset(@text)}"
      @text = ""
    
    when :dl
      @lines << "" 
    when :dl_header
      @lines << "]"
    when :dl_text
      @lines << ""
    
    # Tags
    when :ac, :tt, :strong
      @lines << "}"
    
    when :emph
      @lines << "\\/}"

  end
end
generate_node_start(block) click to toggle source

Output only the start of a node

# File lib/bayeux/latex_gen.rb, line 146
def generate_node_start(block)
  
  # Check if we need to add a space
  if not block.content[0] =~ /\s|[:;.,?!]/
    @lines << " "
  end
  
  case block.type
    
    # Headers
    when :h1
      @text << "\\chapter{#{block.content}"
    when :h2
      @text << "\\section{#{block.content}"
    when :h3
      @text << "\\subsection{#{block.content}"
    when :h4
      @text << "\\subsubsection{#{block.content}"
    when :h5
      @text << "\\paragraph{#{block.content}"
    when :h5
      @text << "\\subparagraph{#{block.content}"
    
    # Ordinary paragraphs
    when :paragraph
      @text << "#{block.content}"
      
    when :none
      @text << "#{block.content}"
      
    # Special paragraphs
    when :block_quote
      @lines << "<blockquote>#{typeset(block.content)}"
    when :single_quote
      @text << "'#{block.content}"
    when :double_quote
      @text << "\"{block.content}"
      
    when :note
      @lines << "\\textbf{Note:}~#{typeset(block.content)}\n"
      
    when :command
      @lines << "\\begin{Verbatim}[frame=lines,rulecolor=\\color{highlight}]" << "#{typeset(block.content)}"
      
    when :code_language
      @code_language = block.content
    when :code_start_number
      @code_start_number = block.content
      
    # Special Characters
    when :em_dash
      @text << "---"
    when :en_dash
      @text << "--"
    when :elipses
      @text << "\ldots"
    when :elipses_stop
      @text << "\dots."
      
    # Links
    when :link_target
      @link_target = block.content
    when :link_text
      @link_text = block.content
      
    # Lists
    when :ol
      @lines << "\\begin{enumerate}" 
    when :ul
      @lines << "\\begin{itemize}" 
            
    when :dl
      @lines << "\\begin{description}" 
    when :dl_header
      @lines << "\\item[#{block.content}"
    when :dl_text
      @lines << "#{block.content}"
    
    # Tags
    when :ac
      @text << "\\ac{#{block.content}}"
              
    when :emph 
      @text << "\\emph{#{block.content}\\/}"
      
    when :strong
      @text << "\\texttt{#{block.content}\\/}"
      
    when :tt
      @text << "\\texttt{#{block.content}}"
      
  end
end
typeset(string) click to toggle source

Fix Typography, according to HTML standards

# File lib/bayeux/latex_gen.rb, line 319
def typeset(string)
  unless string.nil? then
    return_str = string
 
    return_str = return_str.strip
    return_str = return_str.squeeze(" ")
    return_str = return_str.word_wrap(70)

    return return_str.rstrip
  else
    return ""
  end
end