summaryrefslogtreecommitdiff
path: root/webgen
blob: 58b6202390e3ff0b0cf1829d5b5a4c3ca6235435 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/bin/sh

#      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.

# CONFIG:

OUT=`pwd`
TPLS=tpl/

HEADER=header
END=end
PAGE_SUFFIX='.html'
TEMPLATES="$1"
DEFAULT_MENU='-menu-none'

# CODE:
[ "$TEMPLATES" = '' ] && TEMPLATES="*"

if [ "$TEMPLATES" = '+' ]
then
 TEMPLATES_PAGE="*"
 TEMPLATES_DYN="NO-TEMPLATES"
elif [ "$TEMPLATES" = '~' ]
then
 TEMPLATES_PAGE="NO-TEMPLATES"
 TEMPLATES_DYN="*"
else
 TEMPLATES_PAGE="$TEMPLATES"
 TEMPLATES_DYN="$TEMPLATES"
fi

cd $TPLS

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 "<h2><a name="\""news.gen.$e"\"">$title</a></h2>"
  echo "<p align=\"right\"><small>$ctime_lr by $user</small></p>"
  echo " <p>"
  sed 's#^$#</p><p>#' < "$line"
  echo " </p>"
 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 "<ul>"
 while read cat
 do
  echo " <li><b>$cat</b>"
  echo "  <ul>"
  grep "^$cat/" < "$OF.FAQINDEX" | \
  while IFS='/' read dummy q
  do
   e=`echo "$q" | sed 's/[^a-zA-Z0-9]/_/g'`
   echo "   <li><a href="\""#faq.gen.$e"\"">$q</a></li>"
  done
  echo "  </ul>"
  echo " </li>"
 done < "$OF.FAQCATS"
 echo "</ul>"

 [ -f ./-faq_eoh ] && cat "./-faq_eoh"

 while read cat
 do
  echo " <h2>$cat</h2>"
  echo
  grep "^$cat/" < "$OF.FAQINDEX" | \
  while IFS='/' read dummy q
  do
   e=`echo "$q" | sed 's/[^a-zA-Z0-9]/_/g'`
   echo " <h3><a name="\""faq.gen.$e"\"">$q</a></h3>"
   echo " <p>"
   sed 's#^$#</p><p>#' < "FAQ/$cat/$q.faq"
   echo " </p>"
  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 -- -$HEADER
  [ -n "$MENU"   ] && cat -- "$MENU"
  [ -e -menu-mid ] && cat -- -menu-mid
  cat --  $_tpl
  [ "$CLEANTPLNAME" = 'FAQ' ] && gen_faq
  [ "$CLEANTPLNAME" = 'NEWS' ] && gen_news
  cat -- -$END
 } | \
   sed "$SECTIONS_REGEX" | \
 sed 's#href="[+~]\([^"]*\)"#href="\1'"$PAGE_SUFFIX"'"#g' > $OF
# perl -pe's#href="\+(.+?)"#href="$1'"$PAGE_SUFFIX"'"#g' > $OF
}

for i in +$TEMPLATES_PAGE \~$TEMPLATES_DYN
do
 [ -f "./$i" ] && proc_page "$i"
done

#ll