This repository has been archived by the owner on Jun 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
blocks.v
280 lines (231 loc) · 6.09 KB
/
blocks.v
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
module vaunt
import json
import net.urllib
import os
import vweb
// Generate Block Html
// =======================================
pub struct Block {
pub:
id string
block_type string @[json: 'type']
data string @[required]
}
// generate returns the html form of `blocks`.
pub fn generate(blocks []Block) string {
mut html := ''
for idx, block in blocks {
html += match block.block_type {
'heading' {
generate_heading(block, idx)
}
'paragraph' {
generate_paragraph(block)
}
'linkTool' {
generate_link(block)
}
'image' {
generate_image(block)
}
'embed' {
generate_embed(block)
}
'quote' {
generate_quote(block)
}
'table' {
generate_table(block)
}
'code' {
generate_code(block)
}
'list' {
generate_list(block)
}
'alert' {
generate_alert(block)
}
else {
''
}
}
}
return html
}
pub struct HeadingData {
pub:
text string
level int
}
pub fn generate_heading(block &Block, block_index int) string {
data := json.decode(HeadingData, block.data) or { HeadingData{} }
data_id := sanitize_path(data.text)
id_name := data_id + '-' + block_index.str()
if data.level == 1 {
return $tmpl('./templates/blocks/h1.html')
} else if data.level == 2 {
return $tmpl('./templates/blocks/h2.html')
} else if data.level == 3 {
return $tmpl('./templates/blocks/h3.html')
} else if data.level == 4 {
return $tmpl('./templates/blocks/h4.html')
} else if data.level == 5 {
return $tmpl('./templates/blocks/h5.html')
} else if data.level == 6 {
return $tmpl('./templates/blocks/h6.html')
} else {
return ''
}
}
pub struct ParagraphData {
pub:
text string
}
pub fn generate_paragraph(block &Block) string {
data := json.decode(ParagraphData, block.data) or { ParagraphData{} }
return $tmpl('./templates/blocks/p.html')
}
pub fn generate_link(block &Block) string {
data := json.decode(LinkData, block.data) or { LinkData{} }
url := urllib.parse(data.link) or { urllib.URL{} }
anchor := '${url.scheme}://${url.host}'
return $tmpl('./templates/blocks/link.html')
}
pub struct ImageData {
pub:
caption string
file map[string]string
}
pub fn generate_image(block &Block) string {
data := json.decode(ImageData, block.data) or { ImageData{} }
mut url := data.file['url']
// properties for the html `picture` `srcset`.
mut url_small, mut url_medium := '', ''
name := os.file_name(url)
alt := if data.caption != '' { '[${data.caption}]' } else { '[${name}]' }
if url.starts_with('http://127.0.0.1') || url.starts_with('http://localhost') {
// image is local
url_s := urllib.parse(data.file['url']) or { urllib.URL{} }
url = url_s.path
url_small = os.dir(url) + '/small/' + name
url_medium = os.dir(url) + '/medium/' + name
wd := os.getwd()
// check if the small and medium image path exist
if os.exists(os.join_path(wd, url_small[1..])) == false {
url_small = ''
}
if os.exists(os.join_path(wd, url_medium[1..])) == false {
url_medium = ''
}
}
picture := get_html_picture_from_src(url, alt)
return $tmpl('./templates/blocks/img.html')
}
// get_html_picture_from_url returns a `picture` html element containing 3
// sizes of the image: small (640px), medium (1280px) and full-size, if they exist
// in the `uploads` folder.
pub fn get_html_picture_from_src(url string, alt string) vweb.RawHtml {
// properties for the html `picture` `srcset`.
mut url_small, mut url_medium := '', ''
name := os.file_name(url)
url_small = os.dir(url) + '/small/' + name
url_medium = os.dir(url) + '/medium/' + name
wd := os.getwd()
// check if the small and medium image path exist
if os.exists(os.join_path(wd, url_small[1..])) == false {
url_small = ''
}
if os.exists(os.join_path(wd, url_medium[1..])) == false {
url_medium = ''
}
return $tmpl('./templates/blocks/picture.html')
}
pub struct EmbedData {
pub:
service string
source string @[skip]
embed string
width int
height int
caption string
}
pub fn generate_embed(block &Block) string {
data := json.decode(EmbedData, block.data) or { EmbedData{} }
return $tmpl('./templates/blocks/embed.html')
}
pub struct QuoteData {
pub:
text string
caption string
}
pub fn generate_quote(block &Block) string {
data := json.decode(QuoteData, block.data) or { QuoteData{} }
return $tmpl('./templates/blocks/quote.html')
}
pub struct TableData {
pub mut:
with_headings bool @[json: withHeadings]
content [][]string
}
pub fn generate_table(block &Block) string {
mut data := json.decode(TableData, block.data) or { TableData{} }
mut table_headers := []string{}
if data.with_headings {
table_headers = data.content[0]
data.content.delete(0)
}
table_rows := data.content
return $tmpl('./templates/blocks/table.html')
}
pub struct CodeData {
pub:
language string
pub mut:
code string
}
pub fn generate_code(block &Block) string {
mut data := json.decode(CodeData, block.data) or { CodeData{} }
// escape html tags
data.code = data.code.replace('<', '<')
data.code = data.code.replace('>', '>')
lang_class := 'language-${data.language.to_lower()}'
return $tmpl('./templates/blocks/code.html')
}
pub struct ListData {
pub mut:
style string
items []ListItem
}
pub struct ListItem {
pub mut:
content string
items []ListItem
}
fn generate_li(data ListItem, list_type string) string {
if data.items.len > 0 {
lis := data.items.map(fn [list_type] (item ListItem) string {
return generate_li(item, list_type)
})
return '<li>${data.content}\n<${list_type}>${lis.join_lines()}</${list_type}></li>'
} else {
return '<li>${data.content}</li>'
}
}
pub fn generate_list(block &Block) string {
mut data := json.decode(ListData, block.data) or { ListData{} }
list_type := if data.style == 'ordered' { 'ol' } else { 'ul' }
lis := data.items.map(fn [list_type] (item ListItem) string {
return generate_li(item, list_type)
})
return '<${list_type}>${lis.join_lines()}</${list_type}>'
}
pub struct AlertData {
pub:
typ string
text string
}
pub fn generate_alert(block &Block) string {
mut data := json.decode(AlertData, block.data) or { AlertData{} }
return $tmpl('./templates/blocks/alert.html')
}