diff options
-rwxr-xr-x | scripts/build_content.sh | 143 | ||||
-rw-r--r-- | scripts/metadata_filter.lua | 35 |
2 files changed, 97 insertions, 81 deletions
diff --git a/scripts/build_content.sh b/scripts/build_content.sh index 0b2ce2e..63bc525 100755 --- a/scripts/build_content.sh +++ b/scripts/build_content.sh | |||
@@ -7,7 +7,7 @@ if [ -f "site.conf" ]; then | |||
7 | fi | 7 | fi |
8 | 8 | ||
9 | get_filters() { | 9 | get_filters() { |
10 | filters_args="" | 10 | local filters_args="" |
11 | 11 | ||
12 | mapfile -d $'\0' filters < <(find $FILTERS_DIR \ | 12 | mapfile -d $'\0' filters < <(find $FILTERS_DIR \ |
13 | -type f \ | 13 | -type f \ |
@@ -18,27 +18,37 @@ get_filters() { | |||
18 | filters_args="$filters_args --lua-filter $file" | 18 | filters_args="$filters_args --lua-filter $file" |
19 | done | 19 | done |
20 | 20 | ||
21 | echo $filters_args | 21 | echo "$filters_args" |
22 | } | 22 | } |
23 | 23 | ||
24 | FILTERS=$(get_filters) | 24 | FILTERS=$(get_filters) |
25 | 25 | ||
26 | get_target_filename() { | 26 | get_filename_out() { |
27 | filename=$1 | 27 | local filename=$1 |
28 | filename=${filename#"$CONTENT_DIR"} | 28 | filename=${filename#"$CONTENT_DIR"} |
29 | if [ "${filename##*.}" = "md" ]; then | 29 | if [ "${filename##*.}" = "md" ]; then |
30 | filename="${filename%.md}.html" | 30 | filename="${filename%.md}.html" |
31 | fi | 31 | fi |
32 | if [ "${filename##*/}" != "index.html" ]; then | ||
33 | filename="${filename%.html}/index.html" | ||
34 | fi | ||
32 | echo "$OUTPUT_DIR$filename" | 35 | echo "$OUTPUT_DIR$filename" |
33 | } | 36 | } |
34 | 37 | ||
35 | get_metadata() { | 38 | get_metadata() { |
36 | pandoc "$2" \ | 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" \ | ||
37 | -f markdown \ | 47 | -f markdown \ |
38 | -t plain \ | 48 | -t plain \ |
39 | --metadata content_dir="$CONTENT_DIR" \ | 49 | --metadata output_dir="$OUTPUT_DIR" \ |
40 | --metadata base_file="$1" \ | 50 | --metadata base_file_out="$base_file_out" \ |
41 | --metadata file="$2" \ | 51 | --metadata file_out="$file_out" \ |
42 | --metadata-file metadata/metadata.yaml \ | 52 | --metadata-file metadata/metadata.yaml \ |
43 | --no-highlight \ | 53 | --no-highlight \ |
44 | --template scripts/metadata_tpl.json \ | 54 | --template scripts/metadata_tpl.json \ |
@@ -46,19 +56,26 @@ get_metadata() { | |||
46 | } | 56 | } |
47 | 57 | ||
48 | get_content() { | 58 | get_content() { |
49 | pandoc "$2" \ | 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" \ | ||
50 | -f markdown \ | 67 | -f markdown \ |
51 | -t markdown \ | 68 | -t markdown \ |
52 | --metadata content_dir="$CONTENT_DIR" \ | 69 | --metadata output_dir="$OUTPUT_DIR" \ |
53 | --metadata base_file="$1" \ | 70 | --metadata base_file_out="$base_file_out" \ |
54 | --metadata file="$2" \ | 71 | --metadata file_out="$file_out" \ |
55 | --metadata-file metadata/metadata.yaml \ | 72 | --metadata-file metadata/metadata.yaml \ |
56 | --no-highlight \ | 73 | --no-highlight \ |
57 | --lua-filter scripts/metadata_filter.lua | 74 | --lua-filter scripts/metadata_filter.lua |
58 | } | 75 | } |
59 | 76 | ||
60 | get_subpages_basedir() { | 77 | get_subpages_basedir() { |
61 | filename=$1 | 78 | local filename=$1 |
62 | filename=${filename%.md} | 79 | filename=${filename%.md} |
63 | if [ "${filename##*/}" = "index" ]; then | 80 | if [ "${filename##*/}" = "index" ]; then |
64 | filename=${filename%index} | 81 | filename=${filename%index} |
@@ -67,71 +84,75 @@ get_subpages_basedir() { | |||
67 | } | 84 | } |
68 | 85 | ||
69 | get_subpages_metadata() { | 86 | get_subpages_metadata() { |
70 | basedir=$(get_subpages_basedir "$1") | 87 | local base_file_in=$1 |
71 | child_pages=() | 88 | local base_file_out=$2 |
89 | local base_dir_in=$(get_subpages_basedir "$base_file_in") | ||
90 | local child_pages=() | ||
72 | 91 | ||
73 | if [ -d "$basedir" ]; then | 92 | if [ -d "$base_dir_in" ]; then |
74 | mapfile -d $'\0' child_pages < <(find "$basedir" \ | 93 | mapfile -d $'\0' child_pages < <(find "$base_dir_in" \ |
75 | -maxdepth 2 \ | 94 | -maxdepth 2 \ |
76 | -type f \ | 95 | -type f \ |
77 | -name "*.md" \ | 96 | -name "*.md" \ |
78 | ! -name "_*.md" \ | 97 | ! -name "_*.md" \ |
79 | ! -wholename "${basedir}index.md" \ | 98 | ! -wholename "${base_dir_in}index.md" \ |
80 | \( -wholename "$basedir*/index.md" -o ! -wholename "$basedir*/*.md" \) \ | 99 | \( -wholename "$base_dir_in*/index.md" -o ! -wholename "$base_dir_in*/*.md" \) \ |
81 | -print0) | 100 | -print0) |
82 | fi | 101 | fi |
83 | 102 | ||
84 | pages="[]" | 103 | local pages="[]" |
85 | 104 | ||
86 | if [ ${#child_pages[@]} -ne 0 ]; then | 105 | for file_in in "${child_pages[@]}"; do |
87 | for file in "${child_pages[@]}"; do | 106 | local content=$(get_content "$file_in" "$base_file_out" | sed -z 's/\\/\\\\/g;s/\n/\\n/g;s/"/\"/g') |
88 | metadata=$(get_metadata "$1" "$file" | jq "{ \ | 107 | |
89 | url: .url, \ | 108 | local metadata=$(get_metadata "$file_in" "$base_file_out" | jq "{ \ |
90 | author: .author, \ | 109 | url: .url, \ |
91 | title: .title, \ | 110 | author: .author, \ |
92 | date: .date, \ | 111 | title: .title, \ |
93 | last_update: .last_update, \ | 112 | date: .date, \ |
94 | category: .category, \ | 113 | last_update: .last_update, \ |
95 | content: \"$(get_content "$1" "$file" | sed -z 's/\\/\\\\/g;s/\n/\\n/g;s/"/\"/g')\" \ | 114 | category: .category, \ |
96 | } | del(.[] | nulls)") | 115 | content: \"$content\" \ |
97 | 116 | } | del(.[] | nulls)") | |
98 | pages=$(echo "$pages" | jq ". += [ $metadata ]") | 117 | |
99 | done | 118 | pages=$(echo "$pages" | jq ". += [ $metadata ]") |
100 | fi | 119 | done |
101 | 120 | ||
102 | echo "{ \"pages\": $pages }" | 121 | echo "{ \"pages\": $pages }" |
103 | } | 122 | } |
104 | 123 | ||
105 | handle () { | 124 | handle () { |
106 | target_file=$(get_target_filename "$1") | 125 | local file_out=$(get_filename_out "$1") |
107 | 126 | ||
108 | mkdir -p $(dirname "$target_file") | 127 | mkdir -p $(dirname "$file_out") |
109 | 128 | ||
110 | if [ "${1#*.}" = "md" ]; then | 129 | if [ "${1#*.}" = "md" ]; then |
111 | echo -e "\033[0;32m[COMPILE ]\033[0m $1 -> $target_file" | 130 | echo -e "\033[0;32m[COMPILE ]\033[0m $1 -> $file_out" |
112 | 131 | ||
113 | echo -e "\033[0;90m[COMPILE ]\033[0m Getting metadata" | 132 | echo -e "\033[0;90m[COMPILE ]\033[0m Getting metadata" |
114 | 133 | ||
115 | included_metadata=$(get_metadata "$1" "$1") | 134 | local included_metadata=$(get_metadata "$1") |
116 | added_metadata=$(get_subpages_metadata "$1") | 135 | local added_metadata=$(get_subpages_metadata "$1" "$file_out") |
117 | 136 | ||
118 | create_feed=$(echo "$included_metadata" | jq -r .create_feed) | 137 | local create_feed=$(echo "$included_metadata" | jq -r .create_feed) |
119 | 138 | ||
120 | meta_file=$(mktemp) | 139 | local meta_file=$(mktemp) |
121 | 140 | ||
122 | echo "$added_metadata" > "$meta_file" | 141 | echo "$added_metadata" > "$meta_file" |
123 | 142 | ||
124 | if [ "$create_feed" = "true" ]; then | 143 | if [ "$create_feed" = "true" ]; then |
125 | echo -e "\033[0;90m[COMPILE ]\033[0m Creating feed" | 144 | echo -e "\033[0;90m[COMPILE ]\033[0m Creating feed" |
126 | 145 | ||
146 | local feed_file_out="${file_out%.html}.xml" | ||
147 | |||
127 | pandoc "$1" \ | 148 | pandoc "$1" \ |
128 | -f markdown \ | 149 | -f markdown \ |
129 | -t html5 \ | 150 | -t html5 \ |
130 | --no-highlight \ | 151 | --no-highlight \ |
131 | --template "${TEMPLATES_DIR}feed.xml" \ | 152 | --template "${TEMPLATES_DIR}feed.xml" \ |
132 | -o "${target_file%.html}.xml" \ | 153 | -o "$feed_file_out" \ |
133 | --metadata content_dir="$CONTENT_DIR" \ | 154 | --metadata output_dir="$OUTPUT_DIR" \ |
134 | --metadata file="$1" \ | 155 | --metadata file_out="$feed_file_out" \ |
135 | --metadata page_type=feed \ | 156 | --metadata page_type=feed \ |
136 | --metadata-file metadata/metadata.yaml \ | 157 | --metadata-file metadata/metadata.yaml \ |
137 | --metadata-file "$meta_file" \ | 158 | --metadata-file "$meta_file" \ |
@@ -145,9 +166,9 @@ handle () { | |||
145 | -t html5 \ | 166 | -t html5 \ |
146 | --no-highlight \ | 167 | --no-highlight \ |
147 | --template "${TEMPLATES_DIR}base.html" \ | 168 | --template "${TEMPLATES_DIR}base.html" \ |
148 | -o "$target_file" \ | 169 | -o "$file_out" \ |
149 | --metadata content_dir="$CONTENT_DIR" \ | 170 | --metadata output_dir="$OUTPUT_DIR" \ |
150 | --metadata file="$1" \ | 171 | --metadata file_out="$file_out" \ |
151 | --metadata-file metadata/metadata.yaml \ | 172 | --metadata-file metadata/metadata.yaml \ |
152 | --metadata-file "$meta_file" \ | 173 | --metadata-file "$meta_file" \ |
153 | --lua-filter scripts/metadata_filter.lua \ | 174 | --lua-filter scripts/metadata_filter.lua \ |
@@ -158,8 +179,8 @@ handle () { | |||
158 | # -t plain \ | 179 | # -t plain \ |
159 | # --no-highlight \ | 180 | # --no-highlight \ |
160 | # --template scripts/metadata_tpl.json \ | 181 | # --template scripts/metadata_tpl.json \ |
161 | # --metadata content_dir="$CONTENT_DIR" \ | 182 | # --metadata output_dir="$OUTPUT_DIR" \ |
162 | # --metadata file="$1" \ | 183 | # --metadata file_out="$file_out" \ |
163 | # --metadata-file metadata/metadata.yaml \ | 184 | # --metadata-file metadata/metadata.yaml \ |
164 | # --metadata-file "$meta_file" \ | 185 | # --metadata-file "$meta_file" \ |
165 | # --lua-filter scripts/metadata_filter.lua \ | 186 | # --lua-filter scripts/metadata_filter.lua \ |
@@ -172,9 +193,9 @@ handle () { | |||
172 | 193 | ||
173 | echo -e "\033[0;90m[COMPILE ]\033[0m Done" | 194 | echo -e "\033[0;90m[COMPILE ]\033[0m Done" |
174 | else | 195 | else |
175 | echo -e "\033[0;32m[COPY ]\033[0m $1 -> $target_file" | 196 | echo -e "\033[0;32m[COPY ]\033[0m $1 -> $file_out" |
176 | 197 | ||
177 | cp "$1" "$target_file" | 198 | cp "$1" "$file_out" |
178 | fi | 199 | fi |
179 | } | 200 | } |
180 | 201 | ||
@@ -182,18 +203,18 @@ if [ -z "$1" ]; then | |||
182 | find "$CONTENT_DIR" \ | 203 | find "$CONTENT_DIR" \ |
183 | -type f \ | 204 | -type f \ |
184 | ! -name "_*.md" \ | 205 | ! -name "_*.md" \ |
185 | | while read file | 206 | | while read file_in |
186 | do | 207 | do |
187 | handle "$file" | 208 | handle "$file_in" |
188 | done | 209 | done |
189 | elif [ "$1" = "all_md" ]; then | 210 | elif [ "$1" = "all_md" ]; then |
190 | find "$CONTENT_DIR" \ | 211 | find "$CONTENT_DIR" \ |
191 | -type f \ | 212 | -type f \ |
192 | -name "*.md" \ | 213 | -name "*.md" \ |
193 | ! -name "_*.md" \ | 214 | ! -name "_*.md" \ |
194 | | while read file | 215 | | while read file_in |
195 | do | 216 | do |
196 | handle "$file" | 217 | handle "$file_in" |
197 | done | 218 | done |
198 | elif [ "$1" = "single" ]; then | 219 | elif [ "$1" = "single" ]; then |
199 | if [ -z "$2" ]; then | 220 | if [ -z "$2" ]; then |
@@ -208,10 +229,10 @@ elif [ "$1" = "delete" ]; then | |||
208 | if [ -z "$2" ]; then | 229 | if [ -z "$2" ]; then |
209 | echo -e "\033[0;31m[ERROR ]\033[0m \"delete\" operation requires file argument" | 230 | echo -e "\033[0;31m[ERROR ]\033[0m \"delete\" operation requires file argument" |
210 | else | 231 | else |
211 | target_file=$(get_target_filename "$2") | 232 | file_out=$(get_filename_out "$2") |
212 | if [ -f "$target_file" ] || [ -d "$target_file" ]; then | 233 | if [ -f "$file_out" ] || [ -d "$file_out" ]; then |
213 | echo -e "\033[0;32m[DELETE ]\033[0m $2 -> $target_file" | 234 | echo -e "\033[0;32m[DELETE ]\033[0m $2 -> $file_out" |
214 | rm -rf $target_file | 235 | rm -rf $file_out |
215 | fi | 236 | fi |
216 | fi | 237 | fi |
217 | else | 238 | else |
diff --git a/scripts/metadata_filter.lua b/scripts/metadata_filter.lua index fb7b3c9..61fe90f 100644 --- a/scripts/metadata_filter.lua +++ b/scripts/metadata_filter.lua | |||
@@ -13,7 +13,8 @@ function format_date(date) | |||
13 | return pandoc.MetaMap({ | 13 | return pandoc.MetaMap({ |
14 | yyyy_mm_dd = pandoc.MetaString(os.date("%F", time)), | 14 | yyyy_mm_dd = pandoc.MetaString(os.date("%F", time)), |
15 | yyyy = pandoc.MetaString(os.date("%Y", time)), | 15 | yyyy = pandoc.MetaString(os.date("%Y", time)), |
16 | mm_dd = pandoc.MetaString(os.date("%m-%d", time)), | 16 | mm = pandoc.MetaString(os.date("%m", time)), |
17 | dd = pandoc.MetaString(os.date("%d", time)), | ||
17 | rfc3339 = pandoc.MetaString(os.date("%FT%T+00:00", time)) | 18 | rfc3339 = pandoc.MetaString(os.date("%FT%T+00:00", time)) |
18 | }) | 19 | }) |
19 | end | 20 | end |
@@ -91,15 +92,11 @@ function relative_to(dir, target) | |||
91 | return path | 92 | return path |
92 | end | 93 | end |
93 | 94 | ||
94 | function resolve_url(page_type, site_url, content_dir, base_dir, cur_file) | 95 | function resolve_url(page_type, site_url, output_dir, base_dir_out, cur_file_out) |
95 | if page_type == "page" then | 96 | local abs = cur_file_out:gsub("^" .. output_dir, ""):gsub("/index.html$", |
96 | cur_file = cur_file:gsub("%.md$", ".html") | 97 | "/") |
97 | elseif page_type == "feed" then | 98 | local rel = |
98 | cur_file = cur_file:gsub("%.md$", ".xml") | 99 | relative_to(base_dir_out, cur_file_out):gsub("/index.html$", "/") |
99 | end | ||
100 | |||
101 | local abs = cur_file:gsub("^" .. content_dir, ""):gsub("/index.html$", "/") | ||
102 | local rel = relative_to(base_dir, cur_file):gsub("/index.html$", "/") | ||
103 | 100 | ||
104 | return pandoc.MetaMap({ | 101 | return pandoc.MetaMap({ |
105 | abs = pandoc.MetaString(abs), | 102 | abs = pandoc.MetaString(abs), |
@@ -143,9 +140,7 @@ function create_main_menu_state(section, main_menu) | |||
143 | local item = main_menu[i] | 140 | local item = main_menu[i] |
144 | local active = pandoc.utils.stringify(item.id) == section.id | 141 | local active = pandoc.utils.stringify(item.id) == section.id |
145 | item.active = pandoc.MetaBool(active) | 142 | item.active = pandoc.MetaBool(active) |
146 | if active then | 143 | if active then active_item = item end |
147 | active_item = item | ||
148 | end | ||
149 | end | 144 | end |
150 | 145 | ||
151 | return pandoc.MetaMap({ | 146 | return pandoc.MetaMap({ |
@@ -206,10 +201,10 @@ function organize_subpages(pages) | |||
206 | end | 201 | end |
207 | 202 | ||
208 | function Meta(meta) | 203 | function Meta(meta) |
209 | meta.content_dir = meta.content_dir:gsub("/$", "") | 204 | meta.output_dir = meta.output_dir:gsub("/$", "") |
210 | meta.site.url = pandoc.utils.stringify(meta.site.url):gsub("/$", "") | 205 | meta.site.url = pandoc.utils.stringify(meta.site.url):gsub("/$", "") |
211 | meta.base_file = meta.base_file or meta.file | 206 | meta.base_file_out = meta.base_file_out or meta.file_out |
212 | meta.base_dir = meta.base_file:gsub("^(.*)/.-$", "%1") | 207 | meta.base_dir_out = meta.base_file_out:gsub("^(.*)/.-$", "%1") |
213 | meta.page_type = meta.page_type or "page" | 208 | meta.page_type = meta.page_type or "page" |
214 | 209 | ||
215 | meta.layout = resolve_layout(meta.layout) | 210 | meta.layout = resolve_layout(meta.layout) |
@@ -223,13 +218,13 @@ function Meta(meta) | |||
223 | 218 | ||
224 | if meta.page_type == "feed" then | 219 | if meta.page_type == "feed" then |
225 | meta.page = pandoc.MetaMap({ | 220 | meta.page = pandoc.MetaMap({ |
226 | url = resolve_url("page", meta.site.url, meta.content_dir, | 221 | url = resolve_url("page", meta.site.url, meta.output_dir, |
227 | meta.base_dir, meta.file) | 222 | meta.base_dir_out, meta.file_out) |
228 | }) | 223 | }) |
229 | end | 224 | end |
230 | 225 | ||
231 | meta.url = resolve_url(meta.page_type, meta.site.url, meta.content_dir, | 226 | meta.url = resolve_url(meta.page_type, meta.site.url, meta.output_dir, |
232 | meta.base_dir, meta.file) | 227 | meta.base_dir_out, meta.file_out) |
233 | meta.section = resolve_section(meta.url.abs) | 228 | meta.section = resolve_section(meta.url.abs) |
234 | meta.category = resolve_category(meta.categories[meta.section.id], | 229 | meta.category = resolve_category(meta.categories[meta.section.id], |
235 | meta.category) | 230 | meta.category) |