-
Notifications
You must be signed in to change notification settings - Fork 0
/
ms.go
229 lines (202 loc) · 5.18 KB
/
ms.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package troff
import (
"bytes"
"fmt"
"io"
"strings"
"text/tabwriter"
"time"
"sevki.org/x/markdown"
)
// Macro is a troff macro
type Macro string
const (
// https://9p.io/magic/man2html/6/ms
singleColumn = "1C"
doubleColumn = "2C"
beginAbstract = "AB"
endAbstract = "AE"
institution = "AI"
author = "AU"
bold = "B"
dateOnPage = "DA"
displayEnd = "DE"
displayStart = "DS"
tableEnd = "TE"
tableStart = "TS"
beginEquation = "EQ"
endEquation = "EN"
beginFootnote = "FS"
endFootnote = "FE"
italic = "I"
beginAndIndentParagraph = "IP"
endKeep = "KE"
beginKeep = "KF"
startKeep = "KS"
increaseTypeSize = "LG"
leftAlignedParagraph = "LP"
changeDate = "ND"
numberedHeading = "NH"
normalType = "NL"
beginParagraph = "PP"
roman = "R"
releasePaper = "RP"
endIndent = "RE"
beginIndent = "RS"
signature = "SG"
sectionHeading = "SH"
decreaseTypeSize = "SM"
title = "TL"
tableHeading = "TH"
underline = "UL"
beginCodeBlock = "P1"
endCodeBlock = "P2"
html = "HTML"
figure = "FG"
// https://9p.io/magic/man2html/6/mpictures
beginPicture = "BP"
endPicture = "EP"
linebreak = "\n"
)
func msPrint(ms Macro, args ...interface{}) []byte {
frmt := ".%s"
elems := []interface{}{ms}
for _, arg := range args {
switch arg.(type) {
case string:
frmt += " %s"
case int:
frmt += " %d"
}
elems = append(elems, arg)
}
frmt += "\n"
return []byte(fmt.Sprintf(frmt, elems...))
}
// Display prints
// .DS
// <p>
// .DE
func Display(w io.Writer, p []byte) {
w.Write(msPrint(displayStart))
w.Write(p)
w.Write(msPrint(displayEnd))
}
// Table prints
// .TS
// .DS
// <p>
// .DE
// .TE
func Table(w io.Writer, p []byte) {
buf := bytes.NewBuffer(nil)
tw := tabwriter.NewWriter(buf, 0, 0, 4, ' ', tabwriter.TabIndent)
tw.Write(p)
tw.Flush()
tbl := buf.Bytes()
i := bytes.IndexRune(tbl, '\n')
header, body := tbl[:i], tbl[i+1:]
buf = bytes.NewBuffer(nil)
Esc(buf, msPrint(tableStart, "H"))
Esc(buf, header)
LineBreak(buf)
TableHeading(buf)
Esc(buf, body)
Esc(buf, msPrint(tableEnd))
Display(w, buf.Bytes())
}
// Esc should print escaped bytes, but it just writes it.
// TODO(sevki): fix!
func Esc(w io.Writer, p []byte) { w.Write(p) }
// TableHeading prints
// .TH\n
func TableHeading(w io.Writer) { w.Write(msPrint(tableHeading)) }
// Indent prints
// .RS\n
func Indent(w io.Writer) { w.Write(msPrint(beginIndent)) }
// Outdent prints
// .RE\n
func Outdent(w io.Writer) { w.Write(msPrint(endIndent)) }
// IndentParagraph takes a <title> and prints
// .IP <title>\n
func IndentParagraph(w io.Writer, title string) { w.Write(msPrint(beginAndIndentParagraph, title)) }
// LeftAlignedParagraph prints
// .LP
func LeftAlignedParagraph(w io.Writer, title string, level int) {
if level < 0 {
w.Write(msPrint(leftAlignedParagraph, linebreak, title))
} else {
io.WriteString(w, linebreak)
io.WriteString(w, title)
}
}
// Bold prints
// .B
func Bold(w io.Writer, lit ...interface{}) { w.Write(msPrint(bold, lit...)) }
// Roman prints
// .R
func Roman(w io.Writer) { w.Write(msPrint(roman)) }
// Italic prints
// .I
func Italic(w io.Writer) { w.Write(msPrint(italic)) }
// Underline prints
// .UL
func Underline(w io.Writer) { w.Write(msPrint(underline)) }
// CodeBlock prints codeblock
// .P1
// <codeblock>
// .P2
func CodeBlock(w io.Writer, code []byte) {
w.Write(msPrint(beginCodeBlock))
io.WriteString(w, strings.TrimLeft(string(code), " ")+linebreak)
w.Write(msPrint(endCodeBlock))
}
// HTML prints .HTML <attribute>\n
func HTML(w io.Writer, attribute string) { w.Write(msPrint(html, attribute)) }
// Title prints
// .TL
// <title>\n
func Title(w io.Writer, tl string) { w.Write(msPrint(title, linebreak, tl)) }
// Abstract prints
// .AB
// <abstract>
// .AE
func Abstract(w io.Writer, abstract string) {
w.Write(msPrint(beginAbstract, linebreak, strings.TrimLeft(abstract, " ")))
w.Write(msPrint(endAbstract))
}
// AuthorBio prints
// .AU
// .I <a.Name>
// .I <a.Email>
// .AI <a.Affiliation>
func AuthorBio(w io.Writer, a markdown.Author) {
w.Write(msPrint(author))
w.Write(msPrint(italic, a.Name))
w.Write(msPrint(italic, a.Email))
if len(a.Affiliation) > 1 {
w.Write(msPrint(institution, a.Affiliation))
}
}
// ChangeDate changes the date to the given date by printing
// .ND May 8, 1945
func ChangeDate(w io.Writer, d time.Time) {
format := `Jan 2, 2006`
w.Write(msPrint(changeDate, d.Format(format)))
}
func printTitleBlock(w io.Writer, bl *markdown.Post) {
HTML(w, bl.Title)
Title(w, bl.Title)
// Authors
for _, author := range bl.Authors {
AuthorBio(w, author)
}
ChangeDate(w, time.Time(bl.Date.Time))
if len(bl.Abstract) > 0 {
Abstract(w, bl.Abstract)
}
w.Write(msPrint(beginParagraph))
}
// LineBreak prints a new line
// \n
func LineBreak(w io.Writer) { w.Write([]byte(linebreak)) }