lib/polytexnic/preprocessors/polytex.rb
# Converts Markdown to PolyTeX.
# We adopt a unified approach: rather than convert "Markdown" (I use
# the term loosely*) directly to HTML, we convert it to PolyTeX and
# then run everything through the PolyTeX pipeline. Happily, kramdown
# comes equipped with a `to_latex` method that does most of the heavy
# lifting. The ouput isn't as clean as that produced by Pandoc (our
# previous choice), but it comes with significant advantages: (1) It's
# written in Ruby, available as a gem, so its use eliminates an external
# dependency. (2) It's the foundation for the "Markdown" interpreter
# used by Leanpub, so by using it ourselves we ensure greater
# compatibility with Leanpub books.
#
# * <rant>The number of mutually incompatible markup languages going
# by the name "Markdown" is truly mind-boggling. Most of them add things
# to John Gruber's original Markdown language in an ever-expanding
# attempt to bolt on the functionality needed to write longer documents.
# At this point, I fear that "Markdown" has become little more than a
# marketing term.</rant>
Converts Markdown to PolyTeX.
We adopt a unified approach: rather than convert "Markdown" (I use
the term loosely*) directly to HTML, we convert it to PolyTeX and
then run everything through the PolyTeX pipeline. Happily, kramdown
comes equipped with a `to_latex` method that does most of the heavy
lifting. The ouput isn't as clean as that produced by Pandoc (our
previous choice), but it comes with significant advantages: (1) It's
written in Ruby, available as a gem, so its use eliminates an external
dependency. (2) It's the foundation for the "Markdown" interpreter
used by Leanpub, so by using it ourselves we ensure greater
compatibility with Leanpub books.

The number of mutually incompatible markup languages going
by the name "Markdown" is truly mind-boggling. Most of them add things
to John Gruber's original Markdown language in an ever-expanding
attempt to bolt on the functionality needed to write longer documents.
At this point, I fear that "Markdown" has become little more than a
marketing term.
Converts Markdown to PolyTeX . We adopt a unified approach : rather than convert " Markdown " ( I use the term loosely * ) directly to HTML we convert it to PolyTeX and then run everything through the PolyTeX pipeline . Happily kramdown comes equipped with a ` to_latex ` method that does most of the heavy lifting . The ouput isn ' t as clean as that produced by Pandoc ( our previous choice ) but it comes with significant advantages : ( 1 ) It ' s written in Ruby available as a gem so its use eliminates an external dependency . ( 2 ) It ' s the foundation for the " Markdown " interpreter used by Leanpub so by using it ourselves we ensure greater compatibility with Leanpub books . The number of mutually incompatible markup languages going by the name " Markdown " is truly mind - boggling . Most of them add things to John Gruber ' s original Markdown language in an ever - expanding attempt to bolt on the functionality needed to write longer documents . At this point I fear that " Markdown " has become little more than a marketing term .
def to_polytex
        math_cache = {}
        cleaned_markdown = cache_code_environments(@source)
        expand_input!(cleaned_markdown,
                      Proc.new { |source| cache_code_environments(source) })
        puts cleaned_markdown if debug?
        cleaned_markdown.tap do |markdown|
          convert_code_inclusion(markdown)
          cache_latex_literal(markdown)
          cache_raw_latex(markdown)
          cache_image_locations(markdown)
          puts markdown if debug?
          cache_math(markdown, math_cache)
        end
        puts cleaned_markdown if debug?
        lh = 'chapter,section,subsection,subsubsection,paragraph,subparagraph'
        kramdown = Kramdown::Document.new(cleaned_markdown, latex_headers: lh)
        puts kramdown.inspect if debug?
        puts kramdown.to_html if debug?
        puts kramdown.to_latex if debug?
        @source = kramdown.to_latex.tap do |polytex|
                    remove_kramdown_comments(polytex)
                    convert_includegraphics(polytex)
                    restore_math(polytex, math_cache)
                    restore_hashed_content(polytex)
                  end
      end
def to_polytex math_cache = { } cleaned_markdown = cache_code_environments ( @source ) expand_input! ( cleaned_markdown , Proc . new { | source | cache_code_environments ( source ) } ) puts cleaned_markdown if debug? cleaned_markdown . tap do | markdown | convert_code_inclusion ( markdown ) cache_latex_literal ( markdown ) cache_raw_latex ( markdown ) cache_image_locations ( markdown ) puts markdown if debug? cache_math ( markdown , math_cache ) end puts cleaned_markdown if debug? lh = 'chapter,section,subsection,subsubsection,paragraph,subparagraph' kramdown = Kramdown :: Document . new ( cleaned_markdown , latex_headers : lh ) puts kramdown . inspect if debug? puts kramdown . to_html if debug? puts kramdown . to_latex if debug? @source = kramdown . to_latex . tap do | polytex | remove_kramdown_comments ( polytex ) convert_includegraphics ( polytex ) restore_math ( polytex , math_cache ) restore_hashed_content ( polytex ) end end
Converts Markdown to PolyTeX.
Converts Markdown to PolyTeX .
# Override the header ordering, which starts with 'section' by default.