@@ -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,69 @@ 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
+ Dir . mktmpdir ( [ "rails-latex-" , "-#{ config [ :workdir ] . call } " ] , File . join ( Rails . root , 'tmp' ) ) do |dir |
52
+ input = File . join ( dir , 'input.tex' )
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 ) }
58
58
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
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
74
+ 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
74
82
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
83
end
84
+ )
85
+ end
86
+
87
+ pdf_file = input . sub ( /\. tex$/ , '.pdf' )
88
+ success = $?&.exitstatus &.zero? && File . exist? ( pdf_file )
89
+
90
+ # Preserve files if requested
91
+ unless config [ :preservenothing ]
92
+ FileUtils . mkdir_p ( config [ :basedir ] ) unless File . directory? ( config [ :basedir ] )
93
+ FileUtils . cp ( input , File . join ( config [ :basedir ] , 'input.tex' ) )
94
+ FileUtils . cp ( input . sub ( /\. tex$/ , '.log' ) , File . join ( config [ :basedir ] , 'input.log' ) )
95
+
96
+ if config [ :preservework ] || !success
97
+ preservation_dir = File . join ( config [ :basedir ] , config [ :workdir ] . call )
98
+ FileUtils . cp_r ( dir , preservation_dir )
83
99
end
84
- )
85
- end
100
+ end
101
+
102
+ # Finish
103
+ unless success
104
+ error_log_location = config [ :preservenothing ] ? "exception.log" : input . sub ( /\. tex$/ , '.log' )
105
+ raise RailsLatex ::ProcessingError . new (
106
+ "rails-latex failed: See #{ error_log_location } for details" ,
107
+ File . open ( input ) . read ,
108
+ File . open ( input . sub ( /\. tex$/ , '.log' ) ) . read
109
+ )
110
+ end
86
111
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
- )
112
+ return File . read ( pdf_file )
101
113
end
102
- result
103
114
end
104
115
105
116
# Escapes LaTex special characters in text so that they wont be interpreted as LaTex commands.
0 commit comments