webnomad/render

324 lines
7.2 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env zsh
#
# Jaro Web, your slick and static website publisher
#
# Copyleft (C) 2012-2013 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"
typeset -a includejs
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" />
<meta charset="utf-8" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="Generator" content="Jaro Web http://dyne.org/software/jaroweb" />
<meta name="MSSmartTagsPreventParsing" content="True" />
<!-- Le HTML5 shim, for IE6-8 support of HTML5 elements
http://html5shim.googlecode.com/svn/trunk/html5.js -->
<!--[if lt IE 9]>
<script src="js/html5.js"></script>
<![endif]-->
<!-- Bootstrap -->
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link href="css/bootstrap.min.css" rel="stylesheet" />
<link href="css/bootstrap-responsive.min.css" rel="stylesheet" />
EOF
{ test "$1" = "" } || { echo "${@}"; echo }
cat tmpl/header.html
cat <<EOF
</head> <!-- end of <head> -->
<body>
<div class="container">
EOF
cat tmpl/navbar.html
}
render_footer() {
cat tmpl/footer.html
cat <<EOF
</div><!--/.container-->
EOF
{ test "$1" = "" } || { echo "${@}"; echo }
for i in $includejs; do
echo "<script src=\"$i\"></script>"
done
cat <<EOF
</body>
</html>
EOF
}
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
includejs+=(js/bootstrap.min.js)
render_footer >> $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
includejs+=(js/bootstrap.min.js)
render_footer >> $dst
print "done."
}
render_gallery() {
src=`find views -type f -name "${1}.gal"`
{ test -r "${src}" } || {
print "${1} section not found";
return 1 }
dst="pub/${1%.*}"
act -n "rendering gallery: $dst... "
render_header "<link rel=\"stylesheet\" href=\"css/blueimp-gallery.min.css\" />" > $dst
cat <<EOF >> $dst
<!-- The Gallery as lightbox dialog, should be a child element of the document body -->
<div id="blueimp-gallery" class="blueimp-gallery">
<div class="slides"></div>
<h3 class="title"></h3>
<a class="prev"></a>
<a class="next"></a>
<a class="close">×</a>
<a class="play-pause"></a>
<ol class="indicator"></ol>
</div>
EOF
# embed the first slide into the page for non-javascript browsers
nojs_line=`grep -v '^#' ${src} | head -n1`
nojs_file=${nojs_line[(ws: :)1]}
nojs_desc=`echo $nojs_line | awk '{ for(c=2;c<=NF;c++) printf("%s ",$c) }'`
cat <<EOF >> $dst
<noscript>
<p>You either have JavaScript turned off or your browser doesn't support JavaScript.<br />
This website mostly consists of image galleries and needs JavaScript.</p>
<!-- just in case there is no javascript, show the first image-->
<img src="${nojs_file}" alt="${nojs_desc}">
</noscript>
EOF
# generate the gallery's javascript
dst_js="js/${1%.*}-gallery.js"
cat <<EOF > pub/${dst_js}
var slides= [];
EOF
# parse gallery entries
pics=`grep -v '^#' ${src}`
for p in ${(f)pics}; do
file=${p[(ws: :)1]}
desc=`echo $p | awk '{ for(c=2;c<=NF;c++) printf("%s ",$c) }'`
cat << EOF >> pub/$dst_js
slides.push({
href: '${file}',
title: '${desc}'
});
EOF
done
cat <<EOF >> pub/$dst_js
function shuffle(array) {
var m = array.length, t, i;
// While there remain elements to shuffle…
while (m) {
// Pick a remaining element…
i = Math.floor(Math.random() * m--);
// And swap it with the current element.
t = array[m];
array[m] = array[i];
array[i] = t;
}
return array;
}
slides = shuffle(slides);
blueimp.Gallery(slides, {
container: '#blueimp-gallery',
carousel: true,
fullscreen: true,
closeOnEscape: true,
closeOnSlideClick: true,
closeOnSwipeUpOrDown: false,
startSlideshow: true
});
EOF
# includejs+=(js/blueimp-helper.js)
includejs+=(js/jquery.min.js)
includejs+=(js/bootstrap.min.js)
includejs+=(js/blueimp-gallery.min.js)
# includejs+=(js/jquery.blueimp-gallery.min.js)
# includejs+=(js/blueimp-gallery-fullscreen.js)
includejs+=($dst_js)
render_footer >> $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 "\";" }
/gallery/ { printf "gallery=\""; for(i=3;i<=NF;i++) printf "%s ", $i; printf "\";" }
' ${1}`
eval "$tmp"
}
# Main
# render all HTML views
htmls=(`find views -type f -name '*.html'`)
for h in $htmls; do
read_meta ${h}
render_html `basename ${h%.*}`
done
# render all markdown views
mds=(`find views -type f -name '*.md'`)
for m in $mds; do
read_meta ${m}
render_markdown `basename ${m%.*}`
done
# render all image galleries
gals=(`find views -type f -name '*.gal'`)
for g in $gals; do
read_meta ${g}
render_gallery `basename ${g%.*}`
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."