Fixed variables problem and removed html edit.

This commit is contained in:
Josemar Lohn 2021-02-11 03:44:09 -03:00
parent 26468aec34
commit e6161e9717
28 changed files with 223 additions and 213 deletions

View File

@ -1,11 +1,12 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# Create the css file from scratch # Create the css file from scratch
declare css_include
create_css() { create_css() {
# To avoid overwriting manual changes. However it is recommended that # To avoid overwriting manual changes. However it is recommended that
# this function is modified if the user changes the blog.css file # this function is modified if the user changes the blog.css file
(( ${#css_include[@]} > 0 )) && return || css_include=('main.css' 'blog.css') ((${#css_include[@]} > 0)) && return || css_include=('main.css' 'blog.css')
if [[ ! -f blog.css ]]; then if [[ ! -f blog.css ]]; then
# blog.css directives will be loaded after main.css and thus will prevail # blog.css directives will be loaded after main.css and thus will prevail
echo '#title{font-size: x-large;} echo '#title{font-size: x-large;}
a.ablack{color:black !important;} a.ablack{color:black !important;}
@ -19,14 +20,14 @@ create_css() {
h3{margin-top:42px;margin-bottom:8px;} h3{margin-top:42px;margin-bottom:8px;}
h4{margin-left:24px;margin-right:24px;} h4{margin-left:24px;margin-right:24px;}
img{max-width:100%;} img{max-width:100%;}
#twitter{line-height:20px;vertical-align:top;text-align:right;font-style:italic;color:#333;margin-top:24px;font-size:14px;}' > blog.css #twitter{line-height:20px;vertical-align:top;text-align:right;font-style:italic;color:#333;margin-top:24px;font-size:14px;}' >blog.css
fi fi
# If there is a style.css from the parent page (i.e. some landing page) # 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 # 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 # home page. Feel free to edit it out, though it doesn't hurt
if [[ -f ../style.css ]] && [[ ! -f main.css ]]; then if [[ -f ../style.css ]] && [[ ! -f main.css ]]; then
ln -s "../style.css" "main.css" ln -s "../style.css" "main.css"
elif [[ ! -f main.css ]]; then elif [[ ! -f main.css ]]; then
echo 'body{font-family:Georgia,"Times New Roman",Times,serif;margin:0;padding:0;background-color:#F3F3F3;} echo 'body{font-family:Georgia,"Times New Roman",Times,serif;margin:0;padding:0;background-color:#F3F3F3;}
#divbodyholder{padding:5px;background-color:#DDD;width:100%;max-width:874px;margin:24px auto;} #divbodyholder{padding:5px;background-color:#DDD;width:100%;max-width:874px;margin:24px auto;}
@ -43,6 +44,6 @@ create_css() {
a:visited{text-decoration:none;color:#336699 !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{background-color:#f9f9f9;border-left:solid 4px #e9e9e9;margin-left:12px;padding:12px 12px 12px 24px;}
blockquote img{margin:12px 0px;} blockquote img{margin:12px 0px;}
blockquote iframe{margin:12px 0px;}' > main.css blockquote iframe{margin:12px 0px;}' >main.css
fi fi
} }

View File

@ -18,16 +18,16 @@ do_main() {
# Load default configuration, then override settings with the config file # Load default configuration, then override settings with the config file
global_variables global_variables
# shellcheck disable=SC1090 # variable config file # shellcheck disable=SC1090 # variable config file
[[ -f "$global_config" ]] && source "$global_config" &> /dev/null [[ -f "$global_config" ]] && source "$global_config" &>/dev/null
global_variables_check global_variables_check
# Check for $EDITOR # Check for $EDITOR
[[ -z $EDITOR ]] && [[ -z $EDITOR ]] &&
echo "Please set your \$EDITOR environment variable. For example, to use nano, add the line 'export EDITOR=nano' to your \$HOME/.bashrc file" && exit EDITOR=vim
# Check for validity of argument # Check for validity of argument
[[ $1 != "reset" && $1 != "post" && $1 != "rebuild" && $1 != "list" && $1 != "edit" && $1 != "delete" && $1 != "tags" ]] && [[ $1 != "reset" && $1 != "post" && $1 != "rebuild" && $1 != "list" && $1 != "edit" && $1 != "delete" && $1 != "tags" ]] &&
usage && return usage && return
[[ $1 == list ]] && [[ $1 == list ]] &&
list_posts && return list_posts && return
@ -43,10 +43,10 @@ do_main() {
fi fi
# Test for existing html files # Test for existing html files
if ls ./*.md &> /dev/null; then if ls ./*.md &>/dev/null; then
# We're going to back up just in case # We're going to back up just in case
tar -c -z -f ".backup.tar.gz" -- *.html && tar -c -z -f ".backup.tar.gz" -- *.html &&
chmod 600 ".backup.tar.gz" chmod 600 ".backup.tar.gz"
elif [[ $1 == rebuild ]]; then elif [[ $1 == rebuild ]]; then
echo "Can't find any html files, nothing to rebuild" echo "Can't find any html files, nothing to rebuild"
return return
@ -54,7 +54,7 @@ do_main() {
# Keep first backup of this day containing yesterday's version of the blog # Keep first backup of this day containing yesterday's version of the blog
[[ ! -f .yesterday.tar.gz || $(date -r .yesterday.tar.gz +'%d') != "$(date +'%d')" ]] && [[ ! -f .yesterday.tar.gz || $(date -r .yesterday.tar.gz +'%d') != "$(date +'%d')" ]] &&
cp .backup.tar.gz .yesterday.tar.gz &> /dev/null cp .backup.tar.gz .yesterday.tar.gz &>/dev/null
[[ $1 == reset ]] && [[ $1 == reset ]] &&
reset && return reset && return
@ -63,7 +63,7 @@ do_main() {
create_includes create_includes
[[ $1 == post ]] && write_entry "$@" [[ $1 == post ]] && write_entry "$@"
[[ $1 == rebuild ]] && rebuild_all_entries && rebuild_tags [[ $1 == rebuild ]] && rebuild_all_entries && rebuild_tags
[[ $1 == delete ]] && rm "$2" &> /dev/null && rebuild_tags [[ $1 == delete ]] && rm "$2" &>/dev/null && rebuild_tags
if [[ $1 == edit ]]; then if [[ $1 == edit ]]; then
if [[ $2 == -n ]]; then if [[ $2 == -n ]]; then
edit "$3" edit "$3"
@ -80,4 +80,4 @@ do_main() {
make_gophermap make_gophermap
make_gemini make_gemini
delete_includes delete_includes
} }

View File

@ -12,16 +12,18 @@
# "keep" to keep old filename # "keep" to keep old filename
# "full" to edit full HTML, and not only text part (keeps 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) # leave empty for default behavior (edit only text part and change name)
declare date_format_full
declare date_format_timestamp
declare template_tags_line_header
declare prefix_tags
edit() { edit() {
declare date_format_full
declare date_format_timestamp [[ ! -f "${1%%.*}.html" ]] &&
declare template_tags_line_header
declare prefix_tags
[[ ! -f "${1%%.*}.html" ]] && \
printf "Can't edit post \"%s.html\", did you mean to use \"bb.sh post <draft_file>\"?\\n" "${1%%.*}" && exit 1 printf "Can't edit post \"%s.html\", did you mean to use \"bb.sh post <draft_file>\"?\\n" "${1%%.*}" && exit 1
# Original post timestamp # Original post timestamp
edit_timestamp=$(LC_ALL=C date -r "${1%%.*}.html" +"$date_format_full" ) edit_timestamp=$(LC_ALL=C date -r "${1%%.*}.html" +"$date_format_full")
touch_timestamp=$(LC_ALL=C date -r "${1%%.*}.html" +"$date_format_timestamp") touch_timestamp=$(LC_ALL=C date -r "${1%%.*}.html" +"$date_format_timestamp")
tags_before=$(tags_in_post "${1%%.*}.html") tags_before=$(tags_in_post "${1%%.*}.html")
if [[ $2 == full ]]; then if [[ $2 == full ]]; then
@ -29,21 +31,17 @@ edit() {
filename=$1 filename=$1
else else
if [[ ${1##*.} == md ]]; then if [[ ${1##*.} == md ]]; then
if test_markdown; then
echo "Markdown is not working, please edit HTML file directly."
exit
fi
# editing markdown file # editing markdown file
$EDITOR "$1" $EDITOR "$1"
TMPFILE=$(mrkdwn "$1") TMPFILE=$(make_html "$1")
filename=${1%%.*}.html filename=${1%%.*}.html
else else
# Create the content file # Create the content file
TMPFILE=$(basename "$1").$RANDOM.html TMPFILE=$(basename "$1").$RANDOM.html
# Title # Title
get_post_title "$1" > "$TMPFILE" get_post_title "$1" >"$TMPFILE"
# Post text with plaintext tags # Post text with plaintext tags
get_html_file_content 'text' 'text' <"$1" | sed "/^<p>$template_tags_line_header/s|<a href='$prefix_tags\([^']*\).html'>\\1</a>|\\1|g" >> "$TMPFILE" get_html_file_content 'text' 'text' <"$1" | sed "/^<p>$template_tags_line_header/s|<a href='$prefix_tags\([^']*\).html'>\\1</a>|\\1|g" >>"$TMPFILE"
$EDITOR "$TMPFILE" $EDITOR "$TMPFILE"
filename=$1 filename=$1
fi fi

View File

@ -6,9 +6,9 @@
# $3 "cut" to remove text from <hr /> to <!-- text end --> # $3 "cut" to remove text from <hr /> to <!-- text end -->
# note that this does not remove <hr /> line itself, # note that this does not remove <hr /> line itself,
# so you can see if text was cut or not # so you can see if text was cut or not
declare cut_line cut_tags
declare template_tags_line_header
get_html_file_content() { get_html_file_content() {
declare cut_line cut_tags
declare template_tags_line_header
awk "/<!-- $1 begin -->/, /<!-- $2 end -->/{ awk "/<!-- $1 begin -->/, /<!-- $2 end -->/{
if (!/<!-- $1 begin -->/ && !/<!-- $2 end -->/) print if (!/<!-- $1 begin -->/ && !/<!-- $2 end -->/) print
if (\"$3\" == \"cut\" && /$cut_line/){ if (\"$3\" == \"cut\" && /$cut_line/){
@ -18,4 +18,4 @@ get_html_file_content() {
} }
} }
}" }"
} }

View File

@ -5,5 +5,5 @@
# $1 the html file # $1 the html file
get_post_title() { get_post_title() {
#awk '/<h3><a class="ablack" href=".+">/, /<\/a><\/h3>/{if (!/<h3><a class="ablack" href=".+">/ && !/<\/a><\/h3>/) print}' "$1" #awk '/<h3><a class="ablack" href=".+">/, /<\/a><\/h3>/{if (!/<h3><a class="ablack" href=".+">/ && !/<\/a><\/h3>/) print}' "$1"
head -1 $1 head -1 "$1"
} }

View File

@ -13,14 +13,13 @@ global_config=".tildelog"
global_variables() { global_variables() {
global_software_name="tildelog" global_software_name="tildelog"
global_software_version="0.1" global_software_version="0.1"
# Blog title # Blog title
global_title="my tildelog" global_title="my tildelog"
# The typical subtitle for each blog # The typical subtitle for each blog
global_description="a blog about tildes" global_description="a blog about tildes"
# The server base domain # The server base domain
global_domain="tilde.team" global_domain="tilde.team"
# The public base URL for this blog # The public base URL for this blog
global_url="https://${global_domain}/~$USER/blog" global_url="https://${global_domain}/~$USER/blog"
# Your name # Your name
global_author="~$USER" global_author="~$USER"
@ -32,8 +31,7 @@ global_variables() {
# CC by-nc-nd is a good starting point, you can change this to "&copy;" for Copyright # CC by-nc-nd is a good starting point, you can change this to "&copy;" for Copyright
global_license="CC by-nc-nd" global_license="CC by-nc-nd"
# Leave this empty (i.e. "") if you don't want to use feedburner,
# Leave this empty (i.e. "") if you don't want to use feedburner,
# or change it to your own URL # or change it to your own URL
global_feedburner="" global_feedburner=""
@ -73,8 +71,6 @@ global_variables() {
# Regexp matching the HTML line where to do the cut # Regexp matching the HTML line where to do the cut
# note that slash is regexp separator so you need to prepend it with backslash # note that slash is regexp separator so you need to prepend it with backslash
cut_line='<hr ?\/?>' cut_line='<hr ?\/?>'
# save markdown file when posting with "bb post -m". Leave blank to discard it.
save_markdown="yes"
# prefix for tags/categories files # prefix for tags/categories files
# please make sure that no other html file starts with this prefix # please make sure that no other html file starts with this prefix
prefix_tags="tag_" prefix_tags="tag_"
@ -110,7 +106,7 @@ global_variables() {
template_tags_title="all tags" template_tags_title="all tags"
# "posts" (on "All tags" page, text at the end of each tag line, like "2. Music - 15 posts") # "posts" (on "All tags" page, text at the end of each tag line, like "2. Music - 15 posts")
template_tags_posts="posts" template_tags_posts="posts"
template_tags_posts_2_4="posts" # Some slavic languages use a different plural form for 2-4 items template_tags_posts_2_4="posts" # Some slavic languages use a different plural form for 2-4 items
template_tags_posts_singular="post" template_tags_posts_singular="post"
# "Posts tagged" (text on a title of a page with index of one tag, like "My Blog - Posts tagged "Music"") # "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" template_tag_title="posts tagged"
@ -149,17 +145,17 @@ global_variables() {
# Markdown location. Trying to autodetect by default. # Markdown location. Trying to autodetect by default.
# The invocation must support the signature 'markdown_bin in.md > out.html' # The invocation must support the signature 'markdown_bin in.md > out.html'
# shellcheck disable=SC2016
markdown_bin=$(which md2html.awk) md2html_bin='awk "$md2html_awk"'
} }
# Check for the validity of some variables # Check for the validity of some variables
# DO NOT EDIT THIS FUNCTION unless you know what you're doing # DO NOT EDIT THIS FUNCTION unless you know what you're doing
global_variables_check() { global_variables_check() {
[[ $header_file == .header.html ]] && \ [[ $header_file == .header.html ]] &&
echo "Please check your configuration. '.header.html' is not a valid value for the setting 'header_file'" && \ echo "Please check your configuration. '.header.html' is not a valid value for the setting 'header_file'" &&
exit exit
[[ $footer_file == .footer.html ]] && \ [[ $footer_file == .footer.html ]] &&
echo "Please check your configuration. '.footer.html' is not a valid value for the setting 'footer_file'" && \ echo "Please check your configuration. '.footer.html' is not a valid value for the setting 'footer_file'" &&
exit exit
} }

View File

@ -8,19 +8,17 @@
# #
# Return 0 (bash return value 'true') if the input file is an index, feed, etc # Return 0 (bash return value 'true') if the input file is an index, feed, etc
# or 1 (bash return value 'false') if it is a blogpost # or 1 (bash return value 'false') if it is a blogpost
declare non_blogpost_files
declare index_file
declare archive_index
declare gophermap
declare gemini_index
declare tags_index
declare footer_file
declare header_file
declare prefix_tags
declare html_exclude
is_boilerplate_file() { is_boilerplate_file() {
return 1 ### Ugly Hack to Temporally disable the verification
declare non_blogpost_files
declare index_file
declare archive_index
declare gophermap
declare gemini_index
declare tags_index
declare footer_file
declare header_file
declare prefix_tags
declare html_exclude
name=${1#./} name=${1#./}
# First check against user-defined non-blogpost pages # First check against user-defined non-blogpost pages
for item in "${non_blogpost_files[@]}"; do for item in "${non_blogpost_files[@]}"; do
@ -28,12 +26,14 @@ is_boilerplate_file() {
done done
case $name in case $name in
( "$index_file" | "$archive_index" | "$gophermap" | "$gemini_index" | "$tags_index" | "$footer_file" | "$header_file" | "$prefix_tags"* ) "$index_file" | "$archive_index" | "$gophermap" | "$gemini_index" | "$tags_index" | "$footer_file" | "$header_file" | "$prefix_tags"*)
return 0 ;; return 0
( * ) # Check for excluded ;;
for excl in "${html_exclude[@]}"; do *) # Check for excluded
[[ $name == "$excl" ]] && return 0 for excl in "${html_exclude[@]}"; do
done [[ $name == "$excl" ]] && return 0
return 1 ;; done
return 1
;;
esac esac
} }

View File

@ -3,13 +3,15 @@
# Displays a list of the tags # Displays a list of the tags
# #
# $2 if "-n", tags will be sorted by number of posts # $2 if "-n", tags will be sorted by number of posts
declare prefix_tags
declare template_tags_posts_singular
declare template_tags_posts
list_tags() { list_tags() {
declare prefix_tags
declare template_tags_posts_singular
declare template_tags_posts
if [[ $2 == -n ]]; then do_sort=1; else do_sort=0; fi if [[ $2 == -n ]]; then do_sort=1; else do_sort=0; fi
if ls "./${prefix_tags}"*.html > /dev/null; then if ls "./${prefix_tags}"*.html >/dev/null; then
echo "No posts yet. Use 'bb.sh post' to create one" && return echo "No posts yet. Use 'bb.sh post' to create one" && return
fi fi
@ -24,9 +26,9 @@ list_tags() {
lines+=$line\\n lines+=$line\\n
done done
if (( do_sort == 1 )); then if ((do_sort == 1)); then
echo -e "$lines" | column -t -s "#" | sort -nrk 2 echo -e "$lines" | column -t -s "#" | sort -nrk 2
else else
echo -e "$lines" | column -t -s "#" echo -e "$lines" | column -t -s "#"
fi fi
} }

View File

@ -1,7 +1,9 @@
#!/usr/bin/env bash #!/usr/bin/env bash
declare gemini_index
make_gemini() { make_gemini() {
declare gemini_index return
if [ ! -d "${HOME}/public_gemini" ]; then if [ ! -d "${HOME}/public_gemini" ]; then
printf "Creating ~/public_gemini\\n" printf "Creating ~/public_gemini\\n"
mkdir "${HOME}/public_gemini" mkdir "${HOME}/public_gemini"
@ -12,7 +14,7 @@ make_gemini() {
fi fi
if [ ! -f "${HOME}/public_gemini/blog/$gemini_index" ]; then if [ ! -f "${HOME}/public_gemini/blog/$gemini_index" ]; then
cat <<- 'EOF' > "${HOME}/public_gemini/blog/${gemini_index}" cat <<-'EOF' >"${HOME}/public_gemini/blog/${gemini_index}"
#!/usr/bin/env sh #!/usr/bin/env sh
printf "20 text/gemini\r\n" printf "20 text/gemini\r\n"
printf "my bashblog posts\r\n" printf "my bashblog posts\r\n"
@ -24,4 +26,4 @@ make_gemini() {
EOF EOF
chmod +x "${HOME}/public_gemini/blog/${gemini_index}" chmod +x "${HOME}/public_gemini/blog/${gemini_index}"
fi fi
} }

View File

@ -1,7 +1,9 @@
#!/usr/bin/env bash #!/usr/bin/env bash
declare gophermap
make_gophermap() { make_gophermap() {
declare gophermap return
if [ ! -d "${HOME}/public_gopher" ]; then if [ ! -d "${HOME}/public_gopher" ]; then
printf "Creating gopher hole\\n" printf "Creating gopher hole\\n"
mkdir "${HOME}/public_gopher" mkdir "${HOME}/public_gopher"
@ -12,7 +14,7 @@ make_gophermap() {
fi fi
if [ ! -f "${HOME}/public_gopher/blog/$gophermap" ]; then if [ ! -f "${HOME}/public_gopher/blog/$gophermap" ]; then
cat <<- 'EOF' > "${HOME}/public_html/blog/${gophermap}" cat <<-'EOF' >"${HOME}/public_html/blog/${gophermap}"
#!/usr/bin/env sh #!/usr/bin/env sh
printf "my bashblog posts\n" printf "my bashblog posts\n"
user=$(stat -c '%U' .) user=$(stat -c '%U' .)
@ -24,4 +26,4 @@ EOF
chmod +x "${HOME}/public_html/blog/${gophermap}" chmod +x "${HOME}/public_html/blog/${gophermap}"
fi fi
chmod 644 "./*.md" chmod 644 "./*.md"
} }

12
lib/make_html.sh Normal file
View File

@ -0,0 +1,12 @@
#!/usr/bin/env bash
# Parse a Markdown file into HTML and return the generated file
declare md2html_bin
make_html() {
out=${1%.md}.html
while [[ -f $out ]]; do
out=${out%.html}.$RANDOM.html
done
eval "$md2html_bin" "$1" >"$out"
echo "$out"
}

View File

@ -1,15 +1,16 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# Generate the feed file # Generate the feed file
declare blog_feed
declare date_format_full
declare global_title
declare global_url
declare global_description
declare index_file
declare number_of_feed_articles
declare cut_do
make_rss() { make_rss() {
declare blog_feed
declare date_format_full
declare global_title
declare global_url
declare global_description
declare index_file
declare number_of_feed_articles
declare cut_do
echo -n "Making RSS " echo -n "Making RSS "
rssfile=$blog_feed.$RANDOM rssfile=$blog_feed.$RANDOM
@ -17,8 +18,8 @@ make_rss() {
{ {
pubdate=$(LC_ALL=C date +"$date_format_full") pubdate=$(LC_ALL=C date +"$date_format_full")
echo '<?xml version="1.0" encoding="UTF-8" ?>' 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/">' echo '<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">'
echo "<channel><title>$global_title</title><link>$global_url/$index_file</link>" echo "<channel><title>$global_title</title><link>$global_url/$index_file</link>"
echo "<description>$global_description</description><language>en</language>" echo "<description>$global_description</description><language>en</language>"
echo "<lastBuildDate>$pubdate</lastBuildDate>" echo "<lastBuildDate>$pubdate</lastBuildDate>"
@ -30,16 +31,16 @@ make_rss() {
is_boilerplate_file "$i" && continue is_boilerplate_file "$i" && continue
((n >= number_of_feed_articles)) && break # max 10 items ((n >= number_of_feed_articles)) && break # max 10 items
echo -n "." 1>&3 echo -n "." 1>&3
echo '<item><title>' echo '<item><title>'
get_post_title "$i" get_post_title "$i"
echo '</title><description><![CDATA[' echo '</title><description><![CDATA['
get_html_file_content 'text' 'entry' "$cut_do" <"$i" get_html_file_content 'text' 'entry' "$cut_do" <"$i"
echo "]]></description><link>$global_url/${i#./}</link>" echo "]]></description><link>$global_url/${i#./}</link>"
echo "<guid>$global_url/$i</guid>" echo "<guid>$global_url/$i</guid>"
echo "<dc:creator>$(get_post_author "$i")</dc:creator>" echo "<dc:creator>$(get_post_author "$i")</dc:creator>"
echo "<pubDate>$(LC_ALL=C date -r "$i" +"$date_format_full")</pubDate></item>" echo "<pubDate>$(LC_ALL=C date -r "$i" +"$date_format_full")</pubDate></item>"
n=$(( n + 1 )) n=$((n + 1))
done < <(ls -t ./*.html) done < <(ls -t ./*.html)
echo '</channel></rss>' echo '</channel></rss>'
@ -48,4 +49,4 @@ make_rss() {
mv "$rssfile" "$blog_feed" mv "$rssfile" "$blog_feed"
chmod 644 "$blog_feed" chmod 644 "$blog_feed"
} }

View File

@ -1,4 +1,4 @@
#!/bin/awk -f #!/usr/bin/awk -f
# #
# by: Josemar Lohn <j@lo.hn> # by: Josemar Lohn <j@lo.hn>
# lo.hn on www/gemini/gopher # lo.hn on www/gemini/gopher

View File

@ -1,13 +0,0 @@
#!/usr/bin/env bash
# Parse a Markdown file into HTML and return the generated file
declare markdown_bin
mrkdwn() {
out=${1%.md}.html
while [[ -f $out ]]
do
out=${out%.md}.$RANDOM.html
done
$markdown_bin "$1" > "$out"
echo "$out"
}

View File

@ -8,11 +8,13 @@
# note that although timestamp is optional, something must be provided at its # note that although timestamp is optional, something must be provided at its
# place if destination file name is provided, i.e: # place if destination file name is provided, i.e:
# parse_file source.txt "" destination.html # parse_file source.txt "" destination.html
declare convert_filename
declare template_tags_line_header
declare prefix_tags
declare global_author
parse_file() { parse_file() {
declare convert_filename
declare template_tags_line_header
declare prefix_tags
declare global_author
# Read for the title and check that the filename is ok # Read for the title and check that the filename is ok
title="" title=""
while IFS='' read -r line; do while IFS='' read -r line; do
@ -36,18 +38,18 @@ parse_file() {
# Parse possible tags # Parse possible tags
elif [[ $line == "<p>$template_tags_line_header"* ]]; then 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') 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" IFS=, read -r -a array <<<"$tags"
echo -n "<p>$template_tags_line_header " >> "$content" echo -n "<p>$template_tags_line_header " >>"$content"
for item in "${array[@]}"; do for item in "${array[@]}"; do
echo -n "<a href='$prefix_tags$item.html'>$item</a>, " echo -n "<a href='$prefix_tags$item.html'>$item</a>, "
done | sed 's/, $/<\/p>/g' >> "$content" done | sed 's/, $/<\/p>/g' >>"$content"
else else
echo "$line" >> "$content" echo "$line" >>"$content"
fi fi
done < "$1" done <"$1"
# Create the actual html page # Create the actual html page
create_html_page "$content" "$filename" no "$title" "$2" "$global_author" create_html_page "$content" "$filename" no "$title" "$2" "$global_author"
rm "$content" rm "$content"
} }

View File

@ -3,9 +3,12 @@
# Finds all posts referenced in a number of tags. # Finds all posts referenced in a number of tags.
# Arguments are tags # Arguments are tags
# Prints one line with space-separated tags to stdout # Prints one line with space-separated tags to stdout
declare prefix_tags
posts_with_tags() { posts_with_tags() {
(($# < 1)) && return (($# < 1)) && return
set -- "${@/#/$prefix_tags}" set -- "${@/#/$prefix_tags}"
set -- "${@/%/.html}" set -- "${@/%/.html}"
sed -n '/^<h3><a class="ablack" href="[^"]*">/{s/.*href="\([^"]*\)">.*/\1/;p;}' "$@" 2> /dev/null sed -n '/^<h3><a class="ablack" href="[^"]*">/{s/.*href="\([^"]*\)">.*/\1/;p;}' "$@" 2>/dev/null
} }

View File

@ -2,14 +2,17 @@
# Regenerates all the single post entries, keeping the post content but modifying # Regenerates all the single post entries, keeping the post content but modifying
# the title, html structure, etc # the title, html structure, etc
declare date_inpost
declare date_format_full
declare date_format_timestamp
rebuild_all_entries() { rebuild_all_entries() {
declare date_inpost
declare date_format_full
declare date_format_timestamp
echo -n "Rebuilding all entries " echo -n "Rebuilding all entries "
for i in ./*.html; do for i in ./*.html; do
is_boilerplate_file "$i" && continue; is_boilerplate_file "$i" && continue
contentfile=.tmp.$RANDOM contentfile=.tmp.$RANDOM
while [[ -f $contentfile ]]; do contentfile=.tmp.$RANDOM; done while [[ -f $contentfile ]]; do contentfile=.tmp.$RANDOM; done
@ -17,7 +20,7 @@ rebuild_all_entries() {
# Get the title and entry, and rebuild the html structure from scratch (divs, title, description...) # Get the title and entry, and rebuild the html structure from scratch (divs, title, description...)
title=$(get_post_title "$i") title=$(get_post_title "$i")
get_html_file_content 'text' 'text' <"$i" >> "$contentfile" get_html_file_content 'text' 'text' <"$i" >>"$contentfile"
# Read timestamp from post, if present, and sync file timestamp # Read timestamp from post, if present, and sync file timestamp
timestamp=$(awk '/<!-- '"$date_inpost"': .+ -->/ { print }' "$i" | cut -d '#' -f 2) timestamp=$(awk '/<!-- '"$date_inpost"': .+ -->/ { print }' "$i" | cut -d '#' -f 2)
@ -34,4 +37,4 @@ rebuild_all_entries() {
rm "$contentfile" rm "$contentfile"
done done
echo "" echo ""
} }

View File

@ -1,25 +1,27 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# Generate the index.html with the content of the latest posts # Generate the index.html with the content of the latest posts
declare index_file
declare number_of_index_articles
declare cut_do
declare cut_line
declare template_read_more
declare template_archive
declare template_subscribe
declare template_tags_title
declare blog_feed
declare global_author
declare global_feedburner
declare global_title
declare archive_index
declare tags_index
rebuild_index() { rebuild_index() {
declare index_file
declare number_of_index_articles
declare cut_do
declare cut_line
declare template_read_more
declare template_archive
declare template_subscribe
declare template_tags_title
declare blog_feed
declare global_author
declare global_feedburner
declare global_title
declare archive_index
declare tags_index
echo -n "Rebuilding the index " echo -n "Rebuilding the index "
newindexfile=$index_file.$RANDOM newindexfile=$index_file.$RANDOM
contentfile=$newindexfile.content contentfile=$newindexfile.content
while [[ -f $newindexfile ]]; do while [[ -f $newindexfile ]]; do
newindexfile=$index_file.$RANDOM newindexfile=$index_file.$RANDOM
contentfile=$newindexfile.content contentfile=$newindexfile.content
done done
@ -28,7 +30,7 @@ rebuild_index() {
{ {
n=0 n=0
while IFS='' read -r i; do while IFS='' read -r i; do
is_boilerplate_file "$i" && continue; is_boilerplate_file "$i" && continue
if ((n >= number_of_index_articles)); then break; fi if ((n >= number_of_index_articles)); then break; fi
if [[ -n $cut_do ]]; then 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" 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"
@ -36,7 +38,7 @@ rebuild_index() {
get_html_file_content 'entry' 'entry' <"$i" get_html_file_content 'entry' 'entry' <"$i"
fi fi
echo -n "." 1>&3 echo -n "." 1>&3
n=$(( n + 1 )) n=$((n + 1))
done < <(ls -t ./*.html) # sort by date, newest first done < <(ls -t ./*.html) # sort by date, newest first
feed=$blog_feed feed=$blog_feed
@ -50,4 +52,4 @@ rebuild_index() {
rm "$contentfile" rm "$contentfile"
mv "$newindexfile" "$index_file" mv "$newindexfile" "$index_file"
chmod 644 "$index_file" chmod 644 "$index_file"
} }

View File

@ -9,14 +9,16 @@
# example: # example:
# rebuild_tags "one_post.html another_article.html" "example-tag another-tag" # rebuild_tags "one_post.html another_article.html" "example-tag another-tag"
# mind the quotes! # mind the quotes!
declare prefix_tags
declare cut_do
declare cut_line
declare template_read_more
declare template_tag_title
declare global_title
declare global_author
rebuild_tags() { rebuild_tags() {
declare prefix_tags
declare cut_do
declare cut_line
declare template_read_more
declare template_tag_title
declare global_title
declare global_author
if (($# < 2)); then if (($# < 2)); then
# will process all files and tags # will process all files and tags
files=$(ls -t ./*.html) files=$(ls -t ./*.html)
@ -31,10 +33,10 @@ rebuild_tags() {
echo -n "Rebuilding tag pages " echo -n "Rebuilding tag pages "
#n=0 #n=0
if [[ -n $all_tags ]]; then if [[ -n $all_tags ]]; then
rm ./"$prefix_tags"*.html &> /dev/null rm ./"$prefix_tags"*.html &>/dev/null
else else
for i in $tags; do for i in $tags; do
rm "./$prefix_tags$i.html" &> /dev/null rm "./$prefix_tags$i.html" &>/dev/null
done done
fi fi
# First we will process all files and create temporal tag files # First we will process all files and create temporal tag files
@ -42,7 +44,7 @@ rebuild_tags() {
tmpfile=tmp.$RANDOM tmpfile=tmp.$RANDOM
while [[ -f $tmpfile ]]; do tmpfile=tmp.$RANDOM; done while [[ -f $tmpfile ]]; do tmpfile=tmp.$RANDOM; done
while IFS='' read -r i; do while IFS='' read -r i; do
is_boilerplate_file "$i" && continue; is_boilerplate_file "$i" && continue
echo -n "." echo -n "."
if [[ -n $cut_do ]]; then 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" 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"
@ -51,10 +53,10 @@ rebuild_tags() {
fi >"$tmpfile" fi >"$tmpfile"
for tag in $(tags_in_post "$i"); do for tag in $(tags_in_post "$i"); do
if [[ -n $all_tags || " $tags " == *" $tag "* ]]; then if [[ -n $all_tags || " $tags " == *" $tag "* ]]; then
cat "$tmpfile" >> "$prefix_tags$tag".tmp.html cat "$tmpfile" >>"$prefix_tags$tag".tmp.html
fi fi
done done
done <<< "$files" done <<<"$files"
rm "$tmpfile" rm "$tmpfile"
# Now generate the tag files with headers, footers, etc # Now generate the tag files with headers, footers, etc
while IFS='' read -r i; do while IFS='' read -r i; do
@ -64,4 +66,4 @@ rebuild_tags() {
rm "$i" rm "$i"
done < <(ls -t ./"$prefix_tags"*.tmp.html 2>/dev/null) done < <(ls -t ./"$prefix_tags"*.tmp.html 2>/dev/null)
echo echo
} }

View File

@ -1,15 +1,16 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# Delete all generated content, leaving only this script # Delete all generated content, leaving only this script
reset() { reset() {
echo "Are you sure you want to delete all blog entries? Please write \"Yes, I am!\" " echo "Are you sure you want to delete all blog entries? Please write \"Yes, I am!\" "
read -r line read -r line
if [[ $line == "Yes, I am!" ]]; then if [[ $line == "Yes, I am!" ]]; then
rm .*.html ./*.html ./*.css ./*.rss &> /dev/null rm .*.html ./*.html ./*.css ./*.rss &>/dev/null
echo echo
echo "Deleted all posts, stylesheets and feeds." echo "Deleted all posts, stylesheets and feeds."
echo "Kept your old '.backup.tar.gz' just in case, please delete it manually if needed." echo "Kept your old '.backup.tar.gz' just in case, please delete it manually if needed."
else else
echo "Phew! You dodged a bullet there. Nothing was modified." echo "Phew! You dodged a bullet there. Nothing was modified."
fi fi
} }

View File

@ -3,7 +3,9 @@
# Finds all tags referenced in one post. # Finds all tags referenced in one post.
# Accepts either filename as first argument, or post content at stdin # Accepts either filename as first argument, or post content at stdin
# Prints one line with space-separated tags to stdout # Prints one line with space-separated tags to stdout
declare template_tags_line_header
tags_in_post() { tags_in_post() {
declare template_tags_line_header
sed -n "/^<p>$template_tags_line_header/{s/^<p>$template_tags_line_header//;s/<[^>]*>//g;s/[ ,]\+/ /g;p;}" "$1" | tr ', ' ' ' sed -n "/^<p>$template_tags_line_header/{s/^<p>$template_tags_line_header//;s/<[^>]*>//g;s/[ ,]\+/ /g;p;}" "$1" | tr ', ' ' '
} }

View File

@ -1,16 +0,0 @@
#!/usr/bin/env bash
# Test if the markdown script is working correctly
test_markdown() {
declare markdown_bin
if [[ -n $markdown_bin ]]; then
local m1; m1=$("$markdown_bin" <<< "$'line 1\n\nline 2'")
local m2; m2=$("$markdown_bin" <<< "$'line 1\n\nline 2'")
local c1=$'<p>line 1</p>\n\n<p>line 2</p>'
local c2=$'<p>line 1</p>\n<p>line 2</p>'
# shellcheck disable=SC22350 # must enforce order of operations
[[ "$m1" == "$c1" ]] || [[ "$m2" == "$c2" ]]
else
return 1
fi
}

View File

@ -4,20 +4,23 @@
# #
# $1 the post file # $1 the post file
# $2 the title # $2 the title
declare global_twitter_username
declare template_tags_line_header
declare global_url
declare global_twitter_cookieless
declare template_twitter_button
declare template_twitter_comment
declare template_comments
twitter_card() { twitter_card() {
declare global_twitter_username
declare template_tags_line_header
declare global_url
declare global_twitter_cookieless
declare template_twitter_button
declare template_twitter_comment
declare template_comments
[[ -z $global_twitter_username ]] && return [[ -z $global_twitter_username ]] && return
echo "<meta name='twitter:card' content='summary'>" echo "<meta name='twitter:card' content='summary'>"
echo "<meta name='twitter:site' content='@$global_twitter_username'>" echo "<meta name='twitter:site' content='@$global_twitter_username'>"
echo "<meta name='twitter:title' content='$2'>" # Twitter truncates at 70 char echo "<meta name='twitter:title' content='$2'>" # Twitter truncates at 70 char
description=$(grep -v "^<p>$template_tags_line_header" "$1" | sed -e 's/<[^>]*>//g' | head -c 250 | tr '\n' ' ' | sed "s/\"/'/g") description=$(grep -v "^<p>$template_tags_line_header" "$1" | sed -e 's/<[^>]*>//g' | head -c 250 | tr '\n' ' ' | sed "s/\"/'/g")
echo "<meta name='twitter:description' content=\"$description\">" echo "<meta name='twitter:description' content=\"$description\">"
image=$(sed -n 's/.*<img.*src="\([^"]*\)".*/\1/p' "$1" | head -n 1) # First image is fine image=$(sed -n 's/.*<img.*src="\([^"]*\)".*/\1/p' "$1" | head -n 1) # First image is fine
[[ -z $image ]] && return [[ -z $image ]] && return
@ -38,13 +41,13 @@ twitter() {
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> " 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> "
echo "<a href='$search_engine""$1'><span id='count-$id'></span></a>&nbsp;</p>" echo "<a href='$search_engine""$1'><span id='count-$id'></span></a>&nbsp;</p>"
return; return
else else
echo "<p id='twitter'>$template_comments&nbsp;"; echo "<p id='twitter'>$template_comments&nbsp;"
fi fi
echo "<a href=\"https://twitter.com/share\" class=\"twitter-share-button\" data-text=\"$template_twitter_comment\" data-url=\"$1\"" echo "<a href=\"https://twitter.com/share\" class=\"twitter-share-button\" data-text=\"$template_twitter_comment\" data-url=\"$1\""
echo " data-via=\"$global_twitter_username\"" echo " data-via=\"$global_twitter_username\""
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 ">$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>" echo "</p>"
} }

View File

@ -1,8 +1,10 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# Displays the help
declare global_software_name declare global_software_name
declare global_software_version declare global_software_version
# Displays the help
usage() { usage() {
echo "$global_software_name $global_software_version" echo "$global_software_name $global_software_version"
echo "usage: $0 command [filename]" echo "usage: $0 command [filename]"

View File

@ -2,26 +2,28 @@
# Manages the creation of the text file and the parsing to html file # Manages the creation of the text file and the parsing to html file
# also the drafts # also the drafts
declare template_tags_line_header declare template_tags_line_header
declare global_url declare global_url
declare convert_filename declare convert_filename
write_entry() { write_entry() {
f=$2 f=$2
extension=${f##*.}
if [[ $extension != md ]]; then if [[ -n $f ]]; then
f=${f}.md extension=${f##*.}
fi if [[ $extension != md ]]; then
if [[ -n $f ]]; thenq f=${f}.md
fi
if [[ -f $f ]]; then if [[ -f $f ]]; then
echo "The file $f doesn't exist" echo "The file $f doesn't exist"
delete_includes delete_includes
exit exit
fi fi
else else
TMPFILE=.entry-$RANDOM.$fmt TMPFILE=.entry-$RANDOM.md
echo -e "Title on this line\n" >> "$TMPFILE" echo -e "Title on this line\n" >>"$TMPFILE"
cat <<EOF >>"$TMPFILE"
cat << EOF >> "$TMPFILE"
The rest of the text file is a **Markdown** blog post. The process will continue The rest of the text file is a **Markdown** blog post. The process will continue
as soon as you exit your editor. as soon as you exit your editor.
@ -37,12 +39,10 @@ EOF
$EDITOR "$TMPFILE" $EDITOR "$TMPFILE"
html_from_md=$(mrkdwn "$TMPFILE") html_from_md=$(make_html "$TMPFILE")
parse_file "$html_from_md"
parse_file "$html_from_md"
rm "$html_from_md" rm "$html_from_md"
chmod 644 "$filename" chmod 644 "$filename"
[[ -n $preview_url ]] || preview_url=$global_url [[ -n $preview_url ]] || preview_url=$global_url
echo "To preview the entry, open $preview_url/$filename in your browser" echo "To preview the entry, open $preview_url/$filename in your browser"
@ -65,7 +65,7 @@ EOF
[[ -n $convert_filename ]] && title=$(echo "$title" | eval "$convert_filename") [[ -n $convert_filename ]] && title=$(echo "$title" | eval "$convert_filename")
[[ -n $title ]] || title=$RANDOM [[ -n $title ]] || title=$RANDOM
draft=drafts/$title.$fmt draft=drafts/$title.md
mv "$TMPFILE" "$draft" mv "$TMPFILE" "$draft"
chmod 600 "$draft" chmod 600 "$draft"
rm "$filename" rm "$filename"
@ -84,4 +84,4 @@ EOF
relevant_posts="$(posts_with_tags $relevant_tags) $filename" relevant_posts="$(posts_with_tags $relevant_tags) $filename"
rebuild_tags "$relevant_posts" "$relevant_tags" rebuild_tags "$relevant_posts" "$relevant_tags"
fi fi
} }

View File

@ -1,12 +1,18 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# shellcheck disable=SC1091
# shellcheck disable=SC2034
# TildeLog, a not-so-simple blog/gemlog/phlog system made for tilde.team # TildeLog, a not-so-simple blog/gemlog/phlog system made for tilde.team
# By Josemar Lohn <j@lo.hn> # By Josemar Lohn <j@lo.hn>
# #
# Heavily based on BashBlog, by Carlos Fenollosa <carlos.fenollosa@gmail.com> # Heavily based on BashBlog, by Carlos Fenollosa <carlos.fenollosa@gmail.com>
### BEGIN SOURCEFILES -> DO NOT REMOVE THIS LINE md2html_awk=$(<lib/md2html.awk)
md2gemini_awk=$(<lib/md2gemini.awk)
md2gopher_awk=$(<lib/md2gopher.awk)
### BEGIN SOURCEFILES -> DO NOT REMOVE THIS LINE
source ./lib/all_posts.sh source ./lib/all_posts.sh
source ./lib/all_tags.sh source ./lib/all_tags.sh
source ./lib/create_css.sh source ./lib/create_css.sh
@ -26,7 +32,7 @@ source ./lib/list_tags.sh
source ./lib/make_gemini.sh source ./lib/make_gemini.sh
source ./lib/make_gophermap.sh source ./lib/make_gophermap.sh
source ./lib/make_rss.sh source ./lib/make_rss.sh
source ./lib/mrkdwn.sh source ./lib/make_html.sh
source ./lib/parse_file.sh source ./lib/parse_file.sh
source ./lib/posts_with_tags.sh source ./lib/posts_with_tags.sh
source ./lib/rebuild_all_entries.sh source ./lib/rebuild_all_entries.sh
@ -34,7 +40,6 @@ source ./lib/rebuild_index.sh
source ./lib/rebuild_tags.sh source ./lib/rebuild_tags.sh
source ./lib/reset.sh source ./lib/reset.sh
source ./lib/tags_in_post.sh source ./lib/tags_in_post.sh
source ./lib/test_markdown.sh
source ./lib/twitter_card.sh source ./lib/twitter_card.sh
source ./lib/usage.sh source ./lib/usage.sh
source ./lib/write_entry.sh source ./lib/write_entry.sh
@ -47,4 +52,4 @@ source ./lib/write_entry.sh
# #
do_main "$@" do_main "$@"
# vim: set shiftwidth=4 tabstop=4 expandtab: # vim: set shiftwidth=4 tabstop=4 expandtab: