-
Notifications
You must be signed in to change notification settings - Fork 0
/
markdown.go
185 lines (167 loc) · 4.11 KB
/
markdown.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
// Package markdown are a set of common markdownparser utils used in
// pllaces like sevki.org/troff
package markdown
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"os/exec"
"path"
"strings"
"time"
"github.com/BurntSushi/toml"
"github.com/araddon/dateparse"
"github.com/russross/blackfriday/v2"
"sevki.org/x/oututil"
)
// Time is markdown post block time format
type Time struct {
time.Time
}
// YYYY-MM-DD hh:mm:ss tz
const fuzzyFormat = "2006-01-02 15:04:05-07:00"
// UnmarshalText satisfies textunmarshaller
func (t *Time) UnmarshalText(text []byte) error {
postTime, err := dateparse.ParseAny(string(text))
if err != nil {
return err
}
t.Time = postTime
return nil
}
// Author information
type Author struct {
Name string
Affiliation string
Email string
Twitter string
Github string
}
// Post holds info about the post
type Post struct {
Title string
Date Time
Slug string
Authors []Author
Abstract string
Tags []string
}
// Renderer wraps the blackfriday.Renderer interface
type Renderer interface {
blackfriday.Renderer
Post() *Post
}
type renderer struct {
blackfriday.Renderer
parsingTitle bool
title *Post
}
func (r *renderer) RenderNode(w io.Writer, n *blackfriday.Node, entering bool) blackfriday.WalkStatus {
switch n.Type {
case blackfriday.Heading:
r.parsingTitle = n.IsTitleblock
if n.IsTitleblock {
if entering {
return blackfriday.GoToNext
}
return blackfriday.SkipChildren
}
case blackfriday.Text:
if r.parsingTitle {
magic := []byte(`title=`)
// BUG(sevki): we get things that are not title blocks here
isTitle := strings.Index(string(n.Literal), string(magic)) == 0
if err := toml.Unmarshal(n.Literal, &r.title); isTitle && err != nil {
return blackfriday.Terminate
}
r.parsingTitle = false
return blackfriday.GoToNext
}
case blackfriday.Image:
if entering {
s := strings.Split(string(n.LinkData.Destination), "=")
filename := strings.TrimSpace(s[0])
sizebit := "0x0"
if len(s) > 1 {
sizebit = s[1]
}
width, height := parseSize(sizebit)
if path.Ext(filename) == ".dot" {
bytez, err := dot(filename, int(width), int(height))
if err != nil {
return blackfriday.Terminate
}
w.Write(bytez)
return blackfriday.SkipChildren
}
n.LinkData.Destination = []byte(filename)
}
}
return r.Renderer.RenderNode(w, n, entering)
}
func (r *renderer) Post() *Post {
return r.title
}
// NewRenderer takes a blackfriday.Renderer and returns a Renderer
func NewRenderer(r blackfriday.Renderer) Renderer { return &renderer{r, false, nil} }
func parseSize(s string) (w int, h int) {
fmt.Sscanf(strings.TrimSpace(s), "%dx%d", &w, &h)
return
}
func dot(filename string, w int, h int) ([]byte, error) {
psname := strings.Replace(filename, ".dot", ".svg", -1)
_, psname = path.Split(psname)
args := []string{"-T", "svg", filename}
if w > 0 && h > 0 {
// args = append(args, "-size", fmt.Sprintf("%dx%d", w, h))
}
cmd := exec.CommandContext(context.Background(), "dot", args...)
stdOut := bytes.NewBuffer(nil)
stdErr := bytes.NewBuffer(nil)
cmd.Stdout = stdOut
cmd.Stderr = stdErr
fmt.Printf("running dot: %v\n", args)
if err := cmd.Run(); err != nil {
oututil.Indent(stdErr, 1)
fmt.Fprintf(stdErr, `dot: %v
args= %v
%s
`,
err,
args,
stdErr.String(),
)
return nil, errors.New(stdErr.String())
}
return stdOut.Bytes(), nil
}
func convertToPs(filename string, w int, h int) (string, error) {
psname := strings.Replace(filename, ".ps", ".svg", -1)
_, psname = path.Split(psname)
args := []string{"convert"}
if w > 0 && h > 0 {
args = append(args, "-size", fmt.Sprintf("%dx%d", w, h))
}
args = append(args, filename, psname)
cmd := exec.CommandContext(context.Background(), "magick", args...)
stdOut := bytes.NewBuffer(nil)
stdErr := bytes.NewBuffer(nil)
cmd.Stdout = stdOut
cmd.Stderr = stdErr
fmt.Printf("running magick: %v\n", args)
if err := cmd.Run(); err != nil {
oututil.Indent(stdErr, 1)
fmt.Fprintf(stdErr, `magick: %v
args= %v
%s
`,
err,
args,
stdErr.String(),
)
return "", errors.New(stdErr.String())
}
return psname, nil
}