-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathformat.go
192 lines (175 loc) · 3.08 KB
/
format.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package main
import (
"bytes"
"strings"
"unicode"
)
func escape(in []byte) []byte {
var buf bytes.Buffer
var last rune
for _, r := range bytes.Runes(in) {
switch r {
case rune('\n'):
buf.WriteByte(' ')
last = rune(' ')
continue
case rune('\\'):
buf.WriteString("\\e")
continue
case rune('-'):
buf.WriteByte('\\')
case rune('.'):
if last == 0 || unicode.IsSpace(last) {
buf.WriteString("\\&")
}
case rune('\''):
if last == 0 || unicode.IsSpace(last) {
buf.WriteString("\\(fm")
last = rune('\'')
continue
}
}
buf.WriteRune(r)
last = r
}
return buf.Bytes()
}
type BR struct {
c, v []string
bold bool
}
func NewBR() *BR {
return &BR{[]string{}, []string{}, true}
}
func (s *BR) witch() {
if len(s.c) == 0 {
return
}
s.v = append(s.v, "\""+strings.Join(s.c, "")+"\"")
s.c = []string{}
s.bold = !s.bold
}
func (s *BR) B(str string) {
if !s.bold {
s.witch()
}
s.c = append(s.c, str)
}
func (s *BR) R(str string) {
if s.bold {
s.witch()
}
s.c = append(s.c, str)
}
type F struct {
*bytes.Buffer
BR *BR
}
func Formatter() *F {
return &F{&bytes.Buffer{}, NewBR()}
}
func (m *F) br() {
m.BR.witch()
if len(m.BR.v) == 0 {
return
}
m.WriteString(".BR " + strings.Join(m.BR.v, " ") + "\n")
m.BR.v = []string{}
m.BR.bold = true
}
func (m *F) nl() {
if m.Bytes()[m.Len()-1] != '\n' {
m.WriteByte('\n')
}
}
func (m *F) PP() {
m.nl()
m.WriteString(".PP\n")
}
func (m *F) section(name string) {
m.nl()
m.WriteString(".SH \"")
m.WriteString(strings.TrimSpace(name))
m.WriteString("\"\n")
}
var wrx = RX("[ \n\t]")
func (m *F) words(sentence []byte) {
for _, word := range inverseMatch(wrx, bytes.TrimSpace(sentence)) {
word = bytes.TrimSpace(word)
if len(word) == 0 {
continue
}
switch {
case bytes.HasPrefix(word, []byte("-")):
m.nl()
m.WriteString(".B \\")
m.Write(word)
m.nl()
case refrx.Match(word): //defined above find_refs()
m.nl()
m.WriteString(".BR ")
piv := bytes.IndexByte(word, '(')
m.Write(escape(word[:piv]))
m.WriteByte(' ')
m.Write(word[piv:])
m.nl()
default:
m.Write(escape(word))
m.WriteByte(' ')
}
}
}
func (m *F) text(p []byte) {
for _, s := range sentences(p) {
m.nl()
m.words(s)
}
}
func (m *F) paras(ps []interface{}) {
for i, P := range ps {
if i != 0 {
m.PP()
}
switch p := P.(type) {
case []byte: // raw section
m.nl()
m.Write(p)
m.nl()
case [][]byte:
for _, s := range p {
m.nl()
m.words(s)
}
case []*loc:
last, depth := 0, 0
for j, loc := range p {
m.nl()
line, in := loc.line, loc.indent
if in == -1 {
m.WriteString(".sp\n")
continue
} else if last < in {
depth++
m.WriteString(".RS\n")
} else if last > in {
depth--
if depth < 0 {
fatal("Impossible indentation.")
}
m.WriteString(".RE\n")
}
m.Write(escape(line))
m.nl()
if j != len(p)-1 {
m.WriteString(".sp 0\n")
}
last = in
}
//make sure we unindent as much as we've indented
for ; depth > 0; depth-- {
m.nl()
m.WriteString(".RE")
}
}
}
}