-
Notifications
You must be signed in to change notification settings - Fork 2
/
texout.go
104 lines (91 loc) · 1.84 KB
/
texout.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package main
// texout handles TeX output.
import (
"bufio"
"fmt"
"io"
"io/ioutil"
"os"
)
func SourceToLatex(filename string) (tex string, err error) {
tex = `\documentclass[11pt]{article}
\usepackage{parskip}
\setlength{\parindent}{11pt}
\setlength{\parindent}{0cm}
\usepackage[margin=0.75in]{geometry}
\usepackage{fancyvrb}
\usepackage{iwona,palatino}
\usepackage[OT1]{fontenc}
\usepackage{textcomp}
\usepackage{lmodern}
\usepackage[hidelinks]{hyperref}
\usepackage{xltxtra}
\usepackage{graphicx}
\usepackage{algpseudocode}
\usepackage{amssymb}
\usepackage{listings}
\usepackage{framed}
\title{%s}
\author{literate listing}
\begin{document}
\maketitle
`
tex = fmt.Sprintf(tex, filename)
file, err := os.Open(filename)
if err != nil {
return
}
defer file.Close()
buf := bufio.NewReader(file)
var (
line string
longLine bool
lineBytes []byte
isPrefix bool
comment = true
)
for {
err = nil
lineBytes, isPrefix, err = buf.ReadLine()
if io.EOF == err {
err = nil
break
} else if err != nil {
break
} else if isPrefix {
line += string(lineBytes)
longLine = true
continue
} else if longLine {
line += string(lineBytes)
longLine = false
} else {
line = string(lineBytes)
}
if CommentLine.MatchString(line) {
if !comment {
tex += "\\end{lstlisting}\n\n"
}
tex += CommentLine.ReplaceAllString(line, "")
tex += "\n"
comment = true
} else {
if comment {
tex += "\n\n\\begin{lstlisting}[frame=single]\n"
comment = false
}
tex += line + "\n"
}
}
if !comment {
tex += "\\end{lstlisting}\n\n"
}
tex += "\\end{document}\n"
return
}
// TexWriter writes the transformed listing to a TeX file.
func TexWriter(listing string, filename string) (err error) {
outFile := GetOutFile(filename + ".tex")
err = ioutil.WriteFile(outFile, []byte(listing), 0644)
return
}