From 92239bf018607ae675925b7da140f7bde9fe7d5d Mon Sep 17 00:00:00 2001 From: Jaromil Date: Sun, 30 Apr 2017 15:03:30 +0200 Subject: [PATCH] new maildir rendering (moved from jaromail) --- maildir | 508 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ render | 78 ++++++++- 2 files changed, 579 insertions(+), 7 deletions(-) create mode 100755 maildir diff --git a/maildir b/maildir new file mode 100755 index 0000000..3cdbf9f --- /dev/null +++ b/maildir @@ -0,0 +1,508 @@ +#!/usr/bin/env zsh +# +# WebNomad, your slick and static website publisher +# (this 'maildir' code is taken from jaromail) +# +# Copyright (C) 2010-2017 Denis Roio +# +# 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. + +# checks if its a maildir +# returns 0 (success) if yes +# no in all other cases +maildircheck() { + fn maildircheck $* + + [[ -r "$1" ]] || { + func "Maildir not existing: $1" + return 1 } + [[ -r "$1/cur" ]] && { + return 0 } # Yes is a maildir + # shortened test to speedup + # && { test -r $1/new } \ + # && { test -r $1/tmp } \ + error "Not a maildir: $1" + return 1 +} + +# short utility to print only mail headers +hdr() { + [[ -r "$1" ]] || { + error "hdr() called on non existing file: $1" + return 1 } + awk '{ print $0 } +/^$/ { exit }' "$1" +} + +# creates the database keeping up-to-date information +# on which emails inside the maildir are already published +maildb_create() { + fn pubdb_create $* + [[ -r "$1" ]] && { + warning "Maildir database already exists: $1" + return 1 } + + cat <${mail_subj} +

From: ${mail_from} - ${mail_date}

+ +EOF + + # check if it has already html + _html=`mu extract "$_path" | awk '/text\/html/ {print $1; exit}'` + [[ "$_html" = "" ]] || { + + mu extract --overwrite --parts="$_html" "$_path" + # check if there is an html header to weed out + grep '' "$_html".part > /dev/null + if [ $? = 0 ]; then + awk ' +BEGIN { body=0 } +/&|g' + + # here we tweak the origin to avoid headers in markdown + # preferring to interpret # as inline preformat + + # cat "$_text".part | sed ' + # s/^#/ /g + # ' | iconv -c | maruku --html-frag 2>/dev/null | sed ' + # s|http://[^ |<]*|&|g + # s|https://[^ |<]*|&|g + # ' + + # check if its an html only mail + # _html=`mu extract "$_path" | awk '/text\/html/ {print $1; exit}'` + # { test "$_html" = "" } || { + # mu extract --overwrite --parts="$_html" "$_path" + # elinks -dump "$_html".part + # rm "$_html".part + # return 0 } + +} + +mail_index_render() { + fn mail_index_render $* + _db="${mdsrc}.db" + req=(mdsrc mdname) + freq=($mdsrc $_db) + ckreq || return 1 + + cat < "views/${mdname}.html" + + + + + +EOF + + for m in ${(f)"$(maildb_list $_db)"}; do + mail_fill "$m" + + cat <> "views/${mdname}.html" + + + + + +EOF + done +} + +mail_atom_render() { + fn mail_atom_render $* + _db="${mdsrc}.db" + req=(mdsrc mdname) + freq=($mdsrc $_db) + ckreq || return 1 + + cat < "views/${mdname}.atom.xml" + + + +${TITLE} + + + +${WEB_ROOT}/${mdname}.atom.xml + +`date --rfc-3339=seconds | sed 's/ /T/'` +WebNomad + +${DESCRIPTION} +http://dyne.org/dyne.png + +EOF + + # write out the atom entry + for m in ${(f)"$(maildb_list $_db)"}; do + mail_fill "$m" + eurl="${WEB_ROOT}/${mdname}.msg/${mail_id}.html" + cat <> "views/${mdname}.atom.xml" + + ${mail_subj} + + ${eurl} + ${mail_insert_date} + +${mail_subj} + + + ${mail_from} + ${eurl} + + + ${mail_subj} + From: ${mail_from} + ${mail_insert_date} + + ${eurl} + + + +EOF + done + + cat <> "views/${mdname}.atom.xml" + +EOF +} + + # iterate through the pubdb and publish an rss +pubdb_render_maildir() { + func "publish_render_maildir() $@" + md="$1" + [[ "$md" = "" ]] && { + error "Publish_render_maildir: not found: $md" + return 1 + } + maildircheck "${md}" || { + error "Publish_render_maildir: not a maildir: $md" + return 1 + } + [[ -r "${md}/pub" ]] || { + error "Publish_render_maildir: webnomad not found in ${md}" + error "Initialize Webnomad inside the maildir that you want published." + return 1 + } + + views="${md}/views" + pubdb="${md}/.pubdb" + [[ -r "$pubdb" ]] || { + error "Publish_render_maildir: first run update_pubdb for $md"; return 1 } + + # ${=mkdir} $TMPPREFIX/pubdb + + mails=`maildb_list $md | head -n ${FEED_LIMIT:=30}` + + # source webnomad's configurations + [[ -r "${md}/config.zsh" ]] && { source "${md}/config.zsh" } + + cat < ${md}/views/atom.xml + + + +${TITLE} + + + +${WEB_ROOT}/atom.xml + +`date --rfc-3339=seconds | sed 's/ /T/'` +JaroMail + +${DESCRIPTION} +http://dyne.org/dyne.png + +EOF + + + cat < "${md}/views/index.html" +
+ ${mail_from} +${mail_subj} +
+ + + + + +EOF + + # main loop + c=0 + + for m in ${(f)mails}; do + [[ "$m[1]" = "." ]] && { _base=`pwd` } + # fill in uid and upath + pubdb_getuid "$_base/$m" + + # but skip entries no more existing in maildir + [[ -r "$m" ]] || { continue } + # TODO: remove $m from database if not in maildir + + _from=`hdr "$m" | ${WORKDIR}/bin/fetchaddr -x From -a` + + _fname=`print ${(Q)_from[(ws:,:)2]}` # | iconv -c` + + func "From: ${_fname}" + _subject=`hdr "$m" | awk ' +/^Subject:/ { for(i=2;i<=NF;i++) printf "%s ", $i; printf "\n" } +' | escape_html` + + # fill in uid and upath + pubdb_getuid "$m" + + # fill in the body + _body=`pubdb_extract_body $_base/$m` + + [[ "$_body" = "" ]] && { error "Error rendering $m" } + + (( ++c )) + if (( $c < ${FEED_LIMIT:=30} )); then + + # write out the atom entry + cat <> ${md}/views/atom.xml + + + $_subject + + ${WEB_ROOT}/${upath} + `pubdb_date "$m"` + +`print ${(f)_body} | escape_html` + + + ${_fname} + ${WEB_ROOT}/${upath} + + + ${_subject} + From: ${_fname} + ${_daterss} + + ${WEB_ROOT}/${upath} + + + +EOF + + fi # FEED LIMIT not reached + + ####### + # now build an index and the sitemap + + + # if using webnomad write out also the message page + [[ -d "${md}/views" ]] && { + act "${_datestring} - ${_subject}" + _datestring=`hdr "$_path" | awk '/^Date/ { print $0 }'` + cat < "${md}/views/${upath}" +

${_subject}

+

From: ${_fname} - ${_datestring}

+ +${_body} + +EOF + # add entry in index + cat <> "${md}/views/index.html" + +
+ + + + +EOF + } + done # loop is over + + cat <> "${md}/views/atom.xml" + +EOF + + cat <> "${md}/views/index.html" +
${_fname} +${_subject} +
+EOF + + notice "Archive website rendering completed" + } diff --git a/render b/render index aa88ece..1fbd92c 100755 --- a/render +++ b/render @@ -24,6 +24,7 @@ SYS="`pwd`/webnomad" +helpers=1 source $SYS/zuper vars+=(SYS DIR CMD dst) @@ -32,10 +33,13 @@ vars+=(TITLE DESCRIPTION KEYWORDS TYPE IMAGE EXTENSION) vars+=(WEB_ROOT FILES_ROOT THUMB_SIZE BOOTSTRAP destination) vars+=(FLOWTYPE FONT_RATIO EXIF_CLEAN EXIF_COMMENT EXIF_ROTATE) vars+=(total_fonts cssfound) - arrs+=(includecss includejs fonts) arrs+=(custom_fonts) +# used by maildir +vars+=(mail_id mail_render mail_date mail_insert_date mail_from mail_subj) +vars+=(mdsrc mdname) + source $SYS/zuper.init @@ -345,6 +349,11 @@ render_html() { } read_meta() { + # read metadata on each file which can change global settings + # format: + # # title put anything here + # # description put your description here + # # keywords list of keywords here tmp=`head -n 3 | awk ' !/^#/ { next } /title/ { printf "title=\""; for(i=3;i<=NF;i++) printf "%s ", $i; printf "\";" } @@ -405,7 +414,7 @@ mkdir -p "$destination/css" mkdir -p "$destination/js" mkdir -p "$destination/img" -[[ $BOOTSTRAP = 0 ]] || { +[[ "$BOOTSTRAP" = "" ]] || { #{ test -r "$destination"/css/bootstrap.css } || { cp "$SYS"/css/bootstrap.css "$destination"/css/ cp "$SYS"/img/* "$destination"/img/ @@ -501,6 +510,62 @@ EOF } + + +# render all maildirs +maildirs=(`find views -type f -name '*.maildir'`) +maildirs+=(`find views -type f -name '*.maildirs'`) +[[ "${#maildirs}" > 0 ]] && { + source $SYS/maildir + for mdsrc in $maildirs; do + act "Maildirs list: $mdsrc" + + # base directory where to render + mdname=${"$(basename $mdsrc)"[(ws:.:)1]} + act "rendering in views/${mdname}.msg" + mkdir -p views/${mdname}.msg + + cat ${mdsrc} | read_meta + for md in ${(f)"$(cat $mdsrc)"}; do + act "Maildir rendering: $md" + maildircheck "$md" || continue + mdb="${mdsrc}.db" + + [[ -r "$mdb" ]] || maildb_create "$mdb" + # iterate through emails in maildir and insert new ones + mails=`find "${md}/new" "${md}/cur" "${md}/tmp" -type f` + act "`print ${mails} | wc -l` total emails found" + for m in ${(f)mails}; do + mail_id=${"$(sha256sum $m)"[(w)1]} + u=`maildb_lookup_uid "$mdb" "$mail_id"` + [[ "$u" = "" ]] && { + mail_getinfo "$m" + # $u is a new message + act "new message found: $mail_subj" + maildb_insert "$mdb" "$m" + } + done + + # iterate through database and render emails missing + for m in ${(f)"$(maildb_list $mdb)"}; do + mail_fill "$m" + [[ -r pub/${mdname}.msg/${mail_id}.html ]] && { + func "skip already rendered: $mail_id" + continue } + mail_file_render \ + "$mail_file" \ + > "views/${mdname}.msg/${mail_id}.html" + done + done + + # render indexes + mail_index_render + + mail_atom_render + done +} + + # render all HTML views htmls=(`find views -type f -name '*.html'`) for src in $htmls; do @@ -516,10 +581,10 @@ done # render all image galleries -source $SYS/gallery gals=(`find views -type f -name '*.gal'`) gals+=(`find views -type f -name '*.gallery'`) -if [[ "${#gals}" > 0 ]]; then +[[ "${#gals}" > 0 ]] && { + source $SYS/gallery cp "$SYS"/js/*blueimp* "$destination"/js/ cp "$SYS"/css/*blueimp* "$destination"/css/ @@ -532,10 +597,9 @@ if [[ "${#gals}" > 0 ]]; then cat $src | render_gallery > $dst [[ $QUIET = 1 ]] || print "done" done +} -fi - -# render all directory indexes +# render all directory indexes (TODO: properly fix recursion) idxs=(`find views -type f -name '*.idx'`) idxs+=(`find views -type f -name '*.index'`) { test ${#idxs} = 0 } || {