Class: JekyllLocalDiagram::JekyllLocalDiagramBlock

Inherits:
Liquid::Block
  • Object
show all
Defined in:
lib/jekyll-local-diagram.rb

Direct Known Subclasses

MathJaxBlock, MermaidBlock, PlantumlBlock

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, markup, tokens) ⇒ JekyllLocalDiagramBlock

Returns a new instance of JekyllLocalDiagramBlock.



10
11
12
13
14
15
# File 'lib/jekyll-local-diagram.rb', line 10

def initialize(tag_name, markup, tokens)
  super
  @html = (markup or '').strip
  @logger = Logger.new(STDOUT)
  @logger.level = Logger::DEBUG
end

Instance Method Details

#render(context) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/jekyll-local-diagram.rb', line 17

def render(context)
    site = context.registers[:site]
    name = Digest::MD5.hexdigest(super)
    path = File.join('assets', 'images', @ext)
    type = 'svg'
    mimetype = 'svg+xml'
    imgfile = "#{name}.#{type}"
    imgpath = File.join(site.source, path)
    if !File.exists?(File.join(imgpath, imgfile))
      txtfile = File.join(imgpath, "#{name}.#{@ext}")
      img = File.join(imgpath, imgfile)
      if File.exists?(img)
        @logger.debug("File #{imgfile} already exists (#{File.size(img)} bytes)")
      else
        FileUtils.mkdir_p(imgpath)
        File.open(txtfile, 'w') { |f|
          f.write(super)
        }
        cmd = build_cmd(txtfile, img)
        @logger.debug("Executing #{@blockclass} command: #{cmd}")
        system(cmd) or raise "#{@blockclass} error: #{super}"
        site.static_files << Jekyll::StaticFile.new(
          site, site.source, path, imgfile
        )
        @logger.debug("File #{imgfile} created (#{File.size(img)} bytes)")
      end
    end
    "<p><object data='#{site.baseurl}/#{path}/#{imgfile}' type='image/#{mimetype}' #{@html} class='#{@blockclass}'></object></p>"
end