summaryrefslogtreecommitdiffstats
path: root/render
blob: b63ac191d2e263fba2c3fa281f96093afce04a72 (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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#!/usr/bin/env zsh
#
# WebNomad, your slick and static website publisher
#
# Copyright (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.

SYS=`dirname $0`
source $SYS/utils



{ test -r config.zsh } || {
    error "Directory not configured for WebNomad. First use /webnomad/init"
    exit 1 }


####################################

source config.zsh

notice "Rendering $B $BRAND $r website"
act "Title: $B $TITLE $r"

typeset -a includejs


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="WebNomad $VERSION - http://dyne.org/software/webnomad" />
    <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 -r views/css/custom.css } && {
cat <<EOF
    <link href="css/custom.css" rel="stylesheet" />

EOF
}

    { test "$1" = "" } || { echo "${@}"; echo }

    cat tmpl/header.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() {


#######################################
## we support the <markdown> tag inline

    tmp="tmp.$RANDOM"
    awk 'BEGIN { srand(); markdown=0; }
/^<markdown>/ { markdown=1; out="tmp.md" rand(); print out; next }
/^<\/markdown>/ { markdown=0; next }
{ if(markdown==1) { print $0 >out; next } else { print $0 } }
' > $tmp

# first pass marks the markdown parts and saves them separate

    mds=(`find . -name 'tmp.md*'`)
    { test "${#mds}" = "0" } || {
# second pass substituted saved parts with rendered markdown
	act -n "${#mds} markdown fields "
	


    # check which markdown parser is available in PATH
    command -v markdown > /dev/null
    if [ "$?" = "0" ]; then parser=markdown
    else command -v multimarkdown > /dev/null
    if [ "$?" = "0"]; then parser=multimarkdown; fi
    fi

    for i in $mds; do
       md=`basename $i`
       newtemp="tmp.$RANDOM"
       cat $tmp | awk '
/^'"$md"'/ { system("cat '"$md"' | '"$parser"'"); next }
{ print $0; }' > $newtemp
       rm $tmp; tmp=$newtemp
    done
    }

    cat $tmp

# clean up from temporary files
    rm -f tmp.*


}

render_gallery() {

    render_header

    # close <head> with gallery specific styles
    cat <<EOF >> $dst
    <link rel="stylesheet" href="css/blueimp-gallery.min.css" />

</head> <!-- end of <head> -->

<body>
  <div class="container">
EOF

    # don't forget the navbar
    cat tmpl/navbar.html

    cat <<EOF
<!-- 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

    # put a notice for non-javascript browsers
    cat <<EOF
<noscript>
<p>&nbsp;</p>
<div class="alert">
  <button type="button" class="close" data-dismiss="alert">&times;</button>
  <strong>Javascript missing</strong>.
You either have JavaScript turned off or your browser doesn't support JavaScript.
This website mostly consists of image galleries and needs JavaScript.
</div>
</noscript>
EOF

    # if a file was specified as argument, include it
    { test -r "$1" } && { cat "$1" | render_html }

# generate the gallery's javascript
    dst_js="js/temp-${RANDOM}-gallery.js"

    cat <<EOF > pub/${dst_js}
var slides= [];
EOF

    # parse gallery entries
    pics=`grep -v '^#'`
    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,
	closeOnSwipeUpOrDown: true,
	startSlideshow: true,
	slideshowInterval: 4500,
});
EOF
# closeOnSlideClick: true,

	includejs=(js/jquery.min.js)
	includejs+=(js/bootstrap.min.js)
	includejs+=(js/blueimp-gallery.min.js)


# not really needed
	# includejs+=(js/blueimp-helper.js)
	# includejs+=(js/blueimp-gallery-indicator.js)
	# includejs+=(js/blueimp-gallery-fullscreen.js)
	# includejs+=(js/jquery.blueimp-gallery.min.js)

	includejs+=($dst_js)

	render_footer

}

read_meta() {
    tmp=`head -n 3 | 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 "\";" }
    '`
    eval "$tmp"
}


{ test "$1" = "source" } && { return 0 }


# Main

mkdir -p pub
cat << EOF > pub/.htaccess
DirectoryIndex index index.html index.php
DefaultType text/html
EOF

act -n "Clean up all temp files ... "
temps=(`find pub -type f -name 'temp-*'`)
for t in $temps; do rm -f $t; done
print "done"


# render all HTML views
htmls=(`find views -type f -name '*.html'`)
for src in $htmls; do

    # read meta commands
    cat ${src} | read_meta

    # compute destination file
    dst="pub/`basename ${src%.*}`${EXTENSION}"

    render_header > $dst

    # close <head> as nothing else is needed
    cat <<EOF >> $dst
</head> <!-- end of <head> -->

<body>
  <div class="container">
EOF

    # don't forget the navbar
    cat tmpl/navbar.html >> $dst

    cat <<EOF >> $dst
<p>&nbsp;</p>
<article>
EOF

    # render html
    act -n "Html rendering: $B $dst $r"
    cat $src | render_html >> $dst

cat <<EOF >> $dst
</article>
<p>&nbsp;</p>
<p>&nbsp;</p>

EOF

    includejs=(js/bootstrap.min.js)
    render_footer >> $dst

    act "done"

done

# render all image galleries
gals=(`find views -type f -name '*.gal'`)
for src in $gals; do
    cat ${src} | read_meta
    dst="pub/`basename ${src%.*}`"
    act -n "Gallery rendering: $B $dst $r ... "
    cat $src | render_gallery > $dst
    print "done"
done

for m in `find views -mindepth 1 -type d `; do
    act -n "publishing $B $m $r ... "
    rsync -r $m pub/
    print "done"
done

# if the whole website is a "slideshow" typology then we start with
# a full screen slideshow of all uploaded photos, cycling random every time.
# galleries are supported and can be linked in menu and pages.
{ test "$WEBSITE" = "slideshow" } && {
    notice "Site is configured as slideshow"
    # generate a list of all images (removing duplicates)
    act "Indexing all images ... "
    find pub -iname '*.jpg' | sed -e 's/^pub\///g' -e 's/^.\/pub\///g' -e "s@'@@g" | sort | uniq \
	| render_gallery views/index.html > pub/index
}


notice "Website refreshed."

{ test "$DIR" = "." } || { sleep 10 }