# Encoding: UTF-8 # PublishR -- Fast publishing for ebooks and paper # Copyright (C) 2012 Michael Franzl # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as # published by the Free Software Foundation, either version 3 of the # License, or (at your option) any later version. # # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Affero General Public License for more details. # # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . module Publishr class LatexProcessor def initialize(latex,inpath) @lines = latex.split("\n") @line = '' @inpath = inpath @custom_fixes = File.open(File.join(@inpath,'latex_postprocessing.rb'), 'r').read if File.exists?(File.join(@inpath,'latex_postprocessing.rb')) end def fix processed_lines = [] @lines.each do |l| @line = l process_line processed_lines << @line end processed_lines.join("\n") end def process_line @line.gsub! /.hypertarget{.*?}{}/, '' @line.gsub! /.label{.*?}/, '' # latex uses eps, so remove jpg ending @line.gsub! /(.includegraphics{.*?).jpg}/, '\1}' # better formatting for names, book titles and technical terms @line.gsub! /name\((.*?)\)/, '\name{\1}' @line.gsub! /title\((.*?)\)/, '\book{\1}' @line.gsub! /{\\tt (.*?)}/, '\object{\1}' # covert kramdown css attributes to LaTeX environments @line.gsub! /{quot(.*?)} % class="(.*?)"/, '{quot\1\2}' @line.gsub! /{quote}/, '{quotenormal}' @line.gsub! /{quotation}/, '{quotationnormal}' # transform special kramdown comments to latex commands @line.gsub! /% (\\.*)/, '$1' # bind ellipsis to previous word @line.gsub! /(\w \\ldots{})/, '\\mbox{\1}' # set better space for [...] @line.gsub! /\[\\ldots{}\]/, '\\omission{}' # separate thousands, beginning with 10000 @line.gsub! /(\d\d)(\d\d\d) /, '\1\\,\2 ' @line.gsub! '°', '\\textdegree' eval(@custom_fixes, binding) if @custom_fixes # use correct hyphen code for better automatic hyphenation @line.gsub! /(\w)-(\w)/, '\1\\hyp{}\2' end end end