Skip to content

Commit 1612d73

Browse files
committed
support multiple root tex files
1 parent e3699d2 commit 1612d73

File tree

2 files changed

+81
-50
lines changed

2 files changed

+81
-50
lines changed

controllers/papers.go

+73-48
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import (
44
"fmt"
55
"io/ioutil"
66
"net/http"
7-
"os"
8-
"os/exec"
97
"path/filepath"
108
"strings"
119

@@ -26,7 +24,27 @@ func readFile(path string) (string, error) {
2624
return string(str), nil
2725
}
2826

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) {
3048
// read all \input or \include tag and
3149
// obtain all related sources concatenated string
3250
source, err := readFile(mainLatexPath)
@@ -60,7 +78,7 @@ func readAllSources(mainLatexPath string, basePath string) (string, error) {
6078
endIndex += startIndex
6179

6280
// read path in the brace
63-
path := source[startIndex+len(com)+1 : endIndex-1]
81+
path := source[startIndex+len(com) : endIndex-1]
6482
if filepath.Ext(path) == "" {
6583
path = path + ".tex"
6684
}
@@ -79,61 +97,71 @@ func readAllSources(mainLatexPath string, basePath string) (string, error) {
7997
return source, nil
8098
}
8199

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)
88113
if err != nil {
89114
return "", err
90115
}
91-
source = latex.RemoveComment(source)
92-
if strings.Contains(source, `\documentclass`) {
93-
found = true
94-
mainPath = path
95-
}
116+
allSources = append(allSources, source)
96117
}
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
99122
}
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
101133
}
102134

103135
func (paper *Paper) readLatexSource(path string) error {
104136
var err error
105137

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+
// }
112144

113145
// decompress tarball
114146
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+
// }
121153

122154
// list all *.tex
155+
fmt.Println("list")
123156
pattern := filepath.Join(sourcePath, "**/*.tex")
124157
files, err := zglob.Glob(pattern)
125158
if err != nil {
126159
return newErrorWithMsg(err, "Error occurred during processing tex files(1)")
127160
}
128161

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-
135162
// obtain all latex source
136-
allSource, err := readAllSources(rootFile, sourcePath)
163+
fmt.Println("all")
164+
allSource, err := readAllSources(files, sourcePath)
137165
if err != nil {
138166
return newErrorWithMsg(err, "Error occurred during processing tex files(3)")
139167
}
@@ -144,13 +172,15 @@ func (paper *Paper) readLatexSource(path string) error {
144172
}
145173

146174
// obtain macros
175+
fmt.Println("macro")
147176
macros, err := latex.FindMacros(allSource)
148177
if err != nil {
149178
return newErrorWithMsg(err, "Error occurred during extracting macros")
150179
}
151180
paper.Macros = strings.Join(macros, "\n")
152181

153182
// obtain equations
183+
fmt.Println("eq")
154184
equationStrs, err := latex.FindEquations(allSource)
155185
if err != nil {
156186
return newErrorWithMsg(err, "Error occurred during extracting equations")
@@ -164,13 +194,9 @@ func (paper *Paper) readLatexSource(path string) error {
164194
}
165195
paper.Equations = equations
166196

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)
174200

175201
return nil
176202
}
@@ -244,9 +270,9 @@ func FindPaper() echo.HandlerFunc {
244270
return err
245271
}
246272

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+
// }
250276
paper = _paper
251277
} else {
252278
database.Model(&paper).Related(&paper.Equations).Related(&paper.Authors)
@@ -258,7 +284,6 @@ func FindPaper() echo.HandlerFunc {
258284
`\newcommand{textnormal}[1]{\textrm{#1}}`,
259285
}
260286
paper.Macros += "\n" + strings.Join(defaultMacros, "\n")
261-
fmt.Println(paper.Macros)
262287

263288
response := map[string]interface{}{
264289
"paper": paper,

latex/latex.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,20 @@ func FindCommandEnd(str string) (int, error) {
5151
}
5252

5353
func RemoveComment(str string) string {
54+
str = strings.Replace(str, "%\n", "", -1)
55+
5456
for {
5557
if !strings.Contains(str, "%") {
5658
break
5759
}
5860

5961
startIndex := strings.Index(str, "%")
60-
endIndex := startIndex + strings.Index(str[startIndex:], "\n")
61-
str = str[:startIndex] + str[endIndex:]
62+
endIndex := strings.Index(str[startIndex:], "\n")
63+
if endIndex > 0 {
64+
str = str[:startIndex] + str[startIndex+endIndex:]
65+
} else {
66+
str = str[:startIndex]
67+
}
6268
}
6369

6470
return str

0 commit comments

Comments
 (0)