webnomad/render

266 lines
5.4 KiB
Bash
Executable File

#!/usr/bin/env zsh
#
# Jaro Web, your slick and static website publisher
#
# Copyleft (C) 2012 Denis Roio <jaromil@dyne.org>
#
# This source code is free software; you can redistribute it and/or
# modify it under the terms of the GNU Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This source code is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# Please refer to the GNU Public License for more details.
#
# You should have received a copy of the GNU Public License along with
# this source code; if not, write to:
# Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
source jaroweb/utils
{ test -r config.zsh } || {
error "Directory not configured for Jaro Web. First use jaroweb init"
exit 1 }
####################################
source config.zsh
notice "Rendering $BRAND website"
act "Title: $TITLE"
mkdir -p pub
cat << EOF > pub/.htaccess
DirectoryIndex index index.html index.php
DefaultType text/html
EOF
render_header() {
cat <<EOF
<!DOCTYPE html>
<html>
<head>
<title>$TITLE</title>
<meta name="description" content="$DESCRIPTION">
<meta name="keywords" content="$KEYWORDS">
EOF
cat tmpl/header.html
echo "<nav>"
render_navbar ${sec}
echo "</nav>"
}
render_html() {
src=`find views -type f -name "${1}.html"`
{ test -r "${src}" } || {
print "${1} section not found";
return 1 }
dst="pub/${1}"
act -n "rendering html: $dst... "
# TODO: check duplicates
# establish if we are rendering a section
if [[ ${sections[(i)$1]} -le ${#sections} ]]; then
sec=${1}
else
sec=`dirname $src`
sec=${sec[(ws:/:)2]}
fi
render_header > $dst
echo "<article>" >> $dst
grep -v '^#' ${src} >> $dst
echo "</article>" >> $dst
cat tmpl/footer.html >> $dst
print "done."
}
render_markdown() {
src=`find views -type f -name "${1}.md"`
{ test -r "${src}" } || {
print "${1} section not found";
return 1 }
dst="pub/${1%.*}"
act -n "rendering markdown: $dst... "
# TODO: check duplicates
# establish if we are rendering a section
if [[ ${sections[(i)$1]} -le ${#sections} ]]; then
sec=${1}
else
sec=`dirname $src`
sec=${sec[(ws:/:)2]}
fi
render_header > $dst
echo "<article>" >> $dst
cat ${src} | markdown >> $dst
echo "</article>" >> $dst
cat tmpl/footer.html >> $dst
print "done."
}
read_meta() {
tmp=`awk '
!/^#/ { next }
/title/ { printf "title=\""; for(i=3;i<=NF;i++) printf "%s ", $i; printf "\";" }
/description/ { printf "description=\""; for(i=3;i<=NF;i++) printf "%s ", $i; printf "\";" }
/keywords/ { printf "keywords=\""; for(i=3;i<=NF;i++) printf "%s ", $i; printf "\";" }
/link/ { printf "link=\""; for(i=3;i<=NF;i++) printf "%s ", $i; printf "\";" }
/icon/ { printf "icon=\""; for(i=3;i<=NF;i++) printf "%s ", $i; printf "\";" }
/image/ { printf "image=\""; for(i=3;i<=NF;i++) printf "%s ", $i; printf "\";" }
' ${1}`
eval "$tmp"
}
render_section() {
sname=${1}
spath=views/${sname}
{ test -d ${spath} } && {
# the section is a directory containing files
# so we generate its html
pages=`find ${spath} -type f`
{ test ${#pages} = 0 } && {
print "${sname} section is an empty directory"
return 1 }
print "${sname} section"
print "<h1>${sname//_/ }</h1>" > ${spath}.html
for p in ${(f)pages}; do
pp=`basename $p`
case ${pp[(ws:.:)2]} in
html)
read_meta $p
# fill in entry into section page
cat <<EOF >> ${spath}.html
<h2>$title</h2>
<p>$description</p>
EOF
# print "Title: $title"
# print "Description: $description"
# print "Keywords: $keywords"
render_html ${pp[(ws:.:)1]}
;;
link)
print -n "rendering $pp... "
read_meta $p
cat <<EOF >> ${spath}.html
<h2><a href="$link" target="_blank">$title</a></h2>
<p>$description</p>
EOF
print "done."
;;
*)
print "${p}: page type unknown"
;;
esac
done # pages
}
{ test -r ${spath}.html } || {
print "${sname} section not found in views"
return 1 }
# render section's overview
render_html ${sname}
# at this point the section is an actual html file
}
render_navbar() {
active="$1"
cat <<EOF
<!-- BEGIN NAVIGATION BAR -->
<div class="navbar ${NAVCLASS}">
<div class="navbar-inner">
EOF
# defaults
nav_active="brand"
nav_icon="&nbsp;"
icon=""
# first section: overview
if [ "$active" = "views" ]; then
nav_active+=" active"
current="index"
else
current="$1"
fi
cat <<EOF
<a href="index">
<div class="$nav_active">
${nav_icon}&nbsp;&nbsp;${BRAND}
</div>
</a>
<ul class="nav">
EOF
unset active
# use the plain html navbar in tmpl/
{ test -r tmpl/navbar.html } && { cat tmpl/navbar.html }
cat <<EOF
</ul>
</div>
</div>
<!-- END NAVIGATION BAR -->
EOF
}
# Main
render_html index
# render all HTML views
htmls=(`find views -type f -name '*.html'`)
for h in $htmls; do
render_html `basename ${h%.*}`
done
# render all markdown views
mds=(`find views -type f -name '*.md'`)
for m in $mds; do
render_markdown `basename ${m%.*}`
done
for m in `find views -mindepth 1 -type d `; do
act -n "publishing $m... "
rsync -r $m pub/
print done
done
act "Website refreshed."