Improve index generation

This commit is contained in:
Yann Esposito (Yogsototh) 2021-05-25 16:27:58 +02:00
parent 2ca69dbab5
commit bb24585590
Signed by untrusted user who does not match committer: yogsototh
GPG Key ID: 7B19A4C650D59646
6 changed files with 92 additions and 30 deletions

View File

@ -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

57
engine/mk-index-entry.sh Executable file
View File

@ -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 "<span class=\"tag\">%s</span>" $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<li>"
printf "\\n<span class=\"pubDate\">%s</span>" "$d"
printf "\\n<a href=\"%s\">%s</a>" "${blogfile}" "$title"
printf "\\n</li>\\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}]"

View File

@ -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<li>"
printf "\\n<span class=\"pubDate\">%s</span>" "$d"
printf "\\n<a href=\"%s\">%s</a>" "${blogfile}" "$title"
printf "\\n</li>\\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

View File

@ -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<item>"
printf "\\n<title>%s</title>" "$title"
printf "\\n<guid>%s</guid>" "$absoluteurl"
@ -63,8 +63,10 @@ mkdir -p $(dirname $dst)
printf "\\n<description><![CDATA[\\n%s\\n]]></description>" "$(getcontent "$xfic" "$absoluteurl")"
printf "\\n</item>\\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}]"

View File

@ -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}]"

View File

@ -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