2013-02-21 16:59:44 +02:00
#!/usr/bin/env bash
# BashBlog, a simple blog system written in a single bash script
2016-01-14 17:01:22 +02:00
# (C) Carlos Fenollosa <carlos.fenollosa@gmail.com>, 2011-2016 and contributors
2013-06-05 13:39:52 +03:00
# https://github.com/carlesfe/bashblog/contributors
2014-09-01 17:13:06 +03:00
# Check out README.md for more details
2013-02-21 16:59:44 +02:00
# Global variables
# It is recommended to perform a 'rebuild' after changing any of this in the code
2013-03-13 15:48:44 +02:00
2013-06-05 13:36:47 +03: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 15:48:44 +02:00
global_config = ".config"
2014-06-26 05:25:48 +03:00
# This function will load all the variables defined here. They might be overridden
2013-06-05 13:36:47 +03:00
# by the 'global_config' file contents
2013-02-21 16:59:44 +02:00
global_variables( ) {
2013-02-21 17:37:55 +02:00
global_software_name = "BashBlog"
2021-11-04 22:09:13 +03:00
global_software_version = "2.10"
2013-02-21 17:37:55 +02:00
# Blog title
2013-02-21 18:26:10 +02:00
global_title = "My fancy blog"
2013-02-21 17:37:55 +02:00
# The typical subtitle for each blog
2013-02-21 18:26:10 +02:00
global_description = "A blog about turtles and carrots"
2013-02-21 17:37:55 +02:00
# The public base URL for this blog
2013-02-21 18:26:10 +02:00
global_url = "http://example.com/blog"
2013-02-21 17:37:55 +02:00
# Your name
2013-02-21 18:26:10 +02:00
global_author = "John Smith"
2013-02-21 17:37:55 +02:00
# You can use twitter or facebook or anything for global_author_url
2013-02-21 18:26:10 +02:00
global_author_url = "http://twitter.com/example"
2013-02-21 17:37:55 +02:00
# Your email
2013-02-21 18:26:10 +02:00
global_email = "john@smith.com"
2013-02-21 17:37:55 +02: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 18:34:41 +02: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 18:26:10 +02:00
global_analytics = ""
2014-02-18 18:34:41 +02:00
global_analytics_file = ""
2013-02-21 17:37:55 +02:00
# Leave this empty (i.e. "") if you don't want to use feedburner,
# or change it to your own URL
2013-02-21 18:26:10 +02:00
global_feedburner = ""
2013-02-21 17:37:55 +02:00
2013-06-05 13:36:47 +03:00
# Change this to your username if you want to use twitter for comments
global_twitter_username = ""
2021-11-04 21:39:11 +03:00
# Default image for the Twitter cards. Use an absolute URL
global_twitter_card_image = ""
2014-07-07 18:11:14 +03:00
# Set this to false for a Twitter button with share count. The cookieless version
# is just a link.
global_twitter_cookieless = "true"
2016-02-23 21:18:47 +02:00
# Default search page, where tweets more than a week old are hidden
2015-12-21 20:49:34 +02:00
global_twitter_search = "twitter"
2013-02-21 16:59:44 +02:00
2013-06-05 13:36:47 +03:00
# Change this to your disqus username to use disqus for comments
global_disqus_username = ""
2013-04-03 00:35:33 +03:00
2013-02-21 16:59:44 +02:00
2013-02-21 18:06:02 +02: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 19:07:01 +02:00
tags_index = "all_tags.html"
2016-10-24 17:23:53 +03:00
# Non blogpost files. Bashblog will ignore these. Useful for static pages and custom content
# Add them as a bash array, e.g. non_blogpost_files=("news.html" "test.html")
non_blogpost_files = ( )
2013-02-21 18:06:02 +02:00
# feed file (rss in this case)
2013-02-21 18:16:11 +02:00
blog_feed = "feed.rss"
2013-02-21 18:06:02 +02:00
number_of_feed_articles = "10"
2014-09-01 17:13:06 +03:00
# "cut" blog entry when putting it to index page. Leave blank for full articles in front page
# i.e. include only up to first '<hr>', or '----' in markdown
2014-02-27 23:53:00 +02:00
cut_do = "cut"
2014-09-15 20:23:12 +03:00
# When cutting, cut also tags? If "no", tags will appear in index page for cut articles
2014-09-01 17:13:06 +03:00
cut_tags = "yes"
2014-02-27 23:40:11 +02: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-09-01 17:13:06 +03:00
# save markdown file when posting with "bb post -m". Leave blank to discard it.
2014-03-22 16:14:01 +02:00
save_markdown = "yes"
2014-02-27 17:46:37 +02:00
# prefix for tags/categories files
2014-02-27 14:59:56 +02:00
# please make sure that no other html file starts with this prefix
prefix_tags = "tag_"
2014-01-09 20:50:27 +02: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 21:02:54 +02:00
# extra content to add just after we open the <body> tag
# and before the actual blog content
body_begin_file = ""
2020-05-10 17:22:42 +03:00
# extra content to add just before we close </body>
2016-12-30 07:36:01 +03:00
body_end_file = ""
2020-05-10 17:22:42 +03:00
# extra content to ONLY on the index page AFTER `body_begin_file` contents
# and before the actual content
body_begin_file_index = ""
2014-03-01 17:22:28 +02:00
# CSS files to include on every page, f.ex. css_include=('main.css' 'blog.css')
# leave empty to use generated
css_include = ( )
2015-02-19 10:33:29 +02:00
# HTML files to exclude from index, f.ex. post_exclude=('imprint.html 'aboutme.html')
html_exclude = ( )
2013-02-21 18:06:02 +02:00
# Localization and i18n
# "Comments?" (used in twitter link after every post)
template_comments = "Comments?"
2014-02-27 00:22:26 +02:00
# "Read more..." (link under cut article on index page)
template_read_more = "Read more..."
2013-02-21 18:06:02 +02:00
# "View more posts" (used on bottom of index page as link to archive)
template_archive = "View more posts"
2014-02-24 19:05:55 +02:00
# "All posts" (title of archive page)
template_archive_title = "All posts"
2014-02-27 19:07:01 +02:00
# "All tags"
template_tags_title = "All tags"
2014-02-27 22:28:20 +02:00
# "posts" (on "All tags" page, text at the end of each tag line, like "2. Music - 15 posts")
template_tags_posts = "posts"
2016-10-24 12:16:01 +03:00
template_tags_posts_2_4 = "posts" # Some slavic languages use a different plural form for 2-4 items
2016-01-14 17:39:56 +02:00
template_tags_posts_singular = "post"
2014-02-27 22:28:20 +02:00
# "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 18:06:02 +02: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 09:17:35 +02:00
template_twitter_comment = "<Type your comment here but please leave the URL so that other people can follow the comments>"
2016-04-26 17:16:35 +03:00
# The locale to use for the dates displayed on screen
2013-02-21 18:06:02 +02:00
date_format = "%B %d, %Y"
date_locale = "C"
2016-04-26 17:16:35 +03:00
date_inpost = "bashblog_timestamp"
# Don't change these dates
date_format_full = "%a, %d %b %Y %H:%M:%S %z"
date_format_timestamp = "%Y%m%d%H%M.%S"
date_allposts_header = "%B %Y"
2013-12-23 21:49:31 +02:00
2014-06-05 19:51:28 +03:00
# Perform the post title -> filename conversion
# Experts only. You may need to tune the locales too
# Leave empty for no conversion, which is not recommended
# This default filter respects backwards compatibility
2014-06-06 13:02:08 +03:00
convert_filename = "iconv -f utf-8 -t ascii//translit | sed 's/^-*//' | tr [:upper:] [:lower:] | tr ' ' '-' | tr -dc '[:alnum:]-'"
2014-06-05 19:51:28 +03:00
2014-06-22 09:26:39 +03:00
# URL where you can view the post while it's being edited
# same as global_url by default
# You can change it to path on your computer, if you write posts locally
# before copying them to the server
preview_url = ""
2013-12-23 22:01:01 +02:00
# Markdown location. Trying to autodetect by default.
2014-05-19 22:08:55 +03:00
# The invocation must support the signature 'markdown_bin in.md > out.html'
2016-10-24 12:39:29 +03:00
[ [ -f Markdown.pl ] ] && markdown_bin = ./Markdown.pl || markdown_bin = $( which Markdown.pl 2>/dev/null || which markdown 2>/dev/null)
2013-02-21 18:06:02 +02:00
}
2014-01-09 20:50:27 +02:00
# Check for the validity of some variables
# DO NOT EDIT THIS FUNCTION unless you know what you're doing
global_variables_check( ) {
2016-01-11 00:17:48 +02:00
[ [ $header_file = = .header.html ] ] &&
2014-01-09 20:50:27 +02:00
echo "Please check your configuration. '.header.html' is not a valid value for the setting 'header_file'" &&
exit
2016-01-11 00:17:48 +02:00
[ [ $footer_file = = .footer.html ] ] &&
2014-01-09 20:50:27 +02:00
echo "Please check your configuration. '.footer.html' is not a valid value for the setting 'footer_file'" &&
exit
}
2013-12-23 21:49:31 +02:00
# Test if the markdown script is working correctly
test_markdown( ) {
2016-01-19 01:12:53 +02:00
[ [ -n $markdown_bin ] ] &&
2016-12-29 08:39:28 +03:00
(
[ [ $( " $markdown_bin " <<< $'line 1\n\nline 2' ) = = $'<p>line 1</p>\n\n<p>line 2</p>' ] ] ||
[ [ $( " $markdown_bin " <<< $'line 1\n\nline 2' ) = = $'<p>line 1</p>\n<p>line 2</p>' ] ]
)
2013-12-23 21:49:31 +02:00
}
# Parse a Markdown file into HTML and return the generated file
markdown( ) {
2016-01-11 04:30:41 +02:00
out = ${ 1 %.md } .html
while [ [ -f $out ] ] ; do out = ${ out %.html } .$RANDOM .html; done
2015-09-23 18:43:44 +03:00
$markdown_bin " $1 " > " $out "
echo " $out "
2013-12-23 21:49:31 +02:00
}
2013-02-21 16:59:44 +02:00
# Prints the required google analytics code
google_analytics( ) {
2016-01-11 00:17:48 +02:00
[ [ -z $global_analytics && -z $global_analytics_file ] ] && return
2013-02-21 16:59:44 +02:00
2016-01-11 00:17:48 +02:00
if [ [ -z $global_analytics_file ] ] ; then
2014-02-18 18:34:41 +02:00
echo " <script type=\"text/javascript\">
2013-02-21 16:59:44 +02:00
2014-02-18 18:34:41 +02:00
var _gaq = _gaq || [ ] ;
2015-09-23 18:43:44 +03:00
_gaq.push( [ '_setAccount' , '${global_analytics}' ] ) ;
2014-02-18 18:34:41 +02:00
_gaq.push( [ '_trackPageview' ] ) ;
2013-02-21 16:59:44 +02:00
2014-02-18 18:34:41 +02: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 16:59:44 +02:00
2014-02-18 18:34:41 +02:00
</script>"
else
cat " $global_analytics_file "
fi
2013-02-21 16:59:44 +02:00
}
2013-04-03 00:35:33 +03:00
# Prints the required code for disqus comments
disqus_body( ) {
2016-01-11 00:17:48 +02:00
[ [ -z $global_disqus_username ] ] && return
2013-06-05 13:36:47 +03:00
2013-04-03 00:35:33 +03:00
echo ' <div id = "disqus_thread" ></div>
<script type = "text/javascript" >
/* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
2016-01-18 22:35:29 +02:00
var disqus_shortname = '"' $global_disqus_username '"' ; // required: replace example with your forum shortname
2013-04-03 00:35:33 +03:00
/* * * 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 13:36:47 +03:00
2013-04-03 00:35:33 +03:00
# Prints the required code for disqus in the footer
disqus_footer( ) {
2016-01-11 00:17:48 +02:00
[ [ -z $global_disqus_username ] ] && return
2013-12-23 21:49:31 +02:00
echo ' <script type = "text/javascript" >
/* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
2016-01-18 22:35:29 +02:00
var disqus_shortname = '"' $global_disqus_username '"' ; // required: replace example with your forum shortname
2013-12-23 21:49:31 +02:00
/* * * 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-03 00:35:33 +03:00
}
2014-02-26 22:04:36 +02:00
# Reads HTML file from stdin, prints its content to stdout
2014-02-27 00:04:31 +02:00
# $1 where to start ("text" or "entry")
# $2 where to stop ("text" or "entry")
2014-02-27 23:40:11 +02:00
# $3 "cut" to remove text from <hr /> to <!-- text end -->
# note that this does not remove <hr /> line itself,
2014-02-27 00:04:31 +02:00
# so you can see if text was cut or not
2014-02-26 22:04:36 +02:00
get_html_file_content( ) {
2016-01-11 04:30:41 +02:00
awk " /<!-- $1 begin -->/, /<!-- $2 end -->/{
if ( !/<!-- $1 begin -->/ && !/<!-- $2 end -->/) print
if ( \" $3 \" = = \" cut\" && /$cut_line /) {
if ( \" $2 \" = = \" text\" ) exit # no need to read further
2014-09-01 17:13:06 +03:00
while ( getline > 0 && !/<!-- text end -->/) {
2016-01-11 04:30:41 +02:00
if ( \" $cut_tags \" = = \" no\" && /^<p>$template_tags_line_header / ) print
2014-09-01 17:13:06 +03:00
}
2014-02-27 00:04:31 +02:00
}
2016-01-11 04:30:41 +02:00
} "
2014-02-26 22:04:36 +02:00
}
2013-02-21 16:59:44 +02: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 14:55:55 +02: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 16:59:44 +02:00
#
# $1 the file to edit
2014-03-01 15:34:52 +02: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 16:59:44 +02:00
edit( ) {
2016-04-25 22:14:24 +03:00
[ [ ! -f " ${ 1 %%.* } .html " ] ] && echo "Can't edit post " ${ 1 %%.* } .html", did you mean to use \"bb.sh post <draft_file>\"?" && exit -1
2016-04-26 17:16:35 +03:00
# Original post timestamp
edit_timestamp = $( LC_ALL = C date -r " ${ 1 %%.* } .html " +" $date_format_full " )
touch_timestamp = $( LC_ALL = C date -r " ${ 1 %%.* } .html " +" $date_format_timestamp " )
2016-01-10 20:31:49 +02:00
tags_before = $( tags_in_post " ${ 1 %%.* } .html " )
2016-01-11 00:17:48 +02:00
if [ [ $2 = = full ] ] ; then
2016-10-24 16:49:52 +03:00
$EDITOR " $1 "
2016-01-10 20:31:49 +02:00
filename = $1
2014-03-01 15:34:52 +02:00
else
2016-01-11 00:17:48 +02:00
if [ [ ${ 1 ##*. } = = md ] ] ; then
2014-05-21 21:27:41 +03:00
test_markdown
2016-01-11 00:17:48 +02:00
if ( ( $? != 0) ) ; then
2014-05-21 21:27:41 +03:00
echo "Markdown is not working, please edit HTML file directly."
exit
fi
2014-03-22 16:45:56 +02:00
# editing markdown file
2016-10-24 16:49:52 +03:00
$EDITOR " $1 "
2016-01-10 20:31:49 +02:00
TMPFILE = $( markdown " $1 " )
filename = ${ 1 %%.* } .html
2014-03-22 16:45:56 +02:00
else
# Create the content file
2016-01-11 04:30:41 +02:00
TMPFILE = $( basename " $1 " ) .$RANDOM .html
2014-03-22 16:45:56 +02:00
# Title
2016-01-11 04:30:41 +02:00
get_post_title " $1 " > " $TMPFILE "
2014-03-22 16:45:56 +02:00
# Post text with plaintext tags
2016-01-11 04:30:41 +02:00
get_html_file_content 'text' 'text' <" $1 " | sed " /^<p> $template_tags_line_header /s|<a href=' $prefix_tags \([^']*\).html'>\\1</a>|\\1|g " >> " $TMPFILE "
2016-10-24 16:49:52 +03:00
$EDITOR " $TMPFILE "
2016-01-10 20:31:49 +02:00
filename = $1
2014-03-22 16:45:56 +02:00
fi
rm " $filename "
2016-01-11 00:17:48 +02:00
if [ [ $2 = = keep ] ] ; then
2014-03-22 16:45:56 +02:00
parse_file " $TMPFILE " " $edit_timestamp " " $filename "
2014-03-18 23:17:34 +02:00
else
parse_file " $TMPFILE " " $edit_timestamp " # this command sets $filename as the html processed file
2016-01-11 00:17:48 +02:00
[ [ ${ 1 ##*. } = = md ] ] && mv " $1 " " ${ filename %%.* } .md " 2>/dev/null
2014-03-01 15:34:52 +02:00
fi
2014-03-18 23:17:34 +02:00
rm " $TMPFILE "
2014-03-01 15:34:52 +02:00
fi
2016-10-24 16:49:52 +03:00
touch -t " $touch_timestamp " " $filename "
touch -t " $touch_timestamp " " $1 "
2014-03-01 14:55:55 +02:00
chmod 644 " $filename "
echo " Posted $filename "
2016-01-11 04:30:41 +02:00
tags_after = $( tags_in_post " $filename " )
2016-10-24 16:49:52 +03:00
relevant_tags = $( echo " $tags_before $tags_after " | tr ',' ' ' | tr ' ' '\n' | sort -u | tr '\n' ' ' )
if [ [ ! -z $relevant_tags ] ] ; then
relevant_posts = " $( posts_with_tags $relevant_tags ) $filename "
rebuild_tags " $relevant_posts " " $relevant_tags "
2014-06-25 07:38:17 +03:00
fi
2013-02-21 16:59:44 +02:00
}
2014-09-15 20:23:12 +03:00
# Create a Twitter summary (twitter "card") for the post
#
# $1 the post file
# $2 the title
twitter_card( ) {
2016-01-11 00:17:48 +02:00
[ [ -z $global_twitter_username ] ] && return
2014-09-15 20:23:12 +03:00
echo "<meta name='twitter:card' content='summary' />"
echo " <meta name='twitter:site' content='@ $global_twitter_username ' /> "
echo " <meta name='twitter:title' content=' $2 ' /> " # Twitter truncates at 70 char
2019-09-01 15:19:21 +03:00
description = $( grep -v " ^<p> $template_tags_line_header " " $1 " | sed -e 's/<[^>]*>//g' | tr '\n' ' ' | sed "s/\"/'/g" | head -c 250)
2014-09-15 20:32:51 +03:00
echo " <meta name='twitter:description' content=\" $description \" /> "
2021-11-04 21:39:11 +03:00
# For the image we try to locate the first image in the article
image = $( sed -n '2,$ d; s/.*<img.*src="\([^"]*\)".*/\1/p' " $1 " )
# If none, then we try a global setting image
[ [ -z $image ] ] && [ [ -n $global_twitter_card_image ] ] && image = $global_twitter_card_image
# If none, return
2016-01-11 00:17:48 +02:00
[ [ -z $image ] ] && return
2021-11-04 21:39:11 +03:00
# Final housekeeping
2016-01-11 04:30:41 +02:00
[ [ $image = ~ ^https?:// ] ] || image = $global_url /$image # Check that URL is absolute
2014-09-15 20:23:12 +03:00
echo " <meta name='twitter:image' content=' $image ' /> "
}
2013-02-21 16:59:44 +02:00
# Adds the code needed by the twitter button
#
# $1 the post URL
twitter( ) {
2016-01-11 00:17:48 +02:00
[ [ -z $global_twitter_username ] ] && return
2013-06-05 13:36:47 +03:00
2016-01-11 00:17:48 +02:00
if [ [ -z $global_disqus_username ] ] ; then
if [ [ $global_twitter_cookieless = = true ] ] ; then
2014-07-07 18:11:14 +03:00
id = $RANDOM
2014-07-17 17:47:57 +03:00
search_engine = "https://twitter.com/search?q="
2016-02-23 21:18:47 +02:00
2014-07-07 18:23:24 +03:00
echo " <p id='twitter'><a href='http://twitter.com/intent/tweet?url= $1 &text= $template_twitter_comment &via= $global_twitter_username '> $template_comments $template_twitter_button </a> "
2014-07-17 17:47:57 +03:00
echo " <a href=' $search_engine " " $1 '><span id='count- $id '></span></a> </p> "
2014-07-07 18:11:14 +03:00
return ;
else
echo " <p id='twitter'> $template_comments " ;
fi
2013-06-05 13:36:47 +03:00
else
echo " <p id='twitter'><a href=\" $1 #disqus_thread\"> $template_comments </a> "
fi
2013-09-26 12:42:03 +03:00
echo " <a href=\"https://twitter.com/share\" class=\"twitter-share-button\" data-text=\" $template_twitter_comment \" data-url=\" $1 \" "
2013-06-05 13:36:47 +03:00
echo " data-via=\" $global_twitter_username \" "
2013-02-21 17:37:55 +02: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 16:59:44 +02:00
}
2014-02-27 14:59:56 +02: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
#
2014-05-19 22:08:55 +03:00
# Return 0 (bash return value 'true') if the input file is an index, feed, etc
2014-02-27 14:59:56 +02:00
# or 1 (bash return value 'false') if it is a blogpost
is_boilerplate_file( ) {
2016-01-18 21:18:23 +02:00
name = ${ 1 #./ }
2016-10-24 17:23:53 +03:00
# First check against user-defined non-blogpost pages
for item in " ${ non_blogpost_files [@] } " ; do
[ [ " $name " = = " $item " ] ] && return 0
done
2016-01-11 00:17:48 +02:00
case $name in
( " $index_file " | " $archive_index " | " $tags_index " | " $footer_file " | " $header_file " | " $global_analytics_file " | " $prefix_tags " * )
return 0 ; ;
( * ) # Check for excluded
2016-01-11 01:16:50 +02:00
for excl in " ${ html_exclude [@] } " ; do
2016-01-11 00:17:48 +02:00
[ [ $name = = " $excl " ] ] && return 0
2015-02-19 10:33:29 +02:00
done
2016-01-11 00:17:48 +02:00
return 1 ; ;
esac
2014-02-27 14:59:56 +02:00
}
2013-02-21 16:59:44 +02: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
2016-03-02 21:34:54 +02:00
# $6 post author
2013-02-21 16:59:44 +02:00
create_html_page( ) {
2016-01-10 20:31:49 +02:00
content = $1
filename = $2
index = $3
title = $4
timestamp = $5
2016-03-02 21:34:54 +02:00
author = $6
2013-02-21 17:37:55 +02:00
# Create the actual blog post
# html, head
2016-01-11 04:30:41 +02:00
{
cat ".header.html"
echo " <title> $title </title> "
google_analytics
twitter_card " $content " " $title "
echo "</head><body>"
# stuff to add before the actual body content
[ [ -n $body_begin_file ] ] && cat " $body_begin_file "
2020-05-10 17:22:42 +03:00
[ [ $filename = $index_file * ] ] && [ [ -n $body_begin_file_index ] ] && cat " $body_begin_file_index "
2016-01-11 04:30:41 +02:00
# body divs
echo '<div id="divbodyholder">'
echo '<div class="headerholder"><div class="header">'
# blog title
echo '<div id="title">'
cat .title.html
echo '</div></div></div>' # title, header, headerholder
echo '<div id="divbody"><div class="content">'
2016-01-18 21:18:23 +02:00
file_url = ${ filename #./ }
file_url = ${ file_url %.rebuilt } # Get the correct URL when rebuilding
2016-01-11 04:30:41 +02:00
# 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 \"> "
# remove possible <p>'s on the title because of markdown conversion
2016-01-18 21:18:23 +02:00
title = ${ title //<p>/ }
title = ${ title //< \/ p >/ }
echo " $title "
2016-01-11 04:30:41 +02:00
echo '</a></h3>'
if [ [ -z $timestamp ] ] ; then
2016-04-26 17:16:35 +03:00
echo " <!-- $date_inpost : # $( LC_ALL = $date_locale date +" $date_format_timestamp " ) # --> "
2016-01-11 04:30:41 +02:00
else
2016-04-26 17:16:35 +03:00
echo " <!-- $date_inpost : # $( LC_ALL = $date_locale date +" $date_format_timestamp " --date= " $timestamp " ) # --> "
2016-01-11 04:30:41 +02:00
fi
2016-04-26 17:16:35 +03:00
if [ [ -z $timestamp ] ] ; then
echo -n " <div class=\"subtitle\"> $( LC_ALL = $date_locale date +" $date_format " ) "
else
echo -n " <div class=\"subtitle\"> $( LC_ALL = $date_locale date +" $date_format " --date= " $timestamp " ) "
fi
[ [ -n $author ] ] && echo -e " — \n $author "
2016-04-13 02:19:30 +03:00
echo "</div>"
2016-01-11 04:30:41 +02:00
echo '<!-- text begin -->' # This marks the text body, after the title, date...
2013-02-21 17:37:55 +02:00
fi
2016-01-11 04:30:41 +02:00
cat " $content " # Actual content
if [ [ $index = = no ] ] ; then
echo -e '\n<!-- text end -->'
2013-02-21 17:37:55 +02:00
2016-01-11 04:30:41 +02:00
twitter " $global_url / $file_url "
2013-02-21 17:37:55 +02:00
2016-01-11 04:30:41 +02:00
echo '<!-- entry end -->' # absolute end of the post
fi
2013-02-21 17:37:55 +02:00
2016-01-11 04:30:41 +02:00
echo '</div>' # content
2013-04-09 18:09:23 +03:00
2016-01-11 04:30:41 +02:00
# Add disqus commments except for index and all_posts pages
[ [ $index = = no ] ] && disqus_body
2014-07-11 10:23:57 +03:00
2016-01-11 04:30:41 +02:00
# page footer
cat .footer.html
# close divs
echo '</div></div>' # divbody and divbodyholder
disqus_footer
2016-12-30 07:36:01 +03:00
[ [ -n $body_end_file ] ] && cat " $body_end_file "
2022-04-18 00:48:27 +03:00
echo ' </body></html>
<button onclick = "topFunction()" id = "myBtn" title = "Go to top" >Top</button>
<script>
//Get the button
var mybutton = document.getElementById( "myBtn" ) ;
// When the user scrolls down 20px from the top of the document, show the button
window.onscroll = function ( ) { scrollFunction( ) } ;
function scrollFunction( ) {
if ( document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) {
mybutton.style.display = "block" ;
} else {
mybutton.style.display = "none" ;
}
}
// When the user clicks on the button, scroll to the top of the document
function topFunction( ) {
document.body.scrollTop = 0;
document.documentElement.scrollTop = 0;
}
</script>'
2016-01-11 04:30:41 +02:00
} > " $filename "
2013-02-21 16:59:44 +02:00
}
# Parse the plain text file into an html file
2014-03-01 18:17:52 +02:00
#
2014-03-18 23:17:34 +02:00
# $1 source file name
2014-03-01 18:17:52 +02:00
# $2 (optional) timestamp for the file
2014-03-18 23:17:34 +02: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 16:59:44 +02:00
parse_file( ) {
2013-02-21 17:37:55 +02:00
# Read for the title and check that the filename is ok
title = ""
2016-10-24 16:49:52 +03:00
while IFS = '' read -r line; do
2016-01-11 00:17:48 +02:00
if [ [ -z $title ] ] ; then
2013-12-23 21:49:31 +02:00
# remove extra <p> and </p> added by markdown
2016-10-24 16:49:52 +03:00
title = $( echo " $line " | sed 's/<\/*p>//g' )
2016-01-11 00:17:48 +02:00
if [ [ -n $3 ] ] ; then
2014-03-18 23:17:34 +02:00
filename = $3
else
2014-05-26 00:03:14 +03:00
filename = $title
2016-01-11 00:17:48 +02:00
[ [ -n $convert_filename ] ] &&
2016-01-11 04:30:41 +02:00
filename = $( echo " $title " | eval " $convert_filename " )
2016-01-11 00:17:48 +02:00
[ [ -n $filename ] ] ||
2014-06-05 19:51:28 +03:00
filename = $RANDOM # don't allow empty filenames
2016-01-10 20:31:49 +02:00
filename = $filename .html
2014-03-18 23:17:34 +02:00
# Check for duplicate file names
2016-01-11 00:17:48 +02:00
while [ [ -f $filename ] ] ; do
2016-01-11 04:30:41 +02:00
filename = ${ filename %.html } $RANDOM .html
2014-03-18 23:17:34 +02:00
done
fi
2016-01-10 20:31:49 +02:00
content = $filename .tmp
2014-03-10 20:18:14 +02:00
# Parse possible tags
2016-01-11 00:17:48 +02:00
elif [ [ $line = = " <p> $template_tags_line_header " * ] ] ; then
2016-10-24 13:02:22 +03:00
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 "
2016-10-24 16:49:52 +03:00
2014-03-01 13:30:08 +02:00
echo -n " <p> $template_tags_line_header " >> " $content "
2016-10-24 13:02:22 +03:00
for item in " ${ array [@] } " ; do
echo -n " <a href=' $prefix_tags $item .html'> $item </a>, "
done | sed 's/, $/<\/p>/g' >> " $content "
2013-02-21 17:37:55 +02:00
else
echo " $line " >> " $content "
fi
2013-12-23 21:49:31 +02:00
done < " $1 "
2013-02-21 17:37:55 +02:00
# Create the actual html page
2016-03-02 21:34:54 +02:00
create_html_page " $content " " $filename " no " $title " " $2 " " $global_author "
2013-02-21 17:37:55 +02:00
rm " $content "
2013-02-21 16:59:44 +02:00
}
# Manages the creation of the text file and the parsing to html file
# also the drafts
write_entry( ) {
2016-01-11 00:17:48 +02:00
test_markdown && fmt = md || fmt = html
2016-01-10 20:31:49 +02:00
f = $2
2016-01-11 00:17:48 +02:00
[ [ $2 = = -html ] ] && fmt = html && f = $3
2013-12-23 21:49:31 +02:00
2016-01-11 00:17:48 +02:00
if [ [ -n $f ] ] ; then
2016-01-10 20:31:49 +02:00
TMPFILE = $f
2016-01-11 00:17:48 +02:00
if [ [ ! -f $TMPFILE ] ] ; then
2013-02-21 17:37:55 +02:00
echo "The file doesn't exist"
delete_includes
exit
fi
2014-06-26 05:13:29 +03:00
# guess format from TMPFILE
2016-01-10 20:31:49 +02:00
extension = ${ TMPFILE ##*. }
2016-01-11 00:17:48 +02:00
[ [ $extension = = md || $extension = = html ] ] && fmt = $extension
2014-06-26 05:13:29 +03:00
# but let user override it (`bb.sh post -html file.md`)
2016-01-11 00:17:48 +02:00
[ [ $2 = = -html ] ] && fmt = html
2014-10-05 19:12:37 +03:00
# Test if Markdown is working before re-posting a .md file
2016-01-11 00:17:48 +02:00
if [ [ $extension = = md ] ] ; then
2014-10-05 19:12:37 +03:00
test_markdown
2016-01-11 00:17:48 +02:00
if ( ( $? != 0) ) ; then
2014-10-05 19:12:37 +03:00
echo "Markdown is not working, please edit HTML file directly."
exit
fi
fi
2013-02-21 17:37:55 +02:00
else
2016-01-11 00:17:48 +02:00
TMPFILE = .entry-$RANDOM .$fmt
2014-02-27 17:46:37 +02:00
echo -e "Title on this line\n" >> " $TMPFILE "
2016-01-11 00:17:48 +02:00
[ [ $fmt = = html ] ] && cat << EOF >> "$TMPFILE "
2014-02-27 17:46:37 +02:00
<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 22:28:20 +02:00
<p>$template_tags_line_header keep-this-tag-format, tags-are-optional, example</p>
2014-02-27 17:46:37 +02:00
EOF
2016-01-11 00:17:48 +02:00
[ [ $fmt = = md ] ] && cat << EOF >> "$TMPFILE "
2014-02-27 17:46:37 +02:00
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 22:28:20 +02:00
$template_tags_line_header keep-this-tag-format, tags-are-optional, beware-with-underscores-in-markdown, example
2014-02-27 17:46:37 +02:00
EOF
2013-02-21 17:37:55 +02:00
fi
chmod 600 " $TMPFILE "
post_status = "E"
2014-03-19 00:11:14 +02:00
filename = ""
2016-01-11 00:17:48 +02:00
while [ [ $post_status != "p" && $post_status != "P" ] ] ; do
[ [ -n $filename ] ] && rm " $filename " # Delete the generated html file, if any
2016-10-24 16:49:52 +03:00
$EDITOR " $TMPFILE "
2016-01-11 00:17:48 +02:00
if [ [ $fmt = = md ] ] ; then
2016-01-10 20:31:49 +02:00
html_from_md = $( markdown " $TMPFILE " )
2013-12-23 21:49:31 +02:00
parse_file " $html_from_md "
rm " $html_from_md "
else
parse_file " $TMPFILE " # this command sets $filename as the html processed file
fi
2014-06-26 04:53:09 +03:00
chmod 644 " $filename "
2016-01-11 00:17:48 +02:00
[ [ -n $preview_url ] ] || preview_url = $global_url
2014-06-26 04:53:09 +03:00
echo " To preview the entry, open $preview_url / $filename in your browser "
2013-02-21 17:37:55 +02:00
echo -n "[P]ost this entry, [E]dit again, [D]raft for later? (p/E/d) "
2016-01-11 04:30:41 +02:00
read -r post_status
2016-01-11 00:17:48 +02:00
if [ [ $post_status = = d || $post_status = = D ] ] ; then
2013-02-21 17:37:55 +02:00
mkdir -p "drafts/"
chmod 700 "drafts/"
2016-01-10 20:31:49 +02:00
title = $( head -n 1 $TMPFILE )
2016-01-11 04:30:41 +02:00
[ [ -n $convert_filename ] ] && title = $( echo " $title " | eval " $convert_filename " )
2016-01-11 00:17:48 +02:00
[ [ -n $title ] ] || title = $RANDOM
2014-06-05 19:51:28 +03:00
2016-01-11 00:17:48 +02:00
draft = drafts/$title .$fmt
2013-02-21 17:37:55 +02:00
mv " $TMPFILE " " $draft "
chmod 600 " $draft "
rm " $filename "
delete_includes
echo " Saved your draft as ' $draft ' "
exit
fi
done
2016-01-11 00:17:48 +02:00
if [ [ $fmt = = md && -n $save_markdown ] ] ; then
2014-03-22 16:14:01 +02:00
mv " $TMPFILE " " ${ filename %%.* } .md "
else
rm " $TMPFILE "
fi
2013-02-21 17:37:55 +02:00
chmod 644 " $filename "
echo " Posted $filename "
2016-01-10 20:31:49 +02:00
relevant_tags = $( tags_in_post $filename )
2016-01-11 00:17:48 +02:00
if [ [ -n $relevant_tags ] ] ; then
2016-10-24 16:49:52 +03:00
relevant_posts = " $( posts_with_tags $relevant_tags ) $filename "
rebuild_tags " $relevant_posts " " $relevant_tags "
2014-06-25 07:38:17 +03:00
fi
2013-02-21 16:59:44 +02:00
}
# Create an index page with all the posts
all_posts( ) {
2013-02-21 17:37:55 +02:00
echo -n "Creating an index page with all the posts "
2016-01-10 20:31:49 +02:00
contentfile = $archive_index .$RANDOM
2016-01-11 00:17:48 +02:00
while [ [ -f $contentfile ] ] ; do
2016-01-10 20:31:49 +02:00
contentfile = $archive_index .$RANDOM
2013-02-21 17:37:55 +02:00
done
2016-01-11 04:30:41 +02:00
{
echo " <h3> $template_archive_title </h3> "
prev_month = ""
2016-10-24 16:49:52 +03:00
while IFS = '' read -r i; do
2016-01-11 04:30:41 +02:00
is_boilerplate_file " $i " && continue
echo -n "." 1>& 3
# Month headers
month = $( LC_ALL = $date_locale date -r " $i " +" $date_allposts_header " )
if [ [ $month != " $prev_month " ] ] ; then
[ [ -n $prev_month ] ] && echo "</ul>" # Don't close ul before first header
echo " <h4 class='allposts_header'> $month </h4> "
echo "<ul>"
prev_month = $month
fi
# Title
title = $( get_post_title " $i " )
echo -n " <li><a href=\" $i \"> $title </a> — "
# Date
date = $( LC_ALL = $date_locale date -r " $i " +" $date_format " )
echo " $date </li> "
2016-10-24 16:49:52 +03:00
done < <( ls -t ./*.html)
2016-01-11 04:30:41 +02:00
echo "" 1>& 3
echo "</ul>"
2016-01-29 16:57:15 +02:00
echo " <div id=\"all_posts\"><a href=\"./ $index_file \"> $template_archive_index_page </a></div> "
2016-01-11 04:30:41 +02:00
} 3>& 1 >" $contentfile "
2013-02-21 17:37:55 +02:00
2016-03-02 21:34:54 +02:00
create_html_page " $contentfile " " $archive_index .tmp " yes " $global_title — $template_archive_title " " $global_author "
2013-02-21 17:37:55 +02:00
mv " $archive_index .tmp " " $archive_index "
chmod 644 " $archive_index "
rm " $contentfile "
2013-02-21 16:59:44 +02:00
}
2014-02-27 19:07:01 +02:00
# Create an index page with all the tags
all_tags( ) {
echo -n "Creating an index page with all the tags "
2016-01-10 20:31:49 +02:00
contentfile = $tags_index .$RANDOM
2016-01-11 00:17:48 +02:00
while [ [ -f $contentfile ] ] ; do
2016-01-10 20:31:49 +02:00
contentfile = $tags_index .$RANDOM
2014-02-27 19:07:01 +02:00
done
2016-01-11 04:30:41 +02:00
{
echo " <h3> $template_tags_title </h3> "
echo "<ul>"
2016-10-24 16:49:52 +03:00
for i in $prefix_tags *.html; do
2016-01-11 04:30:41 +02:00
[ [ -f " $i " ] ] || break
echo -n "." 1>& 3
nposts = $( grep -c "<\!-- text begin -->" " $i " )
2016-01-18 21:18:23 +02:00
tagname = ${ i # " $prefix_tags " }
tagname = ${ tagname %.html }
2016-10-24 12:16:01 +03:00
case $nposts in
1) word = $template_tags_posts_singular ; ;
2| 3| 4) word = $template_tags_posts_2_4 ; ;
2016-10-24 17:23:53 +03:00
*) word = $template_tags_posts ; ;
2016-10-24 12:16:01 +03:00
esac
2016-01-14 17:39:56 +02:00
echo " <li><a href=\" $i \"> $tagname </a> — $nposts $word </li> "
2016-01-11 04:30:41 +02:00
done
echo "" 1>& 3
echo "</ul>"
2016-01-29 16:57:15 +02:00
echo " <div id=\"all_posts\"><a href=\"./ $index_file \"> $template_archive_index_page </a></div> "
2016-01-11 04:30:41 +02:00
} 3>& 1 > " $contentfile "
2014-02-27 19:07:01 +02:00
2016-03-02 21:34:54 +02:00
create_html_page " $contentfile " " $tags_index .tmp " yes " $global_title — $template_tags_title " " $global_author "
2014-02-27 19:07:01 +02:00
mv " $tags_index .tmp " " $tags_index "
chmod 644 " $tags_index "
rm " $contentfile "
}
2013-02-21 17:58:05 +02:00
# Generate the index.html with the content of the latest posts
2013-02-21 16:59:44 +02:00
rebuild_index( ) {
2013-02-21 17:37:55 +02:00
echo -n "Rebuilding the index "
2016-01-10 20:31:49 +02:00
newindexfile = $index_file .$RANDOM
contentfile = $newindexfile .content
2016-01-11 00:17:48 +02:00
while [ [ -f $newindexfile ] ] ; do
2016-01-10 20:31:49 +02:00
newindexfile = $index_file .$RANDOM
contentfile = $newindexfile .content
2013-02-21 17:37:55 +02:00
done
2013-02-21 17:58:05 +02:00
# Create the content file
2016-01-11 04:30:41 +02:00
{
n = 0
2016-10-24 16:49:52 +03:00
while IFS = '' read -r i; do
2016-01-11 04:30:41 +02:00
is_boilerplate_file " $i " && continue ;
if ( ( n >= number_of_index_articles) ) ; then break; fi
if [ [ -n $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 "
else
get_html_file_content 'entry' 'entry' <" $i "
fi
echo -n "." 1>& 3
n = $(( n + 1 ))
2016-10-24 16:49:52 +03:00
done < <( ls -t ./*.html) # sort by date, newest first
2013-02-21 17:37:55 +02:00
2016-01-11 04:30:41 +02:00
feed = $blog_feed
if [ [ -n $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> "
} 3>& 1 >" $contentfile "
2013-02-21 17:37:55 +02:00
echo ""
2016-03-02 21:34:54 +02:00
create_html_page " $contentfile " " $newindexfile " yes " $global_title " " $global_author "
2013-02-21 17:37:55 +02:00
rm " $contentfile "
mv " $newindexfile " " $index_file "
chmod 644 " $index_file "
2013-02-21 16:59:44 +02:00
}
2014-06-24 07:51:27 +03:00
# Finds all tags referenced in one post.
# Accepts either filename as first argument, or post content at stdin
2016-10-24 16:49:52 +03:00
# Prints one line with space-separated tags to stdout
2014-06-24 07:51:27 +03:00
tags_in_post( ) {
2016-10-24 16:49:52 +03:00
sed -n " /^<p> $template_tags_line_header /{s/^<p> $template_tags_line_header //;s/<[^>]*>//g;s/[ ,]\+/ /g;p;} " " $1 " | tr ', ' ' '
2014-06-24 07:51:27 +03:00
}
2014-06-24 08:16:57 +03:00
# Finds all posts referenced in a number of tags.
2016-10-24 16:49:52 +03:00
# Arguments are tags
# Prints one line with space-separated tags to stdout
2014-06-24 08:16:57 +03:00
posts_with_tags( ) {
2016-01-11 00:17:48 +02:00
( ( $# < 1) ) && return
2016-01-11 01:16:50 +02:00
set -- " ${ @/#/ $prefix_tags } "
set -- " ${ @/%/.html } "
sed -n '/^<h3><a class="ablack" href="[^"]*">/{s/.*href="\([^"]*\)">.*/\1/;p;}' " $@ " 2> /dev/null
2014-06-24 08:16:57 +03:00
}
2014-06-24 07:25:55 +03:00
# Rebuilds tag_*.html files
# if no arguments given, rebuilds all of them
# if arguments given, they should have this format:
2016-10-24 16:49:52 +03:00
# "FILE1 [FILE2 [...]]" "TAG1 [TAG2 [...]]"
2014-06-24 07:25:55 +03:00
# where FILEn are files with posts which should be used for rebuilding tags,
# and TAGn are names of tags which should be rebuilt.
# example:
2016-10-24 16:49:52 +03:00
# rebuild_tags "one_post.html another_article.html" "example-tag another-tag"
# mind the quotes!
2014-02-27 14:59:56 +02:00
rebuild_tags( ) {
2016-10-24 16:49:52 +03:00
if ( ( $# < 2) ) ; then
2014-06-24 07:25:55 +03:00
# will process all files and tags
2016-10-24 16:49:52 +03:00
files = $( ls -t ./*.html)
2016-01-11 00:17:48 +02:00
all_tags = yes
2014-06-24 07:25:55 +03:00
else
# will process only given files and tags
2016-10-24 16:49:52 +03:00
files = $( printf '%s\n' $1 | sort -u)
files = $( ls -t $files )
tags = $2
2014-06-24 07:25:55 +03:00
fi
2014-02-27 14:59:56 +02:00
echo -n "Rebuilding tag pages "
n = 0
2016-01-11 00:17:48 +02:00
if [ [ -n $all_tags ] ] ; then
2016-10-24 16:49:52 +03:00
rm ./" $prefix_tags " *.html & > /dev/null
2014-06-24 07:25:55 +03:00
else
2016-10-24 16:49:52 +03:00
for i in $tags ; do
rm " ./ $prefix_tags $i .html " & > /dev/null
2014-06-24 07:25:55 +03:00
done
fi
2014-02-27 17:46:37 +02:00
# First we will process all files and create temporal tag files
# with just the content of the posts
2016-01-11 17:10:17 +02:00
tmpfile = tmp.$RANDOM
while [ [ -f $tmpfile ] ] ; do tmpfile = tmp.$RANDOM ; done
2016-10-24 16:49:52 +03:00
while IFS = '' read -r i; do
is_boilerplate_file " $i " && continue ;
2014-02-27 17:46:37 +02:00
echo -n "."
2016-01-11 00:17:48 +02:00
if [ [ -n $cut_do ] ] ; then
2016-01-11 04:30:41 +02:00
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 "
2014-03-02 00:01:47 +02:00
else
2016-01-11 04:30:41 +02:00
get_html_file_content 'entry' 'entry' <" $i "
fi >" $tmpfile "
for tag in $( tags_in_post " $i " ) ; do
2016-10-24 16:49:52 +03:00
if [ [ -n $all_tags || " $tags " = = *" $tag " * ] ] ; then
2014-06-24 07:51:27 +03:00
cat " $tmpfile " >> " $prefix_tags $tag " .tmp.html
2014-02-27 17:46:37 +02:00
fi
2014-06-24 07:51:27 +03:00
done
2016-10-24 16:49:52 +03:00
done <<< " $files "
2016-01-11 17:10:17 +02:00
rm " $tmpfile "
2014-02-27 17:46:37 +02:00
# Now generate the tag files with headers, footers, etc
2016-10-24 16:49:52 +03:00
while IFS = '' read -r i; do
2016-01-18 21:18:23 +02:00
tagname = ${ i #./ " $prefix_tags " }
tagname = ${ tagname %.tmp.html }
2016-03-02 21:34:54 +02:00
create_html_page " $i " " $prefix_tags $tagname .html " yes " $global_title — $template_tag_title \" $tagname \" " " $global_author "
2014-02-27 17:46:37 +02:00
rm " $i "
2016-10-24 16:49:52 +03:00
done < <( ls -t ./" $prefix_tags " *.tmp.html 2>/dev/null)
2014-02-27 17:46:37 +02:00
echo
2014-02-27 14:59:56 +02: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 "
}
2016-03-02 21:34:54 +02:00
# Return the post author
#
# $1 the html file
get_post_author( ) {
awk '/<div class="subtitle">.+/, /<!-- text begin -->/{if (!/<div class="subtitle">.+/ && !/<!-- text begin -->/) print}' " $1 " | sed 's/<\/div>//g'
}
2016-01-14 17:30:01 +02:00
# Displays a list of the tags
#
# $2 if "-n", tags will be sorted by number of posts
list_tags( ) {
2016-01-18 21:18:23 +02:00
if [ [ $2 = = -n ] ] ; then do_sort = 1; else do_sort = 0; fi
2016-01-14 17:30:01 +02:00
2016-10-24 16:49:52 +03:00
ls ./$prefix_tags *.html & > /dev/null
( ( $? != 0) ) && echo "No posts yet. Use 'bb.sh post' to create one" && return
2016-01-14 17:30:01 +02:00
2016-10-24 16:49:52 +03:00
lines = ""
for i in $prefix_tags *.html; do
2016-01-14 17:30:01 +02:00
[ [ -f " $i " ] ] || break
nposts = $( grep -c "<\!-- text begin -->" " $i " )
2016-01-18 21:18:23 +02:00
tagname = ${ i # " $prefix_tags " }
tagname = ${ tagname #.html }
( ( nposts > 1) ) && word = $template_tags_posts || word = $template_tags_posts_singular
2016-01-14 17:39:56 +02:00
line = " $tagname # $nposts # $word "
2016-01-14 17:30:01 +02:00
lines += $line \\ n
done
2016-01-18 21:18:23 +02:00
if ( ( do_sort = = 1 ) ) ; then
2016-01-14 17:30:01 +02:00
echo -e " $lines " | column -t -s "#" | sort -nrk 2
else
echo -e " $lines " | column -t -s "#"
fi
}
2013-02-21 16:59:44 +02:00
# Displays a list of the posts
list_posts( ) {
2016-10-24 16:49:52 +03:00
ls ./*.html & > /dev/null
( ( $? != 0) ) && echo "No posts yet. Use 'bb.sh post' to create one" && return
2013-03-13 10:16:44 +02:00
2013-02-21 17:37:55 +02:00
lines = ""
n = 1
2016-10-24 16:49:52 +03:00
while IFS = '' read -r i; do
2014-02-27 14:59:56 +02:00
is_boilerplate_file " $i " && continue
2016-01-11 04:30:41 +02:00
line = " $n # $( get_post_title " $i " ) # $( LC_ALL = $date_locale date -r " $i " +" $date_format " ) "
2016-01-10 20:31:49 +02:00
lines += $line \\ n
2015-09-23 18:43:44 +03:00
n = $(( n + 1 ))
2016-10-24 16:49:52 +03:00
done < <( ls -t ./*.html)
2013-02-21 17:37:55 +02:00
echo -e " $lines " | column -t -s "#"
2013-02-21 16:59:44 +02:00
}
# Generate the feed file
make_rss( ) {
2013-02-21 17:37:55 +02:00
echo -n "Making RSS "
2016-01-10 20:31:49 +02:00
rssfile = $blog_feed .$RANDOM
2016-01-11 00:17:48 +02:00
while [ [ -f $rssfile ] ] ; do rssfile = $blog_feed .$RANDOM ; done
2013-02-21 17:37:55 +02:00
2016-01-11 04:30:41 +02:00
{
2016-04-26 17:16:35 +03:00
pubdate = $( LC_ALL = C date +" $date_format_full " )
2016-01-11 04:30:41 +02:00
echo '<?xml version="1.0" encoding="UTF-8" ?>'
echo '<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">'
2016-02-08 11:25:43 +02:00
echo " <channel><title> $global_title </title><link> $global_url / $index_file </link> "
2016-01-11 04:30:41 +02:00
echo " <description> $global_description </description><language>en</language> "
echo " <lastBuildDate> $pubdate </lastBuildDate> "
echo " <pubDate> $pubdate </pubDate> "
echo " <atom:link href=\" $global_url / $blog_feed \" rel=\"self\" type=\"application/rss+xml\" /> "
n = 0
2016-10-24 16:49:52 +03:00
while IFS = '' read -r i; do
2016-01-11 04:30:41 +02:00
is_boilerplate_file " $i " && continue
( ( n >= number_of_feed_articles) ) && break # max 10 items
echo -n "." 1>& 3
echo '<item><title>'
get_post_title " $i "
echo '</title><description><![CDATA['
get_html_file_content 'text' 'entry' $cut_do <" $i "
2016-01-18 21:18:23 +02:00
echo " ]]></description><link> $global_url / ${ i #./ } </link> "
2016-01-11 04:30:41 +02:00
echo " <guid> $global_url / $i </guid> "
2016-03-02 21:34:54 +02:00
echo " <dc:creator> $( get_post_author " $i " ) </dc:creator> "
2016-04-26 17:16:35 +03:00
echo " <pubDate> $( LC_ALL = C date -r " $i " +" $date_format_full " ) </pubDate></item> "
2016-01-11 04:30:41 +02:00
n = $(( n + 1 ))
2016-10-24 16:49:52 +03:00
done < <( ls -t ./*.html)
2016-01-11 04:30:41 +02:00
echo '</channel></rss>'
} 3>& 1 >" $rssfile "
2013-02-21 17:37:55 +02:00
echo ""
mv " $rssfile " " $blog_feed "
chmod 644 " $blog_feed "
2013-02-21 16:59:44 +02:00
}
# generate headers, footers, etc
create_includes( ) {
2016-01-11 04:30:41 +02:00
{
2016-02-08 11:25:43 +02:00
echo " <h1 class=\"nomargin\"><a class=\"ablack\" href=\" $global_url / $index_file \"> $global_title </a></h1> "
2022-04-18 00:48:27 +03:00
echo " <div id=\"description\"> $global_description <br/> <a href= " all_posts.html">Post Archive</a> | <a href=" https://caylak.truvalinux.org.tr/p/iletisim.html" target=" _blank">Contact</a> </div> "
2016-01-11 04:30:41 +02:00
} > ".title.html"
2014-01-09 21:02:54 +02:00
2016-01-11 00:17:48 +02:00
if [ [ -f $header_file ] ] ; then cp " $header_file " .header.html
2016-01-11 04:30:41 +02:00
else {
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 '<meta http-equiv="Content-type" content="text/html;charset=UTF-8" />'
2016-01-18 21:08:52 +02:00
echo '<meta name="viewport" content="width=device-width, initial-scale=1.0" />'
2016-01-11 04:30:41 +02:00
printf '<link rel="stylesheet" href="%s" type="text/css" />\n' " ${ css_include [@] } "
2016-01-11 00:17:48 +02:00
if [ [ -z $global_feedburner ] ] ; then
2016-01-11 04:30:41 +02:00
echo " <link rel=\"alternate\" type=\"application/rss+xml\" title=\" $template_subscribe_browser_button \" href=\" $blog_feed \" /> "
2014-01-09 20:50:27 +02:00
else
2016-01-11 04:30:41 +02:00
echo " <link rel=\"alternate\" type=\"application/rss+xml\" title=\" $template_subscribe_browser_button \" href=\" $global_feedburner \" /> "
2014-01-09 20:50:27 +02:00
fi
2016-01-11 04:30:41 +02:00
} > ".header.html"
2013-02-21 17:37:55 +02:00
fi
2016-01-11 00:17:48 +02:00
if [ [ -f $footer_file ] ] ; then cp " $footer_file " .footer.html
2016-01-11 04:30:41 +02:00
else {
protected_mail = ${ global_email //@/@ }
protected_mail = ${ protected_mail //./. }
echo " <div id=\"footer\"> $global_license <a href=\" $global_author_url \"> $global_author </a> — <a href=\"mailto: $protected_mail \"> $protected_mail </a><br/> "
2022-04-18 00:48:27 +03:00
echo 'Generated with <a href="https://github.com/cfenollosa/bashblog">bashblog</a>, a single bash script to easily create blogs like this one | <a href="#top">/\</a></div>'
2016-01-11 04:30:41 +02:00
} >> ".footer.html"
2014-01-09 20:50:27 +02:00
fi
2013-02-21 16:59:44 +02:00
}
# Delete the temporarily generated include files
delete_includes( ) {
2013-02-21 17:37:55 +02:00
rm ".title.html" ".footer.html" ".header.html"
2013-02-21 16:59:44 +02:00
}
# Create the css file from scratch
create_css( ) {
2013-02-21 17:37:55 +02:00
# To avoid overwriting manual changes. However it is recommended that
# this function is modified if the user changes the blog.css file
2022-04-18 00:48:27 +03:00
( ( ${# css_include [@] } > 0 ) ) && return || css_include = ( 'main.css' 'blog.css' 'custom.css' )
2016-01-11 00:17:48 +02:00
if [ [ ! -f blog.css ] ] ; then
2013-02-21 17:37:55 +02: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; }
2016-04-13 02:19:30 +03:00
img{ max-width:100%; }
2013-02-21 17:37:55 +02:00
#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 21:49:31 +02: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
2016-01-11 00:17:48 +02:00
if [ [ -f ../style.css ] ] && [ [ ! -f main.css ] ] ; then
2013-12-23 21:49:31 +02:00
ln -s "../style.css" "main.css"
2016-01-11 00:17:48 +02:00
elif [ [ ! -f main.css ] ] ; then
2013-02-21 17:37:55 +02:00
echo ' body{ font-family:Georgia,"Times New Roman" ,Times,serif; margin:0; padding:0; background-color:#F3F3F3; }
2015-09-02 12:30:47 +03:00
#divbodyholder{padding:5px;background-color:#DDD;width:100%;max-width:874px;margin:24px auto;}
#divbody{border:solid 1px #ccc;background-color:#fff;padding:0px 48px 24px 48px;top:0;}
2013-02-21 17:37:55 +02:00
.headerholder{ background-color:#f9f9f9; border-top:solid 1px #ccc;border-left:solid 1px #ccc;border-right:solid 1px #ccc;}
2015-09-02 12:30:47 +03:00
.header{ width:100%; max-width:800px; margin:0px auto; padding-top:24px; padding-bottom:8px; }
.content{ margin-bottom:5%; }
2013-02-21 17:37:55 +02:00
.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
2022-04-18 00:48:27 +03:00
echo ' pre {
background: #f4f4f4;
border: 1px solid #ddd;
border-left: 3px solid #f36d33;
color: #666;
page-break-inside: avoid;
font-family: monospace;
font-size: 12px;
line-height: 1.6;
margin-bottom: 1.6em;
max-width: 100%;
overflow: auto;
padding: 1em 1.5em;
display: block;
word-wrap: break-word;
}
#myBtn {
display: none;
position: fixed;
bottom: 20px;
right: 30px;
z-index: 99;
font-size: 18px;
border: none;
outline: none;
background-color: red;
color: white;
cursor: pointer;
padding: 15px;
border-radius: 4px;
}
#myBtn:hover {
background-color: #555;
} ' > custom.css
2013-02-21 17:37:55 +02:00
fi
2013-02-21 16:59:44 +02:00
}
# Regenerates all the single post entries, keeping the post content but modifying
# the title, html structure, etc
rebuild_all_entries( ) {
2013-02-21 17:37:55 +02:00
echo -n "Rebuilding all entries "
2016-10-24 16:49:52 +03:00
for i in ./*.html; do
2014-02-27 14:59:56 +02:00
is_boilerplate_file " $i " && continue ;
2016-01-11 00:17:48 +02:00
contentfile = .tmp.$RANDOM
while [ [ -f $contentfile ] ] ; do contentfile = .tmp.$RANDOM ; done
2013-02-21 17:37:55 +02:00
echo -n "."
# Get the title and entry, and rebuild the html structure from scratch (divs, title, description...)
2016-01-10 20:31:49 +02:00
title = $( get_post_title " $i " )
2016-03-02 21:34:54 +02:00
2016-01-11 04:30:41 +02:00
get_html_file_content 'text' 'text' <" $i " >> " $contentfile "
2013-02-21 17:37:55 +02:00
2016-04-26 17:16:35 +03:00
# Read timestamp from post, if present, and sync file timestamp
timestamp = $( awk '/<!-- ' $date_inpost ': .+ -->/ { print }' " $i " | cut -d '#' -f 2)
2016-04-26 17:29:25 +03:00
[ [ -n $timestamp ] ] && touch -t " $timestamp " " $i "
2016-04-26 17:16:35 +03:00
# Read timestamp from file in correct format for 'create_html_page'
timestamp = $( LC_ALL = C date -r " $i " +" $date_format_full " )
2013-02-21 17:37:55 +02:00
2016-03-02 21:34:54 +02:00
create_html_page " $contentfile " " $i .rebuilt " no " $title " " $timestamp " " $( get_post_author " $i " ) "
2013-02-21 17:37:55 +02:00
# keep the original timestamp!
2016-04-26 17:16:35 +03:00
timestamp = $( LC_ALL = C date -r " $i " +" $date_format_timestamp " )
2013-02-21 17:37:55 +02:00
mv " $i .rebuilt " " $i "
chmod 644 " $i "
2015-09-23 18:43:44 +03:00
touch -t " $timestamp " " $i "
2013-02-21 17:37:55 +02:00
rm " $contentfile "
done
echo ""
2013-02-21 16:59:44 +02:00
}
# Displays the help
2014-05-19 22:08:55 +03:00
usage( ) {
echo " $global_software_name v $global_software_version "
echo " Usage: $0 command [filename] "
echo ""
echo "Commands:"
2014-06-26 05:23:38 +03:00
echo " post [-html] [filename] insert a new blog post, or the filename of a draft to continue editing it"
echo " it tries to use markdown by default, and falls back to HTML if it's not available."
echo " use '-html' to override it and edit the post as HTML even when markdown is available"
2014-05-19 22:08:55 +03:00
echo " edit [-n|-f] [filename] edit an already published .html or .md file. **NEVER** edit manually a published .html file,"
echo " always use this function as it keeps internal data and rebuilds the blog"
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)"
echo " delete [filename] deletes the post and rebuilds the blog"
echo " rebuild regenerates all the pages and posts, preserving the content of the entries"
echo " reset deletes everything except this script. Use with a lot of caution and back up first!"
echo " list list all posts"
2016-01-14 17:30:01 +02:00
echo " tags [-n] list all tags in alphabetical order"
echo " use '-n' to sort list by number of posts"
2014-05-19 22:08:55 +03:00
echo ""
echo " For more information please open $0 in a code editor and read the header and comments "
2013-02-21 16:59:44 +02:00
}
# Delete all generated content, leaving only this script
reset( ) {
2013-02-21 17:37:55 +02:00
echo "Are you sure you want to delete all blog entries? Please write \"Yes, I am!\" "
2015-09-23 18:43:44 +03:00
read -r line
2016-01-11 00:17:48 +02:00
if [ [ $line = = "Yes, I am!" ] ] ; then
2016-10-24 16:49:52 +03:00
rm .*.html ./*.html ./*.css ./*.rss & > /dev/null
2013-03-13 10:16:44 +02:00
echo
2013-02-21 17:37:55 +02:00
echo "Deleted all posts, stylesheets and feeds."
2013-03-13 10:16:44 +02:00
echo "Kept your old '.backup.tar.gz' just in case, please delete it manually if needed."
2013-02-21 17:37:55 +02:00
else
echo "Phew! You dodged a bullet there. Nothing was modified."
fi
2013-02-21 16:59:44 +02:00
}
2013-06-05 13:36:47 +03:00
# Detects if GNU date is installed
2013-04-02 17:40:10 +03:00
date_version_detect( ) {
2013-04-09 18:09:23 +03:00
date --version >/dev/null 2>& 1
2016-01-11 00:17:48 +02:00
if ( ( $? != 0) ) ; then
2013-04-02 17:40:10 +03:00
# date utility is BSD. Test if gdate is installed
if gdate --version >/dev/null 2>& 1 ; then
2013-06-05 13:36:47 +03:00
date( ) {
gdate " $@ "
}
2013-04-02 17:40:10 +03:00
else
2013-12-23 21:49:31 +02:00
# BSD date
date( ) {
2016-01-11 00:17:48 +02:00
if [ [ $1 = = -r ] ] ; then
2013-12-23 21:49:31 +02:00
# Fall back to using stat for 'date -r'
2016-01-11 04:30:41 +02:00
format = ${ 3 //+/ }
2013-12-23 21:49:31 +02:00
stat -f "%Sm" -t " $format " " $2 "
2016-01-11 04:30:41 +02:00
elif [ [ $2 = = --date* ] ] ; then
2013-12-23 21:49:31 +02:00
# convert between dates using BSD date syntax
2016-04-26 17:16:35 +03:00
command date -j -f " $date_format_full " " ${ 2 #--date= } " " $1 "
2013-12-23 21:49:31 +02:00
else
# acceptable format for BSD date
2014-09-12 18:00:01 +03:00
command date -j " $@ "
2013-12-23 21:49:31 +02:00
fi
}
fi
fi
2013-04-02 17:40:10 +03:00
}
2013-04-09 18:25:35 +03:00
2013-02-21 16:59:44 +02:00
# Main function
2013-03-07 22:51:54 +02:00
# Encapsulated on its own function for readability purposes
2013-02-21 16:59:44 +02:00
#
# $1 command to run
# $2 file name of a draft to continue editing (optional)
do_main( ) {
2013-04-02 17:40:10 +03:00
# Detect if using BSD date or GNU date
date_version_detect
2013-06-05 13:36:47 +03:00
# Load default configuration, then override settings with the config file
global_variables
2016-01-11 00:17:48 +02:00
[ [ -f $global_config ] ] && source " $global_config " & > /dev/null
2014-01-09 20:50:27 +02:00
global_variables_check
2013-02-21 17:37:55 +02:00
2013-03-07 20:30:59 +02:00
# Check for $EDITOR
2016-01-11 00:17:48 +02:00
[ [ -z $EDITOR ] ] &&
2016-10-24 12:41:30 +03:00
echo "Please set your \$EDITOR environment variable. For example, to use nano, add the line 'export EDITOR=nano' to your \$HOME/.bashrc file" && exit
2013-03-07 20:30:59 +02:00
2013-02-21 17:37:55 +02:00
# Check for validity of argument
2016-01-14 17:30:01 +02:00
[ [ $1 != "reset" && $1 != "post" && $1 != "rebuild" && $1 != "list" && $1 != "edit" && $1 != "delete" && $1 != "tags" ] ] &&
2013-12-23 21:49:31 +02:00
usage && exit
2013-02-21 17:37:55 +02:00
2016-01-11 00:17:48 +02:00
[ [ $1 = = list ] ] &&
2013-12-23 21:49:31 +02:00
list_posts && exit
2013-02-21 17:37:55 +02:00
2016-01-14 17:30:01 +02:00
[ [ $1 = = tags ] ] &&
list_tags " $@ " && exit
2016-01-11 00:17:48 +02:00
if [ [ $1 = = edit ] ] ; then
if ( ( $# < 2) ) || [ [ ! -f ${ !# } ] ] ; then
2014-09-01 17:13:06 +03:00
echo "Please enter a valid .md or .html file to edit"
2013-03-07 19:54:33 +02:00
exit
fi
fi
2013-02-21 17:37:55 +02:00
# Test for existing html files
2016-10-24 16:49:52 +03:00
if ls ./*.html & > /dev/null; then
2016-01-11 17:10:17 +02:00
# We're going to back up just in case
2016-10-24 16:49:52 +03:00
tar -c -z -f ".backup.tar.gz" -- *.html &&
2016-01-11 17:10:17 +02:00
chmod 600 ".backup.tar.gz"
elif [ [ $1 = = rebuild ] ] ; then
echo "Can't find any html files, nothing to rebuild"
exit
fi
2013-02-21 17:37:55 +02:00
2014-03-01 18:37:57 +02:00
# Keep first backup of this day containing yesterday's version of the blog
2016-01-11 17:10:17 +02:00
[ [ ! -f .yesterday.tar.gz || $( date -r .yesterday.tar.gz +'%d' ) != " $( date +'%d' ) " ] ] &&
2014-03-01 18:37:57 +02:00
cp .backup.tar.gz .yesterday.tar.gz & > /dev/null
2016-01-11 00:17:48 +02:00
[ [ $1 = = reset ] ] &&
2013-12-23 21:49:31 +02:00
reset && exit
2013-02-21 17:37:55 +02:00
create_css
2014-03-01 17:22:28 +02:00
create_includes
2016-01-11 00:17:48 +02:00
[ [ $1 = = post ] ] && write_entry " $@ "
[ [ $1 = = rebuild ] ] && rebuild_all_entries && rebuild_tags
[ [ $1 = = delete ] ] && rm " $2 " & > /dev/null && rebuild_tags
if [ [ $1 = = edit ] ] ; then
if [ [ $2 = = -n ] ] ; then
2014-03-09 12:49:18 +02:00
edit " $3 "
2016-01-11 00:17:48 +02:00
elif [ [ $2 = = -f ] ] ; then
2014-03-09 12:49:18 +02:00
edit " $3 " full
else
edit " $2 " keep
fi
fi
2013-02-21 17:37:55 +02:00
rebuild_index
all_posts
2014-02-27 19:07:01 +02:00
all_tags
2013-02-21 17:37:55 +02:00
make_rss
delete_includes
2013-02-21 16:59:44 +02:00
}
#
# MAIN
# Do not change anything here. If you want to modify the code, edit do_main()
#
2015-09-23 18:43:44 +03:00
do_main " $@ "
2014-03-01 17:35:00 +02:00
2014-03-18 22:40:50 +02:00
# vim: set shiftwidth=4 tabstop=4 expandtab: