This repository has been archived by the owner on Aug 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
mycomarkup.go
108 lines (99 loc) · 2.83 KB
/
mycomarkup.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
// Package mycomarkup provides an API for processing Mycomarkup-formatted documents.
package mycomarkup
import (
"errors"
"github.com/bouncepaw/mycomarkup/v4/blocks"
"github.com/bouncepaw/mycomarkup/v4/genhtml"
"github.com/bouncepaw/mycomarkup/v4/genhtml/tag"
"github.com/bouncepaw/mycomarkup/v4/mycocontext"
"github.com/bouncepaw/mycomarkup/v4/parser"
"github.com/bouncepaw/mycomarkup/v4/temporary_workaround"
)
// BlockTree returns a slice of blocks parsed from the Mycomarkup document contained in ctx.
//
// Pass visitors. Visitors are functions (usually closures) that are called on every top-level found block.
//
// Some pre-implemented visitors are in the tools package.
func BlockTree(ctx mycocontext.Context, visitors ...func(block blocks.Block)) []blocks.Block {
var (
tokens = make([]blocks.Block, 0)
token blocks.Block
done bool
)
for !done {
select {
case <-ctx.Done():
return tokens
default:
token, done = parser.NextToken(ctx)
if token != nil {
tokens = append(tokens, token)
for _, visitor := range visitors {
visitor := visitor
visitor(token)
}
}
}
}
return tokens
}
// BlocksToHTML turns the blocks into their HTML representation.
func BlocksToHTML(ctx mycocontext.Context, ast []blocks.Block) string {
counter := blocks.NewIDCounter()
var res []tag.Tag
for _, block := range ast {
res = append(res, genhtml.BlockToTag(ctx, block, counter))
}
return tag.
NewClosed("article").
WithAttrs(map[string]string{
"class": "mycomarkup-doc",
}).
WithChildren(res...).
String()
}
// transclusionVisitor returns a visitor to pass to BlockTree and a function to get the results.
func transclusionVisitor(xcl blocks.Transclusion) (
visitor func(block blocks.Block),
result func() ([]blocks.Block, error),
) {
var (
collected []blocks.Block
metDescriptionAlready = false
)
visitor = func(block blocks.Block) {
switch xcl.Selector {
case blocks.SelectorAttachment:
// We don't need any of that when we only transclude attachments.
case blocks.SelectorText, blocks.SelectorFull:
collected = append(collected, block)
case blocks.SelectorOverview, blocks.SelectorDescription:
switch block.(type) {
case blocks.Paragraph:
if metDescriptionAlready {
break
}
metDescriptionAlready = true
collected = append(collected, block)
}
}
}
result = func() ([]blocks.Block, error) {
if len(collected) == 0 {
switch xcl.Selector {
case blocks.SelectorDescription:
// Asked for a description, got no description.
return nil, errors.New("no description")
case blocks.SelectorText:
// Asked for a text, found emptiness...
return nil, errors.New("no text")
}
}
return collected, nil
}
return
}
func init() {
temporary_workaround.BlockTree = BlockTree
temporary_workaround.TransclusionVisitor = transclusionVisitor
}