class Jekyll::Latex::Pdf::Latex

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

Attributes

latex[RW]
pdf_file[R]
source[RW]

Public Class Methods

new(source, site, page, data, name) click to toggle source
# 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

Public Instance Methods

add_bibliography() click to toggle source
# File lib/jekyll/latex/pdf/latex.rb, line 117
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
compile() click to toggle source
# File lib/jekyll/latex/pdf/latex.rb, line 147
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_template() click to toggle source
# 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) click to toggle source
# File lib/jekyll/latex/pdf/latex.rb, line 83
def latex_same?(file, code)
  return false unless File.exist? file

  File.open(file, 'r') do |latexfile|
    latexfile.read == code
  end
end
prepare() click to toggle source
# File lib/jekyll/latex/pdf/latex.rb, line 91
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_abstract() click to toggle source
# File lib/jekyll/latex/pdf/latex.rb, line 63
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_cmds() click to toggle source
# File lib/jekyll/latex/pdf/latex.rb, line 133
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_note() click to toggle source
# File lib/jekyll/latex/pdf/latex.rb, line 75
def prepare_date_as_note
  @kramdowndata.add(note: "\\printdate{#{@kramdowndata.data[:date_str]}}")
end
prepare_latex() click to toggle source
# 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,
    }

    prep_latex = Jekyll::Renderer.new(@site, @page).render_liquid(@source,
                                                                  @site.site_payload,
                                                                  info,
                                                                  @page.path)

    options = {"data" => @kramdowndata.data,
               "template" => find_template}
    @latex = Kramdown::Document.new(prep_latex, options).to_latex
  end
end
prepare_tikz() click to toggle source
# File lib/jekyll/latex/pdf/latex.rb, line 79
def prepare_tikz
  @kramdowndata.add(tikzlibraries: Tikz::TikzLibraries.render)
end