Skip to content

Commit 80ae88d

Browse files
committed
update function to search position of end command
1 parent 1612d73 commit 80ae88d

File tree

3 files changed

+177
-148
lines changed

3 files changed

+177
-148
lines changed

controllers/papers.go

+22-25
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"fmt"
55
"io/ioutil"
66
"net/http"
7+
"os"
8+
"os/exec"
79
"path/filepath"
810
"strings"
911

@@ -71,11 +73,10 @@ func resolveInputs(mainLatexPath string, basePath string) (string, error) {
7173

7274
// find command
7375
startIndex := strings.Index(source, com)
74-
endIndex, err := latex.FindCommandEnd(source[startIndex:])
76+
endIndex, err := latex.FindEndOfOneLineCommand(source, startIndex)
7577
if err != nil {
7678
return "", err
7779
}
78-
endIndex += startIndex
7980

8081
// read path in the brace
8182
path := source[startIndex+len(com) : endIndex-1]
@@ -103,12 +104,10 @@ func readAllSources(latexFiles []string, basePath string) (string, error) {
103104
if err != nil {
104105
return "", err
105106
}
106-
fmt.Println(rootFiles)
107107

108108
// resolve \input, \include commands for each root file
109109
allSources := []string{}
110110
for _, rootFile := range rootFiles {
111-
fmt.Println(rootFile)
112111
source, err := resolveInputs(rootFile, basePath)
113112
if err != nil {
114113
return "", err
@@ -135,52 +134,50 @@ func readAllSources(latexFiles []string, basePath string) (string, error) {
135134
func (paper *Paper) readLatexSource(path string) error {
136135
var err error
137136

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+
}
144143

145144
// decompress tarball
146145
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+
}
153152

154153
// list all *.tex
155-
fmt.Println("list")
156154
pattern := filepath.Join(sourcePath, "**/*.tex")
157155
files, err := zglob.Glob(pattern)
158156
if err != nil {
159157
return newErrorWithMsg(err, "Error occurred during processing tex files(1)")
160158
}
161159

162160
// obtain all latex source
163-
fmt.Println("all")
164161
allSource, err := readAllSources(files, sourcePath)
165162
if err != nil {
166163
return newErrorWithMsg(err, "Error occurred during processing tex files(3)")
167164
}
168165

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`})
170169
if err != nil {
171170
return newErrorWithMsg(err, "Error occurred during removing unnecessary commands")
172171
}
173172

174173
// obtain macros
175-
fmt.Println("macro")
176-
macros, err := latex.FindMacros(allSource)
174+
macros, err := latex.FindMacroCommands(allSource)
177175
if err != nil {
178176
return newErrorWithMsg(err, "Error occurred during extracting macros")
179177
}
180178
paper.Macros = strings.Join(macros, "\n")
181179

182180
// obtain equations
183-
fmt.Println("eq")
184181
equationStrs, err := latex.FindEquations(allSource)
185182
if err != nil {
186183
return newErrorWithMsg(err, "Error occurred during extracting equations")
@@ -194,9 +191,9 @@ func (paper *Paper) readLatexSource(path string) error {
194191
}
195192
paper.Equations = equations
196193

197-
// // remove tarball
198-
// os.Remove(tarballPath)
199-
// os.RemoveAll(sourcePath)
194+
// remove tarball
195+
os.Remove(tarballPath)
196+
os.RemoveAll(sourcePath)
200197

201198
return nil
202199
}

0 commit comments

Comments
 (0)