@@ -5,175 +5,98 @@ package mdplain
5
5
6
6
import (
7
7
"bytes"
8
+ "io"
8
9
9
- "github.com/russross/blackfriday"
10
+ "github.com/yuin/goldmark/ast"
11
+ extAST "github.com/yuin/goldmark/extension/ast"
12
+ "github.com/yuin/goldmark/renderer"
10
13
)
11
14
12
- type Text struct {}
13
-
14
- func TextRenderer () blackfriday.Renderer {
15
- return & Text {}
16
- }
17
-
18
- func (options * Text ) GetFlags () int {
19
- return 0
20
- }
21
-
22
- func (options * Text ) TitleBlock (out * bytes.Buffer , text []byte ) {
23
- text = bytes .TrimPrefix (text , []byte ("% " ))
24
- text = bytes .Replace (text , []byte ("\n % " ), []byte ("\n " ), - 1 )
25
- out .Write (text )
26
- out .WriteString ("\n " )
27
- }
28
-
29
- func (options * Text ) Header (out * bytes.Buffer , text func () bool , level int , id string ) {
30
- marker := out .Len ()
31
- doubleSpace (out )
32
-
33
- if ! text () {
34
- out .Truncate (marker )
35
- return
15
+ type TextRender struct {}
16
+
17
+ func NewTextRenderer () * TextRender {
18
+ return & TextRender {}
19
+ }
20
+
21
+ func (r * TextRender ) Render (w io.Writer , source []byte , n ast.Node ) error {
22
+ out := bytes .NewBuffer ([]byte {})
23
+ err := ast .Walk (n , func (node ast.Node , entering bool ) (ast.WalkStatus , error ) {
24
+ if ! entering || node .Type () == ast .TypeDocument {
25
+ return ast .WalkContinue , nil
26
+ }
27
+
28
+ switch node := node .(type ) {
29
+ case * ast.Blockquote , * ast.Heading :
30
+ doubleSpace (out )
31
+ out .Write (node .Text (source ))
32
+ return ast .WalkSkipChildren , nil
33
+ case * ast.ThematicBreak :
34
+ doubleSpace (out )
35
+ return ast .WalkSkipChildren , nil
36
+ case * ast.CodeBlock :
37
+ doubleSpace (out )
38
+ for i := 0 ; i < node .Lines ().Len (); i ++ {
39
+ line := node .Lines ().At (i )
40
+ out .Write (line .Value (source ))
41
+ }
42
+ return ast .WalkSkipChildren , nil
43
+ case * ast.FencedCodeBlock :
44
+ doubleSpace (out )
45
+ doubleSpace (out )
46
+ for i := 0 ; i < node .Lines ().Len (); i ++ {
47
+ line := node .Lines ().At (i )
48
+ _ , _ = out .Write (line .Value (source ))
49
+ }
50
+ return ast .WalkSkipChildren , nil
51
+ case * ast.List :
52
+ doubleSpace (out )
53
+ return ast .WalkContinue , nil
54
+ case * ast.Paragraph :
55
+ doubleSpace (out )
56
+ if node .Text (source )[0 ] == '|' { // Write tables as-is.
57
+ for i := 0 ; i < node .Lines ().Len (); i ++ {
58
+ line := node .Lines ().At (i )
59
+ out .Write (line .Value (source ))
60
+ }
61
+ return ast .WalkSkipChildren , nil
62
+ }
63
+ return ast .WalkContinue , nil
64
+ case * ast.AutoLink , * extAST.Strikethrough :
65
+ out .Write (node .Text (source ))
66
+ return ast .WalkContinue , nil
67
+ case * ast.CodeSpan :
68
+ out .Write (node .Text (source ))
69
+ return ast .WalkSkipChildren , nil
70
+ case * ast.Link :
71
+ _ , err := out .Write (node .Text (source ))
72
+ if ! isRelativeLink (node .Destination ) {
73
+ out .WriteString (" " )
74
+ out .Write (node .Destination )
75
+ }
76
+ return ast .WalkSkipChildren , err
77
+ case * ast.Text :
78
+ out .Write (node .Text (source ))
79
+ if node .SoftLineBreak () {
80
+ doubleSpace (out )
81
+ }
82
+ return ast .WalkContinue , nil
83
+ case * ast.Image :
84
+ return ast .WalkSkipChildren , nil
85
+
86
+ }
87
+ return ast .WalkContinue , nil
88
+ })
89
+ if err != nil {
90
+ return err
36
91
}
37
- }
38
-
39
- func (options * Text ) BlockHtml (out * bytes.Buffer , text []byte ) {
40
- doubleSpace (out )
41
- out .Write (text )
42
- out .WriteByte ('\n' )
43
- }
44
-
45
- func (options * Text ) HRule (out * bytes.Buffer ) {
46
- doubleSpace (out )
47
- }
48
-
49
- func (options * Text ) BlockCode (out * bytes.Buffer , text []byte , lang string ) {
50
- options .BlockCodeNormal (out , text , lang )
51
- }
52
-
53
- func (options * Text ) BlockCodeNormal (out * bytes.Buffer , text []byte , lang string ) {
54
- doubleSpace (out )
55
- out .Write (text )
56
- }
57
-
58
- func (options * Text ) BlockQuote (out * bytes.Buffer , text []byte ) {
59
- doubleSpace (out )
60
- out .Write (text )
61
- }
62
-
63
- func (options * Text ) Table (out * bytes.Buffer , header []byte , body []byte , columnData []int ) {
64
- doubleSpace (out )
65
- out .Write (header )
66
- out .Write (body )
67
- }
68
-
69
- func (options * Text ) TableRow (out * bytes.Buffer , text []byte ) {
70
- doubleSpace (out )
71
- out .Write (text )
72
- }
73
-
74
- func (options * Text ) TableHeaderCell (out * bytes.Buffer , text []byte , align int ) {
75
- doubleSpace (out )
76
- out .Write (text )
77
- }
78
-
79
- func (options * Text ) TableCell (out * bytes.Buffer , text []byte , align int ) {
80
- doubleSpace (out )
81
- out .Write (text )
82
- }
83
-
84
- func (options * Text ) Footnotes (out * bytes.Buffer , text func () bool ) {
85
- options .HRule (out )
86
- options .List (out , text , 0 )
87
- }
88
-
89
- func (options * Text ) FootnoteItem (out * bytes.Buffer , name , text []byte , flags int ) {
90
- out .Write (text )
91
- }
92
-
93
- func (options * Text ) List (out * bytes.Buffer , text func () bool , flags int ) {
94
- marker := out .Len ()
95
- doubleSpace (out )
96
-
97
- if ! text () {
98
- out .Truncate (marker )
99
- return
92
+ _ , err = w .Write (out .Bytes ())
93
+ if err != nil {
94
+ return err
100
95
}
96
+ return nil
101
97
}
102
98
103
- func (options * Text ) ListItem (out * bytes.Buffer , text []byte , flags int ) {
104
- out .Write (text )
105
- }
106
-
107
- func (options * Text ) Paragraph (out * bytes.Buffer , text func () bool ) {
108
- marker := out .Len ()
109
- doubleSpace (out )
110
-
111
- if ! text () {
112
- out .Truncate (marker )
113
- return
114
- }
115
- }
116
-
117
- func (options * Text ) AutoLink (out * bytes.Buffer , link []byte , kind int ) {
118
- out .Write (link )
119
- }
120
-
121
- func (options * Text ) CodeSpan (out * bytes.Buffer , text []byte ) {
122
- out .Write (text )
123
- }
124
-
125
- func (options * Text ) DoubleEmphasis (out * bytes.Buffer , text []byte ) {
126
- out .Write (text )
127
- }
128
-
129
- func (options * Text ) Emphasis (out * bytes.Buffer , text []byte ) {
130
- if len (text ) == 0 {
131
- return
132
- }
133
- out .Write (text )
134
- }
135
-
136
- func (options * Text ) Image (out * bytes.Buffer , link []byte , title []byte , alt []byte ) {}
137
-
138
- func (options * Text ) LineBreak (out * bytes.Buffer ) {}
139
-
140
- func (options * Text ) Link (out * bytes.Buffer , link []byte , title []byte , content []byte ) {
141
- out .Write (content )
142
- if ! isRelativeLink (link ) {
143
- out .WriteString (" " )
144
- out .Write (link )
145
- }
146
- }
147
-
148
- func (options * Text ) RawHtmlTag (out * bytes.Buffer , text []byte ) {}
149
-
150
- func (options * Text ) TripleEmphasis (out * bytes.Buffer , text []byte ) {
151
- out .Write (text )
152
- }
153
-
154
- func (options * Text ) StrikeThrough (out * bytes.Buffer , text []byte ) {
155
- out .Write (text )
156
- }
157
-
158
- func (options * Text ) FootnoteRef (out * bytes.Buffer , ref []byte , id int ) {}
159
-
160
- func (options * Text ) Entity (out * bytes.Buffer , entity []byte ) {
161
- out .Write (entity )
162
- }
163
-
164
- func (options * Text ) NormalText (out * bytes.Buffer , text []byte ) {
165
- out .Write (text )
166
- }
167
-
168
- func (options * Text ) Smartypants (out * bytes.Buffer , text []byte ) {}
169
-
170
- func (options * Text ) DocumentHeader (out * bytes.Buffer ) {}
171
-
172
- func (options * Text ) DocumentFooter (out * bytes.Buffer ) {}
173
-
174
- func (options * Text ) TocHeader (text []byte , level int ) {}
175
-
176
- func (options * Text ) TocFinalize () {}
99
+ func (r * TextRender ) AddOptions (... renderer.Option ) {}
177
100
178
101
func doubleSpace (out * bytes.Buffer ) {
179
102
if out .Len () > 0 {
0 commit comments