From 465c4ba5b4f0b4f69fd3773fc4f9f9650b7fffa0 Mon Sep 17 00:00:00 2001 From: Hsieh Chin Fan Date: Sun, 15 Dec 2024 00:41:50 +0800 Subject: add rss --- www/Makefile | 5 +- www/scripts/build.sh | 172 ++++++++++++++++++++++++++------------------------- 2 files changed, 91 insertions(+), 86 deletions(-) diff --git a/www/Makefile b/www/Makefile index ab6a39e..cf45b29 100644 --- a/www/Makefile +++ b/www/Makefile @@ -4,8 +4,9 @@ ROOT_DIR := $(dir $(realpath $(lastword $(MAKEFILE_LIST)))) # build html files for public/ build: cd $(ROOT_DIR) - export input_dir=~/log/public - export output_dir=/srv/http + export INPUT_DIR=~/log/public + export OUTPUT_DIR=/srv/http + export HOST=topo.tw @scripts/build.sh # Migrage frontmatter from '---' to HTML comment '' diff --git a/www/scripts/build.sh b/www/scripts/build.sh index e3757ac..23c4a70 100755 --- a/www/scripts/build.sh +++ b/www/scripts/build.sh @@ -3,13 +3,15 @@ set -e # Executable command for markdown -markdown_bin="markdown -f fencedcode,autolink,alphalist,autolink,footnote,definitionlist" +declare -r MARKDOWN_BIN="markdown -f fencedcode,autolink,alphalist,autolink,footnote,definitionlist" # Directory for input/output -input_dir=${input_dir:?ENV \"input_dir\" is not set} -output_dir=${output_dir:?ENV \"output_dir\" is not set} -assets_dir=${assets_dir:-`pwd`/assets} -template_dir=${template_dir:-`pwd`/templates} +declare -r INPUT_DIR=${INPUT_DIR:?ENV \"INPUT_DIR\" is not set} +declare -r OUTPUT_DIR=${OUTPUT_DIR:?ENV \"OUTPUT_DIR\" is not set} +declare -r ASSET_DIR=${ASSET_DIR:-`pwd`/assets} +declare -r TEMPLATE_DIR=${TEMPLATE_DIR:-`pwd`/templates} +declare -r RSSFILE=feed.rss +declare -r HOST=${HOST:?} # functions {{{ @@ -41,22 +43,22 @@ comment_block() { } # }}} -# use heredoc to generate html from .md file and templates {{{ +# generate html by heredoc from .md file and templates {{{ html() { <<-END_OF_HTML sed '1d;$d' - ${head} + $(echo "$HEAD" | { [ -z "${title}" ] && cat || sed "/^ */c \ \ <title>${title}"; } ) - ${header} + ${HEADER}

- $(${markdown_bin} | indent 4) + $(${MARKDOWN_BIN} | indent 4)
$(comment_block | indent 2)

- ${footer} + ${FOOTER} @@ -73,31 +75,28 @@ latest_posts() { } # }}} -# print frontmatter from markdown file with format: " " {{{ +# process frontmatter {{{ get_frontmatter() { sed -n '1 {//q; s/"//g; s/://p' } -# }}} -# process frontmatter {{{ - -add_index() { - unset title public index date draft +check_frontmatter() { + unset title public index type date draft # define local variables for frontmatter while read key value; do - local -r $key="$value" + export ${key,,}="$value" done <<<"$(get_frontmatter)" # don't process draft after function call test "$draft" != "" && return 1 # skip making index in some cases + test "$title" = "" && return 0 test "$public" = false && return 0 test "$index" = false && return 0 test "$type" = demo && return 0 - test "$title" = "" && return 0 iso8601=$(date --iso --date "${date:-NULL}" 2>/dev/null) test "$iso8601" = "" && return 0 @@ -109,99 +108,97 @@ add_index() { # remove SGML comments but keep the top one as frontmatter {{{ ignore_comment() { - sed '1 !{ /^$/ d }' + sed '1! { /^$/ d }' } # }}} -# Generate the feed file {{{ +# RSS Feed {{{ + +get_channel_item() { + path=${1#${OUTPUT_DIR}} + <<-ITEM cat + + $(grep -Po '.+' ${1} | head -1) + https://${HOST}${path} + $(md5sum ${1} | cut -d' ' -f1) + $(date --rfc-email -r ${1}) + + ITEM +} make_rss() { - echo -n "Making RSS " - - rssfile=$blog_feed.$RANDOM - while [[ -f $rssfile ]]; do rssfile=$blog_feed.$RANDOM; done - - { - pubdate=$(LC_ALL=C date +"$date_format_full") - cat <<-EOF - ' - ' - Dummy Websitehttps://topo.tw/index.xml" - $global_descriptionen" - $pubdate" - $pubdate" - " - EOF - - n=0 - while IFS='' read -r i; do - is_boilerplate_file "$i" && continue - ((n >= number_of_feed_articles)) && break # max 10 items - echo -n "." 1>&3 - echo '' - get_post_title "$i" - echo '$global_url/${i#./}" - echo "$global_url/$i" - echo "$(get_post_author "$i")" - echo "$(LC_ALL=C date -r "$i" +"$date_format_full")" - - n=$(( n + 1 )) - done < <(ls -t ./*.html) - - echo '' - } 3>&1 >"$rssfile" - echo "" - - mv "$rssfile" "$blog_feed" - chmod 644 "$blog_feed" + # Use modified time to get latest html files + items="$( + ls -1t $OUTPUT_DIR/**/**html \ + | head -10 \ + | while read html_file; do + get_channel_item $html_file + done \ + | indent 2 \ + )" + + <<-RSS cat + + + Dummy Website + https://${HOST} + + $global_description + zh-tw + $(date --rfc-email) + https://www.rssboard.org/rss-specification + ${items} + + + RSS } # }}} + # }}} # prepare directory for outputs {{{ -mkdir -p $output_dir/ -rm -rf $output_dir/** -ln -s $assets_dir/* $output_dir/ +mkdir -p $OUTPUT_DIR/ +rm -rf $OUTPUT_DIR/** +ln -s $ASSET_DIR/* $OUTPUT_DIR/ # }}} # content of templates {{{ -head="$(cat $template_dir/head.html)" -header="$(cat $template_dir/header.html | indent 2)" -footer="$(cat $template_dir/footer.html | indent 2)" +declare -r HEAD="$(cat $TEMPLATE_DIR/head.html)" +declare -r HEADER="$(<$TEMPLATE_DIR/header.html indent 2)" +declare -r FOOTER="$(<$TEMPLATE_DIR/footer.html indent 2)" +declare -r INDEX_TEMPLATE="$(cat $TEMPLATE_DIR/index.md)" index_list=() -index_template="$(cat $template_dir/index.md)" # }}} # for each markdown file {{{ -files="$(find "$input_dir" -type f -name '*md')" +files="$(find "$INPUT_DIR" -type f -name '*md')" total=$(wc -l <<<"$files") declare -i counter for file in $files; do # set variables - path=$(<<<"$file" sed "s#^${input_dir}/##; s/\.md$//").html; mkdir -p $(dirname $output_dir/$path) + path=$(<<<"$file" sed "s#^${INPUT_DIR}/##; s/\.md$//").html; mkdir -p $(dirname $OUTPUT_DIR/$path) content="$(<${file} ignore_comment)" # use frontmatter to decide making html file or not - <<<"$content" add_index || continue + <<<"$content" check_frontmatter || continue # log echo -e "\033[1K\r$((counter+=1))/$total \t\t processing $path" - # make html file for draft - h1="$(<<<"$content" get_frontmatter | sed -n 's/^title *//p')" - comment="$(<<<"$content" get_frontmatter | sed -n 's/^comment *//p')" - - # add original content to $output_dir + # add original content to $OUTPUT_DIR # and generate html file with

echo "$content" \ - | tee $output_dir/${file#${input_dir}/} \ - | { [ -n "$h1" ] && echo "# $h1"; cat; } \ - | html >$output_dir/$path + | tee $OUTPUT_DIR/${file#${INPUT_DIR}/} \ + | { [ -n "$title" ] && echo "# $title"; cat; } \ + | html >$OUTPUT_DIR/$path + + # Set modified time for index/RSS + mtime='@0' + [ ! "$public" = false ] && [ -n "$iso8601" ] && mtime="$iso8601" + touch -m -d "$mtime" $OUTPUT_DIR/$path unset h1 comment done @@ -210,19 +207,26 @@ echo # }}} # make index.html {{{ +echo -en "Making\t\tindex.html" +export title="" { - echo "${index_template}" + echo "${INDEX_TEMPLATE}" echo -e '

\n\n' latest_posts } \ -| tee $output_dir/index.md \ -| html >$output_dir/index.html +| tee $OUTPUT_DIR/index.md \ +| html >$OUTPUT_DIR/index.html -echo -e index.html "\t" generated +touch -m -d @0 $OUTPUT_DIR/index.html +echo -e "\tgenerated" # }}} -# make index.xml {{{ - +# make RSS file {{{ +echo -en "Making\t\tRSS" +make_rss >$OUTPUT_DIR/$RSSFILE +echo -e "\t\tgenerated" # }}} + +# vim: fdm=marker fdl=0 -- cgit v1.2.3-70-g09d2