forked from knieriem/markdown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathout-groffmm.go
212 lines (198 loc) · 4.57 KB
/
out-groffmm.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/* Original C version https://github.com/jgm/peg-markdown/
* Copyright 2008 John MacFarlane (jgm at berkeley dot edu).
*
* Modifications and translation from C into Go
* based on markdown_output.c
* Copyright 2010 Michael Teichgräber (mt at wmipf dot de)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License or the MIT
* license. See LICENSE for details.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
package markdown
// groff mm output functions
import (
"log"
"strings"
)
type troffOut struct {
baseWriter
strikeMacroWritten bool
inListItem bool
escape *strings.Replacer
}
// Returns a formatter that writes the document in groff mm format.
func ToGroffMM(w Writer) Formatter {
f := new(troffOut)
f.baseWriter = baseWriter{w, 2}
f.escape = strings.NewReplacer(`\`, `\e`)
return f
}
func (f *troffOut) FormatBlock(tree *element) {
f.elist(tree)
}
func (f *troffOut) Finish() {
f.WriteByte('\n')
f.padded = 2
}
func (h *troffOut) sp() *troffOut {
h.pad(2)
return h
}
func (h *troffOut) br() *troffOut {
h.pad(1)
return h
}
func (h *troffOut) skipPadding() *troffOut {
h.padded = 2
return h
}
// write a string
func (w *troffOut) s(s string) *troffOut {
w.WriteString(s)
return w
}
// write string, escape '\'
func (w *troffOut) str(s string) *troffOut {
if strings.HasPrefix(s, ".") {
w.WriteString(`\[char46]`)
s = s[1:]
}
w.escape.WriteString(w, s)
return w
}
func (w *troffOut) children(el *element) *troffOut {
return w.elist(el.children)
}
func (w *troffOut) inline(pfx string, el *element, sfx string) *troffOut {
return w.s(pfx).children(el).s(sfx)
}
func (w *troffOut) req(name string) *troffOut {
return w.br().s(".").s(name)
}
// write a list of elements
func (w *troffOut) elist(list *element) *troffOut {
for i := 0; list != nil; i++ {
w.elem(list, i == 0)
list = list.next
}
return w
}
func (w *troffOut) elem(elt *element, isFirst bool) *troffOut {
var s string
switch elt.key {
case SPACE:
s = elt.contents.str
case LINEBREAK:
w.req("br\n")
case STR:
w.str(elt.contents.str)
case ELLIPSIS:
s = "..."
case EMDASH:
s = `\[em]`
case ENDASH:
s = `\[en]`
case APOSTROPHE:
s = "'"
case SINGLEQUOTED:
w.inline("`", elt, "'")
case DOUBLEQUOTED:
w.inline(`\[lq]`, elt, `\[rq]`)
case CODE:
w.s(`\fC`).str(elt.contents.str).s(`\fR`)
case HTML:
/* don't print HTML */
case LINK:
link := elt.contents.link
w.elist(link.label)
w.s(" (").s(link.url).s(")")
case IMAGE:
w.s("[IMAGE: ").elist(elt.contents.link.label).s("]")
/* not supported */
case EMPH:
w.inline(`\fI`, elt, `\fR`)
case STRONG:
w.inline(`\fB`, elt, `\fR`)
case STRIKE:
w.s("\\c\n")
if !w.strikeMacroWritten {
w.s(`.de ST
.nr width \w'\\$1'
\Z@\v'-.25m'\l'\\n[width]u'@\\$1\c
..
`)
w.strikeMacroWritten = true
}
w.inline(".ST \"", elt, `"`).br()
case LIST:
w.children(elt)
case RAW:
/* Shouldn't occur - these are handled by process_raw_blocks() */
log.Fatalf("RAW")
case H1, H2, H3, H4, H5, H6:
h := ".H " + string('1'+elt.key-H1) + ` "` /* assumes H1 ... H6 are in order */
w.br().inline(h, elt, `"`)
case PLAIN:
w.br().children(elt)
case PARA:
if !w.inListItem || !isFirst {
w.req("P\n").children(elt)
} else {
w.br().children(elt)
}
case HRULE:
w.br().s(`\l'\n(.lu*8u/10u'`)
case HTMLBLOCK:
/* don't print HTML block */
case VERBATIM:
w.req("VERBON 2\n")
w.str(elt.contents.str)
w.s(".VERBOFF")
case BULLETLIST:
w.req("BL").children(elt).req("LE 1")
case ORDEREDLIST:
w.req("AL").children(elt).req("LE 1")
case DEFINITIONLIST:
w.req(`BVL \\n(Pin`).children(elt).req("LE 1")
case DEFTITLE:
w.req(`DLI "`).children(elt).s(`"`)
case DEFDATA:
w.children(elt)
w.req("br")
case LISTITEM:
w.req("LI\n")
w.inListItem = true
w.skipPadding()
w.children(elt)
w.inListItem = false
case BLOCKQUOTE:
w.req("DS I\n")
w.skipPadding()
w.children(elt)
w.req("DE")
case NOTE:
/* if contents.str == 0, then print note; else ignore, since this
* is a note block that has been incorporated into the notes list */
if elt.contents.str == "" {
w.s("\\*F\n")
w.s(".FS\n")
w.skipPadding()
w.children(elt)
w.req("FE")
}
case REFERENCE:
/* Nonprinting */
default:
log.Fatalf("troffOut.elem encountered unknown element key = %d\n", elt.key)
}
if s != "" {
w.s(s)
}
return w
}