2013-02-21 14:59:44 +00:00
#!/usr/bin/env bash
# BashBlog, a simple blog system written in a single bash script
2014-02-27 09:24:54 +00:00
# Copyright: Carlos Fenollosa <carlos.fenollosa@gmail.com>, 2011-2014
2013-06-05 10:36:47 +00:00
# With contributions from many others:
2013-06-05 10:39:52 +00:00
# https://github.com/carlesfe/bashblog/contributors
2013-02-21 14:59:44 +00:00
#########################################################################################
#
# README
#
#########################################################################################
#
# This is a very basic blog system
#
# Basically it asks the user to create a text file, then converts it into a .html file
# and then rebuilds the index.html and feed.rss.
#
2013-04-09 17:41:37 +00:00
# Comments are supported via external service (Disqus).
2013-12-23 20:01:01 +00:00
# Markdown syntax is supported via third party library (e.g. Gruber's Markdown.pl)
2013-02-21 14:59:44 +00:00
#
# This script is standalone, it doesn't require any other file to run
#
# Files that this script generates:
# - main.css (inherited from my web page) and blog.css (blog-specific stylesheet)
# - one .html for each post
2014-02-27 17:07:01 +00:00
# - one tag_*.html file for each tag
2013-02-21 14:59:44 +00:00
# - index.html (regenerated each run)
2014-02-27 17:07:01 +00:00
# - feed.rss (idem)
# - all_posts.html (idem)
# - all_tags.html (idem)
2013-02-21 14:59:44 +00:00
# - it also generates temporal files, which are removed afterwards
#
# It generates valid html and rss files, so keep care to use valid xhtml when editing a post
#
2014-02-27 17:07:01 +00:00
# There are many loops which iterate on '*.html' so make sure not to manually put other
# html files on this folder.
2013-12-23 20:01:01 +00:00
#
# Read more: https://github.com/cfenollosa/bashblog
2013-02-21 14:59:44 +00:00
#########################################################################################
#
# LICENSE
#
#########################################################################################
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program 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. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#########################################################################################
#
# CHANGELOG
#
#########################################################################################
#
2014-02-27 15:46:37 +00:00
# 2.1 Support for tags/categories
2014-02-27 17:07:01 +00:00
# 'delete' command
2014-02-18 16:34:41 +00:00
# 2.0.3 Support for other analytics code, via external file
2014-01-17 09:03:07 +00:00
# 2.0.2 Fixed bug when $body_begin_file was empty
# Added extra line in the footer linking to the github project
2014-01-09 18:50:27 +00:00
# 2.0.1 Allow personalized header/footer files
2013-12-23 19:49:31 +00:00
# 2.0 Added Markdown support
# Fully support BSD date
2013-09-26 09:38:55 +00:00
# 1.6.4 Fixed bug in localized dates
2013-06-12 21:18:58 +00:00
# 1.6.3 Now supporting BSD date
2013-06-05 10:36:47 +00:00
# 1.6.2 Simplified some functions and variables to avoid duplicated information
2013-04-09 17:41:37 +00:00
# 1.6.1 'date' fix when hours are 1 digit.
# 1.6.0 Disqus comments. External configuration file. Check of 'date' command version.
2013-03-07 18:30:59 +00:00
# 1.5.1 Misc bugfixes and parameter checks
2013-06-11 00:55:12 +00:00
# 1.5 Đurađ Radojičić (djura-san) refactored some code and added flexibility and i18n
2013-02-21 15:37:55 +00:00
# 1.4.2 Now issues are handled at Github
2013-02-21 14:59:44 +00:00
# 1.4.1 Some code refactoring
# 1.4 Using twitter for comments, improved 'rebuild' command
# 1.3 'edit' command
# 1.2.2 Feedburner support
# 1.2.1 Fixed the timestamps bug
# 1.2 'list' command
# 1.1 Draft and preview support
# 1.0 Read http://is.gd/Bkdoru
#########################################################################################
#
# CODE
#
#########################################################################################
#
# As usual with bash scripts, scroll all the way to the bottom for the main routine
# All other functions are declared above main.
# Global variables
# It is recommended to perform a 'rebuild' after changing any of this in the code
2013-03-13 13:48:44 +00:00
2013-06-05 10:36:47 +00:00
# Config file. Any settings "key=value" written there will override the
# global_variables defaults. Useful to avoid editing bb.sh and having to deal
# with merges in VCS
2013-03-13 13:48:44 +00:00
global_config = ".config"
2013-06-05 10:36:47 +00:00
# This function will load all the variables defined here. They might be overriden
# by the 'global_config' file contents
2013-02-21 14:59:44 +00:00
global_variables( ) {
2013-02-21 15:37:55 +00:00
global_software_name = "BashBlog"
2014-02-27 15:46:37 +00:00
global_software_version = "2.1"
2013-02-21 15:37:55 +00:00
# Blog title
2013-02-21 16:26:10 +00:00
global_title = "My fancy blog"
2013-02-21 15:37:55 +00:00
# The typical subtitle for each blog
2013-02-21 16:26:10 +00:00
global_description = "A blog about turtles and carrots"
2013-02-21 15:37:55 +00:00
# The public base URL for this blog
2013-02-21 16:26:10 +00:00
global_url = "http://example.com/blog"
2013-02-21 15:37:55 +00:00
# Your name
2013-02-21 16:26:10 +00:00
global_author = "John Smith"
2013-02-21 15:37:55 +00:00
# You can use twitter or facebook or anything for global_author_url
2013-02-21 16:26:10 +00:00
global_author_url = "http://twitter.com/example"
2013-02-21 15:37:55 +00:00
# Your email
2013-02-21 16:26:10 +00:00
global_email = "john@smith.com"
2013-02-21 15:37:55 +00:00
# CC by-nc-nd is a good starting point, you can change this to "©" for Copyright
global_license = "CC by-nc-nd"
2014-02-18 16:34:41 +00:00
# If you have a Google Analytics ID (UA-XXXXX) and wish to use the standard
# embedding code, put it on global_analytics
# If you have custom analytics code (i.e. non-google) or want to use the Universal
# code, leave global_analytics empty and specify a global_analytics_file
2013-02-21 16:26:10 +00:00
global_analytics = ""
2014-02-18 16:34:41 +00:00
global_analytics_file = ""
2013-02-21 15:37:55 +00:00
# Leave this empty (i.e. "") if you don't want to use feedburner,
# or change it to your own URL
2013-02-21 16:26:10 +00:00
global_feedburner = ""
2013-02-21 15:37:55 +00:00
2013-06-05 10:36:47 +00:00
# Change this to your username if you want to use twitter for comments
global_twitter_username = ""
2013-02-21 14:59:44 +00:00
2013-06-05 10:36:47 +00:00
# Change this to your disqus username to use disqus for comments
global_disqus_username = ""
2013-04-02 21:35:33 +00:00
2013-02-21 14:59:44 +00:00
2013-02-21 16:06:02 +00:00
# Blog generated files
# index page of blog (it is usually good to use "index.html" here)
index_file = "index.html"
number_of_index_articles = "8"
# global archive
archive_index = "all_posts.html"
2014-02-27 17:07:01 +00:00
tags_index = "all_tags.html"
2013-02-21 16:06:02 +00:00
# feed file (rss in this case)
2013-02-21 16:16:11 +00:00
blog_feed = "feed.rss"
2013-02-21 16:06:02 +00:00
number_of_feed_articles = "10"
2014-02-27 21:53:00 +00:00
# "cut" blog entry when putting it to index page
# i.e. include only up to first <hr> (---- in markdown)
# possible values: "cut", ""
cut_do = "cut"
2014-02-27 21:40:11 +00:00
# Regexp matching the HTML line where to do the cut
# note that slash is regexp separator so you need to prepend it with backslash
cut_line = '<hr ?\/?>'
2014-02-27 15:46:37 +00:00
# prefix for tags/categories files
2014-02-27 12:59:56 +00:00
# please make sure that no other html file starts with this prefix
prefix_tags = "tag_"
2014-01-09 18:50:27 +00:00
# personalized header and footer (only if you know what you're doing)
# DO NOT name them .header.html, .footer.html or they will be overwritten
# leave blank to generate them, recommended
header_file = ""
footer_file = ""
2014-01-09 19:02:54 +00:00
# extra content to add just after we open the <body> tag
# and before the actual blog content
body_begin_file = ""
2014-03-01 15:22:28 +00:00
# CSS files to include on every page, f.ex. css_include=('main.css' 'blog.css')
# leave empty to use generated
css_include = ( )
2013-02-21 16:06:02 +00:00
# Localization and i18n
# "Comments?" (used in twitter link after every post)
template_comments = "Comments?"
2014-02-26 22:22:26 +00:00
# "Read more..." (link under cut article on index page)
template_read_more = "Read more..."
2013-02-21 16:06:02 +00:00
# "View more posts" (used on bottom of index page as link to archive)
template_archive = "View more posts"
2014-02-24 17:05:55 +00:00
# "All posts" (title of archive page)
template_archive_title = "All posts"
2014-02-27 17:07:01 +00:00
# "All tags"
template_tags_title = "All tags"
2014-02-27 20:28:20 +00:00
# "posts" (on "All tags" page, text at the end of each tag line, like "2. Music - 15 posts")
template_tags_posts = "posts"
# "Posts tagged" (text on a title of a page with index of one tag, like "My Blog - Posts tagged "Music"")
template_tag_title = "Posts tagged"
# "Tags:" (beginning of line in HTML file with list of all tags for this article)
template_tags_line_header = "Tags:"
2013-02-21 16:06:02 +00:00
# "Back to the index page" (used on archive page, it is link to blog index)
template_archive_index_page = "Back to the index page"
# "Subscribe" (used on bottom of index page, it is link to RSS feed)
template_subscribe = "Subscribe"
# "Subscribe to this page..." (used as text for browser feed button that is embedded to html)
template_subscribe_browser_button = "Subscribe to this page..."
# "Tweet" (used as twitter text button for posting to twitter)
template_twitter_button = "Tweet"
2013-03-29 07:17:35 +00:00
template_twitter_comment = "<Type your comment here but please leave the URL so that other people can follow the comments>"
2013-02-21 16:06:02 +00:00
# The locale to use for the dates displayed on screen (not for the timestamps)
date_format = "%B %d, %Y"
date_locale = "C"
2013-12-23 19:49:31 +00:00
2013-12-23 20:01:01 +00:00
# Markdown location. Trying to autodetect by default.
# The invocation must support the signature 'markdown_bin in.html > out.md'
2013-12-23 19:49:31 +00:00
markdown_bin = " $( which Markdown.pl) "
2013-02-21 16:06:02 +00:00
}
2014-01-09 18:50:27 +00:00
# Check for the validity of some variables
# DO NOT EDIT THIS FUNCTION unless you know what you're doing
global_variables_check( ) {
[ [ " $header_file " = = ".header.html" ] ] &&
echo "Please check your configuration. '.header.html' is not a valid value for the setting 'header_file'" &&
exit
[ [ " $footer_file " = = ".footer.html" ] ] &&
echo "Please check your configuration. '.footer.html' is not a valid value for the setting 'footer_file'" &&
exit
}
2013-12-23 19:49:31 +00:00
# Test if the markdown script is working correctly
test_markdown( ) {
2013-12-23 20:09:22 +00:00
[ [ -z " $markdown_bin " ] ] && return 1
2013-12-24 14:05:27 +00:00
[ [ -z " $( which diff) " ] ] && return 1
2013-12-23 19:49:31 +00:00
in = " /tmp/md-in- $( echo $RANDOM ) .md "
out = " /tmp/md-out- $( echo $RANDOM ) .html "
good = " /tmp/md-good- $( echo $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 [ [ $? -ne 0 ] ] ; then
rm -f $in $good $out
return 1
fi
rm -f $in $good $out
return 0
}
# Parse a Markdown file into HTML and return the generated file
markdown( ) {
out = " $( echo $1 | sed 's/md$/html/g' ) "
while [ -f " $out " ] ; do out = " $( echo $out | sed 's/\.html$/\.' $RANDOM '\.html' ) " ; done
$markdown_bin $1 > $out
echo $out
}
2013-02-21 14:59:44 +00:00
# Prints the required google analytics code
google_analytics( ) {
2014-02-18 16:34:41 +00:00
[ [ -z " $global_analytics " ] ] && [ [ -z " $global_analytics_file " ] ] && return
2013-02-21 14:59:44 +00:00
2014-02-18 16:34:41 +00:00
if [ [ -z " $global_analytics_file " ] ] ; then
echo " <script type=\"text/javascript\">
2013-02-21 14:59:44 +00:00
2014-02-18 16:34:41 +00:00
var _gaq = _gaq || [ ] ;
_gaq.push( [ '_setAccount' , '"$global_analytics"' ] ) ;
_gaq.push( [ '_trackPageview' ] ) ;
2013-02-21 14:59:44 +00:00
2014-02-18 16:34:41 +00:00
( function ( ) {
var ga = document.createElement( 'script' ) ; ga.type = 'text/javascript' ; ga.async = true;
ga.src = ( 'https:' = = document.location.protocol ? 'https://ssl' : 'http://www' ) + '.google-analytics.com/ga.js' ;
var s = document.getElementsByTagName( 'script' ) [ 0] ; s.parentNode.insertBefore( ga, s) ;
} ) ( ) ;
2013-02-21 14:59:44 +00:00
2014-02-18 16:34:41 +00:00
</script>"
else
cat " $global_analytics_file "
fi
2013-02-21 14:59:44 +00:00
}
2013-04-02 21:35:33 +00:00
# Prints the required code for disqus comments
disqus_body( ) {
2013-12-23 19:49:31 +00:00
[ [ -z " $global_disqus_username " ] ] && return
2013-06-05 10:36:47 +00:00
2013-04-02 21:35:33 +00:00
echo ' <div id = "disqus_thread" ></div>
<script type = "text/javascript" >
/* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
var disqus_shortname = '\' $global_disqus_username \' ' ; // required: replace example with your forum shortname
/* * * DONT EDIT BELOW THIS LINE * * */
( function ( ) {
var dsq = document.createElement( "script" ) ; dsq.type = "text/javascript" ; dsq.async = true;
dsq.src = "//" + disqus_shortname + ".disqus.com/embed.js" ;
( document.getElementsByTagName( "head" ) [ 0] || document.getElementsByTagName( "body" ) [ 0] ) .appendChild( dsq) ;
} ) ( ) ;
</script>
<noscript>Please enable JavaScript to view the <a href = "http://disqus.com/?ref_noscript" >comments powered by Disqus.</a></noscript>
<a href = "http://disqus.com" class = "dsq-brlink" >comments powered by <span class = "logo-disqus" >Disqus</span></a>'
}
2013-06-05 10:36:47 +00:00
2013-04-02 21:35:33 +00:00
# Prints the required code for disqus in the footer
disqus_footer( ) {
2013-12-23 19:49:31 +00:00
[ [ -z " $global_disqus_username " ] ] && return
echo ' <script type = "text/javascript" >
/* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
var disqus_shortname = '\' $global_disqus_username \' ' ; // required: replace example with your forum shortname
/* * * DONT EDIT BELOW THIS LINE * * */
( function ( ) {
var s = document.createElement( "script" ) ; s.async = true;
s.type = "text/javascript" ;
s.src = "//" + disqus_shortname + ".disqus.com/count.js" ;
( document.getElementsByTagName( "HEAD" ) [ 0] || document.getElementsByTagName( "BODY" ) [ 0] ) .appendChild( s) ;
} ( ) ) ;
</script>'
2013-04-02 21:35:33 +00:00
}
2014-02-26 20:04:36 +00:00
# Reads HTML file from stdin, prints its content to stdout
2014-02-26 22:04:31 +00:00
# $1 where to start ("text" or "entry")
# $2 where to stop ("text" or "entry")
2014-02-27 21:40:11 +00:00
# $3 "cut" to remove text from <hr /> to <!-- text end -->
# note that this does not remove <hr /> line itself,
2014-02-26 22:04:31 +00:00
# so you can see if text was cut or not
2014-02-26 20:04:36 +00:00
get_html_file_content( ) {
2014-02-26 22:04:31 +00:00
awk '/<!-- ' $1 ' begin -->/, /<!-- ' $2 ' end -->/{
if ( !/<!-- '$1' begin -->/ && !/<!-- '$2' end -->/) print
2014-02-27 21:40:11 +00:00
if ( " ' $3 ' " = = "cut" && /'"$cut_line"' /) {
2014-02-26 22:04:31 +00:00
if ( " ' $2 ' " = = "text" ) exit # no need to read further
while ( getline > 0 && !/<!-- text end -->/) { }
}
} '
2014-02-26 20:04:36 +00:00
}
2013-02-21 14:59:44 +00:00
# Edit an existing, published .html file while keeping its original timestamp
# Please note that this function does not automatically republish anything, as
# it is usually called from 'main'.
#
2014-03-01 12:55:55 +00:00
# Note that it edits HTML file, even if you wrote the post as markdown originally
# Note that if you edit title then filename might also change
2013-02-21 14:59:44 +00:00
#
# $1 the file to edit
2014-03-01 13:34:52 +00:00
# $2 (optional) edit mode:
# "keep" to keep old filename
# "full" to edit full HTML, and not only text part (keeps old filename)
# leave empty for default behavior (edit only text part and change name)
2013-02-21 14:59:44 +00:00
edit( ) {
2014-03-01 16:17:52 +00:00
# Original post timestamp
edit_timestamp = " $( LC_ALL = $date_locale date -r $1 +"%a, %d %b %Y %H:%M:%S %z" ) "
2014-03-10 18:18:14 +00:00
touch_timestamp = " $( LC_ALL = $date_locale date -r $1 +'%Y%m%d%H%M' ) "
2014-03-01 13:34:52 +00:00
if [ " $2 " = "full" ] ; then
$EDITOR " $1 "
filename = " $1 "
else
# Create the content file
2014-03-01 20:02:13 +00:00
TMPFILE = " $( basename $1 ) . $RANDOM .html "
2014-03-01 13:34:52 +00:00
# Title
echo " $( get_post_title $1 ) " > " $TMPFILE "
# Post text with plaintext tags
get_html_file_content 'text' 'text' <$1 | sed " s|<a href=' $prefix_tags \([^']*\).html'>\\1</a>|\\1|g " >> " $TMPFILE "
rm $1
$EDITOR " $TMPFILE "
if [ " $2 " = "keep" ] ; then
2014-03-18 21:17:34 +00:00
parse_file " $TMPFILE " " $edit_timestamp " " $1 "
else
parse_file " $TMPFILE " " $edit_timestamp " # this command sets $filename as the html processed file
2014-03-01 13:34:52 +00:00
fi
2014-03-18 21:17:34 +00:00
rm " $TMPFILE "
2014-03-01 13:34:52 +00:00
fi
2014-03-10 18:18:14 +00:00
touch -t " $touch_timestamp " " $filename "
2014-03-01 12:55:55 +00:00
chmod 644 " $filename "
echo " Posted $filename "
2013-02-21 14:59:44 +00:00
}
# Adds the code needed by the twitter button
#
# $1 the post URL
twitter( ) {
2013-12-23 19:49:31 +00:00
[ [ -z " $global_twitter_username " ] ] && return
2013-06-05 10:36:47 +00:00
2014-02-04 01:10:00 +00:00
if [ [ -z " $global_disqus_username " ] ] ; then
2013-06-05 10:36:47 +00:00
echo " <p id='twitter'> $template_comments "
else
echo " <p id='twitter'><a href=\" $1 #disqus_thread\"> $template_comments </a> "
fi
2013-09-26 09:42:03 +00:00
echo " <a href=\"https://twitter.com/share\" class=\"twitter-share-button\" data-text=\" $template_twitter_comment \" data-url=\" $1 \" "
2013-02-21 15:37:55 +00:00
2013-06-05 10:36:47 +00:00
echo " data-via=\" $global_twitter_username \" "
2013-02-21 15:37:55 +00:00
echo " > $template_twitter_button </a> <script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=\"//platform.twitter.com/widgets.js\";fjs.parentNode.insertBefore(js,fjs);}}(document,\"script\",\"twitter-wjs\");</script> "
echo "</p>"
2013-02-21 14:59:44 +00:00
}
2014-02-27 12:59:56 +00:00
# Check if the file is a 'boilerplate' (i.e. not a post)
# The return values are designed to be used like this inside a loop:
# is_boilerplate_file <file> && continue
#
# $1 the file
#
# Return 0 (bash return value 'true') if the input file is am index, feed, etc
# or 1 (bash return value 'false') if it is a blogpost
is_boilerplate_file( ) {
2014-02-27 17:07:01 +00:00
if [ [ " $1 " = = " $index_file " ] ] || [ [ " $1 " = = " $archive_index " ] ] || [ [ " $1 " = = " $tags_index " ] ] || [ [ " $1 " = = " $footer_file " ] ] || [ [ " $1 " = = " $header_file " ] ] || [ [ " $1 " = = " $global_analytics_file " ] ] || [ [ " $1 " = " $prefix_tags " * ] ] ; then return 0
2014-02-27 12:59:56 +00:00
else return 1
fi
}
2013-02-21 14:59:44 +00:00
# Adds all the bells and whistles to format the html page
# 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
# <!-- text begin --> to determine just the beginning of the text body of the post
#
# $1 a file with the body of the content
# $2 the output file
# $3 "yes" if we want to generate the index.html,
# "no" to insert new blog posts
# $4 title for the html header
# $5 original blog timestamp
create_html_page( ) {
2013-02-21 15:37:55 +00:00
content = " $1 "
filename = " $2 "
index = " $3 "
title = " $4 "
timestamp = " $5 "
# Create the actual blog post
# html, head
cat ".header.html" > " $filename "
echo " <title> $title </title> " >> " $filename "
google_analytics >> " $filename "
echo "</head><body>" >> " $filename "
2014-01-09 19:02:54 +00:00
# stuff to add before the actual body content
2014-01-17 09:03:07 +00:00
[ [ -n " $body_begin_file " ] ] && cat " $body_begin_file " >> " $filename "
2013-02-21 15:37:55 +00:00
# body divs
echo '<div id="divbodyholder">' >> " $filename "
echo '<div class="headerholder"><div class="header">' >> " $filename "
# blog title
echo '<div id="title">' >> " $filename "
cat .title.html >> " $filename "
echo '</div></div></div>' >> " $filename " # title, header, headerholder
echo '<div id="divbody"><div class="content">' >> " $filename "
file_url = " $( sed 's/.rebuilt//g' <<< $filename ) " # Get the correct URL when rebuilding
# one blog entry
2013-12-23 19:49:31 +00:00
if [ [ " $index " = = "no" ] ] ; then
2013-02-21 15:37:55 +00:00
echo '<!-- entry begin -->' >> " $filename " # marks the beginning of the whole post
2014-02-24 19:46:54 +00:00
echo '<h3><a class="ablack" href="' $file_url '">' >> " $filename "
2013-12-23 19:49:31 +00:00
# remove possible <p>'s on the title because of markdown conversion
2014-02-13 02:25:47 +00:00
echo " $( echo " $title " | sed 's/<\/*p>//g' ) " >> " $filename "
2013-02-21 15:37:55 +00:00
echo '</a></h3>' >> " $filename "
2013-12-23 19:49:31 +00:00
if [ [ " $timestamp " = = "" ] ] ; then
2013-09-26 09:29:03 +00:00
echo '<div class="subtitle">' $( LC_ALL = $date_locale date +" $date_format " ) ' — ' >> " $filename "
2013-02-21 15:37:55 +00:00
else
2013-09-26 09:29:03 +00:00
echo '<div class="subtitle">' $( LC_ALL = $date_locale date +" $date_format " --date= " $timestamp " ) ' — ' >> " $filename "
2013-02-21 15:37:55 +00:00
fi
echo " $global_author </div> " >> " $filename "
echo '<!-- text begin -->' >> " $filename " # This marks the text body, after the title, date...
fi
cat " $content " >> " $filename " # Actual content
2013-12-23 19:49:31 +00:00
if [ [ " $index " = = "no" ] ] ; then
2013-02-21 15:37:55 +00:00
echo '<!-- text end -->' >> " $filename "
2013-06-05 10:36:47 +00:00
twitter " $global_url / $file_url " >> " $filename "
2013-02-21 15:37:55 +00:00
echo '<!-- entry end -->' >> " $filename " # absolute end of the post
fi
echo '</div>' >> " $filename " # content
2013-04-09 15:09:23 +00:00
# Add disqus commments except for index and all_posts pages
2013-04-02 14:52:11 +00:00
if [ [ ${ filename %.*.* } != "index" && ${ filename %.*.* } != "all_posts" ] ] ; then
2013-04-02 21:35:33 +00:00
disqus_body >> " $filename "
2013-04-02 14:52:11 +00:00
fi
2013-02-21 15:37:55 +00:00
# page footer
cat .footer.html >> " $filename "
# close divs
echo '</div></div>' >> " $filename " # divbody and divbodyholder
2013-04-02 21:35:33 +00:00
disqus_footer >> " $filename "
2013-02-21 15:37:55 +00:00
echo '</body></html>' >> " $filename "
2013-02-21 14:59:44 +00:00
}
# Parse the plain text file into an html file
2014-03-01 16:17:52 +00:00
#
2014-03-18 21:17:34 +00:00
# $1 source file name
2014-03-01 16:17:52 +00:00
# $2 (optional) timestamp for the file
2014-03-18 21:17:34 +00:00
# $3 (optional) destination file name
# note that although timestamp is optional, something must be provided at its
# place if destination file name is provided, i.e:
# parse_file source.txt "" destination.html
2013-02-21 14:59:44 +00:00
parse_file( ) {
2013-02-21 15:37:55 +00:00
# Read for the title and check that the filename is ok
title = ""
2014-03-09 15:47:32 +00:00
while IFS = '' read line; do
2013-12-23 19:49:31 +00:00
if [ [ " $title " = = "" ] ] ; then
2014-02-13 02:45:39 +00:00
# set title and
2013-12-23 19:49:31 +00:00
# remove extra <p> and </p> added by markdown
2014-02-13 02:45:39 +00:00
title = $( echo " $line " | sed 's/<\/*p>//g' )
2014-03-18 21:17:34 +00:00
if [ " $3 " ] ; then
filename = $3
else
filename = " $( echo $title | tr [ :upper:] [ :lower:] ) "
filename = " $( echo $filename | sed 's/\ /-/g' ) "
filename = " $( echo $filename | sed 'y/йцукенгшщзхъфывапролджэячсмитьбю/jcukengsszh-fyvaproldzeahsmit-by/' ) "
filename = " $( echo $filename | sed 'y/ЙЦУКЕНГШЩЗХЪФЫВАПРОЛДЖЭЯЧСМИТЬБЮ/jcukengsszh-fyvaproldzeahsmit-by/' ) "
filename = " $( echo $filename | tr -dc '[:alnum:]-' ) " # html likes alphanumeric
filename = " $( echo $filename | sed 's/^-*//' ) " # unix utilities are unhappy if filename starts with -
[ " $filename " ] || filename = $RANDOM # if filename gets empty, put something in it
filename = " $filename .html "
# Check for duplicate file names
while [ -f " $filename " ] ; do
suffix = " $RANDOM "
filename = " $( echo $filename | sed 's/\.html/' $suffix '\.html/g' ) "
done
fi
2013-02-21 15:37:55 +00:00
content = " $filename .tmp "
2014-03-10 18:18:14 +00:00
# Parse possible tags
2014-03-01 11:30:08 +00:00
elif [ [ " $line " = " <p> $template_tags_line_header " * ] ] ; then
tags = " $( echo " $line " | cut -d ":" -f 2- | sed -e 's/<\/p>//g' -e 's/^ *//' -e 's/ *$//' -e 's/, /,/g' ) "
IFS = , read -r -a array <<< " $tags "
echo -n " <p> $template_tags_line_header " >> " $content "
( for item in " ${ array [@] } " ; do
echo -n " <a href=' $prefix_tags $item .html'> $item </a>, "
done ) | sed 's/, $//g' >> " $content "
echo -e "</p>" >> " $content "
2013-02-21 15:37:55 +00:00
else
echo " $line " >> " $content "
fi
2013-12-23 19:49:31 +00:00
done < " $1 "
2013-02-21 15:37:55 +00:00
# Create the actual html page
2014-03-01 16:17:52 +00:00
create_html_page " $content " " $filename " no " $title " " $2 "
2013-02-21 15:37:55 +00:00
rm " $content "
2013-02-21 14:59:44 +00:00
}
# Manages the creation of the text file and the parsing to html file
# also the drafts
write_entry( ) {
2013-12-23 19:49:31 +00:00
fmt = "html" ; f = " $2 "
[ [ " $2 " = = "-m" ] ] && fmt = "md" && f = " $3 "
2013-12-23 20:09:22 +00:00
if [ [ " $fmt " = = "md" ] ] ; then
test_markdown
if [ [ " $? " -ne 0 ] ] ; then
echo "Markdown is not working, please use HTML. Press a key to continue..."
fmt = "html"
read
fi
fi
2013-12-23 19:49:31 +00:00
if [ [ " $f " != "" ] ] ; then
TMPFILE = " $f "
if [ [ ! -f " $TMPFILE " ] ] ; then
2013-02-21 15:37:55 +00:00
echo "The file doesn't exist"
delete_includes
exit
fi
2013-12-23 19:49:31 +00:00
# check if TMPFILE is markdown even though the user didn't specify it
extension = " ${ TMPFILE ##*. } "
[ [ " $extension " = = "md" ] ] && fmt = "md"
2013-02-21 15:37:55 +00:00
else
2013-12-23 19:49:31 +00:00
TMPFILE = " .entry- $RANDOM . $fmt "
2014-02-27 15:46:37 +00:00
echo -e "Title on this line\n" >> " $TMPFILE "
[ [ " $fmt " = = "html" ] ] && cat << EOF >> "$TMPFILE "
<p>The rest of the text file is an <b>html</b> blog post. The process will continue as soon
as you exit your editor.</p>
2014-02-27 20:28:20 +00:00
<p>$template_tags_line_header keep-this-tag-format, tags-are-optional, example</p>
2014-02-27 15:46:37 +00:00
EOF
[ [ " $fmt " = = "md" ] ] && cat << EOF >> "$TMPFILE "
The rest of the text file is a **Markdown** blog post. The process will continue
as soon as you exit your editor.
2014-02-27 20:28:20 +00:00
$template_tags_line_header keep-this-tag-format, tags-are-optional, beware-with-underscores-in-markdown, example
2014-02-27 15:46:37 +00:00
EOF
2013-02-21 15:37:55 +00:00
fi
chmod 600 " $TMPFILE "
post_status = "E"
while [ " $post_status " != "p" ] && [ " $post_status " != "P" ] ; do
$EDITOR " $TMPFILE "
2013-12-23 19:49:31 +00:00
if [ [ " $fmt " = = "md" ] ] ; then
html_from_md = " $( markdown " $TMPFILE " ) "
parse_file " $html_from_md "
rm " $html_from_md "
else
parse_file " $TMPFILE " # this command sets $filename as the html processed file
fi
2013-02-21 15:37:55 +00:00
chmod 600 " $filename "
echo -n "Preview? (Y/n) "
read p
2013-12-23 19:49:31 +00:00
if [ [ " $p " != "n" ] ] && [ [ " $p " != "N" ] ] ; then
2013-02-21 15:37:55 +00:00
chmod 644 " $filename "
echo " Open $global_url / $filename in your browser "
fi
echo -n "[P]ost this entry, [E]dit again, [D]raft for later? (p/E/d) "
read post_status
2013-12-23 19:49:31 +00:00
if [ [ " $post_status " = = "d" ] ] || [ [ " $post_status " = = "D" ] ] ; then
2013-02-21 15:37:55 +00:00
mkdir -p "drafts/"
chmod 700 "drafts/"
title = " $( head -n 1 $TMPFILE ) "
title = " $( echo $title | tr [ :upper:] [ :lower:] ) "
title = " $( echo $title | sed 's/\ /-/g' ) "
title = " $( echo $title | tr -dc '[:alnum:]-' ) "
2013-12-23 19:49:31 +00:00
draft = " drafts/ $title . $fmt "
while [ -f " $draft " ] ; do draft = " drafts/ $title - $RANDOM . $fmt " ; done
2013-02-21 15:37:55 +00:00
mv " $TMPFILE " " $draft "
chmod 600 " $draft "
rm " $filename "
delete_includes
echo " Saved your draft as ' $draft ' "
exit
fi
2013-12-23 19:49:31 +00:00
if [ [ " $post_status " = = "e" ] ] || [ [ " $post_status " = = "E" ] ] ; then
2013-02-21 15:37:55 +00:00
rm " $filename " # Delete the html file as it will be generated again
fi
done
rm " $TMPFILE "
chmod 644 " $filename "
echo " Posted $filename "
2013-02-21 14:59:44 +00:00
}
# Create an index page with all the posts
all_posts( ) {
2013-02-21 15:37:55 +00:00
echo -n "Creating an index page with all the posts "
contentfile = " $archive_index . $RANDOM "
while [ -f " $contentfile " ] ; do
contentfile = " $archive_index . $RANDOM "
done
2014-02-24 17:05:55 +00:00
echo " <h3> $template_archive_title </h3> " >> " $contentfile "
2013-02-21 15:37:55 +00:00
echo "<ul>" >> " $contentfile "
for i in $( ls -t *.html) ; do
2014-02-27 12:59:56 +00:00
is_boilerplate_file " $i " && continue
2013-02-21 15:37:55 +00:00
echo -n "."
# Title
2014-02-27 12:59:56 +00:00
title = " $( get_post_title " $i " ) "
2014-02-24 19:46:54 +00:00
echo -n '<li><a href="' $i '">' $title '</a> —' >> " $contentfile "
2013-02-21 15:37:55 +00:00
# Date
2013-09-26 09:29:03 +00:00
date = " $( LC_ALL = $date_locale date -r " $i " +" $date_format " ) "
2013-02-21 15:37:55 +00:00
echo " $date </li> " >> " $contentfile "
done
echo ""
echo "</ul>" >> " $contentfile "
2014-02-24 19:46:54 +00:00
echo '<div id="all_posts"><a href="' ./'">' $template_archive_index_page '</a></div>' >> " $contentfile "
2013-02-21 15:37:55 +00:00
create_html_page " $contentfile " " $archive_index .tmp " yes " $global_title — All posts "
mv " $archive_index .tmp " " $archive_index "
chmod 644 " $archive_index "
rm " $contentfile "
2013-02-21 14:59:44 +00:00
}
2014-02-27 17:07:01 +00:00
# Create an index page with all the tags
all_tags( ) {
echo -n "Creating an index page with all the tags "
contentfile = " $tags_index . $RANDOM "
while [ -f " $contentfile " ] ; do
contentfile = " $tags_index . $RANDOM "
done
echo " <h3> $template_tags_title </h3> " >> " $contentfile "
echo "<ul>" >> " $contentfile "
2014-02-27 20:28:20 +00:00
for i in $( ls $prefix_tags *.html) ; do
2014-02-27 17:07:01 +00:00
echo -n "."
2014-03-01 22:01:47 +00:00
nposts = " $( grep -c "<\!-- text begin -->" $i ) "
2014-02-27 17:07:01 +00:00
tagname = " $( echo $i | cut -c $(( ${# prefix_tags } + 1 )) - | sed 's/\.html//g' ) "
2014-02-27 20:28:20 +00:00
echo "<li><a href=" $i " > $tagname </a> — $nposts $template_tags_posts </li> " >> " $contentfile "
2014-02-27 17:07:01 +00:00
done
echo ""
echo "</ul>" >> " $contentfile "
echo '<div id="all_posts"><a href="' ./'">' $template_archive_index_page '</a></div>' >> " $contentfile "
create_html_page " $contentfile " " $tags_index .tmp " yes " $global_title — $template_tags_title "
mv " $tags_index .tmp " " $tags_index "
chmod 644 " $tags_index "
rm " $contentfile "
}
2013-02-21 15:58:05 +00:00
# Generate the index.html with the content of the latest posts
2013-02-21 14:59:44 +00:00
rebuild_index( ) {
2013-02-21 15:37:55 +00:00
echo -n "Rebuilding the index "
newindexfile = " $index_file . $RANDOM "
contentfile = " $newindexfile .content "
while [ -f " $newindexfile " ] ; do
newindexfile = " $index_file . $RANDOM "
contentfile = " $newindexfile .content "
done
2013-02-21 15:58:05 +00:00
# Create the content file
2013-02-21 15:37:55 +00:00
n = 0
for i in $( ls -t *.html) ; do # sort by date, newest first
2014-02-27 12:59:56 +00:00
is_boilerplate_file " $i " && continue ;
2013-12-23 19:49:31 +00:00
if [ [ " $n " -ge " $number_of_index_articles " ] ] ; then break; fi
2014-02-27 21:53:00 +00:00
if [ " $cut_do " ] ; then
get_html_file_content 'entry' 'entry' 'cut' <$i | awk '/' " $cut_line " '/ { print "<p class=\"readmore\"><a href=\"' $i '\">' " $template_read_more " '</a></p>" ; next } 1' >> " $contentfile "
else
get_html_file_content 'entry' 'entry' <$i >> " $contentfile "
fi
2013-02-21 15:37:55 +00:00
echo -n "."
n = $(( $n + 1 ))
done
2014-02-27 17:07:01 +00:00
feed = " $blog_feed "
if [ [ " $global_feedburner " != "" ] ] ; then feed = " $global_feedburner " ; fi
echo '<div id="all_posts"><a href="' $archive_index '">' $template_archive '</a> — <a href="' $tags_index '">' $template_tags_title '</a> — <a href="' $feed '">' $template_subscribe '</a></div>' >> " $contentfile "
2013-02-21 15:37:55 +00:00
echo ""
create_html_page " $contentfile " " $newindexfile " yes " $global_title "
rm " $contentfile "
mv " $newindexfile " " $index_file "
chmod 644 " $index_file "
2013-02-21 14:59:44 +00:00
}
2014-02-27 15:46:37 +00:00
# Rebuilds all tag_*.html files
2014-02-27 12:59:56 +00:00
rebuild_tags( ) {
echo -n "Rebuilding tag pages "
n = 0
2014-03-01 22:01:47 +00:00
rm $prefix_tags *.html & > /dev/null
2014-02-27 15:46:37 +00:00
# First we will process all files and create temporal tag files
# with just the content of the posts
2014-02-27 12:59:56 +00:00
for i in $( ls -t *.html) ; do
2014-02-27 15:46:37 +00:00
is_boilerplate_file " $i " && continue ;
echo -n "."
tmpfile = " $( mktemp tmp.XXX) "
2014-03-01 22:01:47 +00:00
if [ " $cut_do " ] ; then
get_html_file_content 'entry' 'entry' 'cut' <$i | awk '/' " $cut_line " '/ { print "<p class=\"readmore\"><a href=\"' $i '\">' " $template_read_more " '</a></p>" ; next } 1' >> " $tmpfile "
else
get_html_file_content 'entry' 'entry' <$i >> " $tmpfile "
fi
2014-03-09 15:47:32 +00:00
while IFS = '' read line; do
2014-02-27 20:28:20 +00:00
if [ [ " $line " = " <p> $template_tags_line_header " * ] ] ; then
2014-02-27 15:46:37 +00:00
# 'split' tags by commas
echo " $line " | cut -c 10- | while IFS = "," read -a tags; do
for dirty_tag in " ${ tags [@] } " ; do # extract html around it
tag = " $( echo $dirty_tag | grep -o ">.*</a>" | awk '{print substr($0, 2, length($0)-5)}' | tr " " "_" ) "
# Add the content of this post to the tag file
cat " $tmpfile " >> " $prefix_tags $tag " .tmp.html
done
done
fi
2014-03-01 22:01:47 +00:00
done < " $i "
2014-02-27 15:46:37 +00:00
rm " $tmpfile "
done
# Now generate the tag files with headers, footers, etc
for i in $( ls -t $prefix_tags *.tmp.html) ; do
tagname = " $( echo $i | cut -c $(( ${# prefix_tags } + 1 )) - | sed 's/\.tmp\.html//g' ) "
2014-02-27 20:28:20 +00:00
create_html_page " $i " " $prefix_tags $tagname .html " yes " $global_title — $template_tag_title \" $tagname \" "
2014-02-27 15:46:37 +00:00
rm " $i "
2014-02-27 12:59:56 +00:00
done
2014-02-27 15:46:37 +00:00
echo
2014-02-27 12:59:56 +00:00
}
# Return the post title
#
# $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 "
}
2013-02-21 14:59:44 +00:00
# Displays a list of the posts
list_posts( ) {
2013-03-13 08:16:44 +00:00
ls *.html & > /dev/null
2013-12-23 19:49:31 +00:00
[ [ $? -ne 0 ] ] && echo "No posts yet. Use 'bb.sh post' to create one" && return
2013-03-13 08:16:44 +00:00
2013-02-21 15:37:55 +00:00
lines = ""
n = 1
for i in $( ls -t *.html) ; do
2014-02-27 12:59:56 +00:00
is_boilerplate_file " $i " && continue
line = " $n # $( get_post_title " $i " ) # $( LC_ALL = $date_locale date -r $i +" $date_format " ) "
2013-02-21 15:37:55 +00:00
lines = " ${ lines } " " $line " "\n" # Weird stuff needed for the newlines
n = $(( $n + 1 ))
done
echo -e " $lines " | column -t -s "#"
2013-02-21 14:59:44 +00:00
}
# Generate the feed file
make_rss( ) {
2013-02-21 15:37:55 +00:00
echo -n "Making RSS "
rssfile = " $blog_feed . $RANDOM "
while [ -f " $rssfile " ] ; do rssfile = " $blog_feed . $RANDOM " ; done
echo '<?xml version="1.0" encoding="UTF-8" ?>' >> " $rssfile "
echo '<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">' >> " $rssfile "
echo '<channel><title>' $global_title '</title><link>' $global_url '</link>' >> " $rssfile "
echo '<description>' $global_description '</description><language>en</language>' >> " $rssfile "
2013-06-07 00:01:22 +00:00
echo '<lastBuildDate>' $( date +"%a, %d %b %Y %H:%M:%S %z" ) '</lastBuildDate>' >> " $rssfile "
echo '<pubDate>' $( date +"%a, %d %b %Y %H:%M:%S %z" ) '</pubDate>' >> " $rssfile "
2013-02-21 15:37:55 +00:00
echo '<atom:link href="' $global_url /$blog_feed '" rel="self" type="application/rss+xml" />' >> " $rssfile "
n = 0
for i in $( ls -t *.html) ; do
2014-02-27 12:59:56 +00:00
is_boilerplate_file " $i " && continue
2013-12-23 19:49:31 +00:00
[ [ " $n " -ge " $number_of_feed_articles " ] ] && break # max 10 items
2013-02-21 15:37:55 +00:00
echo -n "."
echo '<item><title>' >> " $rssfile "
2014-02-27 21:01:01 +00:00
echo " $( get_post_title " $i " ) " >> " $rssfile "
2013-02-21 15:37:55 +00:00
echo '</title><description><![CDATA[' >> " $rssfile "
2014-02-27 21:53:00 +00:00
echo " $( get_html_file_content 'text' 'entry' $cut_do <$i ) " >> " $rssfile "
2013-02-21 15:37:55 +00:00
echo " ]]></description><link> $global_url / $i </link> " >> " $rssfile "
echo " <guid> $global_url / $i </guid> " >> " $rssfile "
echo " <dc:creator> $global_author </dc:creator> " >> " $rssfile "
2013-06-07 00:01:22 +00:00
echo '<pubDate>' $( date -r " $i " +"%a, %d %b %Y %H:%M:%S %z" ) '</pubDate></item>' >> " $rssfile "
2013-02-21 15:37:55 +00:00
n = $(( $n + 1 ))
done
echo '</channel></rss>' >> " $rssfile "
echo ""
mv " $rssfile " " $blog_feed "
chmod 644 " $blog_feed "
2013-02-21 14:59:44 +00:00
}
# generate headers, footers, etc
create_includes( ) {
2014-01-09 19:02:54 +00:00
echo '<h1 class="nomargin"><a class="ablack" href="' $global_url '">' $global_title '</a></h1>' > ".title.html"
echo '<div id="description">' $global_description '</div>' >> ".title.html"
2014-01-09 18:50:27 +00:00
if [ [ -f " $header_file " ] ] ; then cp " $header_file " .header.html
else
echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">' > ".header.html"
echo '<html xmlns="http://www.w3.org/1999/xhtml"><head>' >> ".header.html"
echo '<meta http-equiv="Content-type" content="text/html;charset=UTF-8" />' >> ".header.html"
2014-03-01 15:05:25 +00:00
for css_file in ${ css_include [*] } ; do
echo '<link rel="stylesheet" href="' $css_file '" type="text/css" />' >> ".header.html"
done
2014-01-09 18:50:27 +00:00
if [ [ " $global_feedburner " = = "" ] ] ; then
echo '<link rel="alternate" type="application/rss+xml" title="' $template_subscribe_browser_button '" href="' $blog_feed '" />' >> ".header.html"
else
echo '<link rel="alternate" type="application/rss+xml" title="' $template_subscribe_browser_button '" href="' $global_feedburner '" />' >> ".header.html"
fi
2013-02-21 15:37:55 +00:00
fi
2014-01-09 18:50:27 +00:00
if [ [ -f " $footer_file " ] ] ; then cp " $footer_file " .footer.html
else
protected_mail = " $( echo " $global_email " | sed 's/@/\@/g' | sed 's/\./\./g' ) "
2014-01-17 09:03:07 +00:00
echo '<div id="footer">' $global_license '<a href="' $global_author_url '">' $global_author '</a> — <a href="mailto:' $protected_mail '">' $protected_mail '</a><br/>' >> ".footer.html"
echo '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"
2014-01-09 18:50:27 +00:00
fi
2013-02-21 14:59:44 +00:00
}
# Delete the temporarily generated include files
delete_includes( ) {
2013-02-21 15:37:55 +00:00
rm ".title.html" ".footer.html" ".header.html"
2013-02-21 14:59:44 +00:00
}
# Create the css file from scratch
create_css( ) {
2013-02-21 15:37:55 +00:00
# To avoid overwriting manual changes. However it is recommended that
# this function is modified if the user changes the blog.css file
2014-03-01 15:22:28 +00:00
[ $css_include ] && return || css_include = ( 'main.css' 'blog.css' )
2013-12-23 19:49:31 +00:00
if [ [ ! -f "blog.css" ] ] ; then
2013-02-21 15:37:55 +00:00
# blog.css directives will be loaded after main.css and thus will prevail
echo ' #title{font-size: x-large;}
a.ablack{ color:black !important; }
li{ margin-bottom:8px; }
ul,ol{ margin-left:24px; margin-right:24px; }
#all_posts{margin-top:24px;text-align:center;}
.subtitle{ font-size:small; margin:12px 0px; }
.content p{ margin-left:24px; margin-right:24px; }
h1{ margin-bottom:12px !important; }
#description{font-size:large;margin-bottom:12px;}
h3{ margin-top:42px; margin-bottom:8px; }
h4{ margin-left:24px; margin-right:24px; }
#twitter{line-height:20px;vertical-align:top;text-align:right;font-style:italic;color:#333;margin-top:24px;font-size:14px;}' > blog.css
fi
2013-12-23 19:49:31 +00:00
# If there is a style.css from the parent page (i.e. some landing page)
# then use it. This directive is here for compatibility with my own
# home page. Feel free to edit it out, though it doesn't hurt
if [ [ -f "../style.css" ] ] && [ [ ! -f "main.css" ] ] ; then
ln -s "../style.css" "main.css"
elif [ [ ! -f "main.css" ] ] ; then
2013-02-21 15:37:55 +00:00
echo ' body{ font-family:Georgia,"Times New Roman" ,Times,serif; margin:0; padding:0; background-color:#F3F3F3; }
#divbodyholder{padding:5px;background-color:#DDD;width:874px;margin:24px auto;}
#divbody{width:776px;border:solid 1px #ccc;background-color:#fff;padding:0px 48px 24px 48px;top:0;}
.headerholder{ background-color:#f9f9f9; border-top:solid 1px #ccc;border-left:solid 1px #ccc;border-right:solid 1px #ccc;}
.header{ width:800px; margin:0px auto; padding-top:24px; padding-bottom:8px; }
.content{ margin-bottom:45px; }
.nomargin{ margin:0; }
.description{ margin-top:10px; border-top:solid 1px #666;padding:10px 0;}
h3{ font-size:20pt; width:100%; font-weight:bold; margin-top:32px; margin-bottom:0; }
.clear{ clear:both; }
#footer{padding-top:10px;border-top:solid 1px #666;color:#333333;text-align:center;font-size:small;font-family:"Courier New","Courier",monospace;}
a{ text-decoration:none; color:#003366 !important; }
a:visited{ text-decoration:none; color:#336699 !important; }
blockquote{ background-color:#f9f9f9; border-left:solid 4px #e9e9e9;margin-left:12px;padding:12px 12px 12px 24px;}
blockquote img{ margin:12px 0px; }
blockquote iframe{ margin:12px 0px; } ' > main.css
fi
2013-02-21 14:59:44 +00:00
}
# Regenerates all the single post entries, keeping the post content but modifying
# the title, html structure, etc
rebuild_all_entries( ) {
2013-02-21 15:37:55 +00:00
echo -n "Rebuilding all entries "
for i in *.html; do # no need to sort
2014-02-27 12:59:56 +00:00
is_boilerplate_file " $i " && continue ;
2013-02-21 15:37:55 +00:00
contentfile = " .tmp. $RANDOM "
while [ -f " $contentfile " ] ; do contentfile = " .tmp. $RANDOM " ; done
echo -n "."
# Get the title and entry, and rebuild the html structure from scratch (divs, title, description...)
2014-02-27 12:59:56 +00:00
title = " $( get_post_title " $i " ) "
2014-02-26 21:21:51 +00:00
get_html_file_content 'text' 'text' <$i >> " $contentfile "
2013-02-21 15:37:55 +00:00
# Original post timestamp
2014-01-09 19:19:46 +00:00
timestamp = " $( LC_ALL = $date_locale date -r $i +"%a, %d %b %Y %H:%M:%S %z" ) "
2013-02-21 15:37:55 +00:00
create_html_page " $contentfile " " $i .rebuilt " no " $title " " $timestamp "
# keep the original timestamp!
2014-01-09 19:19:46 +00:00
timestamp = " $( LC_ALL = $date_locale date -r $i +'%Y%m%d%H%M' ) "
2013-02-21 15:37:55 +00:00
mv " $i .rebuilt " " $i "
chmod 644 " $i "
touch -t $timestamp " $i "
rm " $contentfile "
done
echo ""
2013-02-21 14:59:44 +00:00
}
# Displays the help
function usage( ) {
2013-02-21 15:37:55 +00:00
echo " $global_software_name v $global_software_version "
echo " Usage: $0 command [filename] "
echo ""
echo "Commands:"
2014-02-27 17:14:14 +00:00
echo " post [-m] [filename] insert a new blog post, or the filename of a draft to continue editing it"
2013-12-23 19:49:31 +00:00
echo " use '-m' to edit the post as Markdown text"
2014-03-09 10:49:18 +00:00
echo " edit [-n|-f] [filename] edit an already published .html file. **NEVER** edit manually a published .html file,"
2014-02-27 17:14:14 +00:00
echo " always use this function as it keeps internal data and rebuilds the blog"
2014-03-09 10:49:18 +00:00
echo " use '-n' to give the file a new name, if title was changed"
echo " use '-f' to edit full html file, instead of just text part (also preserves name)"
2014-02-27 17:14:14 +00:00
echo " delete [filename] deletes the post and rebuilds the blog"
2013-12-23 19:49:31 +00:00
echo " rebuild regenerates all the pages and posts, preserving the content of the entries"
2014-02-27 17:14:14 +00:00
echo " reset deletes everything except this script. Use with a lot of caution and back up first!"
echo " list list all posts"
2013-02-21 15:37:55 +00:00
echo ""
echo " For more information please open $0 in a code editor and read the header and comments "
2013-02-21 14:59:44 +00:00
}
# Delete all generated content, leaving only this script
reset( ) {
2013-02-21 15:37:55 +00:00
echo "Are you sure you want to delete all blog entries? Please write \"Yes, I am!\" "
read line
2013-12-23 19:49:31 +00:00
if [ [ " $line " = = "Yes, I am!" ] ] ; then
2013-03-13 08:16:44 +00:00
rm .*.html *.html *.css *.rss & > /dev/null
echo
2013-02-21 15:37:55 +00:00
echo "Deleted all posts, stylesheets and feeds."
2013-03-13 08:16:44 +00:00
echo "Kept your old '.backup.tar.gz' just in case, please delete it manually if needed."
2013-02-21 15:37:55 +00:00
else
echo "Phew! You dodged a bullet there. Nothing was modified."
fi
2013-02-21 14:59:44 +00:00
}
2013-06-05 10:36:47 +00:00
# Detects if GNU date is installed
2013-04-02 14:40:10 +00:00
date_version_detect( ) {
2013-04-09 15:09:23 +00:00
date --version >/dev/null 2>& 1
2013-12-23 19:49:31 +00:00
if [ [ $? -ne 0 ] ] ; then
2013-04-02 14:40:10 +00:00
# date utility is BSD. Test if gdate is installed
if gdate --version >/dev/null 2>& 1 ; then
2013-06-05 10:36:47 +00:00
date( ) {
gdate " $@ "
}
2013-04-02 14:40:10 +00:00
else
2013-12-23 19:49:31 +00:00
# BSD date
date( ) {
if [ [ " $1 " = = "-r" ] ] ; then
# Fall back to using stat for 'date -r'
format = $( echo $3 | sed 's/\+//g' )
stat -f "%Sm" -t " $format " " $2 "
elif [ [ $( echo $@ | grep '\-\-date' ) ] ] ; then
# convert between dates using BSD date syntax
2014-01-09 19:19:46 +00:00
/bin/date -j -f "%a, %d %b %Y %H:%M:%S %z" " $( echo $2 | sed 's/\-\-date\=//g' ) " " $1 "
2013-12-23 19:49:31 +00:00
else
# acceptable format for BSD date
/bin/date -j " $@ "
fi
}
fi
fi
2013-04-02 14:40:10 +00:00
}
2013-04-09 15:25:35 +00:00
2013-02-21 14:59:44 +00:00
# Main function
2013-03-07 20:51:54 +00:00
# Encapsulated on its own function for readability purposes
2013-02-21 14:59:44 +00:00
#
# $1 command to run
# $2 file name of a draft to continue editing (optional)
do_main( ) {
2013-04-02 14:40:10 +00:00
# Detect if using BSD date or GNU date
date_version_detect
2013-06-05 10:36:47 +00:00
# Load default configuration, then override settings with the config file
global_variables
[ [ -f " $global_config " ] ] && source " $global_config " & > /dev/null
2014-01-09 18:50:27 +00:00
global_variables_check
2013-02-21 15:37:55 +00:00
2013-03-07 18:30:59 +00:00
# Check for $EDITOR
2013-12-23 19:49:31 +00:00
[ [ -z " $EDITOR " ] ] &&
echo "Please set your \$EDITOR environment variable" && exit
2013-03-07 18:30:59 +00:00
2013-02-21 15:37:55 +00:00
# Check for validity of argument
2014-02-27 17:07:01 +00:00
[ [ " $1 " != "reset" ] ] && [ [ " $1 " != "post" ] ] && [ [ " $1 " != "rebuild" ] ] && [ [ " $1 " != "list" ] ] && [ [ " $1 " != "edit" ] ] && [ [ " $1 " != "delete" ] ] &&
2013-12-23 19:49:31 +00:00
usage && exit
2013-02-21 15:37:55 +00:00
2013-12-23 19:49:31 +00:00
[ [ " $1 " = = "list" ] ] &&
list_posts && exit
2013-02-21 15:37:55 +00:00
2013-03-07 17:54:33 +00:00
if [ [ " $1 " = = "edit" ] ] ; then
2014-03-09 10:49:18 +00:00
if [ [ $# -lt 2 ] ] || [ [ ! -f " ${ !# } " ] ] ; then
2013-03-07 17:54:33 +00:00
echo "Please enter a valid html file to edit"
exit
fi
fi
2013-02-21 15:37:55 +00:00
# Test for existing html files
ls *.html & > /dev/null
2013-12-23 19:49:31 +00:00
[ [ $? -ne 0 ] ] && [ [ " $1 " = = "rebuild" ] ] &&
echo "Can't find any html files, nothing to rebuild" && exit
2013-02-21 15:37:55 +00:00
# We're going to back up just in case
2013-03-13 08:16:44 +00:00
ls *.html & > /dev/null
2013-12-23 19:49:31 +00:00
[ [ $? -eq 0 ] ] &&
tar cfz ".backup.tar.gz" *.html &&
2013-04-02 14:40:10 +00:00
chmod 600 ".backup.tar.gz"
2013-02-21 15:37:55 +00:00
2014-03-01 16:37:57 +00:00
# Keep first backup of this day containing yesterday's version of the blog
[ " $( date -r .yesterday.tar.gz +'%d' ) " != " $( date +'%d' ) " ] &&
cp .backup.tar.gz .yesterday.tar.gz & > /dev/null
2013-12-23 19:49:31 +00:00
[ [ " $1 " = = "reset" ] ] &&
reset && exit
2013-02-21 15:37:55 +00:00
create_css
2014-03-01 15:22:28 +00:00
create_includes
2013-12-23 19:49:31 +00:00
[ [ " $1 " = = "post" ] ] && write_entry " $@ "
[ [ " $1 " = = "rebuild" ] ] && rebuild_all_entries
2014-02-27 17:07:01 +00:00
[ [ " $1 " = = "delete" ] ] && rm " $2 " & > /dev/null
2014-03-09 10:49:18 +00:00
if [ [ " $1 " = = "edit" ] ] ; then
if [ [ " $2 " = = "-n" ] ] ; then
edit " $3 "
elif [ [ " $2 " = = "-f" ] ] ; then
edit " $3 " full
else
edit " $2 " keep
fi
fi
2013-02-21 15:37:55 +00:00
rebuild_index
all_posts
2014-02-27 17:11:47 +00:00
rebuild_tags
2014-02-27 17:07:01 +00:00
all_tags
2013-02-21 15:37:55 +00:00
make_rss
delete_includes
2013-02-21 14:59:44 +00:00
}
#
# MAIN
# Do not change anything here. If you want to modify the code, edit do_main()
#
do_main $*
2014-03-01 15:35:00 +00:00
2014-03-18 20:40:50 +00:00
# vim: set shiftwidth=4 tabstop=4 expandtab: