Class: Octopress::Printable::Tex2imgConverter

Inherits:
Converter
  • Object
show all
Defined in:
lib/octopress-printable/tex2img.rb

Constant Summary collapse

TITLE_REGEX =
/title:['|"](.+?)['|"]/

Instance Attribute Summary

Attributes inherited from Converter

#match

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Converter

#before_xelatex, #last_xelatex, #pandoc_args, timestamp, #xelatex_args

Constructor Details

#initialize(source_dir) ⇒ Tex2imgConverter

Returns a new instance of Tex2imgConverter.



9
10
11
12
13
14
15
# File 'lib/octopress-printable/tex2img.rb', line 9

def initialize(source_dir)
  super()

  @source_dir = source_dir
  @headers = []
  @inputs = []
end

Class Method Details

.get_includes(post, source_dir) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/octopress-printable/tex2img.rb', line 60

def self.get_includes(post, source_dir)
  includes = []
  File.open(post, 'r') do |f|
    while l = f.gets
      if /{% tex2img (?<markup>.*) %}/ =~ l
        texpath, *params = markup.strip.split(/\s/)
        texpath = File.join(source_dir, texpath)
        if !File.exists?(texpath)
          next
        end
        includes << texpath

        File.open(texpath, 'r') do |ff|
          while ll = ff.gets
            if /\\input{(?<inputfile>.*)}/ =~ ll
              includes << File.join(File.dirname(texpath), inputfile)
            end
          end
        end
      end
    end
  end
  includes
end

Instance Method Details

#add_envs(texpath) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/octopress-printable/tex2img.rb', line 46

def add_envs(texpath)
  File.open(texpath, 'r') do |ff|
    while ll = ff.gets
      if /\\input{(?<inputfile>.*)}/ =~ ll
         @inputs << File.dirname(texpath)
      end
    end
  end
end

#convert(line) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/octopress-printable/tex2img.rb', line 17

def convert(line)
  str = line

  if /{% tex2img (?<markup>.*) %}/ =~ str
    @match = true

    texpath, *params = markup.strip.split(/\s/)
    texpath = File.join(@source_dir, texpath)
    if !File.exists?(texpath)
      raise ArgumentError.new("texfile[#{texpath}] does not exist!")
    end

    img = Octopress::Printable::ImgConverter.get_img_label(params.join(" "))
    str="\\begin{figure}[h]\\centering\\input{#{texpath}}\\caption{#{img['title']}}\\label{#{img['alt']}}\\end{figure}"

    @headers += open(texpath) { |f| f.grep(/\\usepackage/) }

    add_envs(texpath)
  end
  str
end

#envsObject



56
57
58
# File 'lib/octopress-printable/tex2img.rb', line 56

def envs
  ["export TEXINPUTS+='#{@inputs.join(':')}:'"]
end

#headerObject



39
40
41
42
43
44
# File 'lib/octopress-printable/tex2img.rb', line 39

def header
  @headers << '\\usepackage{standalone}'
  @headers << '\\usepackage{tikz}'
  @headers.each {|a| a.strip!if a.respond_to? :strip! }
  @headers.uniq!
end