-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinline_code.go
76 lines (57 loc) · 1.15 KB
/
inline_code.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
package hype
import (
"bytes"
"encoding/json"
"fmt"
"html"
)
type InlineCode struct {
*Element
}
func (code *InlineCode) MarshalJSON() ([]byte, error) {
if code == nil {
return nil, ErrIsNil("inline code")
}
code.RLock()
defer code.RUnlock()
m, err := code.JSONMap()
if err != nil {
return nil, err
}
m["type"] = toType(code)
return json.MarshalIndent(m, "", " ")
}
func (code *InlineCode) String() string {
if code == nil || code.Element == nil {
return "<code></code>"
}
bb := &bytes.Buffer{}
fmt.Fprint(bb, code.StartTag())
body := code.Nodes.String()
body = html.EscapeString(body)
fmt.Fprint(bb, body)
fmt.Fprint(bb, code.EndTag())
return bb.String()
}
func (code *InlineCode) MD() string {
if code == nil || code.Element == nil {
return ""
}
return fmt.Sprintf("`%s`", code.Nodes.String())
}
func NewInlineCode(el *Element) (*InlineCode, error) {
if el == nil {
return nil, ErrIsNil("element")
}
code := &InlineCode{
Element: el,
}
return code, nil
}
func NewInlineCodeNodes(p *Parser, el *Element) (Nodes, error) {
code, err := NewInlineCode(el)
if err != nil {
return nil, err
}
return Nodes{code}, nil
}