#!/bin/bash # Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009-2011 # # This file is part of webgen, # a small template engine for easy management of small websites. # # This file is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License version 3 # as published by the Free Software Foundation. # # webgen 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. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this software; see the file COPYING. If not, write to # the Free Software Foundation, 51 Franklin Street, Fifth Floor, # Boston, MA 02110-1301, USA. # Prefixes: # - Include # + Normal page # ~ Dynamically generated page # = Raw mode file. # # Special/Config file # Special files: # -header Is included before all [+~] pages # -end Is included after all [+~] pages # -menu-mid Is included after after menu file (if exists) on all [+~] pages. # -faq_eoh ??? # ?FAQ Will be rendered as FAQ. # ?NEWS Will be rendered as NEWS. # ?NO-TEMPLATES Must not exist. # CONFIG: OUT=`pwd` TPLS=tpl/ HEADER=header END=end PAGE_SUFFIX='.html' TEMPLATES='' DEFAULT_MENU='-menu-none' PREPROCESSOR='none' # CODE: while [ ! -z "$1" ] do case "$1" in '--preprocessor') PREPROCESSOR="$2" shift ;; '--'*) echo "Error: Unknown option: $1" >&2 exit 1 ;; *) TEMPLATES="$1" ;; esac shift done [ "$TEMPLATES" = '' ] && TEMPLATES="*" if [ "$TEMPLATES" = '+' ] then TEMPLATES_PAGE="*" TEMPLATES_DYN="NO-TEMPLATES" TEMPLATES_RAW="NO-TEMPLATES" elif [ "$TEMPLATES" = '~' ] then TEMPLATES_PAGE="NO-TEMPLATES" TEMPLATES_DYN="*" TEMPLATES_RAW="NO-TEMPLATES" elif [ "$TEMPLATES" = '=' ] then TEMPLATES_PAGE="NO-TEMPLATES" TEMPLATES_DYN="NO-TEMPLATES" TEMPLATES_RAW="*" else TEMPLATES_PAGE="$TEMPLATES" TEMPLATES_DYN="$TEMPLATES" TEMPLATES_RAW="$TEMPLATES" fi cd $TPLS cat_file() { _file="$1" case "$PREPROCESSOR" in none) cat -- "$_file" ;; cpp) cpp -P -o - -D__TEMPLATE__="\"$_tpl\"" -D__TEMPLATE_CLEAN__="\"$CLEANTPLNAME\"" -D __OUTPUT_FILE__="\"$OF\"" ./"$_file" ;; *) echo "Error: Unknown preprocessor: $PREPROCESSOR" >&2 ;; esac } gen_news() { if [ ! -d NEWS/ ] then return 1 fi ls -1t NEWS/*.news | while read line do title=`echo "$line" | sed 's/.news$//; s#^NEWS/##;'` e=`echo "$title" | sed 's/[^a-zA-Z0-9]/_/g'` _t=`stat --format='%Z %U' "$line"` ctime=`echo "$_t" | cut -d' ' -f1` user=`echo "$_t" | cut -d' ' -f2` ctime_lr=`date --date "1970-01-01 00:00:00 UTC + ${ctime}sec"` echo "

$title

" echo "

$ctime_lr by $user

" echo "

" sed 's#^$#

#' < "$line" echo "

" done } gen_faq() { find FAQ/ -type d -not -wholename \*CVS\* | sed 's#^FAQ/##;' | grep -v ^$ | sort > "$OF.FAQCATS" find FAQ/ -type f -not -wholename \*CVS\* -and -name \*.faq | sed 's#^FAQ/##; s#\.faq$##' | sort > "$OF.FAQINDEX" echo echo "" [ -f ./-faq_eoh ] && cat_file "-faq_eoh" while read cat do echo "

$cat

" echo grep "^$cat/" < "$OF.FAQINDEX" | \ while IFS='/' read dummy q do e=`echo "$q" | sed 's/[^a-zA-Z0-9]/_/g'` echo "

$q

" echo "

" sed 's#^$#

#' < "FAQ/$cat/$q.faq" echo "

" done done < "$OF.FAQCATS" echo } proc_page() { _tpl="$1" CLEANTPLNAME=$(echo "$_tpl" | sed 's/^[\-\+\~]//') OF="$OUT"/"$CLEANTPLNAME""$PAGE_SUFFIX" echo "Compiling $CLEANTPLNAME -> $OF" SECTIONS=`for _sec in $(grep --no-filename -- "^$_tpl[^a-zA-Z0-9-]" '#section-index'* | sed 's/^[^ \t]*[ \t]*[ \t]//'); do O=''; for i in $(tr . ' ' <<<"$_sec"); do O="$O.$i"; echo $O; done; done`' .ALL' # echo "Sections: $SECTIONS" SECTIONS_REGEX=$(for _sec in $SECTIONS; do echo "s/(section==$_sec:\([^)]*\):)/\1/g;"; done; echo "s/(section==.*:.*:)//g") MENU=$(grep --no-filename -- "^$_tpl[^a-zA-Z0-9-]" '#menu-index'* | sed 's/^[^ \t]*[ \t]*[ \t]//'); [ "$MENU" = '' ] && MENU="$DEFAULT_MENU" { cat_file -$HEADER [ -n "$MENU" ] && cat_file "$MENU" [ -e -menu-mid ] && cat_file -menu-mid cat_file $_tpl [ "$CLEANTPLNAME" = 'FAQ' ] && gen_faq [ "$CLEANTPLNAME" = 'NEWS' ] && gen_news cat_file -$END } | \ sed "$SECTIONS_REGEX" | \ sed 's#href="[+~]\([^"\#]*\)\(\#*[^"]*\)"#href="\1'"$PAGE_SUFFIX"'\2"#g' > "$OF" } proc_raw() { _tpl="$1" CLEANTPLNAME=$(echo "$_tpl" | sed 's/^=//') OF="$OUT"/"$CLEANTPLNAME" echo "Compiling $CLEANTPLNAME -> $OF" cat_file $_tpl > "$OF" } for i in +$TEMPLATES_PAGE \~$TEMPLATES_DYN do [ -f "./$i" ] && proc_page "$i" done for i in =$TEMPLATES_RAW do [ -f "./$i" ] && proc_raw "$i" done exit 0; #ll