@@ -19,31 +19,41 @@ import (
19
19
"github.com/raahii/arxiv-equations/latex"
20
20
)
21
21
22
- func readFile (path string ) string {
22
+ func readFile (path string ) ( string , error ) {
23
23
str , err := ioutil .ReadFile (path )
24
24
if err != nil {
25
- log . Fatal ( err )
25
+ return "" , err
26
26
}
27
27
28
- return string (str )
28
+ return string (str ), nil
29
29
}
30
30
31
- func readAllSources (mainLatexPath string , basePath string ) string {
31
+ func readAllSources (mainLatexPath string , basePath string ) ( string , error ) {
32
32
// read all \input or \include tag and
33
33
// obtain all related sources concatenated string
34
- source := readFile (mainLatexPath )
34
+ source , err := readFile (mainLatexPath )
35
+ if err != nil {
36
+ return "" , err
37
+ }
38
+
35
39
source = latex .RemoveComment (source )
36
40
source = strings .Replace (source , "*{" , "{" , - 1 )
37
41
source = strings .Replace (source , "*}" , "}" , - 1 )
38
42
39
- re := regexp .MustCompile (`\\(input|include)\{(.*?)\}` )
43
+ re , err := regexp .Compile (`\\(input|include)\{(.*?)\}` )
44
+ if err != nil {
45
+ return "" , err
46
+ }
40
47
41
48
resolveInputTag := func (s string ) string {
42
49
path := re .FindStringSubmatch (s )[2 ]
43
50
if filepath .Ext (path ) == "" {
44
51
path = path + ".tex"
45
52
}
46
- _source := readFile (filepath .Join (basePath , path ))
53
+ _source , err := readFile (filepath .Join (basePath , path ))
54
+ if err != nil {
55
+ panic (err )
56
+ }
47
57
_source = latex .RemoveComment (_source )
48
58
return _source
49
59
}
@@ -56,25 +66,28 @@ func readAllSources(mainLatexPath string, basePath string) string {
56
66
source = re .ReplaceAllStringFunc (source , resolveInputTag )
57
67
}
58
68
59
- return source
69
+ return source , nil
60
70
}
61
71
62
- func findSourceRoot (paths []string ) string {
72
+ func findSourceRoot (paths []string ) ( string , error ) {
63
73
// search source which includes '\documentclass'
64
74
found := false
65
75
mainPath := ""
66
76
for _ , path := range paths {
67
- source := readFile (path )
77
+ source , err := readFile (path )
78
+ if err != nil {
79
+ return "" , err
80
+ }
68
81
source = latex .RemoveComment (source )
69
82
if strings .Contains (source , `\documentclass` ) {
70
83
found = true
71
84
mainPath = path
72
85
}
73
86
}
74
87
if ! found {
75
- log . Fatal ( fmt .Errorf ("Main latex source is not found" ) )
88
+ return "" , fmt .Errorf ("Latex file is not found" )
76
89
}
77
- return mainPath
90
+ return mainPath , nil
78
91
}
79
92
80
93
func extractArxivId (arxivUrl string ) string {
@@ -83,54 +96,57 @@ func extractArxivId(arxivUrl string) string {
83
96
return strs [len (strs )- 1 ]
84
97
}
85
98
86
- func (paper * Paper ) readLatexSource (path string ) {
99
+ func (paper * Paper ) readLatexSource (path string ) error {
87
100
var err error
88
101
89
102
// download tarball
90
- log .Println ("Downloading tarball" , paper .TarballUrl )
91
103
tarballPath := filepath .Join (path , paper .ArxivId + ".tar.gz" )
92
104
err = arxiv .DownloadTarball (paper .TarballUrl , tarballPath )
93
105
if err != nil {
94
- log . Fatal (err )
106
+ return newErrorWithMsg (err , "Error occured during downloading tarball" )
95
107
}
96
108
97
109
// decompress tarball
98
110
sourcePath := filepath .Join (path , paper .ArxivId )
99
- log .Println ("Decompressing tarball" , sourcePath )
100
111
os .Mkdir (sourcePath , 0777 )
101
112
102
113
err = exec .Command ("tar" , "-xvzf" , tarballPath , "-C" , sourcePath ).Run ()
103
114
if err != nil {
104
- log . Fatal (err )
115
+ return newErrorWithMsg (err , "Error occured during decompressing tarball." )
105
116
}
106
117
107
118
// list all *.tex
108
- log .Println ("Processing tex files" )
109
119
pattern := filepath .Join (sourcePath , "**/*.tex" )
110
120
files , err := zglob .Glob (pattern )
111
121
if err != nil {
112
- log . Fatal (err )
122
+ return newErrorWithMsg (err , "Error occurred during processing tex files(1)" )
113
123
}
114
124
115
125
// find root latex source file
116
- rootFile := findSourceRoot (files )
126
+ rootFile , err := findSourceRoot (files )
127
+ if err != nil {
128
+ return newErrorWithMsg (err , "Error occurred during processing tex files(2)" )
129
+ }
117
130
118
131
// obtain all latex source
119
- allSource := readAllSources (rootFile , sourcePath )
132
+ allSource , err := readAllSources (rootFile , sourcePath )
133
+ if err != nil {
134
+ return newErrorWithMsg (err , "Error occurred during processing tex files(3)" )
135
+ }
120
136
121
137
// obtain macros
122
138
log .Println ("Extracting macros" )
123
139
macros , err := latex .FindMacros (allSource )
124
140
if err != nil {
125
- log . Fatal (err )
141
+ return newErrorWithMsg (err , "Error occurred during extracting macros" )
126
142
}
127
143
paper .Macros = strings .Join (macros , "\n " )
128
144
129
145
// obtain equations
130
146
log .Println ("Extracting equations" )
131
147
equationStrs , err := latex .FindEquations (allSource )
132
148
if err != nil {
133
- log . Fatal (err )
149
+ return newErrorWithMsg (err , "Error occurred during extracting equations" )
134
150
}
135
151
equations := []Equation {}
136
152
for _ , str := range equationStrs {
@@ -142,21 +158,30 @@ func (paper *Paper) readLatexSource(path string) {
142
158
143
159
// remove tarball
144
160
if err := os .Remove (tarballPath ); err != nil {
145
- log . Fatal ( err )
161
+ return err
146
162
}
147
163
if err := os .RemoveAll (sourcePath ); err != nil {
148
- log . Fatal ( err )
164
+ return err
149
165
}
166
+
167
+ return nil
150
168
}
151
169
152
170
func FetchPaper (arxivId string ) (Paper , error ) {
153
- // search papers
171
+ // search paper from id
154
172
params := map [string ]string {
155
173
"id_list" : arxivId ,
156
174
}
157
- apiResult := arxiv .SearchPapers (params )
158
- // error handling is needed
175
+ apiResult , err := arxiv .SearchPapers (params )
176
+ if err != nil {
177
+ return Paper {}, err
178
+ }
179
+
159
180
apiEntry := apiResult .Entries [0 ]
181
+ if apiEntry .Title == "Error" {
182
+ err := fmt .Errorf (apiEntry .Summary )
183
+ return Paper {}, err
184
+ }
160
185
161
186
// convert api result to paper entity
162
187
authors := []Author {} // for now, authors are just a string
@@ -190,13 +215,10 @@ func FetchPaper(arxivId string) (Paper, error) {
190
215
191
216
func FindPaperFromUrl () echo.HandlerFunc {
192
217
return func (c echo.Context ) error {
193
- err := fmt .Errorf ("test error" )
194
- return err
195
-
196
218
// obtain url from GET parameters
197
219
url := c .QueryParam ("url" )
198
220
if url == "" {
199
- log . Fatal ( fmt .Errorf ("Invalid parameters" ) )
221
+ fmt .Errorf ("Invalid parameters" )
200
222
}
201
223
202
224
// remove version number from url
@@ -207,29 +229,26 @@ func FindPaperFromUrl() echo.HandlerFunc {
207
229
arxivId := extractArxivId (url )
208
230
209
231
// find the paper
210
- database := db .GetConnection ()
211
232
paper := Paper {}
233
+ database := db .GetConnection ()
212
234
if database .Where ("arxiv_id = ?" , arxivId ).First (& paper ).RecordNotFound () {
213
- // if the paper doesn't exist in the database
214
-
215
- // fetch the paper
216
- paper , err := FetchPaper (arxivId )
235
+ // if the paper doesn't exist in the database, fetch the paper
236
+ _paper , err := FetchPaper (arxivId )
217
237
if err != nil {
218
238
return err
219
239
}
240
+ paper = _paper
220
241
221
242
// extract macros and equations
222
243
vars := config .Config .Variables
223
244
tarballDir := vars ["tarballDir" ]
224
245
paper .readLatexSource (tarballDir )
225
246
226
247
if dbc := database .Create (& paper ); dbc .Error != nil {
227
- log . Fatal ( dbc .Error )
248
+ return dbc .Error
228
249
}
229
250
} else {
230
251
database .Model (& paper ).Related (& paper .Equations ).Related (& paper .Authors )
231
- // tarballDir := "tarballs"
232
- // paper.readLatexSource(tarballDir)
233
252
}
234
253
235
254
// add macro to process fine for unsupported command in mathjax
0 commit comments