Sha256: 5f7b06fe8bb95d021e5fd832a5397c747f21cd402c66e7e8c6155f4b1a791c22

Contents?: true

Size: 1.81 KB

Versions: 2

Compression:

Stored size: 1.81 KB

Contents

require_relative "gemtext_refinements"

module Gmi2LaTeX
  class Renderer
    def initialize(document, header: nil, footer: nil)
      @document = document
      @header = header
      @footer = footer
    end

    using Gmi2LaTeX::GemtextRefinements

    def render
      @header and print @header
      @document.nodes.each do |node|
        render_node(node)
      end
      end_itemize
      @footer and print @footer
    end

    def render_node(node)
      case node
      in Gemtext::Heading[content:]
        end_itemize
        print "\\section{#{content}}\n"
      in Gemtext::SubHeading[content:]
        end_itemize
        print "\\subsection{#{content}}\n"
      in Gemtext::SubSubHeading[content:]
        end_itemize
        print "\\subsubsection{#{content}}\n"
      in Gemtext::Whitespace
        end_itemize
        print "\n"
      in Gemtext::Text[content:]
        end_itemize
        content.each_char do |char|
          case char
          in "_"
            print "\\_"
          else
            print char
          end
        end
        print "\n"
      in Gemtext::Link[target:, description:]
        end_itemize
        print "#{description} (\\url{#{target}})\n"
      in Gemtext::Preformatted[content:]
        end_itemize
        print <<~LaTeX
          \\begin{verbatim}
          #{content}
          \\end{verbatim}
        LaTeX
      in Gemtext::ListItem[content:]
        if @listing
          print <<~LaTeX
            \\item{}
            #{content}
          LaTeX
        else
          print <<~LaTeX
            \\begin{itemize}
            \\item{}
            #{content}
          LaTeX
          @listing = true
        end
      else
        raise node.inspect
      end
    end

    def end_itemize
      @listing or return
      @listing = false
      print "\\end{itemize}\n"
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
gmi2latex-0.1.1 lib/gmi2latex/renderer.rb
gmi2latex-0.1.0 lib/gmi2latex/renderer.rb