-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmobiledoc.go
96 lines (84 loc) · 1.92 KB
/
mobiledoc.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
package mobiledoc
import (
"encoding/json"
"errors"
"fmt"
"io"
)
// Marker constants
const (
markerMarkup = iota
markerAtom
)
// Section constants
const (
sectionMarkup = 1
sectionImage = 2
sectionList = 3
sectionCard = 10
)
// Mobiledoc models the data required to render a mobiledoc document
type Mobiledoc struct {
r io.Reader
atoms map[string]Atom
cards map[string]Card
doc doc
root *node
}
// NewMobiledoc creates a new Mobiledoc instance
func NewMobiledoc(src io.Reader) Mobiledoc {
return Mobiledoc{
r: src,
cards: map[string]Card{
"image-card": imagecard,
},
}
}
// WithAtom creates a new Mobiledoc instance that has a registered Atom
func (md Mobiledoc) WithAtom(name string, atom Atom) Mobiledoc {
if md.atoms == nil {
md.atoms = make(map[string]Atom)
}
md.atoms[name] = atom
return md
}
// WithCard creates a new Mobiledoc instance that has a registered Card
func (md Mobiledoc) WithCard(name string, card Card) Mobiledoc {
if md.cards == nil {
md.cards = make(map[string]Card)
}
md.cards[name] = card
return md
}
// Render the Mobiledoc is rendered to the given writer
func (md *Mobiledoc) Render(w io.Writer) error {
if md.root != nil {
return md.root.renderMarkdown(w)
}
var mdmap map[string]json.RawMessage
decoder := json.NewDecoder(md.r)
err := decoder.Decode(&mdmap)
if err != nil {
return fmt.Errorf("unable to decode mobiledoc json: %w", err)
}
verInt, ok := mdmap["version"]
if !ok {
return errors.New("not valid mobiledoc: version not found")
}
var version string
err = json.Unmarshal(verInt, &version)
if err != nil {
return fmt.Errorf("not valid mobiledoc: version string: %w", err)
}
switch version {
case "0.3.0", "0.3.1", "0.3.2":
n, err := md.parseV03(mdmap)
if err != nil {
return fmt.Errorf("unable to parse mobiledoc: %w", err)
}
md.root = n
default:
return fmt.Errorf("unknown version %s", version)
}
return md.root.renderMarkdown(w)
}