rails-latex 2.0.1 → 2.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/examples/rails-latex-demo/app/views/latex_example/index.html.erb +1 -2
- data/examples/rails-latex-demo/app/views/latex_example/index.pdf.erb +0 -1
- data/examples/rails-latex-demo/app/views/layouts/application.pdf.erbtex +27 -3
- data/examples/rails-latex-demo/app/views/layouts/barcode.pdf.erbtex +0 -1
- data/lib/rails-latex/erb_latex.rb +1 -1
- data/lib/rails-latex/latex_to_pdf.rb +73 -32
- data/lib/rails-latex/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5309d586b2ef3b9af95eb0c66e120d4c523773b6
|
4
|
+
data.tar.gz: 93a2ad21871df64a11416a0a26bebae2d059fda2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f942c7f3a80b5e93e4b064bfbbe1adf796a1022df665e2a1b024f482757ea84edad578b01d8402dc3e6f9c9c3745880c647c8d550cb3a3c13f3fd23ee44b3acd
|
7
|
+
data.tar.gz: c8680b6ced9aa002404e4a6969c8c88d23e29e7147041ab1a678da4123d8a35bb0663d99bbbc13141d6e886a6c8adf2afd4cf92f51356eafc8e91aecc2e40711
|
@@ -1,11 +1,10 @@
|
|
1
1
|
<h1>LatexExample#index</h1>
|
2
2
|
<ul>
|
3
|
-
<li
|
3
|
+
<li><%= link_to "print", latex_example_path(:format => :pdf) %></li>
|
4
4
|
|
5
5
|
<li><%= link_to "print barcode", latex_example_barcode_path(:format => :pdf) %></li>
|
6
6
|
|
7
7
|
<li><%= link_to "print barcode - with no '.pdf' in url", latex_example_barcode_path %></li>
|
8
8
|
|
9
|
-
|
10
9
|
<li><%= link_to "generate barcode pdf as string", latex_example_barcode_as_string_path %> and write to <tt><%= "#{Rails.root}/tmp/a.pdf" %></tt> </li>
|
11
10
|
</ul>
|
@@ -1,14 +1,38 @@
|
|
1
1
|
\documentclass[12pt,a4paper,sloppy]{article}
|
2
|
-
<% @latex_config={:
|
2
|
+
<% @latex_config = { :recipe => [
|
3
|
+
{:command => 'pdflatex'},
|
4
|
+
{:command => 'bibtex', :arguments =>[]},
|
5
|
+
{:command => 'pdflatex', :runs => 2}
|
6
|
+
]} # you can override the defaults if you wish; see LatexToPdf#config
|
7
|
+
%>
|
8
|
+
\usepackage{filecontents}
|
3
9
|
\usepackage{lastpage}
|
4
10
|
\usepackage{graphics}
|
5
|
-
\begin{document}
|
6
11
|
|
7
|
-
|
12
|
+
\begin{filecontents}{bibliography.bib}
|
13
|
+
@article{example,
|
14
|
+
author = {John Doe},
|
15
|
+
title = {The title of the work},
|
16
|
+
journal = {The name of the journal},
|
17
|
+
year = 1993,
|
18
|
+
number = 2,
|
19
|
+
pages = {201-213},
|
20
|
+
month = 7,
|
21
|
+
note = {An optional note},
|
22
|
+
volume = 4
|
23
|
+
}
|
24
|
+
\end{filecontents}
|
25
|
+
|
26
|
+
\begin{document}
|
27
|
+
This document\cite{example} uses \verb|parse_runs = 2| to calculate the last page.
|
8
28
|
|
9
29
|
Other defaults can be overridden using \verb|LatexToPdf#config| and \verb|@latex_config|.
|
10
30
|
|
11
31
|
<%= yield %>
|
12
32
|
|
33
|
+
\bibliography{bibliography}
|
34
|
+
\bibliographystyle{plain}
|
35
|
+
|
36
|
+
|
13
37
|
\end{document}
|
14
38
|
|
@@ -13,7 +13,7 @@ module ActionView # :nodoc: all
|
|
13
13
|
def compile(template)
|
14
14
|
erb = "<% __in_erb_template=true %>#{template.source}"
|
15
15
|
out=self.class.erb_implementation.new(erb, :trim=>(self.class.erb_trim_mode == "-")).src
|
16
|
-
out + ";LatexToPdf.generate_pdf(@output_buffer.to_s
|
16
|
+
out + ";LatexToPdf.generate_pdf(@output_buffer.to_s, @latex_config||{})"
|
17
17
|
end
|
18
18
|
end
|
19
19
|
end
|
@@ -1,53 +1,94 @@
|
|
1
1
|
# -*- coding: utf-8 -*-
|
2
2
|
class LatexToPdf
|
3
3
|
def self.config
|
4
|
-
@config||={
|
4
|
+
@config||={
|
5
|
+
:recipe => [
|
6
|
+
# {
|
7
|
+
# :command => 'pdflatex',
|
8
|
+
# :input => 'input.tex',
|
9
|
+
# :arguments => ['-halt-on-error', '-shell-escape', '-interaction=batchmode'],
|
10
|
+
# :extra_arguments => [],
|
11
|
+
# :runs => 1
|
12
|
+
# }
|
13
|
+
],
|
14
|
+
:command => 'pdflatex',
|
15
|
+
:arguments => ['-halt-on-error'],
|
16
|
+
:default_arguments => ['-shell-escape', '-interaction=batchmode'],
|
17
|
+
:parse_runs => 1
|
18
|
+
}
|
5
19
|
end
|
6
20
|
|
7
21
|
# Converts a string of LaTeX +code+ into a binary string of PDF.
|
8
22
|
#
|
9
|
-
# pdflatex is used to convert the file and creates the directory
|
10
|
-
# files.
|
23
|
+
# By default, pdflatex is used to convert the file and creates the directory
|
24
|
+
# +#{Rails.root}/tmp/rails-latex/+ to store intermediate files.
|
11
25
|
#
|
12
|
-
# The config argument defaults to LatexToPdf.config but can be overridden
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
26
|
+
# The config argument defaults to LatexToPdf.config but can be overridden
|
27
|
+
# using @latex_config.
|
28
|
+
def self.generate_pdf(code, config)
|
29
|
+
config = self.config.merge(config)
|
30
|
+
recipe = config[:recipe]
|
31
|
+
|
32
|
+
# Deprecated legacy mode, if no recipe found
|
33
|
+
if recipe.length == 0
|
34
|
+
if config != self.config
|
35
|
+
Rails.logger.warn("Using command, arguments and parse_runs is deprecated in favor of recipe")
|
36
|
+
end
|
37
|
+
# Regression fix -- ability to override some arguments (-halt-on-error) but not other (-interaction),
|
38
|
+
# this is expected behaviour as seen in test_broken_doc_overriding_halt_on_error.
|
39
|
+
# Will be fixed in rails-latex 3
|
40
|
+
recipe = [{
|
41
|
+
:command => config[:command],
|
42
|
+
:arguments => config[:arguments] + config[:default_arguments],
|
43
|
+
:runs => config[:parse_runs]
|
44
|
+
}]
|
45
|
+
end
|
46
|
+
|
47
|
+
# Create directory, prepare additional supporting files (.cls, .sty, ...)
|
48
|
+
dir = File.join(Rails.root, 'tmp', 'rails-latex', "#{Process.pid}-#{Thread.current.hash}")
|
49
|
+
input = File.join(dir, 'input.tex')
|
19
50
|
FileUtils.mkdir_p(dir)
|
20
|
-
# copy any additional supporting files (.cls, .sty, ...)
|
21
51
|
supporting = config[:supporting]
|
22
52
|
if supporting.kind_of?(String) or supporting.kind_of?(Pathname) or (supporting.kind_of?(Array) and supporting.length > 0)
|
23
53
|
FileUtils.cp_r(supporting, dir)
|
24
54
|
end
|
25
|
-
File.open(input,'wb') {|io| io.write(code)
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
55
|
+
File.open(input,'wb') {|io| io.write(code)}
|
56
|
+
|
57
|
+
# Process recipe
|
58
|
+
recipe.each do |item|
|
59
|
+
command = item[:command] || config[:command]
|
60
|
+
runs = item[:runs] || config[:parse_runs]
|
61
|
+
args = item[:arguments] || config[:arguments]
|
62
|
+
args += item[:extra_arguments].to_a + ['input']
|
63
|
+
kwargs = {:out => ["input.log", "a"]}
|
64
|
+
Rails.logger.info "Running #{command} #{args.join(' ')} #{runs} times..."
|
65
|
+
Process.waitpid(
|
66
|
+
fork do
|
67
|
+
begin
|
68
|
+
Dir.chdir dir
|
69
|
+
(runs - 1).times do
|
70
|
+
system command, *args, **kwargs
|
71
|
+
end
|
72
|
+
exec command, *args, **kwargs
|
73
|
+
rescue
|
74
|
+
File.open("input.log", 'a'){|io|
|
75
|
+
io.write("#{$!.message}:\n#{$!.backtrace.join("\n")}\n")
|
76
|
+
}
|
77
|
+
ensure
|
78
|
+
Process.exit! 1
|
34
79
|
end
|
35
|
-
exec config[:command],*args,**kwargs
|
36
|
-
rescue
|
37
|
-
File.open("input.log",'a') {|io|
|
38
|
-
io.write("#{$!.message}:\n#{$!.backtrace.join("\n")}\n")
|
39
|
-
}
|
40
|
-
ensure
|
41
|
-
Process.exit! 1
|
42
80
|
end
|
43
|
-
|
81
|
+
)
|
82
|
+
end
|
83
|
+
|
84
|
+
# Finish
|
44
85
|
if File.exist?(pdf_file=input.sub(/\.tex$/,'.pdf'))
|
45
|
-
FileUtils.mv(input,File.join(dir,'..','input.tex'))
|
46
|
-
FileUtils.mv(input.sub(/\.tex$/,'.log'),File.join(dir,'..','input.log'))
|
47
|
-
result=File.read(pdf_file)
|
86
|
+
FileUtils.mv(input, File.join(dir, '..', 'input.tex'))
|
87
|
+
FileUtils.mv(input.sub(/\.tex$/,'.log'), File.join(dir, '..', 'input.log'))
|
88
|
+
result = File.read(pdf_file)
|
48
89
|
FileUtils.rm_rf(dir)
|
49
90
|
else
|
50
|
-
raise "
|
91
|
+
raise "rails-latex failed: See #{input.sub(/\.tex$/,'.log')} for details"
|
51
92
|
end
|
52
93
|
result
|
53
94
|
end
|
data/lib/rails-latex/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails-latex
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0
|
4
|
+
version: 2.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jan Baier
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: exe
|
11
11
|
cert_chain: []
|
12
|
-
date: 2016-07-
|
12
|
+
date: 2016-07-12 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|