class OctopressTex2img::Tags

Public Class Methods

new(tag_name, markup, tokens) click to toggle source
Calls superclass method
# File lib/octopress-tex2img/tags.rb, line 10
def initialize(tag_name, markup, tokens)
  super
  @texpath, *params = markup.strip.split(/\s/)
  @imgtag = Octopress::Tags::ImageTag::Tag.new(tag_name, params.join(" "), tokens)
end

Public Instance Methods

render(context) click to toggle source
# File lib/octopress-tex2img/tags.rb, line 16
def render(context)
  attributes = @imgtag.image(context)
  site = context.registers[:site]
  pngpath = File.join(site.source, attributes['src'])
  @texpath = File.join(site.source, @texpath)
  if !File.exists?(@texpath)
    raise ArgumentError.new("texfile[#{@texpath}] does not exist!")
  end

  if !File.exists?(pngpath) || timestamp(@texpath) > File.stat(pngpath).mtime
    puts "Generating #{pngpath} from #{@texpath}"
    texdir = File.dirname(@texpath)
    pngdir = File.dirname(pngpath)
    FileUtils.mkdir_p(pngdir)
    base = @texpath.sub(/.tex$/, '')
    pdf = @texpath.sub(/.tex$/, '.pdf')
    system "xelatex -output-directory=#{texdir} --interaction=nonstopmode #{base} >/dev/null"
    system "convert -density 300 #{pdf} -quality 90 #{pngpath}"
    if File.exists?(pngpath)
      site.static_files << Jekyll::StaticFile.new(site,
              site.source, File.dirname(attributes['src']),
              File.basename(pngpath))
    end

    FileUtils.rm_f("#{base}.aux")
    FileUtils.rm_f("#{base}.log")
    FileUtils.rm_f("#{base}.pdf")
  end
  @imgtag.render(context)
end
timestamp(texpath) click to toggle source
# File lib/octopress-tex2img/tags.rb, line 47
def timestamp(texpath)
  ts = File.stat(texpath).mtime
  File.open(texpath, 'r') do |ff|
    while ll = ff.gets
      if /\\input{(?<inputfile>.*)}/ =~ ll
        tn = File.stat(File.join(File.dirname(texpath), inputfile)).mtime
        if tn > ts
          ts = tn
        end
      end
    end
  end
  ts
end