diff options
author | Volpeon <git@volpeon.ink> | 2021-01-10 13:24:21 +0100 |
---|---|---|
committer | Volpeon <git@volpeon.ink> | 2021-01-10 13:24:21 +0100 |
commit | 3ad6bd151364a3278618e3a26e21918d17680d24 (patch) | |
tree | 507d5de22d1e57ab86d4d3321d50cab86a3b38e8 /scripts | |
parent | Added ream journal entry 1 (diff) | |
download | volpeon.ink-3ad6bd151364a3278618e3a26e21918d17680d24.tar.gz volpeon.ink-3ad6bd151364a3278618e3a26e21918d17680d24.tar.bz2 volpeon.ink-3ad6bd151364a3278618e3a26e21918d17680d24.zip |
Moved output filename handling into Pandoc filter, implemented path rewriting
Diffstat (limited to 'scripts')
-rwxr-xr-x | scripts/build_content.sh | 73 | ||||
-rw-r--r-- | scripts/metadata_filter.lua | 79 | ||||
-rwxr-xr-x | scripts/watch_content.sh | 3 |
3 files changed, 85 insertions, 70 deletions
diff --git a/scripts/build_content.sh b/scripts/build_content.sh index 63bc525..97f57a3 100755 --- a/scripts/build_content.sh +++ b/scripts/build_content.sh | |||
@@ -26,29 +26,16 @@ FILTERS=$(get_filters) | |||
26 | get_filename_out() { | 26 | get_filename_out() { |
27 | local filename=$1 | 27 | local filename=$1 |
28 | filename=${filename#"$CONTENT_DIR"} | 28 | filename=${filename#"$CONTENT_DIR"} |
29 | if [ "${filename##*.}" = "md" ]; then | ||
30 | filename="${filename%.md}.html" | ||
31 | fi | ||
32 | if [ "${filename##*/}" != "index.html" ]; then | ||
33 | filename="${filename%.html}/index.html" | ||
34 | fi | ||
35 | echo "$OUTPUT_DIR$filename" | 29 | echo "$OUTPUT_DIR$filename" |
36 | } | 30 | } |
37 | 31 | ||
38 | get_metadata() { | 32 | get_metadata() { |
39 | local file_out=$(get_filename_out "$1") | ||
40 | local base_file_out="$2" | ||
41 | |||
42 | if [ -z "$2" ]; then | ||
43 | base_file_out="$file_out" | ||
44 | fi | ||
45 | |||
46 | pandoc "$1" \ | 33 | pandoc "$1" \ |
47 | -f markdown \ | 34 | -f markdown \ |
48 | -t plain \ | 35 | -t plain \ |
36 | --metadata content_dir="$CONTENT_DIR" \ | ||
49 | --metadata output_dir="$OUTPUT_DIR" \ | 37 | --metadata output_dir="$OUTPUT_DIR" \ |
50 | --metadata base_file_out="$base_file_out" \ | 38 | --metadata file_in="$1" \ |
51 | --metadata file_out="$file_out" \ | ||
52 | --metadata-file metadata/metadata.yaml \ | 39 | --metadata-file metadata/metadata.yaml \ |
53 | --no-highlight \ | 40 | --no-highlight \ |
54 | --template scripts/metadata_tpl.json \ | 41 | --template scripts/metadata_tpl.json \ |
@@ -56,19 +43,12 @@ get_metadata() { | |||
56 | } | 43 | } |
57 | 44 | ||
58 | get_content() { | 45 | get_content() { |
59 | local file_out=$(get_filename_out "$1") | ||
60 | local base_file_out="$2" | ||
61 | |||
62 | if [ -z "$2" ]; then | ||
63 | base_file_out="$file_out" | ||
64 | fi | ||
65 | |||
66 | pandoc "$1" \ | 46 | pandoc "$1" \ |
67 | -f markdown \ | 47 | -f markdown \ |
68 | -t markdown \ | 48 | -t markdown \ |
49 | --metadata content_dir="$CONTENT_DIR" \ | ||
69 | --metadata output_dir="$OUTPUT_DIR" \ | 50 | --metadata output_dir="$OUTPUT_DIR" \ |
70 | --metadata base_file_out="$base_file_out" \ | 51 | --metadata file_in="$1" \ |
71 | --metadata file_out="$file_out" \ | ||
72 | --metadata-file metadata/metadata.yaml \ | 52 | --metadata-file metadata/metadata.yaml \ |
73 | --no-highlight \ | 53 | --no-highlight \ |
74 | --lua-filter scripts/metadata_filter.lua | 54 | --lua-filter scripts/metadata_filter.lua |
@@ -84,9 +64,7 @@ get_subpages_basedir() { | |||
84 | } | 64 | } |
85 | 65 | ||
86 | get_subpages_metadata() { | 66 | get_subpages_metadata() { |
87 | local base_file_in=$1 | 67 | local base_dir_in=$(get_subpages_basedir "$1") |
88 | local base_file_out=$2 | ||
89 | local base_dir_in=$(get_subpages_basedir "$base_file_in") | ||
90 | local child_pages=() | 68 | local child_pages=() |
91 | 69 | ||
92 | if [ -d "$base_dir_in" ]; then | 70 | if [ -d "$base_dir_in" ]; then |
@@ -103,10 +81,10 @@ get_subpages_metadata() { | |||
103 | local pages="[]" | 81 | local pages="[]" |
104 | 82 | ||
105 | for file_in in "${child_pages[@]}"; do | 83 | for file_in in "${child_pages[@]}"; do |
106 | local content=$(get_content "$file_in" "$base_file_out" | sed -z 's/\\/\\\\/g;s/\n/\\n/g;s/"/\"/g') | 84 | local content=$(get_content "$file_in" | sed -z 's/\\/\\\\/g;s/\n/\\n/g;s/"/\"/g') |
107 | 85 | ||
108 | local metadata=$(get_metadata "$file_in" "$base_file_out" | jq "{ \ | 86 | local metadata=$(get_metadata "$file_in" | jq "{ \ |
109 | url: .url, \ | 87 | file_out: .file_out, \ |
110 | author: .author, \ | 88 | author: .author, \ |
111 | title: .title, \ | 89 | title: .title, \ |
112 | date: .date, \ | 90 | date: .date, \ |
@@ -122,19 +100,18 @@ get_subpages_metadata() { | |||
122 | } | 100 | } |
123 | 101 | ||
124 | handle () { | 102 | handle () { |
125 | local file_out=$(get_filename_out "$1") | 103 | if [ "${1#*.}" = "md" ]; then |
104 | local included_metadata=$(get_metadata "$1") | ||
105 | local file_out=$(echo "$included_metadata" | jq -r .file_out) | ||
106 | local create_feed=$(echo "$included_metadata" | jq -r .create_feed) | ||
126 | 107 | ||
127 | mkdir -p $(dirname "$file_out") | 108 | mkdir -p $(dirname "$file_out") |
128 | 109 | ||
129 | if [ "${1#*.}" = "md" ]; then | ||
130 | echo -e "\033[0;32m[COMPILE ]\033[0m $1 -> $file_out" | 110 | echo -e "\033[0;32m[COMPILE ]\033[0m $1 -> $file_out" |
131 | 111 | ||
132 | echo -e "\033[0;90m[COMPILE ]\033[0m Getting metadata" | 112 | echo -e "\033[0;90m[COMPILE ]\033[0m Getting subpages" |
133 | |||
134 | local included_metadata=$(get_metadata "$1") | ||
135 | local added_metadata=$(get_subpages_metadata "$1" "$file_out") | ||
136 | 113 | ||
137 | local create_feed=$(echo "$included_metadata" | jq -r .create_feed) | 114 | local added_metadata=$(get_subpages_metadata "$1") |
138 | 115 | ||
139 | local meta_file=$(mktemp) | 116 | local meta_file=$(mktemp) |
140 | 117 | ||
@@ -143,16 +120,15 @@ handle () { | |||
143 | if [ "$create_feed" = "true" ]; then | 120 | if [ "$create_feed" = "true" ]; then |
144 | echo -e "\033[0;90m[COMPILE ]\033[0m Creating feed" | 121 | echo -e "\033[0;90m[COMPILE ]\033[0m Creating feed" |
145 | 122 | ||
146 | local feed_file_out="${file_out%.html}.xml" | ||
147 | |||
148 | pandoc "$1" \ | 123 | pandoc "$1" \ |
149 | -f markdown \ | 124 | -f markdown \ |
150 | -t html5 \ | 125 | -t html5 \ |
151 | --no-highlight \ | 126 | --no-highlight \ |
152 | --template "${TEMPLATES_DIR}feed.xml" \ | 127 | --template "${TEMPLATES_DIR}feed.xml" \ |
153 | -o "$feed_file_out" \ | 128 | -o "${file_out%.html}.xml" \ |
129 | --metadata content_dir="$CONTENT_DIR" \ | ||
154 | --metadata output_dir="$OUTPUT_DIR" \ | 130 | --metadata output_dir="$OUTPUT_DIR" \ |
155 | --metadata file_out="$feed_file_out" \ | 131 | --metadata file_in="$1" \ |
156 | --metadata page_type=feed \ | 132 | --metadata page_type=feed \ |
157 | --metadata-file metadata/metadata.yaml \ | 133 | --metadata-file metadata/metadata.yaml \ |
158 | --metadata-file "$meta_file" \ | 134 | --metadata-file "$meta_file" \ |
@@ -167,8 +143,9 @@ handle () { | |||
167 | --no-highlight \ | 143 | --no-highlight \ |
168 | --template "${TEMPLATES_DIR}base.html" \ | 144 | --template "${TEMPLATES_DIR}base.html" \ |
169 | -o "$file_out" \ | 145 | -o "$file_out" \ |
146 | --metadata content_dir="$CONTENT_DIR" \ | ||
170 | --metadata output_dir="$OUTPUT_DIR" \ | 147 | --metadata output_dir="$OUTPUT_DIR" \ |
171 | --metadata file_out="$file_out" \ | 148 | --metadata file_in="$1" \ |
172 | --metadata-file metadata/metadata.yaml \ | 149 | --metadata-file metadata/metadata.yaml \ |
173 | --metadata-file "$meta_file" \ | 150 | --metadata-file "$meta_file" \ |
174 | --lua-filter scripts/metadata_filter.lua \ | 151 | --lua-filter scripts/metadata_filter.lua \ |
@@ -179,8 +156,9 @@ handle () { | |||
179 | # -t plain \ | 156 | # -t plain \ |
180 | # --no-highlight \ | 157 | # --no-highlight \ |
181 | # --template scripts/metadata_tpl.json \ | 158 | # --template scripts/metadata_tpl.json \ |
159 | # --metadata content_dir="$CONTENT_DIR" \ | ||
182 | # --metadata output_dir="$OUTPUT_DIR" \ | 160 | # --metadata output_dir="$OUTPUT_DIR" \ |
183 | # --metadata file_out="$file_out" \ | 161 | # --metadata file_in="$1" \ |
184 | # --metadata-file metadata/metadata.yaml \ | 162 | # --metadata-file metadata/metadata.yaml \ |
185 | # --metadata-file "$meta_file" \ | 163 | # --metadata-file "$meta_file" \ |
186 | # --lua-filter scripts/metadata_filter.lua \ | 164 | # --lua-filter scripts/metadata_filter.lua \ |
@@ -188,11 +166,12 @@ handle () { | |||
188 | 166 | ||
189 | rm "$meta_file" | 167 | rm "$meta_file" |
190 | 168 | ||
191 | # echo "$included_metadata" | ||
192 | # echo "$added_metadata" | ||
193 | |||
194 | echo -e "\033[0;90m[COMPILE ]\033[0m Done" | 169 | echo -e "\033[0;90m[COMPILE ]\033[0m Done" |
195 | else | 170 | else |
171 | local file_out=$(get_filename_out "$1") | ||
172 | |||
173 | mkdir -p $(dirname "$file_out") | ||
174 | |||
196 | echo -e "\033[0;32m[COPY ]\033[0m $1 -> $file_out" | 175 | echo -e "\033[0;32m[COPY ]\033[0m $1 -> $file_out" |
197 | 176 | ||
198 | cp "$1" "$file_out" | 177 | cp "$1" "$file_out" |
diff --git a/scripts/metadata_filter.lua b/scripts/metadata_filter.lua index 61fe90f..5ed5382 100644 --- a/scripts/metadata_filter.lua +++ b/scripts/metadata_filter.lua | |||
@@ -92,11 +92,33 @@ function relative_to(dir, target) | |||
92 | return path | 92 | return path |
93 | end | 93 | end |
94 | 94 | ||
95 | function resolve_url(page_type, site_url, output_dir, base_dir_out, cur_file_out) | 95 | function apply_path_rewrites(rewrites, str) |
96 | local abs = cur_file_out:gsub("^" .. output_dir, ""):gsub("/index.html$", | 96 | for i = 1, #rewrites.path do |
97 | "/") | 97 | local r = rewrites.path[i] |
98 | str = str:gsub(pandoc.utils.stringify(r.from), | ||
99 | pandoc.utils.stringify(r.to)) | ||
100 | end | ||
101 | return str | ||
102 | end | ||
103 | |||
104 | function get_file_out(rewrites, content_dir, output_dir, file_in) | ||
105 | local file_out = file_in:gsub("^" .. content_dir, ""):gsub("%.md$", ".html") | ||
106 | |||
107 | if file_out:match(".html$") and not file_out:match("/index%.html$") then | ||
108 | file_out = file_out:gsub("/(.*)%.html$", "/%1/index.html") | ||
109 | end | ||
110 | |||
111 | file_out = apply_path_rewrites(rewrites, file_out) | ||
112 | |||
113 | return pandoc.MetaString(output_dir .. file_out) | ||
114 | end | ||
115 | |||
116 | function resolve_url(site_url, output_dir, ref_file, target_file) | ||
117 | local ref_base_dir = ref_file:gsub("^(.*)/.-$", "%1") | ||
118 | local abs = target_file:gsub("^" .. output_dir, ""):gsub("/index%.html$", | ||
119 | "/") | ||
98 | local rel = | 120 | local rel = |
99 | relative_to(base_dir_out, cur_file_out):gsub("/index.html$", "/") | 121 | relative_to(ref_base_dir, target_file):gsub("/index%.html$", "/") |
100 | 122 | ||
101 | return pandoc.MetaMap({ | 123 | return pandoc.MetaMap({ |
102 | abs = pandoc.MetaString(abs), | 124 | abs = pandoc.MetaString(abs), |
@@ -115,8 +137,9 @@ function resolve_layout(layout) | |||
115 | end | 137 | end |
116 | end | 138 | end |
117 | 139 | ||
118 | function resolve_section(abs_url) | 140 | function resolve_section(content_dir, file_in) |
119 | local section = abs_url:match("^/(.-)[/.]") or "index" | 141 | local section = file_in:gsub("^" .. content_dir, ""):match("^/(.-)[/.]") or |
142 | "index" | ||
120 | return pandoc.MetaMap({ | 143 | return pandoc.MetaMap({ |
121 | id = pandoc.MetaString(section), | 144 | id = pandoc.MetaString(section), |
122 | ["is_" .. section] = pandoc.MetaBool(true) | 145 | ["is_" .. section] = pandoc.MetaBool(true) |
@@ -133,13 +156,15 @@ function resolve_category(categories, category) | |||
133 | end | 156 | end |
134 | end | 157 | end |
135 | 158 | ||
136 | function create_main_menu_state(section, main_menu) | 159 | function prep_main_menu(rewrites, section, main_menu) |
137 | local active_item = nil | 160 | local active_item = nil |
138 | 161 | ||
139 | for i = 1, #main_menu do | 162 | for i = 1, #main_menu do |
140 | local item = main_menu[i] | 163 | local item = main_menu[i] |
141 | local active = pandoc.utils.stringify(item.id) == section.id | 164 | local active = pandoc.utils.stringify(item.id) == section.id |
142 | item.active = pandoc.MetaBool(active) | 165 | item.active = pandoc.MetaBool(active) |
166 | item.url = apply_path_rewrites(rewrites, | ||
167 | pandoc.utils.stringify(item.url)) | ||
143 | if active then active_item = item end | 168 | if active then active_item = item end |
144 | end | 169 | end |
145 | 170 | ||
@@ -151,7 +176,13 @@ function create_main_menu_state(section, main_menu) | |||
151 | }) | 176 | }) |
152 | end | 177 | end |
153 | 178 | ||
154 | function organize_subpages(pages) | 179 | function organize_subpages(site_url, output_dir, ref_file, pages) |
180 | for i = 1, #pages do | ||
181 | local page = pages[i] | ||
182 | page.url = resolve_url(site_url, output_dir, ref_file, | ||
183 | pandoc.utils.stringify(page.file_out)) | ||
184 | end | ||
185 | |||
155 | local grouped_pages = group_by(pages, function(p) return not p.date end) | 186 | local grouped_pages = group_by(pages, function(p) return not p.date end) |
156 | 187 | ||
157 | local pages_undated = grouped_pages[true] or pandoc.MetaList({}) | 188 | local pages_undated = grouped_pages[true] or pandoc.MetaList({}) |
@@ -201,13 +232,17 @@ function organize_subpages(pages) | |||
201 | end | 232 | end |
202 | 233 | ||
203 | function Meta(meta) | 234 | function Meta(meta) |
235 | meta.content_dir = meta.content_dir:gsub("/$", "") | ||
204 | meta.output_dir = meta.output_dir:gsub("/$", "") | 236 | meta.output_dir = meta.output_dir:gsub("/$", "") |
205 | meta.site.url = pandoc.utils.stringify(meta.site.url):gsub("/$", "") | 237 | meta.site.url = pandoc.utils.stringify(meta.site.url):gsub("/$", "") |
206 | meta.base_file_out = meta.base_file_out or meta.file_out | 238 | meta.rewrites = meta.rewrites or |
207 | meta.base_dir_out = meta.base_file_out:gsub("^(.*)/.-$", "%1") | 239 | pandoc.MetaMap({path = pandoc.MetaList({})}) |
208 | meta.page_type = meta.page_type or "page" | 240 | meta.page_type = meta.page_type or "page" |
209 | |||
210 | meta.layout = resolve_layout(meta.layout) | 241 | meta.layout = resolve_layout(meta.layout) |
242 | meta.section = resolve_section(meta.content_dir, meta.file_in) | ||
243 | |||
244 | meta.file_out = get_file_out(meta.rewrites, meta.content_dir, | ||
245 | meta.output_dir, meta.file_in) | ||
211 | 246 | ||
212 | meta.date = format_date(meta.date) | 247 | meta.date = format_date(meta.date) |
213 | if meta.last_update ~= nil then | 248 | if meta.last_update ~= nil then |
@@ -216,26 +251,26 @@ function Meta(meta) | |||
216 | meta.last_update = meta.date | 251 | meta.last_update = meta.date |
217 | end | 252 | end |
218 | 253 | ||
254 | meta.category = resolve_category(meta.categories[meta.section.id], | ||
255 | meta.category) | ||
256 | meta.categories = nil | ||
257 | |||
219 | if meta.page_type == "feed" then | 258 | if meta.page_type == "feed" then |
220 | meta.page = pandoc.MetaMap({ | 259 | meta.page = pandoc.MetaMap({ |
221 | url = resolve_url("page", meta.site.url, meta.output_dir, | 260 | url = resolve_url(meta.site.url, meta.output_dir, meta.file_out, |
222 | meta.base_dir_out, meta.file_out) | 261 | meta.file_out:gsub("%.xml$", ".html")) |
223 | }) | 262 | }) |
224 | end | 263 | end |
225 | 264 | ||
226 | meta.url = resolve_url(meta.page_type, meta.site.url, meta.output_dir, | ||
227 | meta.base_dir_out, meta.file_out) | ||
228 | meta.section = resolve_section(meta.url.abs) | ||
229 | meta.category = resolve_category(meta.categories[meta.section.id], | ||
230 | meta.category) | ||
231 | meta.categories = nil | ||
232 | |||
233 | if meta.menus and meta.menus.main then | 265 | if meta.menus and meta.menus.main then |
234 | meta.menus.main = create_main_menu_state(meta.section, meta.menus.main) | 266 | meta.menus.main = prep_main_menu(meta.rewrites, meta.section, |
267 | meta.menus.main) | ||
235 | end | 268 | end |
236 | 269 | ||
237 | if meta.pages then | 270 | if meta.pages then |
238 | local pages, categories = organize_subpages(meta.pages) | 271 | local pages, categories = organize_subpages(meta.site.url, |
272 | meta.output_dir, | ||
273 | meta.file_out, meta.pages) | ||
239 | meta.pages = pages | 274 | meta.pages = pages |
240 | meta.categories = categories | 275 | meta.categories = categories |
241 | end | 276 | end |
diff --git a/scripts/watch_content.sh b/scripts/watch_content.sh index 92719d3..66b726f 100755 --- a/scripts/watch_content.sh +++ b/scripts/watch_content.sh | |||
@@ -10,7 +10,8 @@ inotifywait -qrme close_write,delete,move --format "%w%f" "${CONTENT_DIR%/}" \ | |||
10 | | while read file | 10 | | while read file |
11 | do | 11 | do |
12 | if [ -f "$file" ]; then | 12 | if [ -f "$file" ]; then |
13 | scripts/build_content.sh "single" "$file" | 13 | # scripts/build_content.sh "single" "$file" |
14 | scripts/build_content.sh "all_md" | ||
14 | elif [ ! -d "$file" ]; then | 15 | elif [ ! -d "$file" ]; then |
15 | scripts/build_content.sh "delete" "$file" | 16 | scripts/build_content.sh "delete" "$file" |
16 | fi | 17 | fi |