From 3ad6bd151364a3278618e3a26e21918d17680d24 Mon Sep 17 00:00:00 2001 From: Volpeon Date: Sun, 10 Jan 2021 13:24:21 +0100 Subject: Moved output filename handling into Pandoc filter, implemented path rewriting --- .../2021-01-09-infinite-skyscrapers/index.md | 28 -------- content/3jF85da/index.md | 5 -- .../2021-01-09-infinite-skyscrapers.md | 28 ++++++++ content/personal_blog/index.md | 5 ++ metadata/metadata.yaml | 12 +++- scripts/build_content.sh | 73 +++++++------------- scripts/metadata_filter.lua | 79 ++++++++++++++++------ scripts/watch_content.sh | 3 +- templates/layouts/page.html | 6 +- 9 files changed, 132 insertions(+), 107 deletions(-) delete mode 100644 content/3jF85da/2021-01-09-infinite-skyscrapers/index.md delete mode 100644 content/3jF85da/index.md create mode 100644 content/personal_blog/2021-01-09-infinite-skyscrapers.md create mode 100644 content/personal_blog/index.md diff --git a/content/3jF85da/2021-01-09-infinite-skyscrapers/index.md b/content/3jF85da/2021-01-09-infinite-skyscrapers/index.md deleted file mode 100644 index a0831c0..0000000 --- a/content/3jF85da/2021-01-09-infinite-skyscrapers/index.md +++ /dev/null @@ -1,28 +0,0 @@ ---- -date: 2021-01-09 -title: Infinite Skyscrapers -category: dre ---- - -I'm in some strange world that consist of huge buildings, looking a lot like very simple skyscrapers, except they were all interconnected. I'm not even sure if there's a floor since I'm just floating, being able to fly around at will. -There is also something chasing me and I'm trying to get away from it. - -The buildings around me get denser and more chaotic, some look like glitches in a computer game. The creature chasing me seems to absorb buildings into its body, getting larger and more powerful. - -Then there's a cut and I'm presumable inside one of those buildings. I'm in a dark room with barely any lighting, looking somewhat like a large office. It's very cluttered, though. There's another similar room next to this one, being separated by a glass wall and a door. -There's another person with me who I seem to know, and that creature that chased me. It seems like it managed to capture us. - -It dares us to escape from this situation, and as soon as it said that, the room begins to get locked off. There are some red lights blinking and the door connecting our current room with the neighboring room is about to close permanently. Panicked, I quickly run through it, but the other person couldn't make it. - -I suddenly realize I'm a cat and the other person an even smaller critter, I don't know what. It was obvious that the creature was just playing with us, watching our futile efforts to escape for its amusement. - -The other person tries to get the door to open by working on its circuitry, and I do my best to block the creature's view. - -After a while, I start exploring the room and notice that there's a hallway in front of it, also separated by a glass wall and a door. However, the wall is broken on one place and has a hole large enough for me to jump through. So that's what I do. I land in a bunch of glass disks lying on the floor, breaking them, but I'm unharmed. - -Now being a human again, I take a glass shard with me and run through a door which leads to a staircase. It's a large round room with stairs along the wall, so I can see all the floors coming before and after. It looks like there's an infinite number of floors. -I know the creature will come after me soon and so I run upwards as fast as I can. - -I made it about 5 floors when I hear a door burst open and I quickly enter the room next to me, hoping that the creature hasn't seen me. - -This floor was very large and open and brightly lit. The dream starts to make no sense anymore and that's it. diff --git a/content/3jF85da/index.md b/content/3jF85da/index.md deleted file mode 100644 index 8f4484b..0000000 --- a/content/3jF85da/index.md +++ /dev/null @@ -1,5 +0,0 @@ ---- -title: Personal Blog -layout: dated_list -create_feed: true ---- diff --git a/content/personal_blog/2021-01-09-infinite-skyscrapers.md b/content/personal_blog/2021-01-09-infinite-skyscrapers.md new file mode 100644 index 0000000..909ecce --- /dev/null +++ b/content/personal_blog/2021-01-09-infinite-skyscrapers.md @@ -0,0 +1,28 @@ +--- +date: 2021-01-09 +title: Infinite Skyscrapers +category: dre +--- + +I'm in some strange world that consist of huge buildings, looking a lot like very simple skyscrapers, except they were all interconnected. I'm not even sure if there's a floor since I'm just floating, being able to fly around at will. +There is also something chasing me and I'm trying to get away from it. + +The buildings around me get denser and more chaotic, some look like glitches in a computer game. The creature chasing me seems to absorb buildings into its body, getting larger and more powerful. + +Then there's a cut and I'm presumable inside one of those buildings. I'm in a dark room with barely any lighting, looking somewhat like a large office. It's very cluttered, though. There's another similar room next to this one, being separated by a glass wall and a door. +There's another person with me who I seem to know, and that creature that chased me. It seems like it managed to capture us. + +It dares us to escape from this situation, and as soon as it said that, the room begins to get locked off. There are some red lights blinking and the door connecting our current room with the neighboring room is about to close permanently. Panicked, I quickly run through it, but the other person couldn't make it. + +I suddenly realize I'm a cat and the other person an even smaller critter, I don't know what. It was obvious that the creature was just playing with us, watching our futile efforts to escape for its amusement. + +The other person tries to get the door to open again by messing with its circuitry, and I do my best to block the creature's view from what was going on without looking suspicious. + +After a while, I start exploring the room and notice that there's a hallway in front of it, also separated by a glass wall and a door. However, the wall is broken in one place and has a hole large enough for me to jump through. So that's what I do. I land in a bunch of glass disks lying on the floor, breaking them, but I'm unharmed. + +Now being a human again, I take a glass shard with me and run through a door which leads to a staircase. It is huge, having the shape of a pill with stairs going along the edge. The center is empty so I can see all the floors coming before and after. It looks like the number of floors is endless. +I know the creature will come after me soon and so I run upwards as fast as I can. + +I made it about 5 floors when I hear a door burst open and I quickly enter the room next to me, hoping that the creature hasn't seen me. + +This floor was very large and open and brightly lit. The dream starts to make no sense anymore and that's it. diff --git a/content/personal_blog/index.md b/content/personal_blog/index.md new file mode 100644 index 0000000..8f4484b --- /dev/null +++ b/content/personal_blog/index.md @@ -0,0 +1,5 @@ +--- +title: Personal Blog +layout: dated_list +create_feed: true +--- diff --git a/metadata/metadata.yaml b/metadata/metadata.yaml index 5c39f19..0cf5db8 100644 --- a/metadata/metadata.yaml +++ b/metadata/metadata.yaml @@ -14,9 +14,10 @@ menus: - id: blog label: Blog url: /blog/ - - id: 3jF85da + - id: personal_blog label: Personal - url: /3jF85da/ + label_long: Personal Blog + url: /personal_blog/ hidden: true feeds: @@ -27,5 +28,10 @@ categories: blog: tes: Testing per: Personal - 3jF85da: + personal_blog: dre: Dream Journal + +rewrites: + path: + - from: ^/personal_blog + to: /3jF85da 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) get_filename_out() { local filename=$1 filename=${filename#"$CONTENT_DIR"} - if [ "${filename##*.}" = "md" ]; then - filename="${filename%.md}.html" - fi - if [ "${filename##*/}" != "index.html" ]; then - filename="${filename%.html}/index.html" - fi echo "$OUTPUT_DIR$filename" } get_metadata() { - local file_out=$(get_filename_out "$1") - local base_file_out="$2" - - if [ -z "$2" ]; then - base_file_out="$file_out" - fi - pandoc "$1" \ -f markdown \ -t plain \ + --metadata content_dir="$CONTENT_DIR" \ --metadata output_dir="$OUTPUT_DIR" \ - --metadata base_file_out="$base_file_out" \ - --metadata file_out="$file_out" \ + --metadata file_in="$1" \ --metadata-file metadata/metadata.yaml \ --no-highlight \ --template scripts/metadata_tpl.json \ @@ -56,19 +43,12 @@ get_metadata() { } get_content() { - local file_out=$(get_filename_out "$1") - local base_file_out="$2" - - if [ -z "$2" ]; then - base_file_out="$file_out" - fi - pandoc "$1" \ -f markdown \ -t markdown \ + --metadata content_dir="$CONTENT_DIR" \ --metadata output_dir="$OUTPUT_DIR" \ - --metadata base_file_out="$base_file_out" \ - --metadata file_out="$file_out" \ + --metadata file_in="$1" \ --metadata-file metadata/metadata.yaml \ --no-highlight \ --lua-filter scripts/metadata_filter.lua @@ -84,9 +64,7 @@ get_subpages_basedir() { } get_subpages_metadata() { - local base_file_in=$1 - local base_file_out=$2 - local base_dir_in=$(get_subpages_basedir "$base_file_in") + local base_dir_in=$(get_subpages_basedir "$1") local child_pages=() if [ -d "$base_dir_in" ]; then @@ -103,10 +81,10 @@ get_subpages_metadata() { local pages="[]" for file_in in "${child_pages[@]}"; do - local content=$(get_content "$file_in" "$base_file_out" | sed -z 's/\\/\\\\/g;s/\n/\\n/g;s/"/\"/g') + local content=$(get_content "$file_in" | sed -z 's/\\/\\\\/g;s/\n/\\n/g;s/"/\"/g') - local metadata=$(get_metadata "$file_in" "$base_file_out" | jq "{ \ - url: .url, \ + local metadata=$(get_metadata "$file_in" | jq "{ \ + file_out: .file_out, \ author: .author, \ title: .title, \ date: .date, \ @@ -122,19 +100,18 @@ get_subpages_metadata() { } handle () { - local file_out=$(get_filename_out "$1") + if [ "${1#*.}" = "md" ]; then + local included_metadata=$(get_metadata "$1") + local file_out=$(echo "$included_metadata" | jq -r .file_out) + local create_feed=$(echo "$included_metadata" | jq -r .create_feed) - mkdir -p $(dirname "$file_out") + mkdir -p $(dirname "$file_out") - if [ "${1#*.}" = "md" ]; then echo -e "\033[0;32m[COMPILE ]\033[0m $1 -> $file_out" - echo -e "\033[0;90m[COMPILE ]\033[0m Getting metadata" - - local included_metadata=$(get_metadata "$1") - local added_metadata=$(get_subpages_metadata "$1" "$file_out") + echo -e "\033[0;90m[COMPILE ]\033[0m Getting subpages" - local create_feed=$(echo "$included_metadata" | jq -r .create_feed) + local added_metadata=$(get_subpages_metadata "$1") local meta_file=$(mktemp) @@ -143,16 +120,15 @@ handle () { if [ "$create_feed" = "true" ]; then echo -e "\033[0;90m[COMPILE ]\033[0m Creating feed" - local feed_file_out="${file_out%.html}.xml" - pandoc "$1" \ -f markdown \ -t html5 \ --no-highlight \ --template "${TEMPLATES_DIR}feed.xml" \ - -o "$feed_file_out" \ + -o "${file_out%.html}.xml" \ + --metadata content_dir="$CONTENT_DIR" \ --metadata output_dir="$OUTPUT_DIR" \ - --metadata file_out="$feed_file_out" \ + --metadata file_in="$1" \ --metadata page_type=feed \ --metadata-file metadata/metadata.yaml \ --metadata-file "$meta_file" \ @@ -167,8 +143,9 @@ handle () { --no-highlight \ --template "${TEMPLATES_DIR}base.html" \ -o "$file_out" \ + --metadata content_dir="$CONTENT_DIR" \ --metadata output_dir="$OUTPUT_DIR" \ - --metadata file_out="$file_out" \ + --metadata file_in="$1" \ --metadata-file metadata/metadata.yaml \ --metadata-file "$meta_file" \ --lua-filter scripts/metadata_filter.lua \ @@ -179,8 +156,9 @@ handle () { # -t plain \ # --no-highlight \ # --template scripts/metadata_tpl.json \ + # --metadata content_dir="$CONTENT_DIR" \ # --metadata output_dir="$OUTPUT_DIR" \ - # --metadata file_out="$file_out" \ + # --metadata file_in="$1" \ # --metadata-file metadata/metadata.yaml \ # --metadata-file "$meta_file" \ # --lua-filter scripts/metadata_filter.lua \ @@ -188,11 +166,12 @@ handle () { rm "$meta_file" - # echo "$included_metadata" - # echo "$added_metadata" - echo -e "\033[0;90m[COMPILE ]\033[0m Done" else + local file_out=$(get_filename_out "$1") + + mkdir -p $(dirname "$file_out") + echo -e "\033[0;32m[COPY ]\033[0m $1 -> $file_out" 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) return path end -function resolve_url(page_type, site_url, output_dir, base_dir_out, cur_file_out) - local abs = cur_file_out:gsub("^" .. output_dir, ""):gsub("/index.html$", - "/") +function apply_path_rewrites(rewrites, str) + for i = 1, #rewrites.path do + local r = rewrites.path[i] + str = str:gsub(pandoc.utils.stringify(r.from), + pandoc.utils.stringify(r.to)) + end + return str +end + +function get_file_out(rewrites, content_dir, output_dir, file_in) + local file_out = file_in:gsub("^" .. content_dir, ""):gsub("%.md$", ".html") + + if file_out:match(".html$") and not file_out:match("/index%.html$") then + file_out = file_out:gsub("/(.*)%.html$", "/%1/index.html") + end + + file_out = apply_path_rewrites(rewrites, file_out) + + return pandoc.MetaString(output_dir .. file_out) +end + +function resolve_url(site_url, output_dir, ref_file, target_file) + local ref_base_dir = ref_file:gsub("^(.*)/.-$", "%1") + local abs = target_file:gsub("^" .. output_dir, ""):gsub("/index%.html$", + "/") local rel = - relative_to(base_dir_out, cur_file_out):gsub("/index.html$", "/") + relative_to(ref_base_dir, target_file):gsub("/index%.html$", "/") return pandoc.MetaMap({ abs = pandoc.MetaString(abs), @@ -115,8 +137,9 @@ function resolve_layout(layout) end end -function resolve_section(abs_url) - local section = abs_url:match("^/(.-)[/.]") or "index" +function resolve_section(content_dir, file_in) + local section = file_in:gsub("^" .. content_dir, ""):match("^/(.-)[/.]") or + "index" return pandoc.MetaMap({ id = pandoc.MetaString(section), ["is_" .. section] = pandoc.MetaBool(true) @@ -133,13 +156,15 @@ function resolve_category(categories, category) end end -function create_main_menu_state(section, main_menu) +function prep_main_menu(rewrites, section, main_menu) local active_item = nil for i = 1, #main_menu do local item = main_menu[i] local active = pandoc.utils.stringify(item.id) == section.id item.active = pandoc.MetaBool(active) + item.url = apply_path_rewrites(rewrites, + pandoc.utils.stringify(item.url)) if active then active_item = item end end @@ -151,7 +176,13 @@ function create_main_menu_state(section, main_menu) }) end -function organize_subpages(pages) +function organize_subpages(site_url, output_dir, ref_file, pages) + for i = 1, #pages do + local page = pages[i] + page.url = resolve_url(site_url, output_dir, ref_file, + pandoc.utils.stringify(page.file_out)) + end + local grouped_pages = group_by(pages, function(p) return not p.date end) local pages_undated = grouped_pages[true] or pandoc.MetaList({}) @@ -201,13 +232,17 @@ function organize_subpages(pages) end function Meta(meta) + meta.content_dir = meta.content_dir:gsub("/$", "") meta.output_dir = meta.output_dir:gsub("/$", "") meta.site.url = pandoc.utils.stringify(meta.site.url):gsub("/$", "") - meta.base_file_out = meta.base_file_out or meta.file_out - meta.base_dir_out = meta.base_file_out:gsub("^(.*)/.-$", "%1") + meta.rewrites = meta.rewrites or + pandoc.MetaMap({path = pandoc.MetaList({})}) meta.page_type = meta.page_type or "page" - meta.layout = resolve_layout(meta.layout) + meta.section = resolve_section(meta.content_dir, meta.file_in) + + meta.file_out = get_file_out(meta.rewrites, meta.content_dir, + meta.output_dir, meta.file_in) meta.date = format_date(meta.date) if meta.last_update ~= nil then @@ -216,26 +251,26 @@ function Meta(meta) meta.last_update = meta.date end + meta.category = resolve_category(meta.categories[meta.section.id], + meta.category) + meta.categories = nil + if meta.page_type == "feed" then meta.page = pandoc.MetaMap({ - url = resolve_url("page", meta.site.url, meta.output_dir, - meta.base_dir_out, meta.file_out) + url = resolve_url(meta.site.url, meta.output_dir, meta.file_out, + meta.file_out:gsub("%.xml$", ".html")) }) end - meta.url = resolve_url(meta.page_type, meta.site.url, meta.output_dir, - meta.base_dir_out, meta.file_out) - meta.section = resolve_section(meta.url.abs) - meta.category = resolve_category(meta.categories[meta.section.id], - meta.category) - meta.categories = nil - if meta.menus and meta.menus.main then - meta.menus.main = create_main_menu_state(meta.section, meta.menus.main) + meta.menus.main = prep_main_menu(meta.rewrites, meta.section, + meta.menus.main) end if meta.pages then - local pages, categories = organize_subpages(meta.pages) + local pages, categories = organize_subpages(meta.site.url, + meta.output_dir, + meta.file_out, meta.pages) meta.pages = pages meta.categories = categories 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%/}" \ | while read file do if [ -f "$file" ]; then - scripts/build_content.sh "single" "$file" + # scripts/build_content.sh "single" "$file" + scripts/build_content.sh "all_md" elif [ ! -d "$file" ]; then scripts/build_content.sh "delete" "$file" fi diff --git a/templates/layouts/page.html b/templates/layouts/page.html index ee1a816..61bcacd 100644 --- a/templates/layouts/page.html +++ b/templates/layouts/page.html @@ -4,7 +4,11 @@ $if(menus.main.active)$ $endif$ -- cgit v1.2.3-54-g00ecf