@@ -15,6 +15,7 @@ def self.config
15
15
:default_arguments => [ '-shell-escape' , '-interaction=batchmode' ] ,
16
16
workdir : -> ( ) { "#{ Process . pid } -#{ Thread . current . hash } " } ,
17
17
preservework : false ,
18
+ preservenothing : false ,
18
19
basedir : File . join ( Rails . root , 'tmp' , 'rails-latex' ) ,
19
20
:parse_runs => 1
20
21
}
@@ -47,59 +48,71 @@ def self.generate_pdf(code, config)
47
48
end
48
49
49
50
# Create directory, prepare additional supporting files (.cls, .sty, ...)
50
- dir = File . join ( config [ :basedir ] , config [ :workdir ] . call )
51
- input = File . join ( dir , 'input.tex' )
52
- FileUtils . mkdir_p ( dir )
53
- supporting = config [ :supporting ]
54
- if supporting . kind_of? ( String ) or supporting . kind_of? ( Pathname ) or ( supporting . kind_of? ( Array ) and supporting . length > 0 )
55
- FileUtils . cp_r ( supporting , dir )
56
- end
57
- File . open ( input , 'wb' ) { |io | io . write ( code ) }
51
+ parent_tmpdir = File . join ( Rails . root , 'tmp' )
52
+ FileUtils . mkdir_p ( parent_tmpdir ) unless File . directory? ( parent_tmpdir )
53
+ Dir . mktmpdir ( [ "rails-latex-" , "-#{ config [ :workdir ] . call } " ] , parent_tmpdir ) do |dir |
54
+ input = File . join ( dir , 'input.tex' )
55
+ supporting = config [ :supporting ]
56
+ if supporting . kind_of? ( String ) or supporting . kind_of? ( Pathname ) or ( supporting . kind_of? ( Array ) and supporting . length > 0 )
57
+ FileUtils . cp_r ( supporting , dir )
58
+ end
59
+ File . open ( input , 'wb' ) { |io | io . write ( code ) }
58
60
59
- # Process recipe
60
- recipe . each do |item |
61
- command = item [ :command ] || config [ :command ]
62
- runs = item [ :runs ] || config [ :parse_runs ]
63
- args = item [ :arguments ] || config [ :arguments ] + config [ :default_arguments ]
64
- args += item [ :extra_arguments ] . to_a + [ 'input' ]
65
- kwargs = { :out => [ "input.log" , "a" ] }
66
- Rails . logger . info "Running '#{ command } #{ args . join ( ' ' ) } ' in #{ dir } #{ runs } times..."
67
- Process . waitpid (
68
- fork do
69
- begin
70
- Dir . chdir dir
71
- ( runs - 1 ) . times do
72
- clean_exit = system command , *args , **kwargs
73
- Process . exit! 1 unless clean_exit
61
+ # Process recipe
62
+ recipe . each do |item |
63
+ command = item [ :command ] || config [ :command ]
64
+ runs = item [ :runs ] || config [ :parse_runs ]
65
+ args = item [ :arguments ] || config [ :arguments ] + config [ :default_arguments ]
66
+ args += item [ :extra_arguments ] . to_a + [ 'input' ]
67
+ kwargs = { :out => [ "input.log" , "a" ] }
68
+ Rails . logger . info "Running '#{ command } #{ args . join ( ' ' ) } ' in #{ dir } #{ runs } times..."
69
+ Process . waitpid (
70
+ fork do
71
+ begin
72
+ Dir . chdir dir
73
+ ( runs - 1 ) . times do
74
+ clean_exit = system command , *args , **kwargs
75
+ Process . exit! 1 unless clean_exit
76
+ end
77
+ exec command , *args , **kwargs
78
+ rescue
79
+ File . open ( "input.log" , 'a' ) { |io |
80
+ io . write ( "#{ $!. message } :\n #{ $!. backtrace . join ( "\n " ) } \n " )
81
+ }
82
+ ensure
83
+ Process . exit! 1
74
84
end
75
- exec command , *args , **kwargs
76
- rescue
77
- File . open ( "input.log" , 'a' ) { |io |
78
- io . write ( "#{ $!. message } :\n #{ $!. backtrace . join ( "\n " ) } \n " )
79
- }
80
- ensure
81
- Process . exit! 1
82
85
end
86
+ )
87
+ end
88
+
89
+ pdf_file = input . sub ( /\. tex$/ , '.pdf' )
90
+ success = $?&.exitstatus &.zero? && File . exist? ( pdf_file )
91
+
92
+ # Preserve files if requested
93
+ unless config [ :preservenothing ]
94
+ FileUtils . mkdir_p ( config [ :basedir ] ) unless File . directory? ( config [ :basedir ] )
95
+ FileUtils . cp ( input , File . join ( config [ :basedir ] , 'input.tex' ) )
96
+ FileUtils . cp ( input . sub ( /\. tex$/ , '.log' ) , File . join ( config [ :basedir ] , 'input.log' ) )
97
+
98
+ if config [ :preservework ] || !success
99
+ preservation_dir = File . join ( config [ :basedir ] , config [ :workdir ] . call )
100
+ FileUtils . cp_r ( dir , preservation_dir )
83
101
end
84
- )
85
- end
102
+ end
103
+
104
+ # Finish
105
+ unless success
106
+ error_log_location = config [ :preservenothing ] ? "exception.log" : input . sub ( /\. tex$/ , '.log' )
107
+ raise RailsLatex ::ProcessingError . new (
108
+ "rails-latex failed: See #{ error_log_location } for details" ,
109
+ File . open ( input ) . read ,
110
+ File . open ( input . sub ( /\. tex$/ , '.log' ) ) . read
111
+ )
112
+ end
86
113
87
- # Finish
88
- if $?. exitstatus . zero? && File . exist? ( pdf_file = input . sub ( /\. tex$/ , '.pdf' ) )
89
- cmd = config [ :preservework ] ? :cp : :mv
90
- FileUtils . send ( cmd , input , File . join ( config [ :basedir ] , 'input.tex' ) )
91
- FileUtils . send ( cmd , input . sub ( /\. tex$/ , '.log' ) ,
92
- File . join ( config [ :basedir ] , 'input.log' ) )
93
- result = File . read ( pdf_file )
94
- FileUtils . rm_rf ( dir ) unless config [ :preservework ]
95
- else
96
- raise RailsLatex ::ProcessingError . new (
97
- "rails-latex failed: See #{ input . sub ( /\. tex$/ , '.log' ) } for details" ,
98
- File . open ( input ) . read ,
99
- File . open ( input . sub ( /\. tex$/ , '.log' ) ) . read
100
- )
114
+ return File . read ( pdf_file )
101
115
end
102
- result
103
116
end
104
117
105
118
# Escapes LaTex special characters in text so that they wont be interpreted as LaTex commands.
0 commit comments