1
0
Fork 0
mirror of https://git.ludikovsky.name/git/fugitive.git synced 2024-05-18 14:35:32 +02:00

conditionnal in template system! \o/

This commit is contained in:
p4bl0 2010-07-24 00:04:32 +02:00
parent 04c828cdf4
commit 864fc488b8
3 changed files with 135 additions and 54 deletions

View file

@ -11,6 +11,18 @@
<body>
<div id="container">
<nav>
<ul class="nav">
<?fugitive ifset:article_previous_file ?>
<li>
<a href="<?fugitive article_previous_file ?>.html">« previous</a>
</li>
<?fugitive endifset:article_previous_file ?>
<?fugitive ifset:article_next_file ?>
<li>
<a href="<?fugitive article_next_file ?>.html">next »</a>
</li>
<?fugitive endifset:article_next_file ?>
</ul>
<ul>
<li><a href="index.html">home</a></li>
<li><a href="archives.html">archives</a></li>
@ -28,13 +40,16 @@
by <?fugitive article_cauthor ?>, on
<time datetime="<?fugitive article_cdatetime ?>">
<?fugitive article_cdate ?> at <?fugitive article_ctime ?>
</time><br />
</time>
<?fugitive ifset:article_mdatetime ?>
<br />
<small>
last update by <?fugitive article_mauthor ?>, on
<time datetime="<?fugitive article_mdatetime ?>">
<?fugitive article_mdate ?> at <?fugitive article_mtime ?>
</time>
</small>
<?fugitive endifset:article_mdatetime ?>
</footer>
<div>
<?fugitive article_content ?>

View file

@ -26,6 +26,7 @@ nav {
text-align: left;
}
nav ul { padding: 0; margin: 0; }
nav ul.nav { float: right; }
nav ul li { display: inline; }
nav ul li:before { content: " — "; }
nav ul li:first-child:before { content: ""; }

View file

@ -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"
}
article_title() {
head -1 "$1"
get_article_previous_file() {
previous=`grep -A1 "$1" "$articles_sorted" | tail -1`
if [ "$previous" != "$1" ]; then
echo "$previous"
fi
}
article_content() {
get_article_next_file() {
next=`grep -B1 "$1" "$articles_sorted" | head -1`
if [ "$next" != "$1" ]; then
echo "$next"
fi
}
get_article_title() {
if [ "$1" != "" ]; then
head -1 "$articles_dir/$1"
fi
}
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/<?fugitive\s\+$1\s*?>/$2/"
process_condition() {
if [ "$2" = "" ]; then
sed "s/<?fugitive\s\+\(end\)\?ifset:$1\s*?>/\n\0\n/g" | \
sed "/<?fugitive\s\+ifset:$1\s*?>/,/<?fugitive\s\+endifset:$1\s*?>/bdel
b
:del
s/<?fugitive\s\+endifset:$1\s*?>.*//
/<?fugitive\s\+endifset:$1\s*?>/b
d"
else
sed "s/<?fugitive\s\+\(end\)\?ifset:$1\s*?>//"
fi
}
replace_str() {
process_condition "$1" "$2" | \
sed "s/<?fugitive\s\+$1\s*?>/$2/"
}
# REMEMBER: 2nd arg should be a tempfile!
replace_var_by_file() {
replace_file() {
sed "/<?fugitive\s\+$1\s*?>/ {
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"