Merge pull request #87 from McDutchie/master

Some more optimizations
This commit is contained in:
Carlos Fenollosa 2016-02-08 10:16:38 +01:00
commit 93717bc9b8

74
bb.sh
View file

@ -161,23 +161,8 @@ global_variables_check() {
# Test if the markdown script is working correctly # Test if the markdown script is working correctly
test_markdown() { test_markdown() {
[[ -z $markdown_bin ]] && return 1 [[ -n $markdown_bin ]] &&
[[ -z $(which diff) ]] && return 1 [[ $("$markdown_bin" <<< $'line 1\n\nline 2') == $'<p>line 1</p>\n\n<p>line 2</p>' ]]
in=/tmp/md-in-${RANDOM}.md
out=/tmp/md-out-${RANDOM}.html
good=/tmp/md-good-${RANDOM}.html
echo -e "line 1\n\nline 2" > "$in"
echo -e "<p>line 1</p>\n\n<p>line 2</p>" > "$good"
"$markdown_bin" "$in" > "$out" 2> /dev/null
diff $good $out &> /dev/null # output is irrelevant, we'll check $?
if (($? != 0)); then
rm -f "$in" "$good" "$out"
return 1
fi
rm -f "$in" "$good" "$out"
return 0
} }
@ -220,7 +205,7 @@ disqus_body() {
echo '<div id="disqus_thread"></div> echo '<div id="disqus_thread"></div>
<script type="text/javascript"> <script type="text/javascript">
/* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */ /* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
var disqus_shortname = '\'$global_disqus_username\''; // required: replace example with your forum shortname var disqus_shortname = '"'$global_disqus_username'"'; // required: replace example with your forum shortname
/* * * DONT EDIT BELOW THIS LINE * * */ /* * * DONT EDIT BELOW THIS LINE * * */
(function() { (function() {
@ -238,7 +223,7 @@ disqus_footer() {
[[ -z $global_disqus_username ]] && return [[ -z $global_disqus_username ]] && return
echo '<script type="text/javascript"> echo '<script type="text/javascript">
/* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */ /* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
var disqus_shortname = '\'$global_disqus_username\''; // required: replace example with your forum shortname var disqus_shortname = '"'$global_disqus_username'"'; // required: replace example with your forum shortname
/* * * DONT EDIT BELOW THIS LINE * * */ /* * * DONT EDIT BELOW THIS LINE * * */
(function () { (function () {
@ -388,7 +373,7 @@ twitter() {
# Return 0 (bash return value 'true') if the input file is an index, feed, etc # Return 0 (bash return value 'true') if the input file is an index, feed, etc
# or 1 (bash return value 'false') if it is a blogpost # or 1 (bash return value 'false') if it is a blogpost
is_boilerplate_file() { is_boilerplate_file() {
name=$(clean_filename "$1") name=${1#./}
case $name in case $name in
( "$index_file" | "$archive_index" | "$tags_index" | "$footer_file" | "$header_file" | "$global_analytics_file" | "$prefix_tags"* ) ( "$index_file" | "$archive_index" | "$tags_index" | "$footer_file" | "$header_file" | "$global_analytics_file" | "$prefix_tags"* )
return 0 ;; return 0 ;;
@ -400,14 +385,6 @@ is_boilerplate_file() {
esac esac
} }
# Filenames sometimes have leading './' or other oddities which need to be cleaned
#
# $1 the file name
# returns the clean file name
clean_filename() {
echo "${1#./}" # Delete leading './'
}
# Adds all the bells and whistles to format the html page # Adds all the bells and whistles to format the html page
# Every blog post is marked with a <!-- entry begin --> and <!-- entry end --> # Every blog post is marked with a <!-- entry begin --> and <!-- entry end -->
# which is parsed afterwards in the other functions. There is also a marker # which is parsed afterwards in the other functions. There is also a marker
@ -445,14 +422,16 @@ create_html_page() {
echo '</div></div></div>' # title, header, headerholder echo '</div></div></div>' # title, header, headerholder
echo '<div id="divbody"><div class="content">' echo '<div id="divbody"><div class="content">'
file_url=$(clean_filename "$filename") file_url=${filename#./}
file_url=$(sed 's/.rebuilt//g' <<< "$file_url") # Get the correct URL when rebuilding file_url=${file_url%.rebuilt} # Get the correct URL when rebuilding
# one blog entry # one blog entry
if [[ $index == no ]]; then if [[ $index == no ]]; then
echo '<!-- entry begin -->' # marks the beginning of the whole post echo '<!-- entry begin -->' # marks the beginning of the whole post
echo "<h3><a class=\"ablack\" href=\"$file_url\">" echo "<h3><a class=\"ablack\" href=\"$file_url\">"
# remove possible <p>'s on the title because of markdown conversion # remove possible <p>'s on the title because of markdown conversion
echo "$title" | sed 's/<\/*p>//g' title=${title//<p>/}
title=${title//<\/p>/}
echo "$title"
echo '</a></h3>' echo '</a></h3>'
if [[ -z $timestamp ]]; then if [[ -z $timestamp ]]; then
echo "<div class=\"subtitle\">$(LC_ALL=$date_locale date +"$date_format") &mdash; " echo "<div class=\"subtitle\">$(LC_ALL=$date_locale date +"$date_format") &mdash; "
@ -684,14 +663,13 @@ all_tags() {
{ {
echo "<h3>$template_tags_title</h3>" echo "<h3>$template_tags_title</h3>"
echo "<ul>" echo "<ul>"
for i in ./$prefix_tags*.html; do for i in $prefix_tags*.html; do
[[ -f "$i" ]] || break [[ -f "$i" ]] || break
echo -n "." 1>&3 echo -n "." 1>&3
nposts=$(grep -c "<\!-- text begin -->" "$i") nposts=$(grep -c "<\!-- text begin -->" "$i")
tagname=$(echo "$i" | cut -c "$((${#prefix_tags}+3))-" | sed 's/\.html//g') tagname=${i#"$prefix_tags"}
i=$(clean_filename "$i") tagname=${tagname%.html}
word=$template_tags_posts_singular ((nposts > 1)) && word=$template_tags_posts || word=$template_tags_posts_singular
(($nposts > 1)) && word=$template_tags_posts
echo "<li><a href=\"$i\">$tagname</a> &mdash; $nposts $word</li>" echo "<li><a href=\"$i\">$tagname</a> &mdash; $nposts $word</li>"
done done
echo "" 1>&3 echo "" 1>&3
@ -810,7 +788,8 @@ rebuild_tags() {
rm "$tmpfile" rm "$tmpfile"
# Now generate the tag files with headers, footers, etc # Now generate the tag files with headers, footers, etc
while IFS='' read -r i; do while IFS='' read -r i; do
tagname=$(echo "$i" | cut -c "$((${#prefix_tags}+3))-" | sed 's/\.tmp\.html//g') tagname=${i#./"$prefix_tags"}
tagname=${tagname%.tmp.html}
create_html_page "$i" "$prefix_tags$tagname.html" yes "$global_title &mdash; $template_tag_title \"$tagname\"" create_html_page "$i" "$prefix_tags$tagname.html" yes "$global_title &mdash; $template_tag_title \"$tagname\""
rm "$i" rm "$i"
done < <(ls -t ./"$prefix_tags"*.tmp.html 2>/dev/null) done < <(ls -t ./"$prefix_tags"*.tmp.html 2>/dev/null)
@ -828,24 +807,23 @@ get_post_title() {
# #
# $2 if "-n", tags will be sorted by number of posts # $2 if "-n", tags will be sorted by number of posts
list_tags() { list_tags() {
if [[ $2 ]] && [[ $2 == -n ]]; then do_sort=1; else do_sort=0; fi if [[ $2 == -n ]]; then do_sort=1; else do_sort=0; fi
ls ./$prefix_tags*.html &> /dev/null ls ./$prefix_tags*.html &> /dev/null
(($? != 0)) && echo "No posts yet. Use 'bb.sh post' to create one" && return (($? != 0)) && echo "No posts yet. Use 'bb.sh post' to create one" && return
lines="" lines=""
for i in ./$prefix_tags*.html; do for i in $prefix_tags*.html; do
[[ -f "$i" ]] || break [[ -f "$i" ]] || break
nposts=$(grep -c "<\!-- text begin -->" "$i") nposts=$(grep -c "<\!-- text begin -->" "$i")
tagname=$(echo "$i" | cut -c "$((${#prefix_tags}+3))-" | sed 's/\.html//g') tagname=${i#"$prefix_tags"}
i=$(clean_filename "$i") tagname=${tagname#.html}
word=$template_tags_posts_singular ((nposts > 1)) && word=$template_tags_posts || word=$template_tags_posts_singular
(($nposts > 1)) && word=$template_tags_posts
line="$tagname # $nposts # $word" line="$tagname # $nposts # $word"
lines+=$line\\n lines+=$line\\n
done done
if (( $do_sort == 1 )); then if (( do_sort == 1 )); then
echo -e "$lines" | column -t -s "#" | sort -nrk 2 echo -e "$lines" | column -t -s "#" | sort -nrk 2
else else
echo -e "$lines" | column -t -s "#" echo -e "$lines" | column -t -s "#"
@ -895,7 +873,7 @@ make_rss() {
get_post_title "$i" get_post_title "$i"
echo '</title><description><![CDATA[' echo '</title><description><![CDATA['
get_html_file_content 'text' 'entry' $cut_do <"$i" get_html_file_content 'text' 'entry' $cut_do <"$i"
echo "]]></description><link>$global_url/$(clean_filename "$i")</link>" echo "]]></description><link>$global_url/${i#./}</link>"
echo "<guid>$global_url/$i</guid>" echo "<guid>$global_url/$i</guid>"
echo "<dc:creator>$global_author</dc:creator>" echo "<dc:creator>$global_author</dc:creator>"
echo "<pubDate>$(LC_ALL=C date -r "$i" +"%a, %d %b %Y %H:%M:%S %z")</pubDate></item>" echo "<pubDate>$(LC_ALL=C date -r "$i" +"%a, %d %b %Y %H:%M:%S %z")</pubDate></item>"
@ -923,7 +901,7 @@ create_includes() {
echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">' echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'
echo '<html xmlns="http://www.w3.org/1999/xhtml"><head>' echo '<html xmlns="http://www.w3.org/1999/xhtml"><head>'
echo '<meta http-equiv="Content-type" content="text/html;charset=UTF-8" />' echo '<meta http-equiv="Content-type" content="text/html;charset=UTF-8" />'
echo '<meta name="viewport" content="width=device-width, initial-scale=1.0">' echo '<meta name="viewport" content="width=device-width, initial-scale=1.0" />'
printf '<link rel="stylesheet" href="%s" type="text/css" />\n' "${css_include[@]}" printf '<link rel="stylesheet" href="%s" type="text/css" />\n' "${css_include[@]}"
if [[ -z $global_feedburner ]]; then if [[ -z $global_feedburner ]]; then
echo "<link rel=\"alternate\" type=\"application/rss+xml\" title=\"$template_subscribe_browser_button\" href=\"$blog_feed\" />" echo "<link rel=\"alternate\" type=\"application/rss+xml\" title=\"$template_subscribe_browser_button\" href=\"$blog_feed\" />"
@ -952,7 +930,7 @@ delete_includes() {
create_css() { create_css() {
# To avoid overwriting manual changes. However it is recommended that # To avoid overwriting manual changes. However it is recommended that
# this function is modified if the user changes the blog.css file # this function is modified if the user changes the blog.css file
[[ -n $css_include ]] && return || css_include=('main.css' 'blog.css') (( ${#css_include[@]} > 0 )) && return || css_include=('main.css' 'blog.css')
if [[ ! -f blog.css ]]; then if [[ ! -f blog.css ]]; then
# blog.css directives will be loaded after main.css and thus will prevail # blog.css directives will be loaded after main.css and thus will prevail
echo '#title{font-size: x-large;} echo '#title{font-size: x-large;}
@ -1125,7 +1103,7 @@ do_main() {
# Test for existing html files # Test for existing html files
if ls ./*.html &> /dev/null; then if ls ./*.html &> /dev/null; then
# We're going to back up just in case # We're going to back up just in case
tar cfz ".backup.tar.gz" *.html && tar -c -z -f ".backup.tar.gz" -- *.html &&
chmod 600 ".backup.tar.gz" chmod 600 ".backup.tar.gz"
elif [[ $1 == rebuild ]]; then elif [[ $1 == rebuild ]]; then
echo "Can't find any html files, nothing to rebuild" echo "Can't find any html files, nothing to rebuild"