Class: Octopress::Printable::Plugin

Inherits:
Ink::Plugin
  • Object
show all
Defined in:
lib/octopress-printable/plugin.rb

Instance Method Summary collapse

Instance Method Details

#gen_pdf(mdfile, pdffile, source_dir, posts_dir, blog_url, bib_dir, bib_file, post_url) ⇒ Object



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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/octopress-printable/plugin.rb', line 66

def gen_pdf(mdfile, pdffile, source_dir, posts_dir, blog_url,
        bib_dir, bib_file, post_url)
  pdfdir = File.dirname(pdffile)
  if ! File.exists?(pdfdir)
    FileUtils.mkdir_p pdfdir
  end

  converters = []
  math = MathConverter.new
  converters << math
  img = ImgConverter.new(source_dir)
  converters << img
  tex2img = Tex2imgConverter.new(source_dir)
  converters << tex2img
  bib = BibConverter.new("#{source_dir}/#{bib_dir}/#{bib_file}",
          "#{pdfdir}/#{bib_file}")
  converters << bib
  gist = GistConverter.new
  converters << gist
  post_link = PostLinkConverter.new(source_dir, posts_dir, blog_url)
  converters << post_link
  latex = LatexConverter.new
  converters << latex

  tmpfile="#{pdffile}.markdown"
  comment = false
  File.open(tmpfile, 'w') do |post|
    File.open(mdfile, "r") do |file|  
      while line = file.gets
        line = line.strip

        if /^{% comment %}$/ =~ line
          comment = true
          next
        end

        if /^{% endcomment %}$/ =~ line
          comment = false
          next
        end

        if comment
          next
        end

        line = line.gsub(/{% comment %} (.*?) {% endcomment %}/, "")

        line = line.gsub(/\* list element with functor item/, '')
        line = line.gsub(/{:toc}/, '\tableofcontents')

        line = line.sub(/^#(#*)/, '\1')

        for converter in converters
          line = converter.convert(line)
        end

        post.puts line
      end  
    end

    url = "#{blog_url}/#{post_url}".gsub(/([^:])\/\/+/, '\1/')
    post.puts "\\renewcommand{\\thefootnote}{}\\footnotetext{Online version at \\url{#{url}}}"
  end 

  cts = []
  for converter in converters
    if converter.match
      cts << converter
    end
  end
  converters = cts

  texfile = pdffile.sub(/.pdf$/, '.tex')
  base = pdffile.sub(/.pdf$/, '')
  pkgfile = "#{pdffile}.header.tex"

  File.open(pkgfile, "w") { |f|
    for converter in converters
      converter.header.each do |h|
        f.puts h
      end
    end
  }

  cmds = []
  args = []
  for converter in converters
    args += converter.pandoc_args
  end

  cmds << "pandoc -s -N #{args.join(" ")} --include-in-header=#{pkgfile} #{tmpfile} -o #{texfile} " 

  (1..2).each do |step|
    args = []
    extra_cmds = []
    for converter in converters
      extra_cmds += converter.before_xelatex(step, texfile)
    end
    for cmd in extra_cmds
      cmds << cmd
    end

    for converter in converters
      args += converter.xelatex_args(step)
    end
	  cmds << "xelatex #{args.join(" ")} -output-directory=#{pdfdir} -no-pdf --interaction=nonstopmode #{base} >/dev/null"
  end

  extra_cmds = []
  for converter in converters
    extra_cmds += converter.before_xelatex(3, texfile)
  end
  for cmd in extra_cmds
    cmds << cmd
  end
	cmds << "xelatex #{args.join(" ")} -output-directory=#{pdfdir} --interaction=nonstopmode #{base} >/dev/null"

  extra_cmds = []
  for converter in converters
    extra_cmds += converter.last_xelatex(texfile)
  end
  for cmd in extra_cmds
    cmds << cmd
  end

  envs = []
  for converter in converters
    envs += converter.envs
  end
  File.open("#{base}.sh", 'w') { |f|
    f.write(envs.join("\n"))
    f.write("\n")
    f.write(cmds.join("\n"))
  }
  FileUtils.chmod(0755, "#{base}.sh")

  if ! @conf['dry_run']
    system "#{base}.sh"
  end

  if ! @conf['dump_cmds']
	  FileUtils.rm_f("#{base}.sh")
  end

  if File.exists?("#{File.basename(base)}.pyg")
    FileUtils.mv("#{File.basename(base)}.pyg", pdfdir)
  end

  if !@conf['keep_tmp_files']
	  FileUtils.rm_f("#{base}.aux")
	  FileUtils.rm_f("#{base}.log")
	  FileUtils.rm_f("#{base}.lot")
	  FileUtils.rm_f("#{base}.out")
	  FileUtils.rm_f("#{base}.toc")
	  FileUtils.rm_f("#{base}.blg")
	  FileUtils.rm_f("#{base}.bbl")
	  FileUtils.rm_f("#{base}.lof")
	  FileUtils.rm_f("#{base}.xdv")
	  FileUtils.rm_f("#{base}.hst")
	  FileUtils.rm_f("#{base}.ver")
	  FileUtils.rm_f("#{base}.synctex.gz")
    FileUtils.rm_f("#{base}.pyg")
  end

  if !@conf['dump_tex_file']
    FileUtils.rm_f("#{texfile}")
    FileUtils.rm_f("#{pkgfile}")
  end

  if !@conf['dump_markdown_file']
    FileUtils.rm_f("#{tmpfile}")
  end

  if !@conf['dump_bib_file'] && bib.match
    bib.cleanup
  end
end

#generate(site, config) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/octopress-printable/plugin.rb', line 22

def generate(site, config)
  @conf = inject_configs

  posts_dir = @conf['posts_dir']
  printables_dir = @conf['printables_dir']
  source_dir = @conf['source_dir']
  blog_url = @conf['blog_url']
  bib_dir = @conf['bibliography_dir']
  bib = @conf['bibliography']

  if !File.exists?(printables_dir)
    FileUtils.mkdir_p(printables_dir)
  end

  site.posts.each do |p|
    post = p.path
    head = YAML.load_file(post)
    
    if head['noprintable']
      next
    end

    pdf = p.url.sub(/^\//, "")
    pdf = pdf.sub(/\/$/, "")
    pdf = pdf.gsub(/\//, "-")
    pdf = "#{printables_dir}/#{pdf}.pdf"

    if !File.exists?(pdf) || timestamp(post, source_dir) > File.stat(pdf).mtime
      puts "Converting #{post} to #{pdf}"
      gen_pdf(post, pdf, source_dir, posts_dir, blog_url,
              bib_dir, bib, p.url)
      if File.exists?(pdf)
        site.static_files << Jekyll::StaticFile.new(site,
                site.source, printables_dir, File.basename(pdf))
      end
    end
  end
end

#inject_configsObject



61
62
63
64
# File 'lib/octopress-printable/plugin.rb', line 61

def inject_configs
  @conf = self.config
  @conf = Jekyll::Utils.deep_merge_hashes(YAML.load(Config.default_config), @conf)
end

#registerObject



15
16
17
18
19
20
# File 'lib/octopress-printable/plugin.rb', line 15

def register
  super
  if Octopress::Ink.enabled?
    self.generate(Octopress.site, config)
  end
end

#timestamp(post, source_dir) ⇒ Object



244
245
246
247
248
249
250
251
252
253
254
# File 'lib/octopress-printable/plugin.rb', line 244

def timestamp(post, source_dir)
  ts = [ File.stat(post).mtime ]
  ts << MathConverter.timestamp(post, source_dir)
  ts << ImgConverter.timestamp(post, source_dir)
  ts << Tex2imgConverter.timestamp(post, source_dir)
  ts << BibConverter.timestamp(post, source_dir)
  ts << GistConverter.timestamp(post, source_dir)
  ts << PostLinkConverter.timestamp(post, source_dir)
  ts << LatexConverter.timestamp(post, source_dir)
  ts.max
end