4
4
"fmt"
5
5
"io/ioutil"
6
6
"net/http"
7
+ "os"
8
+ "os/exec"
7
9
"path/filepath"
8
10
"strings"
9
11
@@ -71,11 +73,10 @@ func resolveInputs(mainLatexPath string, basePath string) (string, error) {
71
73
72
74
// find command
73
75
startIndex := strings .Index (source , com )
74
- endIndex , err := latex .FindCommandEnd (source [ startIndex :] )
76
+ endIndex , err := latex .FindEndOfOneLineCommand (source , startIndex )
75
77
if err != nil {
76
78
return "" , err
77
79
}
78
- endIndex += startIndex
79
80
80
81
// read path in the brace
81
82
path := source [startIndex + len (com ) : endIndex - 1 ]
@@ -103,12 +104,10 @@ func readAllSources(latexFiles []string, basePath string) (string, error) {
103
104
if err != nil {
104
105
return "" , err
105
106
}
106
- fmt .Println (rootFiles )
107
107
108
108
// resolve \input, \include commands for each root file
109
109
allSources := []string {}
110
110
for _ , rootFile := range rootFiles {
111
- fmt .Println (rootFile )
112
111
source , err := resolveInputs (rootFile , basePath )
113
112
if err != nil {
114
113
return "" , err
@@ -135,52 +134,50 @@ func readAllSources(latexFiles []string, basePath string) (string, error) {
135
134
func (paper * Paper ) readLatexSource (path string ) error {
136
135
var err error
137
136
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
- // }
137
+ // download tarball
138
+ tarballPath := filepath .Join (path , paper .ArxivId + ".tar.gz" )
139
+ err = arxiv .DownloadTarball (paper .TarballUrl , tarballPath )
140
+ if err != nil {
141
+ return newErrorWithMsg (err , "Error occured during downloading tarball" )
142
+ }
144
143
145
144
// decompress tarball
146
145
sourcePath := filepath .Join (path , paper .ArxivId )
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
- // }
146
+ os .Mkdir (sourcePath , 0777 )
147
+
148
+ err = exec .Command ("tar" , "-xvzf" , tarballPath , "-C" , sourcePath ).Run ()
149
+ if err != nil {
150
+ return newErrorWithMsg (err , "Error occured during decompressing tarball." )
151
+ }
153
152
154
153
// list all *.tex
155
- fmt .Println ("list" )
156
154
pattern := filepath .Join (sourcePath , "**/*.tex" )
157
155
files , err := zglob .Glob (pattern )
158
156
if err != nil {
159
157
return newErrorWithMsg (err , "Error occurred during processing tex files(1)" )
160
158
}
161
159
162
160
// obtain all latex source
163
- fmt .Println ("all" )
164
161
allSource , err := readAllSources (files , sourcePath )
165
162
if err != nil {
166
163
return newErrorWithMsg (err , "Error occurred during processing tex files(3)" )
167
164
}
168
165
169
- allSource , err = latex .RemoveSimpleCommands (allSource , []string {`\label` })
166
+ // remove comment and \label command
167
+ allSource = latex .RemoveComment (allSource )
168
+ allSource , err = latex .RemoveOneLineCommands (allSource , []string {`\label` })
170
169
if err != nil {
171
170
return newErrorWithMsg (err , "Error occurred during removing unnecessary commands" )
172
171
}
173
172
174
173
// obtain macros
175
- fmt .Println ("macro" )
176
- macros , err := latex .FindMacros (allSource )
174
+ macros , err := latex .FindMacroCommands (allSource )
177
175
if err != nil {
178
176
return newErrorWithMsg (err , "Error occurred during extracting macros" )
179
177
}
180
178
paper .Macros = strings .Join (macros , "\n " )
181
179
182
180
// obtain equations
183
- fmt .Println ("eq" )
184
181
equationStrs , err := latex .FindEquations (allSource )
185
182
if err != nil {
186
183
return newErrorWithMsg (err , "Error occurred during extracting equations" )
@@ -194,9 +191,9 @@ func (paper *Paper) readLatexSource(path string) error {
194
191
}
195
192
paper .Equations = equations
196
193
197
- // // remove tarball
198
- // os.Remove(tarballPath)
199
- // os.RemoveAll(sourcePath)
194
+ // remove tarball
195
+ os .Remove (tarballPath )
196
+ os .RemoveAll (sourcePath )
200
197
201
198
return nil
202
199
}
0 commit comments