Improve index generation
This commit is contained in:
parent
2ca69dbab5
commit
bb24585590
6 changed files with 92 additions and 30 deletions
12
Makefile
12
Makefile
|
@ -64,10 +64,17 @@ indexcache: $(DST_XML_FILES)
|
||||||
ALL += indexcache
|
ALL += indexcache
|
||||||
|
|
||||||
# HTML INDEX
|
# 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
|
HTML_INDEX := $(DST_DIR)/index.html
|
||||||
MKINDEX := engine/mk-index.sh
|
MKINDEX := engine/mk-index.sh
|
||||||
INDEX_TEMPLATE ?= templates/index.html
|
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)
|
@mkdir -p $(DST_DIR)
|
||||||
$(MKINDEX)
|
$(MKINDEX)
|
||||||
.PHONY: index
|
.PHONY: index
|
||||||
|
@ -85,10 +92,9 @@ RSS := $(DST_DIR)/rss.xml
|
||||||
MKRSS := engine/mkrss.sh
|
MKRSS := engine/mkrss.sh
|
||||||
$(RSS): $(DST_RSS_FILES) $(MKRSS)
|
$(RSS): $(DST_RSS_FILES) $(MKRSS)
|
||||||
$(MKRSS)
|
$(MKRSS)
|
||||||
ALL += $(RSS)
|
|
||||||
|
|
||||||
.PHONY: rss
|
.PHONY: rss
|
||||||
rss: $(DST_RSS_FILES) $(RSS)
|
rss: $(RSS)
|
||||||
ALL += rss
|
ALL += rss
|
||||||
|
|
||||||
|
|
||||||
|
|
57
engine/mk-index-entry.sh
Executable file
57
engine/mk-index-entry.sh
Executable 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}]"
|
|
@ -33,36 +33,27 @@ mktaglist(){
|
||||||
|
|
||||||
autoload -U colors && colors
|
autoload -U colors && colors
|
||||||
tmpdir=$(mktemp -d)
|
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"
|
echo "Publishing"
|
||||||
|
|
||||||
# building the body
|
# building the body
|
||||||
|
|
||||||
|
|
||||||
|
dateaccessor='.pubDate'
|
||||||
|
finddate(){ < $1 hxselect -c $dateaccessor }
|
||||||
|
|
||||||
previousyear=""
|
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}"
|
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
|
if (( year != previousyear )); then
|
||||||
echo $year
|
echo $year
|
||||||
if (( previousyear > 0 )); then
|
if (( previousyear > 0 )); then
|
||||||
|
|
|
@ -45,7 +45,7 @@ autoload -U colors && colors
|
||||||
xfic="$fic"
|
xfic="$fic"
|
||||||
postfile="$(echo "$fic"|sed 's#^'$postsdir'/##')"
|
postfile="$(echo "$fic"|sed 's#^'$postsdir'/##')"
|
||||||
blogfile="$(echo "$fic"|sed 's#.xml$#.html#;s#^'$indexdir'/#posts/#')"
|
blogfile="$(echo "$fic"|sed 's#.xml$#.html#;s#^'$indexdir'/#posts/#')"
|
||||||
printf "%-30s" $postfile
|
printf "%-30s" $blogfile
|
||||||
d=$(finddate $xfic)
|
d=$(finddate $xfic)
|
||||||
echo -n " [$d]"
|
echo -n " [$d]"
|
||||||
rssdate=$(formatdate $d)
|
rssdate=$(formatdate $d)
|
||||||
|
@ -54,7 +54,7 @@ keywords=( $(findkeywords $xfic) )
|
||||||
printf ": %-55s" "$title ($keywords)"
|
printf ": %-55s" "$title ($keywords)"
|
||||||
categories=$(mkcategories $keywords)
|
categories=$(mkcategories $keywords)
|
||||||
absoluteurl="${websiteurl}/${blogfile}"
|
absoluteurl="${websiteurl}/${blogfile}"
|
||||||
mkdir -p $(dirname $dst)
|
[[ ! -d $(dirname $dst) ]] && mkdir -p $(dirname $dst)
|
||||||
{ printf "\\n<item>"
|
{ printf "\\n<item>"
|
||||||
printf "\\n<title>%s</title>" "$title"
|
printf "\\n<title>%s</title>" "$title"
|
||||||
printf "\\n<guid>%s</guid>" "$absoluteurl"
|
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<description><![CDATA[\\n%s\\n]]></description>" "$(getcontent "$xfic" "$absoluteurl")"
|
||||||
printf "\\n</item>\\n\\n"
|
printf "\\n</item>\\n\\n"
|
||||||
} > "${dst}.tmp"
|
} > "${dst}.tmp"
|
||||||
|
|
||||||
# overwrite only if the value in the index are different
|
# overwrite only if the value in the index are different
|
||||||
if ! cmp -s ${dst} ${dst}.tmp; then
|
if ! cmp -s ${dst} ${dst}.tmp; then
|
||||||
|
echo " [${fg[yellow]}M${reset_color}]"
|
||||||
mv -f ${dst}.tmp ${dst}
|
mv -f ${dst}.tmp ${dst}
|
||||||
fi
|
fi
|
||||||
echo " [${fg[green]}OK${reset_color}]"
|
echo " [${fg[green]}OK${reset_color}]"
|
||||||
|
|
|
@ -32,7 +32,7 @@ formatdate() {
|
||||||
isodate() {
|
isodate() {
|
||||||
# format the date for sorting
|
# format the date for sorting
|
||||||
local d="$1"
|
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'
|
LC_TIME=en_US date --date "$d" +'%Y-%m-%dT%H:%M:%S'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -45,7 +45,7 @@ dates=( )
|
||||||
tmpdir=$(mktemp -d)
|
tmpdir=$(mktemp -d)
|
||||||
for fic in $indexdir/**/*.rss; do
|
for fic in $indexdir/**/*.rss; do
|
||||||
rssdate=$(finddate $fic)
|
rssdate=$(finddate $fic)
|
||||||
echo -n "${fic:r} [$d]"
|
echo -n "${${fic:h}:t} [$rssdate]"
|
||||||
d=$(isodate $rssdate)
|
d=$(isodate $rssdate)
|
||||||
dates=( $d $dates )
|
dates=( $d $dates )
|
||||||
echo " [${fg[green]}OK${reset_color}]"
|
echo " [${fg[green]}OK${reset_color}]"
|
||||||
|
|
|
@ -308,6 +308,7 @@ ALL += indexcache
|
||||||
So to resume this rule will generate for every file in =site/posts/*.html=
|
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
|
a corresponding =xml= file (=hxclean= takes an HTML an try its best to make
|
||||||
an XML out of it).
|
an XML out of it).
|
||||||
|
|
||||||
** HTML Index
|
** HTML Index
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:CUSTOM_ID: html-index
|
:CUSTOM_ID: html-index
|
||||||
|
@ -335,6 +336,11 @@ My =mk-index.sh= script takes advantage of the index files we constructed
|
||||||
before with =hxclean=.
|
before with =hxclean=.
|
||||||
Mainly I use =hxselect= to find the information I want to find, the
|
Mainly I use =hxselect= to find the information I want to find, the
|
||||||
title, the date and the keywords.
|
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
|
#+begin_src bash
|
||||||
#!/usr/bin/env zsh
|
#!/usr/bin/env zsh
|
Loading…
Reference in a new issue