diff options
Diffstat (limited to 'bb.sh')
-rwxr-xr-x | bb.sh | 118 |
1 files changed, 91 insertions, 27 deletions
@@ -18,6 +18,10 @@ global_config=".config" global_variables() { global_software_name="BashBlog" global_software_version="2.10" + # organise the blog in a git-repo + git_repo="false" + git_push_on_commit="false" + git_push_options="" # Blog title global_title="My fancy blog" @@ -323,7 +327,10 @@ edit() { parse_file "$TMPFILE" "$edit_timestamp" "$filename" else parse_file "$TMPFILE" "$edit_timestamp" # this command sets $filename as the html processed file - [[ ${1##*.} == md ]] && mv "$1" "${filename%%.*}.md" 2>/dev/null + if [[ ${1##*.} == md ]]; then + mv "$1" "${filename%%.*}.md" 2>/dev/null + [[ "$git_repo" == 'true' ]] && git add "${filename%%.*}.md" + fi fi rm "$TMPFILE" fi @@ -331,6 +338,10 @@ edit() { touch -t "$touch_timestamp" "$1" chmod 644 "$filename" echo "Posted $filename" + if [[ $git_repo == 'true' ]]; then + git add $filename + commit_message="Update $filename" + fi tags_after=$(tags_in_post "$filename") relevant_tags=$(echo "$tags_before $tags_after" | tr ',' ' ' | tr ' ' '\n' | sort -u | tr '\n' ' ') if [[ ! -z $relevant_tags ]]; then @@ -453,7 +464,7 @@ create_html_page() { [[ -n $body_begin_file ]] && cat "$body_begin_file" [[ $filename = $index_file* ]] && [[ -n $body_begin_file_index ]] && cat "$body_begin_file_index" # body divs - echo '<div id="divbodyholder">' + echo '<div id="divbodyholder"><div class="inner_page">' echo '<div class="headerholder"><div class="header">' # blog title echo '<div id="title">' @@ -466,12 +477,12 @@ create_html_page() { # one blog entry if [[ $index == no ]]; then echo '<!-- entry begin -->' # marks the beginning of the whole post - echo "<h3><a class=\"ablack\" href=\"$file_url\">" + echo "<div class=\"section_header\"><a class=\"ablack\" href=\"$file_url\">" # remove possible <p>'s on the title because of markdown conversion title=${title//<p>/} title=${title//<\/p>/} echo "$title" - echo '</a></h3>' + echo '</a></div>' if [[ -z $timestamp ]]; then echo "<!-- $date_inpost: #$(LC_ALL=$date_locale date +"$date_format_timestamp")# -->" else @@ -503,7 +514,7 @@ create_html_page() { # page footer cat .footer.html # close divs - echo '</div></div>' # divbody and divbodyholder + echo '</div></div></div>' # divbody, inner_page and divbodyholder disqus_footer [[ -n $body_end_file ]] && cat "$body_end_file" echo '</body></html>' @@ -636,6 +647,12 @@ EOF draft=drafts/$title.$fmt mv "$TMPFILE" "$draft" + if [[ $git_repo == 'true' ]]; then + git add $draft + git commit -m"$commit_message""drafted $draft; " # will exit soon, no later opportunity to commit + [[ $git_push_on_commit == 'true' ]] && git push + echo "Add $draft" + fi chmod 600 "$draft" rm "$filename" delete_includes @@ -657,11 +674,16 @@ EOF if [[ $fmt == md && -n $save_markdown ]]; then mv "$TMPFILE" "${filename%%.*}.md" + [[ $git_repo == 'true' ]] && git add "${filename%%.*}.md" else rm "$TMPFILE" fi chmod 644 "$filename" echo "Posted $filename" + if [[ $git_repo == 'true' ]]; then + git add $filename + commit_message="Add ${filename%%.*}" + fi relevant_tags=$(tags_in_post $filename) if [[ -n $relevant_tags ]]; then relevant_posts="$(posts_with_tags $relevant_tags) $filename" @@ -710,6 +732,7 @@ all_posts() { mv "$archive_index.tmp" "$archive_index" chmod 644 "$archive_index" rm "$contentfile" + [[ $git_repo == 'true' ]] && git add $archive_index } # Create an index page with all the tags @@ -745,6 +768,7 @@ all_tags() { mv "$tags_index.tmp" "$tags_index" chmod 644 "$tags_index" rm "$contentfile" + [[ $git_repo == 'true' ]] && git add $tags_index } # Generate the index.html with the content of the latest posts @@ -782,6 +806,7 @@ rebuild_index() { create_html_page "$contentfile" "$newindexfile" yes "$global_title" "$global_author" rm "$contentfile" mv "$newindexfile" "$index_file" + [[ $git_repo == 'true' ]] && git add $index_file chmod 644 "$index_file" } @@ -799,7 +824,7 @@ posts_with_tags() { (($# < 1)) && return set -- "${@/#/$prefix_tags}" set -- "${@/%/.html}" - sed -n '/^<h3><a class="ablack" href="[^"]*">/{s/.*href="\([^"]*\)">.*/\1/;p;}' "$@" 2> /dev/null + sed -n '/^<div class="section_header"><a class="ablack" href="[^"]*">/{s/.*href="\([^"]*\)">.*/\1/;p;}' "$@" 2> /dev/null } # Rebuilds tag_*.html files @@ -856,7 +881,9 @@ rebuild_tags() { tagname=${tagname%.tmp.html} create_html_page "$i" "$prefix_tags$tagname.html" yes "$global_title — $template_tag_title \"$tagname\"" "$global_author" rm "$i" + [[ $git_repo == 'true' ]] && git add $prefix_tags$tagname.html done < <(ls -t ./"$prefix_tags"*.tmp.html 2>/dev/null) + commit_message="Rebuild tags" echo } @@ -864,7 +891,7 @@ rebuild_tags() { # # $1 the html file get_post_title() { - awk '/<h3><a class="ablack" href=".+">/, /<\/a><\/h3>/{if (!/<h3><a class="ablack" href=".+">/ && !/<\/a><\/h3>/) print}' "$1" + awk '/<div class="section_header"><a class="ablack" href=".+">/, /<\/a><\/div>/{if (!/<div class="section_header"><a class="ablack" href=".+">/ && !/<\/a><\/div>/) print}' "$1" } # Return the post author @@ -987,14 +1014,17 @@ make_rss() { mv "$rssfile" "$blog_feed" chmod 644 "$blog_feed" + [[ $git_repo == true ]] && git add $blog_feed } # generate headers, footers, etc create_includes() { - { + if [[ -f $title_file ]]; then cp "$title_file" .title.html + else { echo "<h1 class=\"nomargin\"><a class=\"ablack\" href=\"$global_url/$index_file\">$global_title</a></h1>" echo "<div id=\"description\">$global_description</div>" - } > ".title.html" + } > ".title.html" + fi if [[ -f $header_file ]]; then cp "$header_file" .header.html else { @@ -1013,10 +1043,14 @@ create_includes() { if [[ -f $footer_file ]]; then cp "$footer_file" .footer.html else { - protected_mail=${global_email//@/@} - protected_mail=${protected_mail//./.} + if [[ $protect_email == true ]]; then + protected_mail=${global_email//@/@} + protected_mail=${protected_mail//./.} + else + protected_mail=${global_email} + fi echo "<div id=\"footer\">$global_license <a href=\"$global_author_url\">$global_author</a> — <a href=\"mailto:$protected_mail\">$protected_mail</a><br>" - echo 'Generated with <a href="https://github.com/cfenollosa/bashblog">bashblog</a>, a single bash script to easily create blogs like this one</div>' + echo '<a href=\"$global_url\">$global_title</a> is generated with <a href="https://github.com/cfenollosa/bashblog">bashblog</a>, a single bash script to easily create blogs like this one</div>' } >> ".footer.html" fi } @@ -1071,6 +1105,8 @@ create_css() { blockquote img{margin:12px 0px;} blockquote iframe{margin:12px 0px;}' > main.css fi + + [[ $git_repo == 'true' ]] && git add $css_include } # Regenerates all the single post entries, keeping the post content but modifying @@ -1101,8 +1137,10 @@ rebuild_all_entries() { mv "$i.rebuilt" "$i" chmod 644 "$i" touch -t "$timestamp" "$i" + [[ $git_repo == 'true' ]] && git add $i rm "$contentfile" done + commit_message="Rebuild all entries" echo "" } @@ -1134,10 +1172,20 @@ reset() { echo "Are you sure you want to delete all blog entries? Please write \"Yes, I am!\" " read -r line if [[ $line == "Yes, I am!" ]]; then - rm .*.html ./*.html ./*.css ./*.rss &> /dev/null + if [[ $git_repo == 'true' ]]; then + git rm .*.html ./*.html ./*.css ./*.rss &> /dev/null + git commit -m"$commit_message""resetted the blog; " # has to be commited right here as will be exited right after. + [[ $git_push_on_commit == 'true' ]] && git push + else + rm .*.html ./*.html ./*.css ./*.rss &> /dev/null + fi echo echo "Deleted all posts, stylesheets and feeds." - echo "Kept your old '.backup.tar.gz' just in case, please delete it manually if needed." + if [[ $git_repo == 'true' ]]; then + echo "Didn't reset the git repo just in case, please do so manually if needed." + else + echo "Kept your old '.backup.tar.gz' just in case, please delete it manually if needed." + fi else echo "Phew! You dodged a bullet there. Nothing was modified." fi @@ -1192,6 +1240,8 @@ do_main() { global_variables [[ -f $global_config ]] && source "$global_config" &> /dev/null global_variables_check + # create git repo if wanted and not there yet + [[ $git_repo == 'true' && ! -d "./.git/" ]] && git init # Check for $EDITOR [[ -z $EDITOR ]] && @@ -1214,19 +1264,21 @@ do_main() { fi fi - # Test for existing html files - if ls ./*.html &> /dev/null; then - # We're going to back up just in case - tar -c -z -f ".backup.tar.gz" -- *.html && - chmod 600 ".backup.tar.gz" - elif [[ $1 == rebuild ]]; then - echo "Can't find any html files, nothing to rebuild" - exit - fi + if [[ $git_repo != 'true' ]]; then # don't use tar-backups when already using git + # Test for existing html files + if ls ./*.html &> /dev/null; then + # We're going to back up just in case + tar -c -z -f ".backup.tar.gz" -- *.html && + chmod 600 ".backup.tar.gz" + elif [[ $1 == rebuild ]]; then + echo "Can't find any html files, nothing to rebuild" + exit + fi - # Keep first backup of this day containing yesterday's version of the blog - [[ ! -f .yesterday.tar.gz || $(date -r .yesterday.tar.gz +'%d') != "$(date +'%d')" ]] && - cp .backup.tar.gz .yesterday.tar.gz &> /dev/null + # Keep first backup of this day containing yesterday's version of the blog + [[ ! -f .yesterday.tar.gz || $(date -r .yesterday.tar.gz +'%d') != "$(date +'%d')" ]] && + cp .backup.tar.gz .yesterday.tar.gz &> /dev/null + fi [[ $1 == reset ]] && reset && exit @@ -1235,7 +1287,15 @@ do_main() { create_includes [[ $1 == post ]] && write_entry "$@" [[ $1 == rebuild ]] && rebuild_all_entries && rebuild_tags - [[ $1 == delete ]] && rm "$2" &> /dev/null && rebuild_tags + if [[ $1 == delete ]]; then + if [[ $git_repo == 'true' ]]; then + git rm $2 &> /dev/null + commit_message="Delete $2" + else + rm "$2" &> /dev/null + fi + rebuild_tags + fi if [[ $1 == edit ]]; then if [[ $2 == -n ]]; then edit "$3" @@ -1251,6 +1311,10 @@ do_main() { make_rss make_sitemap delete_includes + if [[ $git_repo == 'true' ]]; then + git commit -m"$commit_message" + [[ $git_push_on_commit == 'true' ]] && git push $git_push_options + fi } |