// cannot test for err return of exec.Command() func Test_CreatePdf_createRegularPdf(t *testing.T) { a := Article{ Journal: "Acme Journal", Title: "Testing Title", Authors: []string{"Author One"}, Content: "This is the text body.", BaseName: "test", } CreatePdf(a) dst := a.BaseName _, ok := os.Stat(dst + ".pdf") if ok != nil { t.Errorf("PDF file is not present.") } _, okaux := os.Stat(dst + ".aux") _, oklog := os.Stat(dst + ".log") _, okout := os.Stat(dst + ".out") //_, oktex := os.Stat(dst + ".tex") if okaux == nil || oklog == nil || okout == nil { // || oktex == nil { t.Errorf("Latex temp files are present.") } logger.Err(os.Remove(dst + ".pdf")) logger.Err(os.Remove(dst + ".tex")) }
func serveArticle(w http.ResponseWriter, req *http.Request) { urlLink := req.URL.Query().Get("url") format := req.URL.Query().Get("format") excludeFigures := req.URL.Query().Get("excludeFigures") if excludeFigures == "on" { *EXCLUDE_FIGS = true } else { *EXCLUDE_FIGS = false } article, _ := extractArticle(urlLink) switch format { case "json": writeJSON(&w, article.Json()) case "text": writeText(&w, article.Text()) case "html": writeHtml(&w, article.Html()) case "latex": writeText(&w, article.Latex()) case "pdf": CreatePdf(article) pdf := article.BaseName + ".pdf" tex := article.BaseName + ".tex" writePdf(&w, req, pdf) logger.Err(os.Remove(pdf)) logger.Err(os.Remove(tex)) default: writeJSON(&w, article.Json()) } }
func Test_getLatexBody(t *testing.T) { a := Article{ BaseName: "test_article", Title: "Testing Title", Authors: []string{"Author One"}, Content: `Test Text.`, Abstract: "This is the abstract.", Date: "2 June 2015", Journal: "Test Journal", Url: "http://www.test.com", LatexEscUrl: "http://www.test.com", } a.Pdf() got := GetLatexBody(a.BaseName + ".tex") want := `\renewcommand{\title}{{\LARGE{\textbf{Testing Title}}}} \renewcommand{\author}{{By Author One.}} \begin{document} \begin{center} \title \\[0.5em] \author \\[0.5em] \begin{abstract} This is the abstract. \end{abstract} \end{center} \begin{multicols}{2} Test Text. \end{multicols} \vspace{1ex} \par\noindent\rule{0.1\textwidth}{0.5pt}\\ \footnotesize{Published on 2 June 2015 in Test Journal} \footnotesize{\href{http://www.test.com}{\nolinkurl{http://www.test.com}}.} \end{document} ` if err := testutils.MustBeEqual(got, want); err != nil { t.Errorf("strings don't match") } logger.Err(os.Remove("test_article.tex")) logger.Err(os.Remove("test_article.pdf")) }
func (a Article) Html() string { b, err := ioutil.ReadFile(showTemplate) logger.Err(err) t := template.New("html") t, err = t.Parse(string(b)) logger.Err(err) var result bytes.Buffer logger.Err(t.Execute(&result, a)) return result.String() }
func inputs(w http.ResponseWriter, req *http.Request) { if req.Method != "POST" { getTempl.Execute(w, nil) s, err := NewScraperStore(*pluginDir) logger.Err(err) if err == nil { showSupport(w, s) } return } url := req.FormValue("url") format := req.FormValue("format") excludeFigures := req.FormValue("excludeFigures") // create NewClient // send request to backend // handle response from backend // display result http.Redirect(w, req, "/api/?url="+url+"&format="+format+"&excludeFigures="+excludeFigures, http.StatusFound) logger.Debug("submitted url : ", url) logger.Debug("output format : ", format) logger.Debug("exclude figures: ", excludeFigures) }
// cleanup deletes all image and latex tmp files. Taken from // https://www.socketloop.com/tutorials/golang-find-files-by-extension func cleanup() { dirname := "." + string(filepath.Separator) d, err := os.Open(dirname) if err != nil { fmt.Println(err) os.Exit(1) } defer d.Close() files, err := d.Readdir(-1) if err != nil { fmt.Println(err) os.Exit(1) } for _, file := range files { if file.Mode().IsRegular() { isTypeToDelete := isType(file, ".png") || isType(file, ".jpeg") || isType(file, ".gif") || isType(file, ".jpg") || isType(file, ".aux") || isType(file, ".log") || isType(file, ".out") || file.Name() == "want" || file.Name() == "got" if isTypeToDelete { logger.Err(os.Remove(file.Name())) } } } }
// CreateLatex takes an Article a and returns a string with a complete // latex document content. func createLatex(a Article) string { b, err := ioutil.ReadFile(texTemplate) logger.Err(err) t := template.New("latex") t = t.Delims("((", "))") t, err = t.Parse(string(b)) logger.Err(err) // Change the html content to Latex source code a.Texify() log.Println("Article file:") var result bytes.Buffer logger.Err(t.Execute(&result, a)) return result.String() }
func showColordiffOutput(got, want string) error { // write got and Want to disk logger.Err(ioutil.WriteFile("got", []byte(got), 0777)) logger.Err(ioutil.WriteFile("want", []byte(want), 0777)) // open with showColordiffOutput cmd := "colordiff" args := []string{"-w", "-B", "-I=*label*", "-I=*includegraphics*", "got", "want"} out, err := exec.Command(cmd, args...).Output() if err != nil { switch { // if cmd colordiff is not found, try normal diff case err.Error() == "executable file not found In $PATH": showDiffOutput(got, want) default: log.Println("got want") } fmt.Println(string(out)) return err } else { return nil } }
func createTexFile(a Article) { logger.Err(ioutil.WriteFile(a.BaseName+".tex", []byte(createLatex(a)), 0777)) }