several fixes and stable indexing

This commit is contained in:
Jaromil 2014-05-27 10:15:54 +02:00
parent 1287fa4d81
commit f73822ebe9
4 changed files with 217 additions and 89 deletions

2
fonts
View File

@ -9,7 +9,7 @@ total_fonts=${#fonts}
{ test -d fonts } && {
notice "Indexing custom fonts"
rm -f ${destination}/css/custom.fonts.css
mkdir -p ${destination}/fonts
mkdir -p ${destination}/css
ttf=`find -L fonts -iname '*.ttf'`
for f in ${(f)ttf}; do
ffile=`basename "$f"`

135
index
View File

@ -40,8 +40,8 @@ filetype_icon() {
{ test "$1" = "" } && {
error "filetype_icon called without argument"; return 1 }
path="$1"
filename="${path##*/}"
fpath="$1"
filename="${fpath##*/}"
ext="${filename##*.}"
name="${filename%%.*}"
@ -50,7 +50,7 @@ filetype_icon() {
# analize extensions, will be overridden by name
case $ext:l in
txt) res=text-plain.png ;;
html) res=text-x-html.png ;;
html) res=text-html.png ;;
xml) res=text-xml.png ;;
md5|sha*) res=hash.png ;;
asc|gpg) res=signature.png ;;
@ -79,27 +79,125 @@ filetype_icon() {
print "$res"
}
index_dir() {
{ test -d "$1" } || { error "cannot index: not a directory '$1'"; return 1 }
index_short() {
func "index_long_preview \"$1\" \"$2\" \"$3\""
dir="${1}${3}"
{ test -d "$dir" } || { error "cannot index: not a directory '$dir'"; return 1 }
files=()
ttmp=`ls -l --time-style=long-iso "$1" | awk '
ttmp=`ls "$dir" | awk '
/^total/ { next }
/^$/ { next }
{ printf "files+=(\"%s;%s;%s\")", $8, $5, $6 }
{ printf "files+=(\"$1\");" }
'`
{ test $? = 0 } || {
error "Error parsing directory: $1"
error "Error parsing directory: $dir"
return 1 }
eval "$ttmp"
act "${#files} files parsed in $dir"
diralias="$2"
dirbase="$3"
# setup paths for test
if [ "$CMD" = "test" ]; then
LINK_PREFIX="file://${dir}"
else
LINK_PREFIX="${diralias}${dirbase}"
fi
func "LINK_PREFIX = $LINK_PREFIX"
cat <<EOF
<table class="table table-hover table-condensed">
<thead><tr>
<th class="col-sm-1 col-md-1 col-lg-1"><!-- filetype icon --></th>
<th class="col-sm-3 col-md-4 col-lg-4"><!-- file name --></th>
</tr></thead>
EOF
for f in ${files}; do
name="${f}"
ext="${name##*.}" # file extension
file="${1}/${name}" # file path
typefield=""
namefield=""
previewfield=""
link=""
icon=""
icon_width="width=\"50px\""
if [ -L "$file" ]; then # is a symlink
if [ -d "$file" ]; then # symlink to folder
link="<a href=\"${name}/index${EXTENSION}\">"
typefield="${link}<img src=\"${WEB_ROOT}/icons/symlink.png\" alt=\"symlink\" ${icon_width}></a>"
namefield="${link}${name}</a>"
else # symlink to file
link="<a href=\"${LINK_PREFIX}/${name}\">"
typefield="${link}<img src=\"${WEB_ROOT}/icons/symlink.png\" alt=\"symlink\" ${icon_width}></a>"
namefield="${link}${name}</a>"
fi
elif [ -d "$file" ]; then # is a folder
link="<a href=\"${name}/index${EXTENSION}\">"
typefield="${link}<img src=\"${WEB_ROOT}/icons/folder.png\" alt=\"folder\" ${icon_width}></a>"
namefield="${link}${name}</a>"
else # is a file
{ test "$icon" = "" } && { icon="`filetype_icon ${file}`" }
link="<a href=\"${LINK_PREFIX}/${name}\">"
typefield="${link}<img src=\"${WEB_ROOT}/icons/${icon}\" alt=\"${icon}\" ${icon_width}></a>"
namefield="${link}${name}</a>"
fi
# render it all
cat <<EOF
<tr>
<td style="vertical-align:middle;">${typefield}</td>
<td style="vertical-align:middle;font-size:1.5em;word-wrap:break-word">${namefield}</td>
</tr>
EOF
tpwd="`pwd`"
{ test "$icon" = "" } && { continue }
popd
# copy the icon file
{ test -r ${destination}/icons/${icon} } || {
func "copy icon in place: $icon (PWD: `pwd`)"
cp $SYS/icons/$THUMB_SIZE/${icon} \
${destination}/icons/${icon}
}
pushd "$tpwd"
done
print "</table>"
}
index_long_preview() {
func "index_long_preview \"$1\" \"$2\" \"$3\""
dir="${1}${3}"
{ test -d "$dir" } || { error "cannot index: not a directory '$dir'"; return 1 }
files=()
ttmp=`ls -l --time-style=long-iso "$dir" | awk '
/^total/ { next }
/^$/ { next }
{ printf "files+=(\"%s;%s;%s\");", $8, $5, $6 }
'`
{ test $? = 0 } || {
error "Error parsing directory: $dir"
return 1 }
eval "$ttmp"
act "${#files} files parsed in $1"
act "${#files} files parsed in $dir"
diralias="$2"
dirbase="$3"
# setup paths for test
{ test "$destination" = "test" } && {
LINK_PREFIX="file://$1"
act "LINK_PREFIX = $LINK_PREFIX"
}
if [ "$CMD" = "test" ]; then
LINK_PREFIX="file://${dir}"
else
LINK_PREFIX="${diralias}${dirbase}"
fi
func "LINK_PREFIX = $LINK_PREFIX"
# human size dividers
_mb=$((1024 * 1024))
@ -132,7 +230,7 @@ EOF
# format date to human readable form
hdate=`date -d "$date" +'%d %b %Y'`
func "$name \t $size \t $date"
typefield=""
@ -141,8 +239,6 @@ EOF
link=""
icon=""
nametruncate="${(l:20:: :)${name[1,20]}}"
preview=`preview_file "$file"`
{ test "$preview" = "" } || {
@ -160,7 +256,7 @@ EOF
if [ -L "$file" ]; then # is a symlink
if [ -d "$file" ]; then # symlink to folder
link="<a href=\"${name}/index.html\">"
link="<a href=\"${name}/index${EXTENSION}\">"
typefield="${link}<img src=\"${WEB_ROOT}/icons/symlink.png\" alt=\"symlink\" ${icon_width}></a>"
namefield="${link}${name}</a>"
previewfield="<!-- folder -->"
@ -171,7 +267,7 @@ EOF
previewfield="${link}${preview}</a>"
fi
elif [ -d "$file" ]; then # is a folder
link="<a href=\"${name}/index.html\">"
link="<a href=\"${name}/index${EXTENSION}\">"
typefield="${link}<img src=\"${WEB_ROOT}/icons/folder.png\" alt=\"folder\" ${icon_width}></a>"
namefield="${link}${name}</a>"
previewfield="<!-- folder -->"
@ -195,6 +291,7 @@ EOF
EOF
tpwd="`pwd`"
{ test "$icon" = "" } && { continue }
popd
# copy the icon file
{ test -r ${destination}/icons/${icon} } || {

164
render
View File

@ -22,9 +22,13 @@ VERSION=0.5
QUIET=0
SYS=`dirname $0`
# full path to webnomad's system
SYS="`pwd`/webnomad"
source $SYS/utils
# fill path to the source website root
DIR="`pwd`"
CMD="$1"
{ test -r config.zsh } || {
@ -67,6 +71,18 @@ unsetopt CASE_GLOB
notice "Rendering your website"
act "Title: $B $TITLE $r"
# setup paths for test
{ test "$CMD" = "test" } && {
LINK_PREFIX="file://`pwd`"
WEB_ROOT="file://`PWD=${SYS} pwd`/test"
baseurl="$WEB_ROOT"
destination="$DIR/test"
notice "Test settings for indexing"
act "SYS = $SYS"
act "WEB_ROOT = $WEB_ROOT"
}
render_file() { sed -e "s@\${baseurl}@${baseurl}@g" $@ }
render_header() {
cat <<EOF
@ -90,10 +106,9 @@ render_header() {
<!-- Bootstrap -->
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link href="${baseurl}css/bootstrap.min.css" rel="stylesheet" />
<link href="${baseurl}css/bootstrap-responsive.min.css" rel="stylesheet" />
EOF
{ test -r views/css/custom.css } && {
{ test -r "$DIR"/views/css/custom.css } && {
cat <<EOF
<link href="${baseurl}css/custom.css" rel="stylesheet" />
EOF
@ -104,10 +119,10 @@ EOF
cat <<EOF
<link href="${baseurl}css/${c}" rel="stylesheet" />
EOF
cp $SYS/css/${c} ${destination}/css/
cp "$SYS"/css/${c} "${destination}"/css/
done
{ test -f ${destination}/css/custom.fonts.css } && {
{ test -f "${destination}"/css/custom.fonts.css } && {
cat <<EOF
<link href="${baseurl}css/custom.fonts.css" rel="stylesheet" />
EOF
@ -117,12 +132,12 @@ EOF
{ test "$1" = "" } || { print "${@}"; print }
# add the user configured header
cat tmpl/header.html
render_file "$DIR"/tmpl/header.html
}
render_footer() {
cat tmpl/footer.html
render_file "$DIR"/tmpl/footer.html
cat <<EOF
</div><!--/.container-->
@ -136,8 +151,8 @@ EOF
cat <<EOF
<script type="text/javascript" src="${baseurl}/js/$i"></script>
EOF
{ test -r $SYS/js/$i } && {
cp $SYS/js/$i $destination/js }
{ test -r "$SYS"/js/$i } && {
cp "$SYS"/js/$i "$destination"/js }
done
# if test mode then render the test footer
@ -152,40 +167,45 @@ EOF
render_html() {
#######################################
## we support the <markdown> tag inline
#######################################
## we support the <markdown> tag inline
# parses the html and put all stuff contained in <markdown /> tags
# inside separate files, leaving a trace of them into the main html
# (a line starting with tmp.md$RAND)
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
# 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
# second pass substituted saved parts with rendered markdown
act -n "${#mds} markdown fields "
# check which markdown parser is available in PATH
command -v maruku > /dev/null
if [ "$?" = "0" ]; then parser="maruku --html-frag"
else command -v markdown > /dev/null;
if [ "$?" = "0" ]; then parser=markdown
else command -v multimarkdown > /dev/null
if [ "$?" = "0"]; then parser=multimarkdown
fi
fi
fi
# 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 '
# parses all html and renders each markdown in the html
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
rm $tmp; tmp=$newtemp
done
}
cat $tmp
@ -196,23 +216,22 @@ render_html() {
}
index() {
# should be called from inside the destination directory
recursive_index() {
# render_header "<link rel=\"stylesheet\" href=\"css/blueimp-gallery.min.css\" />"
{ test -d "$1" } || { error "cannot index directory not found: $1"; return 1 }
source $SYS/index
archive="$1"
dirs=`find "$archive" -type d`
basedir="/`basename "$archive"`/"
dirs=`find "$1" -type d | grep -v .git`
basedir=`dirname "$1"`
diralias="$2"
# setup paths for test
{ test "$destination" = "test" } && {
LINK_PREFIX="file://$1"
WEB_ROOT="file://`PWD=${SYS} pwd`/test"
notice "Test settings for indexing"
act "SYS = $SYS"
act "WEB_ROOT = $WEB_ROOT"
}
dest="`pwd`"
func "index archive $archive"
func "index basedir: $basedir"
func "index aliasdir: $diralias"
# copy default icons
mkdir -p $destination/icons
cp $SYS/icons/$THUMB_SIZE/image-x-generic.png $destination/icons
@ -220,34 +239,38 @@ index() {
cp $SYS/icons/$THUMB_SIZE/folder.png $destination/icons
for d in ${(f)dirs}; do
dir="${d##*${basedir}}"
# skip some subdirs
dir="${d##*${basedir}}"
func "actual file path: $d"
func "relative path: $dir"
func "destination: ${dest}/${dir}"
mkdir -p "${destination}/$dir"
render_header > "${destination}/$dir/index.html"
cat tmpl/navbar.html >> "${destination}/$dir/index.html"
pushd "${destination}/$dir"
cat <<EOF >> index.html
<div class="container-fluid">
<div class="span6">
mkdir -p "${dest}/${dir}"
pushd "${dest}/${dir}"
render_header > index${EXTENSION}
render_file "$DIR"/tmpl/navbar.html >> index${EXTENSION}
cat <<EOF >> index${EXTENSION}
<div class="container">
<article>
EOF
index_dir "$d" >> index.html
# if there is a README put it besides
{ test -r "${d}/README.md" } && {
cat <<EOF >> index.html
</div>
<div class="span6">
EOF
maruku --html --html-frag -o - "${d}/README.md" >> index.html
}
cat <<EOF >> index.html
</div>
# takes 3 arguments: base dir, alias dir and indexed directory
# we must check if its the parent directory
index_long_preview "${archive}" "${diralias}" "${dir}" >> index${EXTENSION}
# if [ "$dir" = "" ]; then
# index_dir "${d}" "${diralias}" "" >> index${EXTENSION}
# else
# index_dir "${d%%${dir}*}" "${diralias}" "${dir}" >> index${EXTENSION}
# fi
cat <<EOF >> index${EXTENSION}
</article>
</div>
<p>&nbsp;</p>
EOF
render_footer >> index${EXTENSION}
popd
render_footer >> "${destination}/$dir/index.html"
done
}
@ -315,7 +338,7 @@ for src in $htmls; do
EOF
# don't forget the navbar
cat tmpl/navbar.html >> $dst
render_file "$DIR"/tmpl/navbar.html >> $dst
cat <<EOF >> $dst
<p>&nbsp;</p>
@ -357,9 +380,17 @@ done
idxs=(`find views -type f -name '*.idx'`)
idxs+=(`find views -type f -name '*.index'`)
{ test ${#idxs} = 0 } || {
source "$SYS/index"
for idx in $idxs; do
notice "Directory index rendering: $idx"
source ${idx}
dst=`basename ${idx%%.*}`
notice "Directory index rendering to: $dst"
dirs=`cat ${idx}`
for d in ${(f)dirs}; do
mkdir -p "${destination}/${dst}"
pushd "${destination}/${dst}"
recursive_index "${d[(ws: :)1]}" "${d[(ws: :)2]}"
popd
done
done
# copy icons only if needed
rsync -rlt "$SYS/icons" "${destination}/"
@ -386,4 +417,3 @@ done
notice "Website refreshed."
# { test "$DIR" = "." } || { sleep 10 }

5
test
View File

@ -1,15 +1,16 @@
#!/usr/bin/env zsh
STYLING=0
# make it work to browse results on file://
baseurl="file://`pwd`/test/"
destination="test"
includejs+=(jquery.min.js jquery.sidr.js)
includecss+=(jquery.sidr.dark.css)
{ test "$STYLING" = "1" } || { render_test_footer() { return 0 }; return 0 }
# Font size selector
font-size_select() {