From bb245855904133dc129df46f76411fe65a76aa50 Mon Sep 17 00:00:00 2001 From: "Yann Esposito (Yogsototh)" Date: Tue, 25 May 2021 16:27:58 +0200 Subject: [PATCH] Improve index generation --- Makefile | 12 +++- engine/mk-index-entry.sh | 57 +++++++++++++++++++ engine/mk-index.sh | 37 +++++------- engine/mk-rss-entry.sh | 6 +- engine/mkrss.sh | 4 +- .../index.org | 6 ++ 6 files changed, 92 insertions(+), 30 deletions(-) create mode 100755 engine/mk-index-entry.sh rename src/{drafts => posts}/0018-makefile-as-static-site-builder-follow-up/index.org (98%) diff --git a/Makefile b/Makefile index b4fade7..cca0de8 100644 --- a/Makefile +++ b/Makefile @@ -64,10 +64,17 @@ indexcache: $(DST_XML_FILES) ALL += indexcache # HTML INDEX +DST_INDEX_FILES ?= $(patsubst %.xml,%.index, $(DST_XML_FILES)) +MK_INDEX_ENTRY := ./engine/mk-index-entry.sh +INDEX_CACHE_DIR ?= $(CACHE_DIR)/rss +$(INDEX_CACHE_DIR)/%.index: $(INDEX_CACHE_DIR)/%.xml $(MK_INDEX_ENTRY) + @mkdir -p $(INDEX_CACHE_DIR) + $(MK_INDEX_ENTRY) "$<" "$@" + HTML_INDEX := $(DST_DIR)/index.html MKINDEX := engine/mk-index.sh INDEX_TEMPLATE ?= templates/index.html -$(HTML_INDEX): $(DST_XML_FILES) $(MKINDEX) $(INDEX_TEMPLATE) +$(HTML_INDEX): $(DST_INDEX_FILES) $(MKINDEX) $(INDEX_TEMPLATE) @mkdir -p $(DST_DIR) $(MKINDEX) .PHONY: index @@ -85,10 +92,9 @@ RSS := $(DST_DIR)/rss.xml MKRSS := engine/mkrss.sh $(RSS): $(DST_RSS_FILES) $(MKRSS) $(MKRSS) -ALL += $(RSS) .PHONY: rss -rss: $(DST_RSS_FILES) $(RSS) +rss: $(RSS) ALL += rss diff --git a/engine/mk-index-entry.sh b/engine/mk-index-entry.sh new file mode 100755 index 0000000..ea9eb84 --- /dev/null +++ b/engine/mk-index-entry.sh @@ -0,0 +1,57 @@ +#!/usr/bin/env zsh + +cd "$(git rev-parse --show-toplevel)" || exit 1 + +xfic="$1" +dst="$2" + +# Directory +webdir="_site" +postsdir="$webdir/posts" +indexdir=".cache/rss" + +# HTML Accessors (similar to CSS accessors) +dateaccessor='.yyydate' +# title and keyword shouldn't be changed +titleaccessor='title' +keywordsaccessor='meta[name=keywords]::attr(content)' + +formatdate() { + # format the date for RSS + local d="$1" + # echo "DEBUG DATE: $d" >&2 + LC_TIME=en_US date --date $d +'%a, %d %b %Y %H:%M:%S %z' +} +finddate(){ < $1 hxselect -c $dateaccessor | sed 's/\[//g;s/\]//g;s/ .*$//' } +findtitle(){ < $1 hxselect -c $titleaccessor } +findkeywords(){ < $1 hxselect -c $keywordsaccessor | sed 's/,/ /g' } +mktaglist(){ + for keyword in $*; do + printf "%s" $keyword + done | sed 's#><#>, <#g' +} + +autoload -U colors && colors + +postfile="$(echo "$xfic"|sed 's#^'$postsdir'/##')" +blogfile="$(echo "$xfic"|sed 's#.xml$#.html#;s#^'$indexdir'/#posts/#')" +printf "%-30s" $blogfile +d=$(finddate $xfic) +echo -n " [$d]" +rssdate=$(formatdate $d) +title=$(findtitle $xfic) +keywords=( $(findkeywords $xfic) ) +printf ": %-55s" "$title ($keywords)" +taglist=$(mktaglist $keywords) +{ printf "\\n
  • " + printf "\\n%s" "$d" + printf "\\n%s" "${blogfile}" "$title" + printf "\\n
  • \\n\\n" +} >> ${dst}.tmp + +# overwrite only if the value in the index are different +if ! cmp -s ${dst} ${dst}.tmp; then + echo " [${fg[yellow]}M${reset_color}]" + mv -f ${dst}.tmp ${dst} +fi +echo " [${fg[green]}OK${reset_color}]" diff --git a/engine/mk-index.sh b/engine/mk-index.sh index 6d47b22..d2ff684 100755 --- a/engine/mk-index.sh +++ b/engine/mk-index.sh @@ -33,36 +33,27 @@ mktaglist(){ autoload -U colors && colors tmpdir=$(mktemp -d) -typeset -a dates -dates=( ) -for xfic in $indexdir/**/*.xml; do - postfile="$(echo "$xfic"|sed 's#^'$postsdir'/##')" - blogfile="$(echo "$xfic"|sed 's#.xml$#.html#;s#^'$indexdir'/#posts/#')" - printf "%-30s" $postfile - d=$(finddate $xfic) - echo -n " [$d]" - rssdate=$(formatdate $d) - title=$(findtitle $xfic) - keywords=( $(findkeywords $xfic) ) - printf ": %-55s" "$title ($keywords)" - taglist=$(mktaglist $keywords) - { printf "\\n
  • " - printf "\\n%s" "$d" - printf "\\n%s" "${blogfile}" "$title" - printf "\\n
  • \\n\\n" - } >> "$tmpdir/${d}-$(basename $xfic).index" - dates=( $d $dates ) - echo " [${fg[green]}OK${reset_color}]" -done echo "Publishing" # building the body + +dateaccessor='.pubDate' +finddate(){ < $1 hxselect -c $dateaccessor } + previousyear="" -for fic in $(ls $tmpdir/*.index | sort -r | head -n $maxarticles ); do +for fic in $indexdir/**/*.index; do + d=$(finddate $fic) + echo "${${fic:h}:t} [$d]" + cp $fic $tmpdir/$d-${${fic:h}:t}.index +done + +previousyear="" +for fic in $(ls $tmpdir/*.index | sort -r); do + d=$(finddate $fic) echo "${fic:t}" - year=$( echo "${fic:t}" | perl -pe 's#(\d{4})-.*#$1#') + year=$( echo "$d" | perl -pe 's#(\d{4})-.*#$1#') if (( year != previousyear )); then echo $year if (( previousyear > 0 )); then diff --git a/engine/mk-rss-entry.sh b/engine/mk-rss-entry.sh index 946a34d..19dc122 100755 --- a/engine/mk-rss-entry.sh +++ b/engine/mk-rss-entry.sh @@ -45,7 +45,7 @@ autoload -U colors && colors xfic="$fic" postfile="$(echo "$fic"|sed 's#^'$postsdir'/##')" blogfile="$(echo "$fic"|sed 's#.xml$#.html#;s#^'$indexdir'/#posts/#')" -printf "%-30s" $postfile +printf "%-30s" $blogfile d=$(finddate $xfic) echo -n " [$d]" rssdate=$(formatdate $d) @@ -54,7 +54,7 @@ keywords=( $(findkeywords $xfic) ) printf ": %-55s" "$title ($keywords)" categories=$(mkcategories $keywords) absoluteurl="${websiteurl}/${blogfile}" -mkdir -p $(dirname $dst) +[[ ! -d $(dirname $dst) ]] && mkdir -p $(dirname $dst) { printf "\\n" printf "\\n%s" "$title" printf "\\n%s" "$absoluteurl" @@ -63,8 +63,10 @@ mkdir -p $(dirname $dst) printf "\\n" "$(getcontent "$xfic" "$absoluteurl")" printf "\\n\\n\\n" } > "${dst}.tmp" + # overwrite only if the value in the index are different if ! cmp -s ${dst} ${dst}.tmp; then + echo " [${fg[yellow]}M${reset_color}]" mv -f ${dst}.tmp ${dst} fi echo " [${fg[green]}OK${reset_color}]" diff --git a/engine/mkrss.sh b/engine/mkrss.sh index ba6d4b4..0a137fd 100755 --- a/engine/mkrss.sh +++ b/engine/mkrss.sh @@ -32,7 +32,7 @@ formatdate() { isodate() { # format the date for sorting local d="$1" - echo "DEBUG DATE: $d" >&2 + # echo "DEBUG DATE: $d" >&2 LC_TIME=en_US date --date "$d" +'%Y-%m-%dT%H:%M:%S' } @@ -45,7 +45,7 @@ dates=( ) tmpdir=$(mktemp -d) for fic in $indexdir/**/*.rss; do rssdate=$(finddate $fic) - echo -n "${fic:r} [$d]" + echo -n "${${fic:h}:t} [$rssdate]" d=$(isodate $rssdate) dates=( $d $dates ) echo " [${fg[green]}OK${reset_color}]" diff --git a/src/drafts/0018-makefile-as-static-site-builder-follow-up/index.org b/src/posts/0018-makefile-as-static-site-builder-follow-up/index.org similarity index 98% rename from src/drafts/0018-makefile-as-static-site-builder-follow-up/index.org rename to src/posts/0018-makefile-as-static-site-builder-follow-up/index.org index 52fafa1..fbf3f7c 100644 --- a/src/drafts/0018-makefile-as-static-site-builder-follow-up/index.org +++ b/src/posts/0018-makefile-as-static-site-builder-follow-up/index.org @@ -308,6 +308,7 @@ ALL += indexcache So to resume this rule will generate for every file in =site/posts/*.html= a corresponding =xml= file (=hxclean= takes an HTML an try its best to make an XML out of it). + ** HTML Index :PROPERTIES: :CUSTOM_ID: html-index @@ -335,6 +336,11 @@ My =mk-index.sh= script takes advantage of the index files we constructed before with =hxclean=. Mainly I use =hxselect= to find the information I want to find, the title, the date and the keywords. +So my script is composed of three parts: + +1. declare the accessors (think CSS-like accessor) +2. for all files in the cache retrieve the metas, and sort them by date +3. compose the content #+begin_src bash #!/usr/bin/env zsh