From 864fc488b842afafb7c0a0bafcc5e63bb6228c77 Mon Sep 17 00:00:00 2001 From: p4bl0 Date: Sat, 24 Jul 2010 00:04:32 +0200 Subject: conditionnal in template system! \o/ --- post-commit.sh | 171 +++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 118 insertions(+), 53 deletions(-) (limited to 'post-commit.sh') diff --git a/post-commit.sh b/post-commit.sh index c7daa86..565a82a 100644 --- a/post-commit.sh +++ b/post-commit.sh @@ -12,9 +12,6 @@ modified_files=`git log -1 --name-status --pretty="format:" | grep -E '^M' | \ deleted_files=`git log -1 --name-status --pretty="format:" | grep -E '^D' | \ cut -f2` -last_published_article=`git log --name-status --pretty="format:" | \ - grep -E '^A' | cut -f2 | grep -E '^$articles_dir' | head -1` - sanit_mail() { sed "s/@/[at]/;s/\./(dot)/" } @@ -29,88 +26,156 @@ commit_time=`git log -1 --format="%ai" | cut -d' ' -f2` commit_timestamp=`git log -1 --format="%at"` commit_subject=`git log -1 --format="%s"` commit_slug=`git log -1 --format="%f"` -commit_body() { +get_commit_body() { tmp=`tempfile -p "fugitive"` git log -1 --format="%b" > "$tmp" echo "$tmp" } -article_info() { - git log --format="$1" -- "$2" +articles_sorted=`tempfile -p "fugitive"` +for f in $articles_dir/*; do + ts=`git log --format="%at" -- "$f" | tail -1` + if [ "$ts" != "" ]; then + echo "$ts ${f#$articles_dir/}" + fi +done | sort -nr | cut -d' ' -f2 > "$articles_sorted" + +get_article_info() { + git log --format="$1" -- "$articles_dir/$2" +} +get_article_previous_file() { + previous=`grep -A1 "$1" "$articles_sorted" | tail -1` + if [ "$previous" != "$1" ]; then + echo "$previous" + fi +} +get_article_next_file() { + next=`grep -B1 "$1" "$articles_sorted" | head -1` + if [ "$next" != "$1" ]; then + echo "$next" + fi } -article_title() { - head -1 "$1" +get_article_title() { + if [ "$1" != "" ]; then + head -1 "$articles_dir/$1" + fi } -article_content() { +get_article_content() { tmp=`tempfile -p "fugitive"` - tail -n+2 "$1" > "$tmp" + tail -n+2 "$articles_dir/$1" > "$tmp" echo "$tmp" } -replace_var_by_string() { - sed "s//$2/" +process_condition() { + if [ "$2" = "" ]; then + sed "s//\n\0\n/g" | \ + sed "//,//bdel + b + :del + s/.*// + //b + d" + else + sed "s///" + fi +} + +replace_str() { + process_condition "$1" "$2" | \ + sed "s//$2/" } # REMEMBER: 2nd arg should be a tempfile! -replace_var_by_file() { +replace_file() { sed "// { r $2 d }" rm "$2" } replace_commit_info() { - replace_var_by_string "commit_Hash" "$commit_Hash" | \ - replace_var_by_string "commit_hash" "$commit_hash" | \ - replace_var_by_string "commit_author" "$commit_author" | \ - replace_var_by_string "commit_author_email" "$commit_author_email" | \ - replace_var_by_string "commit_datetime" "$commit_datetime" | \ - replace_var_by_string "commit_date" "$commit_date" | \ - replace_var_by_string "commit_time" "$commit_time" | \ - replace_var_by_string "commit_timestamp" "$commit_timestamp" | \ - replace_var_by_string "commit_subject" "$commit_subject" | \ - replace_var_by_string "commit_slug" "$commit_slug" | \ - replace_var_by_file "commit_body" "`commit_body`" + commit_body=`get_commit_body` + replace_str "commit_Hash" "$commit_Hash" | \ + replace_str "commit_hash" "$commit_hash" | \ + replace_str "commit_author" "$commit_author" | \ + replace_str "commit_author_email" "$commit_author_email" | \ + replace_str "commit_datetime" "$commit_datetime" | \ + replace_str "commit_date" "$commit_date" | \ + replace_str "commit_time" "$commit_time" | \ + replace_str "commit_timestamp" "$commit_timestamp" | \ + replace_str "commit_subject" "$commit_subject" | \ + replace_str "commit_slug" "$commit_slug" | \ + replace_file "commit_body" "$commit_body" } replace_article_info() { - cdt=`article_info "%ai" "$1" | tail -1` - mdt=`article_info "%ai" "$1" | head -1` - replace_var_by_string "article_title" "`article_title \"$1\"`" | \ - replace_var_by_string "article_cdatetime" "$cdt" | \ - replace_var_by_string "article_cdate" "`echo $cdt | cut -d' ' -f1`" | \ - replace_var_by_string "article_ctime" "`echo $cdt | cut -d' ' -f2`" | \ - replace_var_by_string "article_ctimestamp" \ - "`article_info \"%at\" \"$1\" | tail -1`" | \ - replace_var_by_string "article_mdatetime" "$mdt" | \ - replace_var_by_string "article_mdate" "`echo $mdt | cut -d' ' -f1`" | \ - replace_var_by_string "article_mtime" "`echo $mdt | cut -d' ' -f2`" | \ - replace_var_by_string "article_mtimestamp" \ - "`article_info \"%at\" \"$1\" | head -1`" | \ - replace_var_by_string "article_cauthor" \ - "`article_info \"%an\" \"$1\" | tail -1`" | \ - replace_var_by_string "article_cauthor_email" \ - "`article_info \"%ae\" \"$1\" | tail -1 | sanit_mail`" | \ - replace_var_by_string "article_mauthor" \ - "`article_info \"%an\" \"$1\" | head -1`" | \ - replace_var_by_string "article_mauthor_email" \ - "`article_info \"%ae\" \"$1\" | head -1 | sanit_mail`" | \ - replace_var_by_string "article_url" "$public_dir/${1#$articles_dir/}.html" + article_title=`get_article_title "$1"` + article_cdatetime=`get_article_info "%ai" "$1" | tail -1` + article_cdate=`echo "$article_cdatetime" | cut -d' ' -f1` + article_ctime=`echo "$article_cdatetime" | cut -d' ' -f2` + article_ctimestamp=`get_article_info "%at" "$1" | tail -1` + u=`get_article_info "%ai" "$1" | wc -l` + article_mdatetime=`if test "$u" -gt 1; then get_article_info "%ai" "$1" | \ + head -1; fi` + article_mdate=`echo "$article_mdatetime" | cut -d' ' -f1` + article_mtime=`echo "$article_mdatetime" | cut -d' ' -f2` + article_mtimestamp=`if test "$u" -gt 1; then get_article_info "%at" \ + "$1" | head -1; fi` + article_cauthor=`get_article_info "%an" "$1" | tail -1` + article_cauthor_email=`get_article_info "%ae" "$1" | tail -1 | sanit_mail` + article_mauthor=`get_article_info "%an" "$1" | head -1` + article_mauthor_email=`get_article_info "%ae" "$1" | head -1 | sanit_mail` + article_previous_file=`get_article_previous_file "$1"` + article_previous_title=`get_article_title "$article_previous_file"` + article_next_file=`get_article_next_file "$1"` + article_next_title=`get_article_title "$article_next_file"` + + replace_str "article_title" "$article_title" | \ + replace_str "article_cdatetime" "$article_cdatetime" | \ + replace_str "article_cdate" "$article_cdate" | \ + replace_str "article_ctime" "$article_ctime" | \ + replace_str "article_ctimestamp" "$article_ctimestamp" | \ + replace_str "article_mdatetime" "$article_mdatetime" | \ + replace_str "article_mdate" "$article_mdate" | \ + replace_str "article_mtime" "$article_mtime" | \ + replace_str "article_mtimestamp" "$article_mtimestamp" | \ + replace_str "article_cauthor" "$article_cauthor" | \ + replace_str "article_cauthor_email" "$article_cauthor_email" | \ + replace_str "article_mauthor" "$article_mauthor" | \ + replace_str "article_mauthor_email" "$article_mauthor_email" | \ + replace_str "article_previous_file" "$article_previous_file" | \ + replace_str "article_previous_title" "$article_previous_title" | \ + replace_str "article_next_file" "$article_next_file" | \ + replace_str "article_next_title" "$article_next_title" +} + +_echo() { + echo -ne "[fugitive] "$* } for f in $deleted_files; do if [ "$f" != "${f#$articles_dir}" ]; then - echo -n "Deleting $public_dir/${f#$articles_dir/}.html... " - rm $public_dir/${f#$articles_dir/}.html + art="${f#$articles_dir/}" + _echo "Deleting $public_dir/$art.html... " + rm "$public_dir/$art.html" echo "done." fi done for f in $added_files $modified_files; do if [ "$f" != "${f#$articles_dir}" ]; then - echo -n "Generating $public_dir/${f#$articles_dir/}.html from $f... " - cat $templates_dir/article.html | \ - replace_var_by_file "article_content" "`article_content \"$f\"`" | \ + art="${f#$articles_dir/}" + _echo "Generating $public_dir/$art.html from $f... " + cat "$templates_dir/article.html" | \ + replace_file "article_content" "`get_article_content \"$art\"`" | \ + process_includes | \ replace_commit_info | \ - replace_article_info "$f" | \ - cat > $public_dir/${f#$articles_dir/}.html + replace_article_info "$art" | \ + cat > "$public_dir/$art.html" echo "done." fi done + +_echo "Using last published article as index page" +cp "$public_dir/`cat $articles_sorted | head -1` $public_dir/index.html" +echo "done". + +rm "$articles_sorted" +_echo "Blog update complete.\n" -- cgit v1.2.3