4
4
"fmt"
5
5
"io/ioutil"
6
6
"net/http"
7
- "os"
8
- "os/exec"
9
7
"path/filepath"
10
8
"strings"
11
9
@@ -26,7 +24,27 @@ func readFile(path string) (string, error) {
26
24
return string (str ), nil
27
25
}
28
26
29
- func readAllSources (mainLatexPath string , basePath string ) (string , error ) {
27
+ func findSourceRoot (paths []string ) ([]string , error ) {
28
+ // search source which includes '\documentclass'
29
+ candidates := []string {}
30
+ for _ , path := range paths {
31
+ source , err := readFile (path )
32
+ if err != nil {
33
+ return []string {}, err
34
+ }
35
+ source = latex .RemoveComment (source )
36
+ if strings .Contains (source , `\documentclass` ) {
37
+ candidates = append (candidates , path )
38
+ }
39
+ }
40
+ if len (candidates ) > 0 {
41
+ return candidates , nil
42
+ } else {
43
+ return []string {}, fmt .Errorf ("Root latex file is not found" )
44
+ }
45
+ }
46
+
47
+ func resolveInputs (mainLatexPath string , basePath string ) (string , error ) {
30
48
// read all \input or \include tag and
31
49
// obtain all related sources concatenated string
32
50
source , err := readFile (mainLatexPath )
@@ -60,7 +78,7 @@ func readAllSources(mainLatexPath string, basePath string) (string, error) {
60
78
endIndex += startIndex
61
79
62
80
// read path in the brace
63
- path := source [startIndex + len (com )+ 1 : endIndex - 1 ]
81
+ path := source [startIndex + len (com ) : endIndex - 1 ]
64
82
if filepath .Ext (path ) == "" {
65
83
path = path + ".tex"
66
84
}
@@ -79,61 +97,71 @@ func readAllSources(mainLatexPath string, basePath string) (string, error) {
79
97
return source , nil
80
98
}
81
99
82
- func findSourceRoot (paths []string ) (string , error ) {
83
- // search source which includes '\documentclass'
84
- found := false
85
- mainPath := ""
86
- for _ , path := range paths {
87
- source , err := readFile (path )
100
+ func readAllSources (latexFiles []string , basePath string ) (string , error ) {
101
+ // find candidates for the root latex file
102
+ rootFiles , err := findSourceRoot (latexFiles )
103
+ if err != nil {
104
+ return "" , err
105
+ }
106
+ fmt .Println (rootFiles )
107
+
108
+ // resolve \input, \include commands for each root file
109
+ allSources := []string {}
110
+ for _ , rootFile := range rootFiles {
111
+ fmt .Println (rootFile )
112
+ source , err := resolveInputs (rootFile , basePath )
88
113
if err != nil {
89
114
return "" , err
90
115
}
91
- source = latex .RemoveComment (source )
92
- if strings .Contains (source , `\documentclass` ) {
93
- found = true
94
- mainPath = path
95
- }
116
+ allSources = append (allSources , source )
96
117
}
97
- if ! found {
98
- return "" , fmt .Errorf ("Latex file is not found" )
118
+
119
+ // if one candidate found, return the source
120
+ if len (allSources ) == 1 {
121
+ return allSources [0 ], nil
99
122
}
100
- return mainPath , nil
123
+
124
+ // if multiple candiates found, the most longest source is
125
+ // thought to be main latex file...
126
+ longestSource := ""
127
+ for _ , source := range allSources {
128
+ if len (source ) > len (longestSource ) {
129
+ longestSource = source
130
+ }
131
+ }
132
+ return longestSource , nil
101
133
}
102
134
103
135
func (paper * Paper ) readLatexSource (path string ) error {
104
136
var err error
105
137
106
- // download tarball
107
- tarballPath := filepath .Join (path , paper .ArxivId + ".tar.gz" )
108
- err = arxiv .DownloadTarball (paper .TarballUrl , tarballPath )
109
- if err != nil {
110
- return newErrorWithMsg (err , "Error occured during downloading tarball" )
111
- }
138
+ // // download tarball
139
+ // tarballPath := filepath.Join(path, paper.ArxivId+".tar.gz")
140
+ // err = arxiv.DownloadTarball(paper.TarballUrl, tarballPath)
141
+ // if err != nil {
142
+ // return newErrorWithMsg(err, "Error occured during downloading tarball")
143
+ // }
112
144
113
145
// decompress tarball
114
146
sourcePath := filepath .Join (path , paper .ArxivId )
115
- os .Mkdir (sourcePath , 0777 )
116
-
117
- err = exec .Command ("tar" , "-xvzf" , tarballPath , "-C" , sourcePath ).Run ()
118
- if err != nil {
119
- return newErrorWithMsg (err , "Error occured during decompressing tarball." )
120
- }
147
+ // os.Mkdir(sourcePath, 0777)
148
+ //
149
+ // err = exec.Command("tar", "-xvzf", tarballPath, "-C", sourcePath).Run()
150
+ // if err != nil {
151
+ // return newErrorWithMsg(err, "Error occured during decompressing tarball.")
152
+ // }
121
153
122
154
// list all *.tex
155
+ fmt .Println ("list" )
123
156
pattern := filepath .Join (sourcePath , "**/*.tex" )
124
157
files , err := zglob .Glob (pattern )
125
158
if err != nil {
126
159
return newErrorWithMsg (err , "Error occurred during processing tex files(1)" )
127
160
}
128
161
129
- // find root latex source file
130
- rootFile , err := findSourceRoot (files )
131
- if err != nil {
132
- return newErrorWithMsg (err , "Error occurred during processing tex files(2)" )
133
- }
134
-
135
162
// obtain all latex source
136
- allSource , err := readAllSources (rootFile , sourcePath )
163
+ fmt .Println ("all" )
164
+ allSource , err := readAllSources (files , sourcePath )
137
165
if err != nil {
138
166
return newErrorWithMsg (err , "Error occurred during processing tex files(3)" )
139
167
}
@@ -144,13 +172,15 @@ func (paper *Paper) readLatexSource(path string) error {
144
172
}
145
173
146
174
// obtain macros
175
+ fmt .Println ("macro" )
147
176
macros , err := latex .FindMacros (allSource )
148
177
if err != nil {
149
178
return newErrorWithMsg (err , "Error occurred during extracting macros" )
150
179
}
151
180
paper .Macros = strings .Join (macros , "\n " )
152
181
153
182
// obtain equations
183
+ fmt .Println ("eq" )
154
184
equationStrs , err := latex .FindEquations (allSource )
155
185
if err != nil {
156
186
return newErrorWithMsg (err , "Error occurred during extracting equations" )
@@ -164,13 +194,9 @@ func (paper *Paper) readLatexSource(path string) error {
164
194
}
165
195
paper .Equations = equations
166
196
167
- // remove tarball
168
- if err := os .Remove (tarballPath ); err != nil {
169
- return err
170
- }
171
- if err := os .RemoveAll (sourcePath ); err != nil {
172
- return err
173
- }
197
+ // // remove tarball
198
+ // os.Remove(tarballPath)
199
+ // os.RemoveAll(sourcePath)
174
200
175
201
return nil
176
202
}
@@ -244,9 +270,9 @@ func FindPaper() echo.HandlerFunc {
244
270
return err
245
271
}
246
272
247
- if dbc := database .Create (& _paper ); dbc .Error != nil {
248
- return dbc .Error
249
- }
273
+ // if dbc := database.Create(&_paper); dbc.Error != nil {
274
+ // return dbc.Error
275
+ // }
250
276
paper = _paper
251
277
} else {
252
278
database .Model (& paper ).Related (& paper .Equations ).Related (& paper .Authors )
@@ -258,7 +284,6 @@ func FindPaper() echo.HandlerFunc {
258
284
`\newcommand{textnormal}[1]{\textrm{#1}}` ,
259
285
}
260
286
paper .Macros += "\n " + strings .Join (defaultMacros , "\n " )
261
- fmt .Println (paper .Macros )
262
287
263
288
response := map [string ]interface {}{
264
289
"paper" : paper ,
0 commit comments