Class: Jekyll::Latex::Pdf::Tikz::TikzHtml

Inherits:
Liquid::Block
  • Object
show all
Includes:
TikzUtils, Utilities
Defined in:
lib/jekyll/latex/pdf/tikz/html.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from TikzUtils

#parse_markup

Methods included from Utilities

#nomarkdown, #nomarkdown_p, #run_cmds, #set_context_to

Constructor Details

#initialize(tag_name, markup, tokens) ⇒ TikzHtml

Returns a new instance of TikzHtml.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/jekyll/latex/pdf/tikz/html.rb', line 38

def initialize(tag_name, markup, tokens)
  super
  parse_markup(markup)

  @header = <<-'END'
        \documentclass{standalone}
        \usepackage{tikz}
  END

  # @header += "\\usetikzlibrary{#{@params['library']}}\n" if @params.key?("library")
  @header += TikzLibraries.render

  @header += <<-'END'
        \begin{document}
        \begin{tikzpicture}
  END

  @footer = <<-'END'
        \end{tikzpicture}
        \end{document}
  END

  @config = Defaults.defaults.dup
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



36
37
38
# File 'lib/jekyll/latex/pdf/tikz/html.rb', line 36

def config
  @config
end

#siteObject (readonly)

Returns the value of attribute site.



36
37
38
# File 'lib/jekyll/latex/pdf/tikz/html.rb', line 36

def site
  @site
end

Instance Method Details

#render(context) ⇒ Object



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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/jekyll/latex/pdf/tikz/html.rb', line 63

def render(context)
  set_context_to context

  tikz_code = @header + super + @footer
  md5, old_md5 = false, false

  tmp_directory = File.join(Dir.pwd,
                            @site.config["pdf"]["latex_cache_path"],
                            "tikz",
                            File.basename(context["page"]["url"], ".*"))
  FileUtils.mkdir_p tmp_directory

  dest_directory = File.join(Dir.pwd, @site.config["pdf"]["tikz_path"], File.basename(context["page"]["url"], ".*"))
  FileUtils.mkdir_p dest_directory

  # use new filename if content has changed.
  if @site.config["pdf"]["tikz_md5"]
    # add it to file name.
    md5 = Digest::MD5.hexdigest(tikz_code).to_s

    md5_filename = File.join(tmp_directory, "md5.txt")

    old_md5 = File.open(md5_filename, 'r').read if File.exist? md5_filename
    old_filename = @file_name + "_#{old_md5}"

    unless old_md5.eql? md5
      FileUtils.rm_rf(Dir.glob(File.join(tmp_directory, "*")))
      svg_path = File.join(dest_directory, "#{old_filename}.svg")
      png_path = File.join(dest_directory, "#{old_filename}.png")
      FileUtils.rm(svg_path) if File.exist? svg_path
      FileUtils.rm(png_path) if File.exist? png_path
    end

    @file_name += "_#{md5}"

    File.open(md5_filename, 'w') {|file| file.write(md5.to_s) }
  end

  tex_path = File.join(tmp_directory, "#{@file_name}.tex")
  pdf_path = File.join(tmp_directory, "#{@file_name}.pdf")

  svg_path = File.join(dest_directory, "#{@file_name}.svg")
  png_path = File.join(dest_directory, "#{@file_name}.png")

  # if the file doesn't exist or the tikz code is not the same with the file, then compile the file
  if !File.exist?(tex_path) || !tikz_same?(tex_path, tikz_code) ||
      !File.exist?(svg_path) || !File.exist?(png_path)
    File.open(tex_path, 'w') {|file| file.write(tikz_code.to_s) }
    cmds = [[@site.config["pdf"]["pdf_engine"],
             "--interaction=batchmode",
             "--output-format=pdf", tex_path],
            ["pdf2svg", pdf_path, svg_path],
            ["gm", "convert", svg_path, "-quality", "90", png_path]]

    run_cmds cmds, tmp_directory
  end

  web_svg_path = File.join(@site.config["pdf"]["tikz_path"],
                           File.basename(context["page"]["url"], ".*"),
                           "#{@file_name}.svg")
  web_png_path = File.join(@site.config["pdf"]["tikz_path"],
                           File.basename(context["page"]["url"], ".*"),
                           "#{@file_name}.png")
  "\n<object data=\"/#{web_svg_path}\" type=\"image/svg+xml\">" \
    "<img src=\"/#{web_png_path}\" />" \
    "</object>\n"
end