Sha256: 722639b72db75a84ed26c403962829caf32f5aba5f25b3fc6ed39a528af0caa4

Contents?: true

Size: 1.91 KB

Versions: 1

Compression:

Stored size: 1.91 KB

Contents

require 'asciidoctor'
require 'asciidoctor/extensions'

# Map $ ... $ to \(..\) before
# running Asciidoctor, and map
# '\$' to 'DOLLOD'.  The latter
# will be mapped back to '$'
# for the HTML backend by the
# postprocessor in 'dollar.rb' and
# to '\$' by the postprocessor
# in 'escape_dollar.rb'
#
# The remaining substitutions will
# be eliminated when I edit the
# relevant source files on noteshare.

module Asciidoctor::LaTeX
  class TeXPreprocessor < Asciidoctor::Extensions::Preprocessor

    # Map $...$ to stem:[...]
    # TEX_DOLLAR_RX = /(^|\s|\()\$(.*?)\$($|\s|\)|,|\.)/
    # TEX_DOLLAR_SUB = '\1latexmath:[\2]\3'
    # TEX_DOLLAR_SUB = '\1\\\(\2\\\)\3'

    TEX_DOLLAR_RX = /\$(.*?)\$/
    TEX_DOLLAR_SUB = '\\\(\1\\\)'
    TEX_DOLLAR_SUB2 = '+\\\(\1\\\)+'


    def process document, reader
      return reader if reader.eof?
      replacement_lines = reader.read_lines.map do |line|
        # (line.include? '$') ? (line.gsub TEX_DOLLAR_RX, TEX_DOLLAR_SUB) : line
        if line.include? '<-->' and document.basebackend? 'tex'
          line = line.gsub('<-->', 'CHEMLEFTRIGHTARROW')
        end
        if line.include? '->' and document.basebackend? 'tex'
          line = line.gsub('->', 'CHEMRIGHTARROW')
        end
        if line.include? '<-' and document.basebackend? 'tex'
          line = line.gsub('<-', 'CHEMLEFTARROW')
        end
        if line.include? '\$' and document.basebackend? 'html'
          line = line.gsub '\$', 'DOLLOD'
        end
        if line.include? '%' and document.basebackend? 'tex'
          line = line.gsub '%', '\%'
        end
        if line.include? '$'
          line = line.gsub TEX_DOLLAR_RX, TEX_DOLLAR_SUB2
        end
        if line.include? '\\['
          line = line.gsub '\\[', '+\\['
        end
        if line.include? '\\]'
          line = line.gsub '\\]', '\\]+'
        end
        line
      end
      reader.unshift_lines replacement_lines
      reader
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
asciidoctor-latex-1.5.0.dev lib/asciidoctor/latex/tex_preprocessor.rb