Merge pull request #13 from carlosbm/master

Disqus and other enhancements
This commit is contained in:
Carlos Fenollosa 2013-06-05 03:05:13 -07:00
commit 1f1ca64990
2 changed files with 87 additions and 9 deletions

View File

@ -50,6 +50,5 @@ Features
Non features (not planned)
--------------------------
- Comments. Would need a CAPTCHA or another antispam mechanism. Comments are handled through twitter, with a Twitter button
Read the CHANGELOG section of the script header for more updates

95
bb.sh
View File

@ -15,7 +15,7 @@
# 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.
#
# Comments are not supported.
# Comments are supported via external service (Disqus).
#
# This script is standalone, it doesn't require any other file to run
#
@ -60,6 +60,8 @@
#
#########################################################################################
#
# 1.6.1 'date' fix when hours are 1 digit.
# 1.6.0 Disqus comments. External configuration file. Check of 'date' command version.
# 1.5.1 Misc bugfixes and parameter checks
# 1.5 Durad Radojicic refactored some code and added flexibility and i18n
# 1.4.2 Now issues are handled at Github
@ -85,9 +87,15 @@
# Global variables
# It is recommended to perform a 'rebuild' after changing any of this in the code
# Config file. Use instead of this function if you want to avoid merges in VCS
global_config=".config"
global_variables() {
echo Loading inline configuration
global_software_name="BashBlog"
global_software_version="1.5.1"
global_software_version="1.6.1"
# Blog title
global_title="My fancy blog"
@ -118,6 +126,10 @@ global_variables() {
global_twitter="true"
global_twitter_username="example"
# Leave these empty if you don't want to use disqus for comments
global_disqus="true"
global_disqus_username="disqus_undefined"
# Blog generated files
# index page of blog (it is usually good to use "index.html" here)
@ -169,6 +181,45 @@ google_analytics() {
</script>"
}
# Prints the required code for disqus comments
disqus_body() {
if [ "$global_disqus" != "" ]; then
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>'
fi
}
# Prints the required code for disqus in the footer
disqus_footer() {
if [ "$global_disqus" != "" ]; then
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>'
fi
}
# 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'.
@ -178,7 +229,7 @@ google_analytics() {
#
# $1 the file to edit
edit() {
timestamp="$(date -r $1 +'%Y%m%d%k%M')"
timestamp="$(date -r $1 +'%Y%m%d%H%M')"
$EDITOR "$1"
touch -t $timestamp "$1"
}
@ -187,8 +238,8 @@ edit() {
#
# $1 the post URL
twitter() {
echo "<p id='twitter'>$template_comments &nbsp;"
echo "<a href=\"https://twitter.com/share\" class=\"twitter-share-button\" data-text=\"$template_twitter_comment\" data-url=\"$1\""
echo "<p id='twitter'> <a href=\"$1#disqus_thread\">$template_comments</a> &nbsp;"
echo "<a href=\"https://twitter.com/share\" class=\"twitter-share-button\" data-text=\"&lt;Type your comment here but please leave the URL so that other people can follow the comments&gt;\" data-url=\"$1\""
if [ "$global_twitter_username" != "" ]; then
echo " data-via=\"$global_twitter_username\""
@ -258,10 +309,16 @@ create_html_page() {
fi
echo '</div>' >> "$filename" # content
# Add disqus commments except for index and all_posts pages
if [[ ${filename%.*.*} != "index" && ${filename%.*.*} != "all_posts" ]]; then
disqus_body >> "$filename"
fi
# page footer
cat .footer.html >> "$filename"
# close divs
echo '</div></div>' >> "$filename" # divbody and divbodyholder
disqus_footer >> "$filename"
echo '</body></html>' >> "$filename"
}
@ -422,7 +479,7 @@ rebuild_index() {
# Displays a list of the posts
list_posts() {
ls *.html &> /dev/null
if [[ $? -ne 0 ]]; then
if [[ $? -ne 0 ]]; then
echo "No posts yet. Use 'bb.sh post' to create one"
return
fi
@ -568,7 +625,7 @@ rebuild_all_entries() {
create_html_page "$contentfile" "$i.rebuilt" no "$title" "$timestamp"
# keep the original timestamp!
timestamp="$(date -r $i +'%Y%m%d%k%M')"
timestamp="$(date -r $i +'%Y%m%d%H%M')"
mv "$i.rebuilt" "$i"
chmod 644 "$i"
touch -t $timestamp "$i"
@ -608,13 +665,35 @@ reset() {
fi
}
date_version_detect() {
date --version >/dev/null 2>&1
if [ $? -ne 0 ]; then
# date utility is BSD. Test if gdate is installed
if gdate --version >/dev/null 2>&1 ; then
date() {
gdate "$@"
}
echo Using gdate.
else
echo ERROR: Not GNU date found.
echo Try installing gdate utility or coreutils.
echo Exiting...
exit
fi
fi
}
# Main function
# Encapsulated on its own function for readability purposes
#
# $1 command to run
# $2 file name of a draft to continue editing (optional)
do_main() {
global_variables
# Detect if using BSD date or GNU date
date_version_detect
# Use config file or fallback to inline configuration
echo Loading configuration
source "$global_config" &> /dev/null || global_variables
# Check for $EDITOR
if [[ -z "$EDITOR" ]]; then