Class: Publishr::LatexProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/publishr/latex_processor.rb

Instance Method Summary collapse

Constructor Details

#initialize(latex, inpath, file) ⇒ LatexProcessor

Returns a new instance of LatexProcessor.



22
23
24
25
26
27
28
29
30
# File 'lib/publishr/latex_processor.rb', line 22

def initialize(latex,inpath,file)
  @lines = latex.split("\n")
  @line = ''
  @inpath = inpath
  @file = file
  @imagewidth = nil

  @custom_fixes = File.open(File.join(@inpath,'latex_postprocessing.rb'), 'r').read if File.exists?(File.join(@inpath,'latex_postprocessing.rb'))
end

Instance Method Details

#fixObject



32
33
34
35
36
37
38
39
40
# File 'lib/publishr/latex_processor.rb', line 32

def fix
  processed_lines = []
  @lines.each do |l|
    @line = l
    process_line
    processed_lines << @line
  end
  processed_lines.join("\n")
end

#process_lineObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/publishr/latex_processor.rb', line 42

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}'

  # set special attributes to quotation like environments
  @line.gsub! /{quot(.*?)}   %  class="(.*?)"/, '{quot\1\2}'
  @line.gsub! /{quote}/,                        '{quotenormal}'
  @line.gsub! /{quotation}/,                    '{quotationnormal}'

  # remember image size for a figure
  width = @line.match(/begin{figure}   %.*width="(.*?)"/)
  @imagewidth = width[1] if width

  # set the width of includegraphics and forget last remembered width
  if @imagewidth and @line.include? 'includegraphics'
    @line.gsub!(/\\includegraphics{(.*?)}/){"\\includegraphics[width=#{ @imagewidth.to_f/100.0 }\\textwidth]{#{ $1 }}"}
    @imagewidth = nil
  end

  # set placement attributes to figure environment
  @line.gsub! /begin{figure}   %.*class="(.*?)".*/,'begin{figure}[\1]'

  # 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)([^\d])/, '\1\\,\2\3'

  # fix LaTeX quirk with certain quotes combined with ! or ?
  @line.gsub! '!', '!{}'
  @line.gsub! '?', '?{}'

  @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