summaryrefslogtreecommitdiffstats
path: root/scripts
diff options
context:
space:
mode:
Diffstat (limited to 'scripts')
-rwxr-xr-xscripts/build_content.sh200
-rw-r--r--scripts/metadata_filter.lua225
-rwxr-xr-xscripts/metadata_tpl.json7
3 files changed, 267 insertions, 165 deletions
diff --git a/scripts/build_content.sh b/scripts/build_content.sh
index c640b12..c88ea86 100755
--- a/scripts/build_content.sh
+++ b/scripts/build_content.sh
@@ -23,8 +23,6 @@ get_filters() {
23 23
24FILTERS=$(get_filters) 24FILTERS=$(get_filters)
25 25
26CATEGORIES=$(cat "$METADATA_DIR/categories.json")
27
28get_target_filename() { 26get_target_filename() {
29 filename=$1 27 filename=$1
30 filename=${filename#"$CONTENT_DIR"} 28 filename=${filename#"$CONTENT_DIR"}
@@ -34,43 +32,29 @@ get_target_filename() {
34 echo "$OUTPUT_DIR$filename" 32 echo "$OUTPUT_DIR$filename"
35} 33}
36 34
37get_url_rel() {
38 basedir=$1
39 filename=$2
40 ext=$3
41 filename=$(realpath --relative-to="$basedir" "$filename")
42 filename="${filename%.md}.$ext"
43 if [ "${filename##*/}" = "index.html" ]; then
44 filename=${filename%index.html}
45 fi
46 echo "$filename"
47}
48
49get_url_abs() {
50 echo "/$(get_url_rel "$CONTENT_DIR" "$1" "$2")"
51}
52
53get_metadata() { 35get_metadata() {
54 pandoc "$1" \ 36 pandoc "$2" \
55 -f markdown \ 37 -f markdown \
56 -t plain \ 38 -t plain \
39 --metadata content_dir="$CONTENT_DIR" \
40 --metadata base_file="$1" \
41 --metadata file="$2" \
42 --metadata-file metadata/metadata.yaml \
57 --no-highlight \ 43 --no-highlight \
58 --template scripts/metadata_tpl.json 44 --template scripts/metadata_tpl.json \
45 --lua-filter scripts/metadata_filter.lua
59} 46}
60 47
61get_content() { 48get_content() {
62 pandoc "$1" \ 49 pandoc "$2" \
63 -f markdown \ 50 -f markdown \
64 -t plain \ 51 -t plain \
65 --no-highlight 52 --metadata content_dir="$CONTENT_DIR" \
66} 53 --metadata base_file="$1" \
67 54 --metadata file="$2" \
68get_section() { 55 --metadata-file metadata/metadata.yaml \
69 filename=$1 56 --no-highlight \
70 filename=${filename#"$CONTENT_DIR"} 57 --lua-filter scripts/metadata_filter.lua
71 filename=${filename%.md}
72 filename=$(echo "$filename" | cut -f1 -d /)
73 echo "$filename"
74} 58}
75 59
76get_subpages_basedir() { 60get_subpages_basedir() {
@@ -82,7 +66,7 @@ get_subpages_basedir() {
82 echo "$filename" 66 echo "$filename"
83} 67}
84 68
85get_subpages_meta() { 69get_subpages_metadata() {
86 basedir=$(get_subpages_basedir "$1") 70 basedir=$(get_subpages_basedir "$1")
87 child_pages=() 71 child_pages=()
88 72
@@ -97,98 +81,25 @@ get_subpages_meta() {
97 -print0) 81 -print0)
98 fi 82 fi
99 83
100 pages=$(echo "{}" | jq ". + { \ 84 pages="[]"
101 all: [], \
102 by_year: {}, \
103 }")
104
105 categories="{}"
106 85
107 if [ ${#child_pages[@]} -ne 0 ]; then 86 if [ ${#child_pages[@]} -ne 0 ]; then
108 #echo -e "\033[0;90m[////////]\033[0m Child pages:"
109
110 for file in "${child_pages[@]}"; do 87 for file in "${child_pages[@]}"; do
111 #echo -e "\033[0;90m[////////]\033[0m - $file" 88 metadata=$(get_metadata "$1" "$file" | jq "{ \
112 89 url: .url, \
113 metadata=$(get_metadata "$file") 90 author: .author, \
114 91 title: .title, \
115 section=$(get_section "$file") 92 date: .date, \
116 93 last_update: .last_update, \
117 url_rel=$(get_url_rel "$basedir" "$file" "html") 94 category: .category, \
118 url_abs=$(get_url_abs "$file" "html") 95 content: \"$(get_content "$1" "$file" | sed -z 's/\\/\\\\/g;s/\n/\\n/g;s/"/\"/g')\" \
119 url_full="${SITE%/}$url_abs" 96 } | del(.[] | nulls)")
120 97
121 date=$(echo "$metadata" | jq -r .date) 98 pages=$(echo "$pages" | jq ". += [ $metadata ]")
122 date_rfc3339=""
123
124 if [ "$date" != "" ]; then
125 date_rfc3339=$(date -d "$date" --rfc-3339=seconds | sed 's/ /T/')
126 fi
127
128 last_update=$(echo "$metadata" | jq -r .last_update)
129 last_update_rfc3339=""
130
131 if [ "$last_update" != "" ]; then
132 last_update=$date
133 fi
134
135 if [ "$last_update" != "" ]; then
136 last_update_rfc3339=$(date -d "$last_update" --rfc-3339=seconds | sed 's/ /T/')
137 fi
138
139 category=$(echo "$metadata" | jq -r .category)
140 category_name=""
141
142 if [ "$section" != "" ] && [ "$category" != "" ]; then
143 category_name=$(echo "$CATEGORIES" | jq -r ".$section.$category")
144 fi
145
146 content=$(get_content "$file")
147
148 metadata=$(echo "$metadata" | jq ". + { \
149 content: \"$(echo "$content" | sed -z 's/\\/\\\\/g;s/\n/\\n/g;s/"/\"/g')\", \
150 date: { \
151 yyyy_mm_dd: \"$date\", \
152 rfc3339: \"$date_rfc3339\" \
153 }, \
154 last_update: { \
155 yyyy_mm_dd: \"$last_update\", \
156 rfc3339: \"$last_update_rfc3339\" \
157 }, \
158 url: { \
159 rel: \"$url_rel\", \
160 abs: \"$url_abs\", \
161 full: \"$url_full\" \
162 } \
163 }")
164
165 pages=$(echo "$pages" | jq ".all += [ $metadata ]")
166
167 if [ "$date" != "" ]; then
168 date_year=$(date -d "$date" +%Y)
169 #date_month=$(date -d "$date" +%m)
170 #date_day=$(date -d "$date" +%d)
171
172 pages=$(echo "$pages" | jq ".by_year.\"$date_year\" += [ $metadata ]")
173 fi
174
175 if [ "$category" != "" ]; then
176 categories=$(echo "$categories" | jq ".$category = { name: \"$category_name\", count: (.$category.count + 1) } ")
177 fi
178 done 99 done
179 fi 100 fi
180 101
181 pages=$(echo "$pages" \ 102 echo "{ \"pages\": $pages }"
182 | jq ".all |= (sort_by(.date) | reverse) \
183 | .by_year |= (to_entries | sort_by(.key) | reverse \
184 | .[].value |= (sort_by(.date) | reverse))")
185
186 categories=$(echo "$categories" | jq ". | to_entries | { by_id: (. | sort_by(.key)), by_count: (. | sort_by(.value.count) | reverse) }")
187
188 echo "{}" | jq ". + { \
189 pages: $pages, \
190 categories: $categories
191 }"
192} 103}
193 104
194handle () { 105handle () {
@@ -201,47 +112,10 @@ handle () {
201 112
202 echo -e "\033[0;90m[COMPILE ]\033[0m Getting metadata" 113 echo -e "\033[0;90m[COMPILE ]\033[0m Getting metadata"
203 114
204 included_metadata=$(get_metadata "$1") 115 included_metadata=$(get_metadata "$1" "$1")
116 added_metadata=$(get_subpages_metadata "$1")
205 117
206 create_feed=$(echo "$included_metadata" | jq -r .create_feed) 118 create_feed=$(echo "$included_metadata" | jq -r .create_feed)
207
208 url_abs=$(get_url_abs "$1" "html")
209 url_full="${SITE%/}$url_abs"
210
211 section=$(get_section "$1")
212
213 is_home=false
214 if [ "$target_file" = "${OUTPUT_DIR}index.html" ]; then
215 is_home=true
216 fi
217
218 added_metadata=$(echo "$(get_subpages_meta "$1")" | jq ". + { \
219 url: { \
220 abs: \"$url_abs\", \
221 full: \"$url_full\" \
222 }, \
223 section: { \
224 id: \"$section\", \
225 is_$section: true \
226 }, \
227 is_home: $is_home \
228 }")
229
230 if [ "$create_feed" = "true" ]; then
231 feed_url_abs="$(get_url_abs "$1" "xml")"
232 feed_url_full="${SITE%/}$feed_url_abs"
233 last_update=$(echo "$added_metadata" | jq -r ".pages.all[0].last_update")
234
235 added_metadata=$(echo "$added_metadata" | jq ". + { \
236 feed: { \
237 last_update: $last_update, \
238 url: { \
239 abs: \"$feed_url_abs\", \
240 full: \"$feed_url_full\" \
241 } \
242 } \
243 }")
244 fi
245 119
246 meta_file=$(mktemp) 120 meta_file=$(mktemp)
247 121
@@ -256,8 +130,13 @@ handle () {
256 --no-highlight \ 130 --no-highlight \
257 --template "${TEMPLATES_DIR}feed.xml" \ 131 --template "${TEMPLATES_DIR}feed.xml" \
258 -o "${target_file%.html}.xml" \ 132 -o "${target_file%.html}.xml" \
133 --metadata content_dir="$CONTENT_DIR" \
134 --metadata base_file="$1" \
135 --metadata file="$1" \
136 --metadata type=feed \
259 --metadata-file metadata/metadata.yaml \ 137 --metadata-file metadata/metadata.yaml \
260 --metadata-file "$meta_file" \ 138 --metadata-file "$meta_file" \
139 --lua-filter scripts/metadata_filter.lua \
261 $FILTERS 140 $FILTERS
262 fi 141 fi
263 142
@@ -269,15 +148,18 @@ handle () {
269 --no-highlight \ 148 --no-highlight \
270 --template "${TEMPLATES_DIR}base.html" \ 149 --template "${TEMPLATES_DIR}base.html" \
271 -o "$target_file" \ 150 -o "$target_file" \
151 --metadata content_dir="$CONTENT_DIR" \
152 --metadata base_file="$1" \
153 --metadata file="$1" \
272 --metadata-file metadata/metadata.yaml \ 154 --metadata-file metadata/metadata.yaml \
273 --metadata-file "$meta_file" \ 155 --metadata-file "$meta_file" \
156 --lua-filter scripts/metadata_filter.lua \
274 $FILTERS 157 $FILTERS
275 158
276 rm "$meta_file" 159 rm "$meta_file"
277 160
278 # echo $(pandoc "$1" \ 161 # echo "$included_metadata"
279 # -f markdown \ 162 # echo "$added_metadata"
280 # -t json)
281 163
282 echo -e "\033[0;90m[COMPILE ]\033[0m Done" 164 echo -e "\033[0;90m[COMPILE ]\033[0m Done"
283 else 165 else
diff --git a/scripts/metadata_filter.lua b/scripts/metadata_filter.lua
new file mode 100644
index 0000000..f31bc1f
--- /dev/null
+++ b/scripts/metadata_filter.lua
@@ -0,0 +1,225 @@
1function format_date(date)
2 if date == nil then
3 return date
4 end
5
6 date = pandoc.utils.normalize_date(pandoc.utils.stringify(date))
7 local year, month, day = date:match("(%d%d%d%d)-(%d%d)-(%d%d)")
8 if year == nil then
9 return nil
10 end
11
12 local time = os.time({
13 year = tonumber(year),
14 month = tonumber(month),
15 day = tonumber(day)
16 })
17 return pandoc.MetaMap({
18 yyyy_mm_dd = pandoc.MetaString(os.date("%F", time)),
19 yyyy = pandoc.MetaString(os.date("%Y", time)),
20 mm_dd = pandoc.MetaString(os.date("%m-%d", time)),
21 rfc3339 = pandoc.MetaString(os.date("%FT%T+00:00", time))
22 })
23end
24
25function table_to_list(t, cmp)
26 local l = pandoc.MetaList({})
27
28 for key, value in pairs(t) do
29 l:insert(pandoc.MetaMap({
30 key = key,
31 value = value
32 }))
33 end
34
35 l:sort(cmp or function(i1, i2)
36 return i1.key < i2.key
37 end)
38
39 return l
40end
41
42function splitstr(input, sep)
43 sep = sep or "%s"
44 local t = {}
45 for str in input:gmatch("([^" .. sep .. "]+)") do
46 table.insert(t, str)
47 end
48 return t
49end
50
51function relative_to(dir, target)
52 dir = splitstr(dir, "/")
53 target = splitstr(target, "/")
54
55 local prefix = true
56 local path = ""
57
58 for i = 1, math.min(#dir, #target) do
59 local t = target[i]
60 if prefix then
61 if dir[i] ~= t then
62 prefix = false
63 path = "../" .. t
64 end
65 else
66 path = "../" .. path .. "/" .. t
67 end
68 end
69
70 if #dir < #target then
71 for i = #dir + 1, #target do
72 path = path .. (path == "" and "" or "/") .. target[i]
73 end
74 elseif #dir > #target then
75 for i = #target + 1, #dir do
76 path = "../" .. path
77 end
78 end
79
80 return path
81end
82
83function resolve_url(page_type, site_url, content_dir, base_dir, cur_file)
84 if page_type == "page" then
85 cur_file = cur_file:gsub("%.md$", ".html")
86 elseif page_type == "feed" then
87 cur_file = cur_file:gsub("%.md$", ".xml")
88 end
89
90 local abs = cur_file:gsub("^" .. content_dir, ""):gsub("/index.html$", "/")
91 local rel = relative_to(base_dir, cur_file):gsub("/index.html$", "/")
92
93 return pandoc.MetaMap({
94 abs = pandoc.MetaString(abs),
95 rel = pandoc.MetaString(rel),
96 full = pandoc.MetaString(site_url .. abs)
97 })
98end
99
100function resolve_section(abs_url)
101 local section = abs_url:match("^/(.-)[/.]") or "index"
102 return pandoc.MetaMap({
103 id = pandoc.MetaString(section),
104 ["is_" .. section] = pandoc.MetaBool(true)
105 })
106end
107
108function organize_subpages(site_url, content_dir, base_dir, pages, categories)
109 local categories_data = pandoc.MetaList({})
110
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)
122
123 local pages_data = pandoc.MetaMap({
124 all = pages,
125 by_year = pandoc.MetaList({}),
126 last_update = nil
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
162 current_category.count = current_category.count + 1
163 end
164 end
165 end
166
167 pages_data.by_year = table_to_list(pages_by_year_map, function(i1, i2)
168 return i1.key > i2.key
169 end)
170
171 categories_data = table_to_list(categories_map)
172
173 for _, item in ipairs(categories_data) do
174 item.value.count = pandoc.MetaString(("%d"):format(item.value.count))
175 end
176
177 if #pages_data.by_year ~= 0 then
178 pages_data.last_update = pages_data.by_year[1].value[1].last_update
179 end
180 end
181
182 return pages_data, categories_data
183end
184
185function Meta(meta)
186 meta.content_dir = meta.content_dir:gsub("/$", "")
187 meta.site.url = pandoc.utils.stringify(meta.site.url):gsub("/$", "")
188 meta.base_dir = meta.base_file:gsub("^(.*)/.-$", "%1")
189 meta.type = meta.type or "page"
190
191 meta.date = format_date(meta.date)
192 if meta.last_update ~= nil then
193 meta.last_update = format_date(meta.last_update)
194 else
195 meta.last_update = meta.date
196 end
197
198 if meta.type == "feed" then
199 meta.page = pandoc.MetaMap({
200 url = resolve_url("page", meta.site.url, meta.content_dir, meta.base_dir, meta.file)
201 })
202 end
203
204 meta.url = resolve_url(meta.type, meta.site.url, meta.content_dir, meta.base_dir, meta.file)
205 meta.section = resolve_section(meta.url.abs)
206 meta.categories = meta.categories[meta.section.id]
207
208 if meta.menus and meta.menus.main then
209 for i = 1, #meta.menus.main do
210 local item = meta.menus.main[i]
211 item.active = pandoc.MetaBool(pandoc.utils.stringify(item.id) == meta.section.id)
212 end
213 end
214
215 if meta.pages then
216 local pages, categories = organize_subpages(meta.site.url, meta.content_dir, meta.base_dir, meta.pages,
217 meta.categories)
218 meta.pages = pages
219 meta.categories = categories
220 else
221 meta.categories = nil
222 end
223
224 return meta
225end
diff --git a/scripts/metadata_tpl.json b/scripts/metadata_tpl.json
index 79eabe1..fb36f0f 100755
--- a/scripts/metadata_tpl.json
+++ b/scripts/metadata_tpl.json
@@ -1,6 +1 @@
1{ $meta-json$
2 "title": "$title$",
3 "date": "$date$",
4 "category": "$category$",
5 "create_feed": "$create_feed$"
6}