summaryrefslogtreecommitdiff
path: root/webgen
blob: d0e8bd7c8788429b8957752fdcaf5e1fd11b2e7d (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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#!/bin/bash

#      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009-2016
#
#  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'
DEFAULT_MENU='-menu-none'
PREPROCESSOR='none'
LANGUAGE=''

# CODE:
setup() {
 mkdir $TPLS
 cd $TPLS
 touch \#section-index
 touch \#menu-index
 touch ./-menu-none
 touch ./-menu-mid
 {
  echo '<html>'
  echo ' <head>'
  echo '  <title>Demo page</title>'
  echo ' </head>'
  echo ' <body>'
 } > ./-header
 {
  echo '  <h1>Demo page</h1>'
  echo '  <p>This is the webgen demo page. This page was generated by <pre>webgen --setup</pre>.</p>'
 } > ./+index
 {
  echo ' </body>'
  echo '</html>'
 } > ./-end
}

list_tpls() {
 cd $TPLS
 ls -1 -- +* \~* =* 2> /dev/null
}

list_dependencies() {
 cd $TPLS

 case "$1" in
  '+'*|'~'*)
   _tpl=`ls -1 -- "$1" 2> /dev/null`
  ;;
  '='*)
   echo "$TPLS/$1"
   return
  ;;
  *'.'*'.html')
   _mask=`echo "$1" | cut -d. -f1`
   _lang=`echo "$1" | cut -d. -f2`
   _tpl=`ls -1 -- ?"$_mask" 2> /dev/null`
   [ "$LANGUAGE" = '' ] && LANGUAGE="$_lang"
  ;;
  *'.html')
   _mask=`echo "$1" | cut -d. -f1`
   _tpl=`ls -1 -- ?"$_mask" 2> /dev/null`
  ;;
 esac

 if [ "$_tpl" = '' ]
 then
  echo "Template not found: $1" >&2
  exit 1
 fi
 MENU=$(grep --no-filename -- "^$_tpl[^a-zA-Z0-9-]" '#menu-index'* | sed 's/^[^ \t]*[ \t]*[ \t]//');
 {
  printf "%s\n" $_tpl -header -end $MENU
  if [ "$LANGUAGE" != '' ]
  then
   ls -1 -- "#translate" "#translate.$LANGUAGE" "#translate$_tpl" "#translate$_tpl.$LANGUAGE" 2> /dev/null
  fi
  ls -1 -- -menu-mid \#menu-index* \#section-index* 2> /dev/null
  if [ "$CLEANTPLNAME" = 'FAQ' ]
  then
   find FAQ/ -type d -not -wholename \*CVS\* | sed 's#^FAQ/##;'  | grep -v ^$
   find FAQ/ -type f -not -wholename \*CVS\* -and -name \*.faq
  fi
  if [ "$CLEANTPLNAME" = 'NEWS' ]
  then
   ls -1 -- NEWS/*.news 2> /dev/null
  fi
 } | sed 's#^#tpl/#'
}

show_help() {
 echo "Usage: webgen [--preprocessor PP] [--language LANG] [{+|~|=}][TPL]"
 echo "   or: webgen [--language LANG] --list-dependencies [{+|~|=}]TPL"
 echo "   or: webgen --setup"
 echo "   or: webgen --list"
 echo "   or: webgen --help"
}

TEMPLATES_PAGE="*"
TEMPLATES_DYN="*"
TEMPLATES_RAW="*"

while [ ! -z "$1" ]
do
 case "$1" in
  '--preprocessor')
   PREPROCESSOR="$2"
   shift
  ;;
  '--language')
   if [ "$2" = 'default' ]
   then
    LANGUAGE=''
   else
    LANGUAGE="$2"
   fi
   shift
  ;;
  '--setup')
   setup
   exit 0
  ;;
  '--list')
   list_tpls
   exit 0
  ;;
  '--list-dependencies')
   list_dependencies $2
   exit 0
  ;;
  '--help')
   show_help
   exit 0
  ;;
  '--'*)
   echo "Error: Unknown option: $1" >&2
   exit 1
  ;;
  '+')
   TEMPLATES_PAGE="*"
   TEMPLATES_DYN="NO-TEMPLATES"
   TEMPLATES_RAW="NO-TEMPLATES"
  ;;
  '~')
   TEMPLATES_PAGE="NO-TEMPLATES"
   TEMPLATES_DYN="*"
   TEMPLATES_RAW="NO-TEMPLATES"
  ;;
  '=')
   TEMPLATES_PAGE="NO-TEMPLATES"
   TEMPLATES_DYN="NO-TEMPLATES"
   TEMPLATES_RAW="*"
  ;;
  '+'*)
   TEMPLATES_PAGE=`echo "$1" | sed s/^.//`
   TEMPLATES_DYN="NO-TEMPLATES"
   TEMPLATES_RAW="NO-TEMPLATES"
  ;;
  '~'*)
   TEMPLATES_PAGE="NO-TEMPLATES"
   TEMPLATES_DYN=`echo "$1" | sed s/^.//`
   TEMPLATES_RAW="NO-TEMPLATES"
  ;;
  '='*)
   TEMPLATES_PAGE="NO-TEMPLATES"
   TEMPLATES_DYN="NO-TEMPLATES"
   TEMPLATES_RAW=`echo "$1" | sed s/^.//`
  ;;
  *)
   TEMPLATES_PAGE="$1"
   TEMPLATES_DYN="$1"
   TEMPLATES_RAW="$1"
  ;;
 esac
 shift
done

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 "<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_file "-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/^[\-\+\~]//')
 if [ "$LANGUAGE" = '' ]
 then
  LANGUAGE_SUFFIX=''
  TRANSLATE_REGEX='s/(translate:\(.*\):)/\1/'
 else
  LANGUAGE_SUFFIX=".$LANGUAGE"
  TRANSLATE_REGEX=`grep "^$LANGUAGE[[:space:]]" "#translate" "#translate$LANGUAGE_SUFFIX" "#translate$_tpl" "#translate$_tpl$LANGUAGE_SUFFIX" 2> /dev/null | sed 's/^[^\t]*\t\t*\([^\t]*\)\t\t*\(.*\)$/s|(translate:\1:)|\2|g;/'`'s/(translate:\(.*\):)/\1/'
 fi
 OF="$OUT"/"$CLEANTPLNAME""$LANGUAGE_SUFFIX""$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 "$TRANSLATE_REGEX" | \
   sed 's#href="[+~]\([^"\#(]*\)(same-language)\(\#*[^"]*\)"#href="\1'"$LANGUAGE_SUFFIX""$PAGE_SUFFIX"'\2"#g; s#href="[+~]\([^"\#]*\)\(\#*[^"]*\)"#href="\1'"$PAGE_SUFFIX"'\2"#g; s#href="=\([^"]*\)"#href="\1"#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