Class: Jekyll::Latex::Pdf::Latex

Inherits:
Document
  • Object
show all
Includes:
Utilities
Defined in:
lib/jekyll/latex/pdf/latex.rb

Overview

The Latex Class will create the tex file and compile it to the pdf file.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utilities

#nomarkdown, #nomarkdown_p, #run_cmds, #set_context_to

Constructor Details

#initialize(source, site, page, data, name) ⇒ Latex

Returns a new instance of Latex.



15
16
17
18
19
20
21
22
23
24
# File 'lib/jekyll/latex/pdf/latex.rb', line 15

def initialize(source, site, page, data, name)
  @kramdowndata = data
  @site = site
  @page = page
  @name = name
  @engine = @site.config["pdf"]["pdf_engine"] # @options.options[:pdf_engine]
  @bibengine = @site.config["pdf"]["bib_engine"] # @options.options[:bib_engine]

  @source = source
end

Instance Attribute Details

#latexObject

Returns the value of attribute latex.



13
14
15
# File 'lib/jekyll/latex/pdf/latex.rb', line 13

def latex
  @latex
end

#pdf_fileObject (readonly)

Returns the value of attribute pdf_file.



14
15
16
# File 'lib/jekyll/latex/pdf/latex.rb', line 14

def pdf_file
  @pdf_file
end

#sourceObject

Returns the value of attribute source.



13
14
15
# File 'lib/jekyll/latex/pdf/latex.rb', line 13

def source
  @source
end

Instance Method Details

#add_bibliographyObject



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/jekyll/latex/pdf/latex.rb', line 120

def add_bibliography
  if @kramdowndata.data.key? :bibtex_files
    @kramdowndata.data[:bibtex_files].each do |bibfile|
      bib_target = File.join(@tempdir, bibfile)
      if File.exist?(bib_target)
        unless FileUtils.compare_file(bibfile, bib_target)
          FileUtils.cp(bibfile, bib_target)
        end
      else
        FileUtils.mkdir_p File.dirname(bib_target)
        FileUtils.cp(bibfile, bib_target)
      end
    end
  end
end

#compileObject



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/jekyll/latex/pdf/latex.rb', line 150

def compile
  prepare
  status = 0
  if @no_need_to_compile.nil?
    cmds = prepare_cmds

    Jekyll.logger.debug "jekyll-latex-pdf", "compiling in tempdir: #{@tempdir}"

    _out, status = run_cmds cmds, @tempdir
  end

  if 0 == status
    @pdf_file = File.join(@tempdir, File.basename(@latexfile, ".*") + ".pdf")
  else
    Jekyll.logger.error "jekyll-latex-pdf",
                        "Error when trying to run #{@site.config['pdf']['pdf_engine']}."
    Jekyll.logger.error "jekyll-latex-pdf", "status: #{status}"
    Jekyll.logger.error "jekyll-latex-pdf",
                        "Hint: Try to run #{site.config['pdf']['pdf_engine']} \
      inside #{@tempdir} by hand."
  end
  status
end

#find_templateObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/jekyll/latex/pdf/latex.rb', line 26

def find_template
  template = @kramdowndata.data[:template]

  template.concat(".latex") unless template.end_with?(".latex")

  t = File.expand_path(File.join(@site.config["pdf"]["template_path"], template))
  if File.file? t
    return t
  else
    t = File.expand_path(File.join(@site.config["pdf"]["default_template_path"], template))
    if File.file? t
      return t
    else
      Jekyll.logger.error "jekyll-latex-pdf", "Could not find template #{t}."
    end
  end
end

#latex_same?(file, code) ⇒ Boolean

Returns:

  • (Boolean)


86
87
88
89
90
91
92
# File 'lib/jekyll/latex/pdf/latex.rb', line 86

def latex_same?(file, code)
  return false unless File.exist? file

  File.open(file, 'r') do |latexfile|
    latexfile.read == code
  end
end

#prepareObject



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/jekyll/latex/pdf/latex.rb', line 94

def prepare
  prepare_abstract
  prepare_date_as_note if @site.config["pdf"]["date_as_note"]
  prepare_tikz
  prepare_latex

  basename = File.basename(@name, ".*")
  @tempdir = File.join(Dir.pwd, @site.config["pdf"]["latex_cache_path"], "pdf", basename)
  FileUtils.mkdir_p @tempdir
  @latexfile = basename + ".tex"

  # stop here, if it does not need to compile
  latex_full_path = File.join(@tempdir, @latexfile)

  if latex_same? latex_full_path, latex
    @no_need_to_compile = true
    return
  end

  File.open(latex_full_path, "w:UTF-8") do |f|
    f.write(latex)
  end

  add_bibliography
end

#prepare_abstractObject



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/jekyll/latex/pdf/latex.rb', line 66

def prepare_abstract
  # only when excerpt is present
  unless @kramdowndata.data.key? :abstract
    Jekyll.logger.debug "Abstract missing. This may be ok for your latex template."
    if @kramdowndata.data.key? :excerpt
      Jekyll.logger.debug "Trying to get the abstract from the excerpt."
      abstract, _, @source = @source.to_s.partition(excerpt_separator)
      @kramdowndata.add(abstract: Kramdown::Document.new(abstract).to_latex)
     end
   end
end

#prepare_cmdsObject



136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/jekyll/latex/pdf/latex.rb', line 136

def prepare_cmds
  cmds = [[@site.config["pdf"]["pdf_engine"],
           "--output-format=pdf",
           "--interaction=batchmode",
           @latexfile]]
  # biber if bibfiles given
  if @kramdowndata.data.key? :bibtex_files
    cmds << [@site.config["pdf"]["bib_engine"],
             File.basename(@latexfile, File.extname(@latexfile))]
    cmds << cmds[0]
  end
  cmds
end

#prepare_date_as_noteObject



78
79
80
# File 'lib/jekyll/latex/pdf/latex.rb', line 78

def prepare_date_as_note
  @kramdowndata.add(note: "\\printdate{#{@kramdowndata.data[:date_str]}}")
end

#prepare_latexObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/jekyll/latex/pdf/latex.rb', line 44

def prepare_latex
  TempLiquid.run do
    info = {
      registers: {site: @site, page: @site.site_payload["page"]},
      strict_filters: true,
      strict_variables: true,
    }
    
    template = Liquid::Template.parse(@source)
    template.warnings.each do |e|
      Jekyll.logger.warn "Liquid Warning:",
                         LiquidRenderer.format_error(e, path || document.relative_path)
    end
    prep_latex = template.render!(@site.site_payload, info)
    

    options = {"data" => @kramdowndata.data,
               "template" => find_template}
    @latex = Kramdown::Document.new(prep_latex, options).to_latex
  end
end

#prepare_tikzObject



82
83
84
# File 'lib/jekyll/latex/pdf/latex.rb', line 82

def prepare_tikz
  @kramdowndata.add(tikzlibraries: Tikz::TikzLibraries.render)
end