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
/
html.go
140 lines (131 loc) · 4.02 KB
/
html.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
package mycomarkup
import (
"fmt"
"github.com/bouncepaw/mycomarkup/blocks"
)
// BlockToHTML turns the given block into HTML. It supports only a subset of Mycomarkup.
func BlockToHTML(block blocks.Block, counter *blocks.IDCounter) string {
switch b := block.(type) {
case blocks.Formatted:
return b.Html
case blocks.Paragraph:
return fmt.Sprintf("\n<p%s>%s</p>", idAttribute(b, counter), b.Html)
case blocks.HorizontalLine:
return fmt.Sprintf(`<hr id="%s"/>`, idAttribute(b, counter))
case blocks.Img:
return imgToHTML(b, counter)
case blocks.ImgEntry:
return imgEntryToHTML(b, counter)
case blocks.LaunchPad:
return launchpadToHTML(b, counter)
case blocks.RocketLink:
return fmt.Sprintf(`
<li class="launchpad__entry"><a href="%s" class="rocketlink %s">%s</a></li>`, b.Href(), b.Classes(), b.Display())
case blocks.Heading:
return fmt.Sprintf(`
<h%[1]d%[4]s>%[2]s<a href="#%[3]s" id="%[3]s" class="heading__link"></a></h%[1]d>
`, b.Level, BlockToHTML(b.Contents(), counter), b.ID(counter), idAttribute(b, counter))
case blocks.Table:
return tableToHTML(b, counter)
case blocks.TableRow:
return tableRowToHTML(b, counter)
case blocks.TableCell:
return tableCellToHTML(b, counter)
case blocks.CodeBlock:
return fmt.Sprintf("\n<pre class='codeblock'%s><code class='language-%s'>%s</code></pre>", idAttribute(b, counter), b.Language(), b.Contents())
case blocks.Quote:
var ret string
for _, b := range b.Contents() {
ret += BlockToHTML(b, counter)
}
return fmt.Sprintf("\n<blockquote%s>%s\n</blockquote>", idAttribute(b, counter.UnusableCopy()), ret)
}
fmt.Printf("%q\n", block)
return "<b>UNKNOWN ELEMENT</b>"
}
func idAttribute(b blocks.Block, counter *blocks.IDCounter) string {
switch id := b.ID(counter); {
case !counter.ShouldUseResults, id == "":
return ""
default:
return fmt.Sprintf(` id="%s"`, id)
}
}
func tableCellToHTML(tc blocks.TableCell, counter *blocks.IDCounter) string {
return fmt.Sprintf(
"\n\t<%[1]s%[2]s>%[3]s</%[1]s>",
tc.TagName(),
tc.ColspanAttribute(),
BlockToHTML(tc.Contents, counter),
)
}
func tableRowToHTML(tr blocks.TableRow, counter *blocks.IDCounter) string {
var ret string
for _, tc := range tr.Cells {
ret += BlockToHTML(*tc, counter)
}
return fmt.Sprintf("<tr>%s</tr>", ret)
}
func tableToHTML(t blocks.Table, counter *blocks.IDCounter) string {
var ret string
if t.Caption != "" {
ret = fmt.Sprintf("<caption>%s</caption>", t.Caption)
}
if len(t.Rows) > 0 && t.Rows[0].LooksLikeThead() {
ret += fmt.Sprintf("<thead>%s</thead>", BlockToHTML(*t.Rows[0], counter))
t.Rows = t.Rows[1:]
}
ret += "\n<tbody>\n"
for _, tr := range t.Rows {
ret += BlockToHTML(*tr, counter)
}
return fmt.Sprintf(`
<table%s>%s</tbody></table>`, idAttribute(t, counter), ret)
}
func launchpadToHTML(lp blocks.LaunchPad, counter *blocks.IDCounter) string {
var ret string
for _, rocket := range lp.Rockets {
ret += BlockToHTML(rocket, counter)
}
return fmt.Sprintf(`<ul class="launchpad"%s>%s
</ul>`, idAttribute(lp, counter), ret)
}
func imgEntryToHTML(entry blocks.ImgEntry, counter *blocks.IDCounter) string {
var ret string
if entry.Srclink.IsBlueLink() {
ret += fmt.Sprintf(
`<a href="%s"><img src="%s" %s %s></a>`,
entry.Srclink.Href(),
entry.Srclink.ImgSrc(),
entry.SizeWAsAttr(),
entry.SizeHAsAttr())
} else {
ret += fmt.Sprintf(
`<a class="%s" href="%s">Hypha <i>%s</i> does not exist</a>`,
entry.Srclink.Classes(),
entry.Srclink.Href(),
entry.Srclink.TargetHypha())
}
return fmt.Sprintf(`<figure class="img-gallery__entry">
%s
<figcaption>%s</figcaption>
</figure>
`, ret, BlockToHTML(entry.Description(), counter))
}
func imgToHTML(img blocks.Img, counter *blocks.IDCounter) string {
img.MarkExistenceOfSrcLinks()
var ret string
for _, entry := range img.Entries {
ret += BlockToHTML(entry, counter)
}
return fmt.Sprintf(`<section class="img-gallery %s"%s>
%s</section>`,
func() string {
if img.HasOneImage() {
return "img-gallery_one-image"
}
return "img-gallery_many-images"
}(),
idAttribute(img, counter),
ret)
}