forked from mandolyte/mdtopdf
/
mdtopdf.go
393 lines (334 loc) · 10.8 KB
/
mdtopdf.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/*
* Markdown to PDF Converter
* Available at http://github.com/mandolyte/mdtopdf
*
* Copyright © 2018 Cecil New <cecil.new@gmail.com>.
* Distributed under the MIT License.
* See README.md for details.
*
* Dependencies
* This package depends on two other packages:
*
* Blackfriday Markdown Processor
* Available at http://github.com/russross/blackfriday
*
* fpdf - a PDF document generator with high level support for
* text, drawing and images.
* Available at https://github.com/go-pdf/fpdf
*/
// Package mdtopdf converts markdown to PDF.
package mdtopdf
import (
"bufio"
"fmt"
"io"
"os"
"strings"
"github.com/go-pdf/fpdf"
bf "github.com/russross/blackfriday/v2"
)
// Color is a RGB set of ints; for a nice picker
// see https://www.w3schools.com/colors/colors_picker.asp
type Color struct {
Red, Green, Blue int
}
// Styler is the struct to capture the styling features for text
// Size and Spacing are specified in points.
// The sum of Size and Spacing is used as line height value
// in the fpdf API
type Styler struct {
Font string
Style string
Size float64
Spacing float64
TextColor Color
FillColor Color
}
// RenderOption allows to define functions to configure the renderer
type RenderOption func(r *PdfRenderer)
// PdfRenderer is the struct to manage conversion of a markdown object
// to PDF format.
type PdfRenderer struct {
// Pdf can be used to access the underlying created fpdf object
// prior to processing the markdown source
Pdf *fpdf.Fpdf
orientation, units string
papersize, fontdir string
// trace/log file if present
pdfFile, tracerFile string
w *bufio.Writer
// default margins for safe keeping
mleft, mtop, mright, mbottom float64
// normal text
Normal Styler
em float64
unicodeTranslator func(string) string
// link text
Link Styler
// backticked text
Backtick Styler
// blockquote text
Blockquote Styler
IndentValue float64
// Headings
H1 Styler
H2 Styler
H3 Styler
H4 Styler
H5 Styler
H6 Styler
// Table styling
THeader Styler
TBody Styler
cs states
// code styling
Code Styler
// update styling
NeedCodeStyleUpdate bool
NeedBlockquoteStyleUpdate bool
}
// NewPdfRenderer creates and configures an PdfRenderer object,
// which satisfies the Renderer interface.
func NewPdfRenderer(orient, papersz, pdfFile, tracerFile string, opts ...RenderOption) *PdfRenderer {
r := new(PdfRenderer)
// set filenames
r.pdfFile = pdfFile
r.tracerFile = tracerFile
// Global things
r.orientation = "portrait"
if orient != "" {
r.orientation = orient
}
r.units = "pt"
r.papersize = "Letter"
if papersz != "" {
r.papersize = papersz
}
r.fontdir = "."
// Normal Text
r.Normal = Styler{Font: "Arial", Style: "", Size: 12, Spacing: 2,
TextColor: Color{0, 0, 0}, FillColor: Color{255, 255, 255}}
// Link text
r.Link = Styler{Font: "Arial", Style: "iu", Size: 12, Spacing: 2,
TextColor: Color{0, 0, 0}, FillColor: Color{255, 255, 255}}
// Backticked text
r.Backtick = Styler{Font: "Courier", Style: "", Size: 12, Spacing: 2,
TextColor: Color{37, 27, 14}, FillColor: Color{200, 200, 200}}
// Code text
r.Code = Styler{Font: "Courier", Style: "", Size: 12, Spacing: 2,
TextColor: Color{37, 27, 14}, FillColor: Color{200, 200, 200}}
// Headings
r.H1 = Styler{Font: "Arial", Style: "b", Size: 24, Spacing: 5,
TextColor: Color{0, 0, 0}, FillColor: Color{255, 255, 255}}
r.H2 = Styler{Font: "Arial", Style: "b", Size: 22, Spacing: 5,
TextColor: Color{0, 0, 0}, FillColor: Color{255, 255, 255}}
r.H3 = Styler{Font: "Arial", Style: "b", Size: 20, Spacing: 5,
TextColor: Color{0, 0, 0}, FillColor: Color{255, 255, 255}}
r.H4 = Styler{Font: "Arial", Style: "b", Size: 18, Spacing: 5,
TextColor: Color{0, 0, 0}, FillColor: Color{255, 255, 255}}
r.H5 = Styler{Font: "Arial", Style: "b", Size: 16, Spacing: 5,
TextColor: Color{0, 0, 0}, FillColor: Color{255, 255, 255}}
r.H6 = Styler{Font: "Arial", Style: "b", Size: 14, Spacing: 5,
TextColor: Color{0, 0, 0}, FillColor: Color{255, 255, 255}}
//r.inBlockquote = false
//r.inHeading = false
r.Blockquote = Styler{Font: "Arial", Style: "i", Size: 12, Spacing: 2,
TextColor: Color{0, 0, 0}, FillColor: Color{255, 255, 255}}
// Table Header Text
r.THeader = Styler{Font: "Arial", Style: "B", Size: 12, Spacing: 2,
TextColor: Color{0, 0, 0}, FillColor: Color{180, 180, 180}}
// Table Body Text
r.TBody = Styler{Font: "Arial", Style: "", Size: 12, Spacing: 2,
TextColor: Color{0, 0, 0}, FillColor: Color{240, 240, 240}}
r.Pdf = fpdf.New(r.orientation, r.units, r.papersize, r.fontdir)
r.Pdf.AddPage()
// set default font
r.setStyler(r.Normal)
r.mleft, r.mtop, r.mright, r.mbottom = r.Pdf.GetMargins()
r.em = r.Pdf.GetStringWidth("m")
r.IndentValue = 3 * r.em
//r.current = r.normal // set default
r.cs = states{stack: make([]*containerState, 0)}
initcurrent := &containerState{containerType: bf.Paragraph,
listkind: notlist,
textStyle: r.Normal, leftMargin: r.mleft}
r.cs.push(initcurrent)
for _, o := range opts {
o(r)
}
return r
}
// NewPdfRendererWithDefaultStyler creates and configures an PdfRenderer object,
// which satisfies the Renderer interface.
// update default styler for normal
func NewPdfRendererWithDefaultStyler(orient, papersz, pdfFile, tracerFile string, defaultStyler Styler, opts ...RenderOption) *PdfRenderer {
opts = append(opts, func(r *PdfRenderer) {
r.Normal = defaultStyler
})
return NewPdfRenderer(orient, papersz, pdfFile, tracerFile, opts...)
}
// Process takes the markdown content, parses it to generate the PDF
func (r *PdfRenderer) Process(content []byte) error {
// try to open tracer
var f *os.File
var err error
if r.tracerFile != "" {
f, err = os.Create(r.tracerFile)
if err != nil {
return fmt.Errorf("os.Create() on tracefile error:%v", err)
}
defer f.Close()
r.w = bufio.NewWriter(f)
defer r.w.Flush()
}
err = r.Run(content)
if err != nil {
return fmt.Errorf("Pdf.OutputFileAndClose() error on %v:%v", r.pdfFile, err)
}
err = r.Pdf.OutputFileAndClose(r.pdfFile)
if err != nil {
return fmt.Errorf("Pdf.OutputFileAndClose() error on %v:%v", r.pdfFile, err)
}
return nil
}
// Run takes the markdown content, parses it but don't generate the PDF. you can access the PDF with youRenderer.Pdf
func (r *PdfRenderer) Run(content []byte) error {
// Preprocess content by changing all CRLF to LF
s := string(content)
s = strings.Replace(s, "\r\n", "\n", -1)
if r.unicodeTranslator != nil {
s = r.unicodeTranslator(s)
}
content = []byte(s)
_ = bf.Run(content, bf.WithRenderer(r))
return nil
}
// UpdateParagraphStyler - update with default styler
func (r *PdfRenderer) UpdateParagraphStyler(defaultStyler Styler) {
initcurrent := &containerState{containerType: bf.Paragraph,
listkind: notlist,
textStyle: defaultStyler, leftMargin: r.mleft}
r.cs.push(initcurrent)
}
// UpdateCodeStyler - update code fill styler
func (r *PdfRenderer) UpdateCodeStyler() {
r.NeedCodeStyleUpdate = true
}
// UpdateBlockquoteStyler - update Blockquote fill styler
func (r *PdfRenderer) UpdateBlockquoteStyler() {
r.NeedBlockquoteStyleUpdate = true
}
func (r *PdfRenderer) setStyler(s Styler) {
r.Pdf.SetFont(s.Font, s.Style, s.Size)
r.Pdf.SetTextColor(s.TextColor.Red, s.TextColor.Green, s.TextColor.Blue)
r.Pdf.SetFillColor(s.FillColor.Red, s.FillColor.Green, s.FillColor.Blue)
}
func (r *PdfRenderer) write(s Styler, t string) {
r.Pdf.Write(s.Size+s.Spacing, t)
}
func (r *PdfRenderer) multiCell(s Styler, t string) {
r.Pdf.MultiCell(0, s.Size+s.Spacing, t, "", "", true)
}
func (r *PdfRenderer) writeLink(s Styler, display, url string) {
r.Pdf.WriteLinkString(s.Size+s.Spacing, display, url)
}
// RenderNode is a default renderer of a single node of a syntax tree. For
// block nodes it will be called twice: first time with entering=true, second
// time with entering=false, so that it could know when it's working on an open
// tag and when on close. It writes the result to w.
//
// The return value is a way to tell the calling walker to adjust its walk
// pattern: e.g. it can terminate the traversal by returning Terminate. Or it
// can ask the walker to skip a subtree of this node by returning SkipChildren.
// The typical behavior is to return GoToNext, which asks for the usual
// traversal to the next node.
// (above taken verbatim from the blackfriday v2 package)
func (r *PdfRenderer) RenderNode(w io.Writer, node *bf.Node, entering bool) bf.WalkStatus {
switch node.Type {
case bf.Text:
r.processText(node)
case bf.Softbreak:
r.tracer("Softbreak", "Output newline")
r.cr()
case bf.Hardbreak:
r.tracer("Hardbreak", "Output newline")
r.cr()
case bf.Emph:
r.processEmph(node, entering)
case bf.Strong:
r.processStrong(node, entering)
case bf.Del:
if entering {
r.tracer("DEL (entering)", "Not handled")
} else {
r.tracer("DEL (leaving)", "Not handled")
}
case bf.HTMLSpan:
r.tracer("HTMLSpan", "Not handled")
case bf.Link:
r.processLink(node, entering)
case bf.Image:
r.processImage(node, entering)
case bf.Code:
r.processCode(node)
case bf.Document:
r.tracer("Document", "Not Handled")
case bf.Paragraph:
r.processParagraph(node, entering)
case bf.BlockQuote:
r.processBlockQuote(node, entering)
case bf.HTMLBlock:
r.processHTMLBlock(node)
case bf.Heading:
r.processHeading(node, entering)
case bf.HorizontalRule:
r.processHorizontalRule(node)
case bf.List:
r.processList(node, entering)
case bf.Item:
r.processItem(node, entering)
case bf.CodeBlock:
r.processCodeblock(node)
case bf.Table:
r.processTable(node, entering)
case bf.TableHead:
r.processTableHead(node, entering)
case bf.TableBody:
r.processTableBody(node, entering)
case bf.TableRow:
r.processTableRow(node, entering)
case bf.TableCell:
r.processTableCell(node, entering)
default:
panic("Unknown node type " + node.Type.String())
}
return bf.GoToNext
}
// RenderHeader is not supported.
func (r *PdfRenderer) RenderHeader(w io.Writer, ast *bf.Node) {
r.tracer("RenderHeader", "Not handled")
}
// RenderFooter is not supported.
func (r *PdfRenderer) RenderFooter(w io.Writer, ast *bf.Node) {
r.tracer("RenderFooter", "Not handled")
}
func (r *PdfRenderer) cr() {
LH := r.cs.peek().textStyle.Size + r.cs.peek().textStyle.Spacing
r.tracer("cr()", fmt.Sprintf("LH=%v", LH))
r.write(r.cs.peek().textStyle, "\n")
//r.Pdf.Ln(-1)
}
// Tracer traces parse and pdf generation activity.
func (r *PdfRenderer) tracer(source, msg string) {
if r.tracerFile != "" {
indent := strings.Repeat("-", len(r.cs.stack)-1)
r.w.WriteString(fmt.Sprintf("%v[%v] %v\n", indent, source, msg))
}
}
// Options
// WithUnicodeTranslator configures a unico translator to support characters for latin, russian, etc..
func WithUnicodeTranslator(cp string) RenderOption {
return func(r *PdfRenderer) {
r.unicodeTranslator = r.Pdf.UnicodeTranslatorFromDescriptor(cp)
}
}