summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVolpeon <git@volpeon.ink>2021-01-06 14:49:43 +0100
committerVolpeon <git@volpeon.ink>2021-01-06 14:49:43 +0100
commitb26abc90439e5e1d25176ffa80f7ba0b34b3fa43 (patch)
tree14a3fff849ac7de28b37e0c9193e7d0f5ca26630
parentMoved all metadata processing from the build script into a Pandoc Lua filter (diff)
downloadvolpeon.ink-b26abc90439e5e1d25176ffa80f7ba0b34b3fa43.tar.gz
volpeon.ink-b26abc90439e5e1d25176ffa80f7ba0b34b3fa43.tar.bz2
volpeon.ink-b26abc90439e5e1d25176ffa80f7ba0b34b3fa43.zip
Improved subpage organization code in metadata filter
-rwxr-xr-xscripts/build_content.sh21
-rw-r--r--scripts/metadata_filter.lua134
-rw-r--r--templates/base.html1
-rw-r--r--templates/feed.xml58
-rw-r--r--templates/list.html18
5 files changed, 118 insertions, 114 deletions
diff --git a/scripts/build_content.sh b/scripts/build_content.sh
index c88ea86..0b2ce2e 100755
--- a/scripts/build_content.sh
+++ b/scripts/build_content.sh
@@ -48,7 +48,7 @@ get_metadata() {
48get_content() { 48get_content() {
49 pandoc "$2" \ 49 pandoc "$2" \
50 -f markdown \ 50 -f markdown \
51 -t plain \ 51 -t markdown \
52 --metadata content_dir="$CONTENT_DIR" \ 52 --metadata content_dir="$CONTENT_DIR" \
53 --metadata base_file="$1" \ 53 --metadata base_file="$1" \
54 --metadata file="$2" \ 54 --metadata file="$2" \
@@ -131,13 +131,11 @@ handle () {
131 --template "${TEMPLATES_DIR}feed.xml" \ 131 --template "${TEMPLATES_DIR}feed.xml" \
132 -o "${target_file%.html}.xml" \ 132 -o "${target_file%.html}.xml" \
133 --metadata content_dir="$CONTENT_DIR" \ 133 --metadata content_dir="$CONTENT_DIR" \
134 --metadata base_file="$1" \
135 --metadata file="$1" \ 134 --metadata file="$1" \
136 --metadata type=feed \ 135 --metadata page_type=feed \
137 --metadata-file metadata/metadata.yaml \ 136 --metadata-file metadata/metadata.yaml \
138 --metadata-file "$meta_file" \ 137 --metadata-file "$meta_file" \
139 --lua-filter scripts/metadata_filter.lua \ 138 --lua-filter scripts/metadata_filter.lua
140 $FILTERS
141 fi 139 fi
142 140
143 echo -e "\033[0;90m[COMPILE ]\033[0m Creating page" 141 echo -e "\033[0;90m[COMPILE ]\033[0m Creating page"
@@ -149,13 +147,24 @@ handle () {
149 --template "${TEMPLATES_DIR}base.html" \ 147 --template "${TEMPLATES_DIR}base.html" \
150 -o "$target_file" \ 148 -o "$target_file" \
151 --metadata content_dir="$CONTENT_DIR" \ 149 --metadata content_dir="$CONTENT_DIR" \
152 --metadata base_file="$1" \
153 --metadata file="$1" \ 150 --metadata file="$1" \
154 --metadata-file metadata/metadata.yaml \ 151 --metadata-file metadata/metadata.yaml \
155 --metadata-file "$meta_file" \ 152 --metadata-file "$meta_file" \
156 --lua-filter scripts/metadata_filter.lua \ 153 --lua-filter scripts/metadata_filter.lua \
157 $FILTERS 154 $FILTERS
158 155
156 # echo "$(pandoc "$1" \
157 # -f markdown \
158 # -t plain \
159 # --no-highlight \
160 # --template scripts/metadata_tpl.json \
161 # --metadata content_dir="$CONTENT_DIR" \
162 # --metadata file="$1" \
163 # --metadata-file metadata/metadata.yaml \
164 # --metadata-file "$meta_file" \
165 # --lua-filter scripts/metadata_filter.lua \
166 # $FILTERS)"
167
159 rm "$meta_file" 168 rm "$meta_file"
160 169
161 # echo "$included_metadata" 170 # echo "$included_metadata"
diff --git a/scripts/metadata_filter.lua b/scripts/metadata_filter.lua
index f31bc1f..d6379b9 100644
--- a/scripts/metadata_filter.lua
+++ b/scripts/metadata_filter.lua
@@ -23,7 +23,7 @@ function format_date(date)
23end 23end
24 24
25function table_to_list(t, cmp) 25function table_to_list(t, cmp)
26 local l = pandoc.MetaList({}) 26 local l = pandoc.List()
27 27
28 for key, value in pairs(t) do 28 for key, value in pairs(t) do
29 l:insert(pandoc.MetaMap({ 29 l:insert(pandoc.MetaMap({
@@ -39,6 +39,32 @@ function table_to_list(t, cmp)
39 return l 39 return l
40end 40end
41 41
42function group_by(l, field, insert)
43 insert = insert or function(group, _, item)
44 if not group then
45 group = l:new({item})
46 return group
47 end
48 group:insert(item)
49 end
50
51 local groups = {}
52
53 for i = 1, #l do
54 local item = l[i]
55 local f = field(item)
56
57 if f ~= nil then
58 local out = insert(groups[f], f, item)
59 if out ~= nil then
60 groups[f] = out
61 end
62 end
63 end
64
65 return groups
66end
67
42function splitstr(input, sep) 68function splitstr(input, sep)
43 sep = sep or "%s" 69 sep = sep or "%s"
44 local t = {} 70 local t = {}
@@ -106,77 +132,50 @@ function resolve_section(abs_url)
106end 132end
107 133
108function organize_subpages(site_url, content_dir, base_dir, pages, categories) 134function organize_subpages(site_url, content_dir, base_dir, pages, categories)
109 local categories_data = pandoc.MetaList({}) 135 local grouped_pages = group_by(pages, function(p)
110 136 return not p.date
111 pages:sort(function(p1, p2)
112 if p1.date and p2.date then
113 return pandoc.utils.stringify(p1.date.yyyy_mm_dd) > pandoc.utils.stringify(p2.date.yyyy_mm_dd)
114 elseif p2.date then
115 return true
116 elseif p1.date then
117 return false
118 else
119 return pandoc.utils.stringify(p1.title) < pandoc.utils.stringify(p2.title)
120 end
121 end) 137 end)
122 138
123 local pages_data = pandoc.MetaMap({ 139 local pages_undated = grouped_pages[true] or pandoc.MetaList({})
124 all = pages, 140 pages_undated:sort(function(p1, p2)
125 by_year = pandoc.MetaList({}), 141 return pandoc.utils.stringify(p1.title) < pandoc.utils.stringify(p2.title)
126 last_update = nil 142 end)
127 })
128
129 if pages then
130 local pages_by_year_map = {}
131 local categories_map = {}
132
133 for i = 1, #pages do
134 local page = pages[i]
135
136 if page.date then
137 local yyyy = pandoc.utils.stringify(page.date.yyyy)
138 local pages_by_yyyy = pages_by_year_map[yyyy]
139
140 if not pages_by_yyyy then
141 pages_by_yyyy = pandoc.MetaList(pandoc.List())
142 pages_by_year_map[yyyy] = pages_by_yyyy
143 end
144
145 pages_by_yyyy:insert(page)
146 end
147
148 if page.category and categories then
149 local category = pandoc.utils.stringify(page.category)
150
151 if categories[category] then
152 local current_category = categories_map[category]
153
154 if not current_category then
155 current_category = pandoc.MetaMap({
156 name = pandoc.MetaString(categories[category]),
157 count = 0
158 })
159 categories_map[category] = current_category
160 end
161 143
162 current_category.count = current_category.count + 1 144 local pages_dated = grouped_pages[false] or pandoc.MetaList({})
163 end 145 pages_dated:sort(function(p1, p2)
164 end 146 return pandoc.utils.stringify(p1.date.yyyy_mm_dd) > pandoc.utils.stringify(p2.date.yyyy_mm_dd)
165 end 147 end)
166 148
167 pages_data.by_year = table_to_list(pages_by_year_map, function(i1, i2) 149 local pages_by_year = group_by(pages_dated, function(p)
168 return i1.key > i2.key 150 return pandoc.utils.stringify(p.date.yyyy)
169 end) 151 end)
152 pages_by_year = pandoc.MetaList(table_to_list(pages_by_year, function(i1, i2)
153 return i1.key > i2.key
154 end))
170 155
171 categories_data = table_to_list(categories_map) 156 local pages_data = pandoc.MetaMap({
157 all_dated = pages_dated,
158 all_undated = pages_undated,
159 by_year = pages_by_year,
160 last_update = #pages_dated ~= 0 and pages_dated[1].last_update
161 })
172 162
173 for _, item in ipairs(categories_data) do 163 local categories_data = group_by(pages_dated, function(p)
174 item.value.count = pandoc.MetaString(("%d"):format(item.value.count)) 164 return p.category and pandoc.utils.stringify(p.category)
165 end, function(stats, category)
166 if not stats then
167 return {
168 name = pandoc.MetaString(categories[category] or category),
169 count = 1
170 }
171 else
172 stats.count = stats.count + 1
175 end 173 end
174 end)
175 categories_data = pandoc.MetaList(table_to_list(categories_data))
176 176
177 if #pages_data.by_year ~= 0 then 177 for i = 1, #categories_data do
178 pages_data.last_update = pages_data.by_year[1].value[1].last_update 178 categories_data[i].value.count = pandoc.MetaString(("%d"):format(categories_data[i].value.count))
179 end
180 end 179 end
181 180
182 return pages_data, categories_data 181 return pages_data, categories_data
@@ -185,8 +184,9 @@ end
185function Meta(meta) 184function Meta(meta)
186 meta.content_dir = meta.content_dir:gsub("/$", "") 185 meta.content_dir = meta.content_dir:gsub("/$", "")
187 meta.site.url = pandoc.utils.stringify(meta.site.url):gsub("/$", "") 186 meta.site.url = pandoc.utils.stringify(meta.site.url):gsub("/$", "")
187 meta.base_file = meta.base_file or meta.file
188 meta.base_dir = meta.base_file:gsub("^(.*)/.-$", "%1") 188 meta.base_dir = meta.base_file:gsub("^(.*)/.-$", "%1")
189 meta.type = meta.type or "page" 189 meta.page_type = meta.page_type or "page"
190 190
191 meta.date = format_date(meta.date) 191 meta.date = format_date(meta.date)
192 if meta.last_update ~= nil then 192 if meta.last_update ~= nil then
@@ -195,13 +195,13 @@ function Meta(meta)
195 meta.last_update = meta.date 195 meta.last_update = meta.date
196 end 196 end
197 197
198 if meta.type == "feed" then 198 if meta.page_type == "feed" then
199 meta.page = pandoc.MetaMap({ 199 meta.page = pandoc.MetaMap({
200 url = resolve_url("page", meta.site.url, meta.content_dir, meta.base_dir, meta.file) 200 url = resolve_url("page", meta.site.url, meta.content_dir, meta.base_dir, meta.file)
201 }) 201 })
202 end 202 end
203 203
204 meta.url = resolve_url(meta.type, meta.site.url, meta.content_dir, meta.base_dir, meta.file) 204 meta.url = resolve_url(meta.page_type, meta.site.url, meta.content_dir, meta.base_dir, meta.file)
205 meta.section = resolve_section(meta.url.abs) 205 meta.section = resolve_section(meta.url.abs)
206 meta.categories = meta.categories[meta.section.id] 206 meta.categories = meta.categories[meta.section.id]
207 207
diff --git a/templates/base.html b/templates/base.html
index 3e079a1..b4a2953 100644
--- a/templates/base.html
+++ b/templates/base.html
@@ -6,6 +6,7 @@
6 <meta name="viewport" content="width=device-width, initial-scale=1" /> 6 <meta name="viewport" content="width=device-width, initial-scale=1" />
7 <meta name="robots" content="noindex" /> 7 <meta name="robots" content="noindex" />
8 8
9 <link rel="canonical" href="$url.full$">
9 <link rel="shortcut icon" href="/favicon.ico" /> 10 <link rel="shortcut icon" href="/favicon.ico" />
10 11
11 $for(author)$ 12 $for(author)$
diff --git a/templates/feed.xml b/templates/feed.xml
index 083049e..a968205 100644
--- a/templates/feed.xml
+++ b/templates/feed.xml
@@ -14,37 +14,33 @@
14 </author> 14 </author>
15 $endfor$ 15 $endfor$
16 16
17 $for(pages)$ 17 $for(pages.all_dated)$
18 $for(it.all)$ 18 <entry>
19 $if(it.date)$ 19 <id>$it.url.full$</id>
20 <entry> 20 <title type="html"><![CDATA[$it.title$]]></title>
21 <id>$it.url.full$</id> 21 $if(it.author)$
22 <title type="html"><![CDATA[$it.title$]]></title> 22 $for(it.author)$
23 $if(it.author)$ 23 <author>
24 $for(it.author)$ 24 <name>$it.name$</name>
25 <author> 25 $if(it.email)$
26 <name>$it.name$</name> 26 <email>$it.email$</email>
27 $if(it.email)$ 27 $endif$
28 <email>$it.email$</email> 28 </author>
29 $endif$ 29 $endfor$
30 </author> 30 $else$
31 $endfor$ 31 $for(author)$
32 $else$ 32 <author>
33 $for(author)$ 33 <name>$author.name$</name>
34 <author> 34 $if(author.email)$
35 <name>$author.name$</name> 35 <email>$author.email$</email>
36 $if(author.email)$ 36 $endif$
37 <email>$author.email$</email> 37 </author>
38 $endif$ 38 $endfor$
39 </author>
40 $endfor$
41 $endif$
42 <link href="$it.url.full$"/>
43 <published>$it.date.rfc3339$</published>
44 <updated>$it.last_update.rfc3339$</updated>
45 <content type="html"><![CDATA[$it.content$]]></content>
46 </entry>
47 $endif$ 39 $endif$
48 $endfor$ 40 <link href="$it.url.full$"/>
41 <published>$it.date.rfc3339$</published>
42 <updated>$it.last_update.rfc3339$</updated>
43 <content type="html"><![CDATA[$it.content$]]></content>
44 </entry>
49 $endfor$ 45 $endfor$
50</feed> 46</feed>
diff --git a/templates/list.html b/templates/list.html
index 2d663e0..d74681c 100644
--- a/templates/list.html
+++ b/templates/list.html
@@ -8,15 +8,13 @@ $if(categories)$
8 </ul> 8 </ul>
9$endif$ 9$endif$
10 10
11$for(pages)$ 11$for(pages.by_year)$
12 $for(it.by_year)$ 12 <ul>
13 <ul> 13 $for(it.value)$
14 $for(it.value)$ 14 <li class="c-page__prefixed c-page__prefixed--ref">
15 <li class="c-page__prefixed c-page__prefixed--ref"> 15 <a href="$it.url.rel$">$it.category/uppercase$ $it.date.yyyy_mm_dd$ - $it.title$</a>
16 <a href="$it.url.rel$">$it.category/uppercase$ $it.date.yyyy_mm_dd$ - $it.title$</a> 16 </li>
17 </li> 17 $endfor$
18 $endfor$ 18 </ul>
19 </ul>
20 $endfor$
21$endfor$ 19$endfor$
22 20