From e6161e9717038facd4f4bc824053975e2af2e000 Mon Sep 17 00:00:00 2001 From: Josemar Lohn Date: Thu, 11 Feb 2021 03:44:09 -0300 Subject: [PATCH] Fixed variables problem and removed html edit. --- lib/create_css.sh | 13 +++++----- lib/do_main.sh | 20 +++++++-------- lib/edit.sh | 24 ++++++++---------- lib/get_html_file_content.sh | 6 ++--- lib/get_post_title.sh | 4 +-- lib/global_variables.sh | 24 ++++++++---------- lib/is_boilerplate_file.sh | 40 +++++++++++++++--------------- lib/list_tags.sh | 14 ++++++----- lib/make_gemini.sh | 8 +++--- lib/make_gophermap.sh | 8 +++--- lib/make_html.sh | 12 +++++++++ lib/make_rss.sh | 35 +++++++++++++------------- md2gemini.awk => lib/md2gemini.awk | 0 md2gopher.awk => lib/md2gopher.awk | 0 md2html.awk => lib/md2html.awk | 2 +- lib/mrkdwn.sh | 13 ---------- lib/parse_file.sh | 22 ++++++++-------- lib/posts_with_tags.sh | 7 ++++-- lib/rebuild_all_entries.sh | 15 ++++++----- lib/rebuild_index.sh | 38 ++++++++++++++-------------- lib/rebuild_tags.sh | 28 +++++++++++---------- lib/reset.sh | 5 ++-- lib/tags_in_post.sh | 6 +++-- lib/test_markdown.sh | 16 ------------ lib/twitter_card.sh | 27 +++++++++++--------- lib/usage.sh | 4 ++- lib/write_entry.sh | 30 +++++++++++----------- tildelog.sh | 15 +++++++---- 28 files changed, 223 insertions(+), 213 deletions(-) create mode 100644 lib/make_html.sh rename md2gemini.awk => lib/md2gemini.awk (100%) rename md2gopher.awk => lib/md2gopher.awk (100%) rename md2html.awk => lib/md2html.awk (99%) delete mode 100644 lib/mrkdwn.sh delete mode 100644 lib/test_markdown.sh diff --git a/lib/create_css.sh b/lib/create_css.sh index 506f6c6..1a97fc6 100644 --- a/lib/create_css.sh +++ b/lib/create_css.sh @@ -1,11 +1,12 @@ #!/usr/bin/env bash # Create the css file from scratch +declare css_include create_css() { # To avoid overwriting manual changes. However it is recommended that # this function is modified if the user changes the blog.css file - (( ${#css_include[@]} > 0 )) && return || css_include=('main.css' 'blog.css') - if [[ ! -f blog.css ]]; then + ((${#css_include[@]} > 0)) && return || css_include=('main.css' 'blog.css') + if [[ ! -f blog.css ]]; then # blog.css directives will be loaded after main.css and thus will prevail echo '#title{font-size: x-large;} a.ablack{color:black !important;} @@ -19,14 +20,14 @@ create_css() { h3{margin-top:42px;margin-bottom:8px;} h4{margin-left:24px;margin-right:24px;} 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 # If there is a style.css from the parent page (i.e. some landing page) # then use it. This directive is here for compatibility with my own # home page. Feel free to edit it out, though it doesn't hurt if [[ -f ../style.css ]] && [[ ! -f main.css ]]; then - ln -s "../style.css" "main.css" + ln -s "../style.css" "main.css" elif [[ ! -f main.css ]]; then 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;} @@ -43,6 +44,6 @@ create_css() { 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 + blockquote iframe{margin:12px 0px;}' >main.css fi -} \ No newline at end of file +} diff --git a/lib/do_main.sh b/lib/do_main.sh index b9f592d..075eb2b 100644 --- a/lib/do_main.sh +++ b/lib/do_main.sh @@ -18,16 +18,16 @@ do_main() { # Load default configuration, then override settings with the config file global_variables # 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 # Check for $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 + [[ -z $EDITOR ]] && + EDITOR=vim # Check for validity of argument - [[ $1 != "reset" && $1 != "post" && $1 != "rebuild" && $1 != "list" && $1 != "edit" && $1 != "delete" && $1 != "tags" ]] && - usage && return + [[ $1 != "reset" && $1 != "post" && $1 != "rebuild" && $1 != "list" && $1 != "edit" && $1 != "delete" && $1 != "tags" ]] && + usage && return [[ $1 == list ]] && list_posts && return @@ -43,10 +43,10 @@ do_main() { fi # 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 tar -c -z -f ".backup.tar.gz" -- *.html && - chmod 600 ".backup.tar.gz" + chmod 600 ".backup.tar.gz" elif [[ $1 == rebuild ]]; then echo "Can't find any html files, nothing to rebuild" return @@ -54,7 +54,7 @@ do_main() { # Keep first backup of this day containing yesterday's version of the blog [[ ! -f .yesterday.tar.gz || $(date -r .yesterday.tar.gz +'%d') != "$(date +'%d')" ]] && - cp .backup.tar.gz .yesterday.tar.gz &> /dev/null + cp .backup.tar.gz .yesterday.tar.gz &>/dev/null [[ $1 == reset ]] && reset && return @@ -63,7 +63,7 @@ do_main() { create_includes [[ $1 == post ]] && write_entry "$@" [[ $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 [[ $2 == -n ]]; then edit "$3" @@ -80,4 +80,4 @@ do_main() { make_gophermap make_gemini delete_includes -} \ No newline at end of file +} diff --git a/lib/edit.sh b/lib/edit.sh index 885f4ef..44a995b 100644 --- a/lib/edit.sh +++ b/lib/edit.sh @@ -12,16 +12,18 @@ # "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) + +declare date_format_full +declare date_format_timestamp +declare template_tags_line_header +declare prefix_tags edit() { - declare date_format_full - declare date_format_timestamp - declare template_tags_line_header - declare prefix_tags - [[ ! -f "${1%%.*}.html" ]] && \ + + [[ ! -f "${1%%.*}.html" ]] && printf "Can't edit post \"%s.html\", did you mean to use \"bb.sh post \"?\\n" "${1%%.*}" && exit 1 # 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") tags_before=$(tags_in_post "${1%%.*}.html") if [[ $2 == full ]]; then @@ -29,21 +31,17 @@ edit() { filename=$1 else if [[ ${1##*.} == md ]]; then - if test_markdown; then - echo "Markdown is not working, please edit HTML file directly." - exit - fi # editing markdown file $EDITOR "$1" - TMPFILE=$(mrkdwn "$1") + TMPFILE=$(make_html "$1") filename=${1%%.*}.html else # Create the content file TMPFILE=$(basename "$1").$RANDOM.html # Title - get_post_title "$1" > "$TMPFILE" + get_post_title "$1" >"$TMPFILE" # Post text with plaintext tags - get_html_file_content 'text' 'text' <"$1" | sed "/^

$template_tags_line_header/s|\\1|\\1|g" >> "$TMPFILE" + get_html_file_content 'text' 'text' <"$1" | sed "/^

$template_tags_line_header/s|\\1|\\1|g" >>"$TMPFILE" $EDITOR "$TMPFILE" filename=$1 fi diff --git a/lib/get_html_file_content.sh b/lib/get_html_file_content.sh index 28cf6b3..9946d1f 100644 --- a/lib/get_html_file_content.sh +++ b/lib/get_html_file_content.sh @@ -6,9 +6,9 @@ # $3 "cut" to remove text from


to # note that this does not remove
line itself, # so you can see if text was cut or not +declare cut_line cut_tags +declare template_tags_line_header get_html_file_content() { - declare cut_line cut_tags - declare template_tags_line_header awk "//, //{ if (!// && !//) print if (\"$3\" == \"cut\" && /$cut_line/){ @@ -18,4 +18,4 @@ get_html_file_content() { } } }" -} \ No newline at end of file +} diff --git a/lib/get_post_title.sh b/lib/get_post_title.sh index 7744155..d94dc7f 100644 --- a/lib/get_post_title.sh +++ b/lib/get_post_title.sh @@ -5,5 +5,5 @@ # $1 the html file get_post_title() { #awk '/

/, /<\/a><\/h3>/{if (!/

/ && !/<\/a><\/h3>/) print}' "$1" - head -1 $1 -} \ No newline at end of file + head -1 "$1" +} diff --git a/lib/global_variables.sh b/lib/global_variables.sh index 0e56d37..0f020ed 100644 --- a/lib/global_variables.sh +++ b/lib/global_variables.sh @@ -13,14 +13,13 @@ global_config=".tildelog" global_variables() { global_software_name="tildelog" global_software_version="0.1" - # Blog title global_title="my tildelog" # The typical subtitle for each blog global_description="a blog about tildes" # The server base domain 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" # Your name global_author="~$USER" @@ -32,8 +31,7 @@ global_variables() { # CC by-nc-nd is a good starting point, you can change this to "©" for Copyright 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 global_feedburner="" @@ -73,8 +71,6 @@ global_variables() { # 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='
' - # save markdown file when posting with "bb post -m". Leave blank to discard it. - save_markdown="yes" # prefix for tags/categories files # please make sure that no other html file starts with this prefix prefix_tags="tag_" @@ -110,7 +106,7 @@ global_variables() { template_tags_title="all tags" # "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_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" # "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" @@ -149,17 +145,17 @@ global_variables() { # Markdown location. Trying to autodetect by default. # The invocation must support the signature 'markdown_bin in.md > out.html' - - markdown_bin=$(which md2html.awk) + # shellcheck disable=SC2016 + md2html_bin='awk "$md2html_awk"' } # Check for the validity of some variables # DO NOT EDIT THIS FUNCTION unless you know what you're doing global_variables_check() { - [[ $header_file == .header.html ]] && \ - echo "Please check your configuration. '.header.html' is not a valid value for the setting 'header_file'" && \ + [[ $header_file == .header.html ]] && + echo "Please check your configuration. '.header.html' is not a valid value for the setting 'header_file'" && exit - [[ $footer_file == .footer.html ]] && \ - echo "Please check your configuration. '.footer.html' is not a valid value for the setting 'footer_file'" && \ + [[ $footer_file == .footer.html ]] && + echo "Please check your configuration. '.footer.html' is not a valid value for the setting 'footer_file'" && exit -} \ No newline at end of file +} diff --git a/lib/is_boilerplate_file.sh b/lib/is_boilerplate_file.sh index 9107038..9111f5f 100644 --- a/lib/is_boilerplate_file.sh +++ b/lib/is_boilerplate_file.sh @@ -8,19 +8,17 @@ # # 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 +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() { - 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#./} # First check against user-defined non-blogpost pages for item in "${non_blogpost_files[@]}"; do @@ -28,12 +26,14 @@ is_boilerplate_file() { done case $name in - ( "$index_file" | "$archive_index" | "$gophermap" | "$gemini_index" | "$tags_index" | "$footer_file" | "$header_file" | "$prefix_tags"* ) - return 0 ;; - ( * ) # Check for excluded - for excl in "${html_exclude[@]}"; do - [[ $name == "$excl" ]] && return 0 - done - return 1 ;; + "$index_file" | "$archive_index" | "$gophermap" | "$gemini_index" | "$tags_index" | "$footer_file" | "$header_file" | "$prefix_tags"*) + return 0 + ;; + *) # Check for excluded + for excl in "${html_exclude[@]}"; do + [[ $name == "$excl" ]] && return 0 + done + return 1 + ;; esac -} \ No newline at end of file +} diff --git a/lib/list_tags.sh b/lib/list_tags.sh index 87dbdd0..7f574b7 100644 --- a/lib/list_tags.sh +++ b/lib/list_tags.sh @@ -3,13 +3,15 @@ # Displays a list of the tags # # $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() { - 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 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 fi @@ -24,9 +26,9 @@ list_tags() { lines+=$line\\n done - if (( do_sort == 1 )); then + if ((do_sort == 1)); then echo -e "$lines" | column -t -s "#" | sort -nrk 2 else - echo -e "$lines" | column -t -s "#" + echo -e "$lines" | column -t -s "#" fi } diff --git a/lib/make_gemini.sh b/lib/make_gemini.sh index bdf3624..5e011fa 100644 --- a/lib/make_gemini.sh +++ b/lib/make_gemini.sh @@ -1,7 +1,9 @@ #!/usr/bin/env bash +declare gemini_index + make_gemini() { - declare gemini_index + return if [ ! -d "${HOME}/public_gemini" ]; then printf "Creating ~/public_gemini\\n" mkdir "${HOME}/public_gemini" @@ -12,7 +14,7 @@ make_gemini() { fi 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 printf "20 text/gemini\r\n" printf "my bashblog posts\r\n" @@ -24,4 +26,4 @@ make_gemini() { EOF chmod +x "${HOME}/public_gemini/blog/${gemini_index}" fi -} \ No newline at end of file +} diff --git a/lib/make_gophermap.sh b/lib/make_gophermap.sh index 6efd0ed..50b8e0c 100644 --- a/lib/make_gophermap.sh +++ b/lib/make_gophermap.sh @@ -1,7 +1,9 @@ #!/usr/bin/env bash +declare gophermap + make_gophermap() { - declare gophermap + return if [ ! -d "${HOME}/public_gopher" ]; then printf "Creating gopher hole\\n" mkdir "${HOME}/public_gopher" @@ -12,7 +14,7 @@ make_gophermap() { fi 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 printf "my bashblog posts\n" user=$(stat -c '%U' .) @@ -24,4 +26,4 @@ EOF chmod +x "${HOME}/public_html/blog/${gophermap}" fi chmod 644 "./*.md" -} \ No newline at end of file +} diff --git a/lib/make_html.sh b/lib/make_html.sh new file mode 100644 index 0000000..c420d84 --- /dev/null +++ b/lib/make_html.sh @@ -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" +} diff --git a/lib/make_rss.sh b/lib/make_rss.sh index 5be3067..b703b34 100644 --- a/lib/make_rss.sh +++ b/lib/make_rss.sh @@ -1,15 +1,16 @@ #!/usr/bin/env bash # 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() { - 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 " rssfile=$blog_feed.$RANDOM @@ -17,8 +18,8 @@ make_rss() { { pubdate=$(LC_ALL=C date +"$date_format_full") - echo '' - echo '' + echo '' + echo '' echo "$global_title$global_url/$index_file" echo "$global_descriptionen" echo "$pubdate" @@ -30,16 +31,16 @@ make_rss() { is_boilerplate_file "$i" && continue ((n >= number_of_feed_articles)) && break # max 10 items echo -n "." 1>&3 - echo '' + echo '<item><title>' get_post_title "$i" - echo '$global_url/${i#./}" - echo "$global_url/$i" - echo "$(get_post_author "$i")" + echo "]]>$global_url/${i#./}" + echo "$global_url/$i" + echo "$(get_post_author "$i")" echo "$(LC_ALL=C date -r "$i" +"$date_format_full")" - n=$(( n + 1 )) + n=$((n + 1)) done < <(ls -t ./*.html) echo '' @@ -48,4 +49,4 @@ make_rss() { mv "$rssfile" "$blog_feed" chmod 644 "$blog_feed" -} \ No newline at end of file +} diff --git a/md2gemini.awk b/lib/md2gemini.awk similarity index 100% rename from md2gemini.awk rename to lib/md2gemini.awk diff --git a/md2gopher.awk b/lib/md2gopher.awk similarity index 100% rename from md2gopher.awk rename to lib/md2gopher.awk diff --git a/md2html.awk b/lib/md2html.awk similarity index 99% rename from md2html.awk rename to lib/md2html.awk index b981c04..f534abb 100755 --- a/md2html.awk +++ b/lib/md2html.awk @@ -1,4 +1,4 @@ -#!/bin/awk -f +#!/usr/bin/awk -f # # by: Josemar Lohn # lo.hn on www/gemini/gopher diff --git a/lib/mrkdwn.sh b/lib/mrkdwn.sh deleted file mode 100644 index 76daa5a..0000000 --- a/lib/mrkdwn.sh +++ /dev/null @@ -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" -} \ No newline at end of file diff --git a/lib/parse_file.sh b/lib/parse_file.sh index 89371c3..3eee48a 100644 --- a/lib/parse_file.sh +++ b/lib/parse_file.sh @@ -8,11 +8,13 @@ # 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 + +declare convert_filename +declare template_tags_line_header +declare prefix_tags +declare global_author + 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 title="" while IFS='' read -r line; do @@ -36,18 +38,18 @@ parse_file() { # Parse possible tags elif [[ $line == "

$template_tags_line_header"* ]]; then tags=$(echo "$line" | cut -d ":" -f 2- | sed -e 's/<\/p>//g' -e 's/^ *//' -e 's/ *$//' -e 's/, /,/g') - IFS=, read -r -a array <<< "$tags" + IFS=, read -r -a array <<<"$tags" - echo -n "

$template_tags_line_header " >> "$content" + echo -n "

$template_tags_line_header " >>"$content" for item in "${array[@]}"; do echo -n "$item, " - done | sed 's/, $/<\/p>/g' >> "$content" + done | sed 's/, $/<\/p>/g' >>"$content" else - echo "$line" >> "$content" + echo "$line" >>"$content" fi - done < "$1" + done <"$1" # Create the actual html page create_html_page "$content" "$filename" no "$title" "$2" "$global_author" rm "$content" -} \ No newline at end of file +} diff --git a/lib/posts_with_tags.sh b/lib/posts_with_tags.sh index 9dd90c2..0ef1aae 100644 --- a/lib/posts_with_tags.sh +++ b/lib/posts_with_tags.sh @@ -3,9 +3,12 @@ # Finds all posts referenced in a number of tags. # Arguments are tags # Prints one line with space-separated tags to stdout + +declare prefix_tags + posts_with_tags() { (($# < 1)) && return set -- "${@/#/$prefix_tags}" set -- "${@/%/.html}" - sed -n '/^

/{s/.*href="\([^"]*\)">.*/\1/;p;}' "$@" 2> /dev/null -} \ No newline at end of file + sed -n '/^

/{s/.*href="\([^"]*\)">.*/\1/;p;}' "$@" 2>/dev/null +} diff --git a/lib/rebuild_all_entries.sh b/lib/rebuild_all_entries.sh index e1ecc37..76cba6c 100644 --- a/lib/rebuild_all_entries.sh +++ b/lib/rebuild_all_entries.sh @@ -2,14 +2,17 @@ # Regenerates all the single post entries, keeping the post content but modifying # the title, html structure, etc + +declare date_inpost +declare date_format_full +declare date_format_timestamp + rebuild_all_entries() { - declare date_inpost - declare date_format_full - declare date_format_timestamp + echo -n "Rebuilding all entries " for i in ./*.html; do - is_boilerplate_file "$i" && continue; + is_boilerplate_file "$i" && continue contentfile=.tmp.$RANDOM 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...) 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 timestamp=$(awk '// { print }' "$i" | cut -d '#' -f 2) @@ -34,4 +37,4 @@ rebuild_all_entries() { rm "$contentfile" done echo "" -} \ No newline at end of file +} diff --git a/lib/rebuild_index.sh b/lib/rebuild_index.sh index 2c8920a..e4297e7 100644 --- a/lib/rebuild_index.sh +++ b/lib/rebuild_index.sh @@ -1,25 +1,27 @@ #!/usr/bin/env bash # 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() { - 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 " newindexfile=$index_file.$RANDOM contentfile=$newindexfile.content - while [[ -f $newindexfile ]]; do + while [[ -f $newindexfile ]]; do newindexfile=$index_file.$RANDOM contentfile=$newindexfile.content done @@ -28,7 +30,7 @@ rebuild_index() { { n=0 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 $cut_do ]]; then get_html_file_content 'entry' 'entry' 'cut' <"$i" | awk "/$cut_line/ { print \"

$template_read_more

\" ; next } 1" @@ -36,7 +38,7 @@ rebuild_index() { get_html_file_content 'entry' 'entry' <"$i" fi echo -n "." 1>&3 - n=$(( n + 1 )) + n=$((n + 1)) done < <(ls -t ./*.html) # sort by date, newest first feed=$blog_feed @@ -50,4 +52,4 @@ rebuild_index() { rm "$contentfile" mv "$newindexfile" "$index_file" chmod 644 "$index_file" -} \ No newline at end of file +} diff --git a/lib/rebuild_tags.sh b/lib/rebuild_tags.sh index e1da522..b548ca7 100644 --- a/lib/rebuild_tags.sh +++ b/lib/rebuild_tags.sh @@ -9,14 +9,16 @@ # example: # rebuild_tags "one_post.html another_article.html" "example-tag another-tag" # 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() { - 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 # will process all files and tags files=$(ls -t ./*.html) @@ -31,10 +33,10 @@ rebuild_tags() { echo -n "Rebuilding tag pages " #n=0 if [[ -n $all_tags ]]; then - rm ./"$prefix_tags"*.html &> /dev/null + rm ./"$prefix_tags"*.html &>/dev/null else for i in $tags; do - rm "./$prefix_tags$i.html" &> /dev/null + rm "./$prefix_tags$i.html" &>/dev/null done fi # First we will process all files and create temporal tag files @@ -42,7 +44,7 @@ rebuild_tags() { tmpfile=tmp.$RANDOM while [[ -f $tmpfile ]]; do tmpfile=tmp.$RANDOM; done while IFS='' read -r i; do - is_boilerplate_file "$i" && continue; + is_boilerplate_file "$i" && continue echo -n "." if [[ -n $cut_do ]]; then get_html_file_content 'entry' 'entry' 'cut' <"$i" | awk "/$cut_line/ { print \"

$template_read_more

\" ; next } 1" @@ -51,10 +53,10 @@ rebuild_tags() { fi >"$tmpfile" for tag in $(tags_in_post "$i"); do if [[ -n $all_tags || " $tags " == *" $tag "* ]]; then - cat "$tmpfile" >> "$prefix_tags$tag".tmp.html + cat "$tmpfile" >>"$prefix_tags$tag".tmp.html fi done - done <<< "$files" + done <<<"$files" rm "$tmpfile" # Now generate the tag files with headers, footers, etc while IFS='' read -r i; do @@ -64,4 +66,4 @@ rebuild_tags() { rm "$i" done < <(ls -t ./"$prefix_tags"*.tmp.html 2>/dev/null) echo -} \ No newline at end of file +} diff --git a/lib/reset.sh b/lib/reset.sh index 216620f..4bdcb7e 100644 --- a/lib/reset.sh +++ b/lib/reset.sh @@ -1,15 +1,16 @@ #!/usr/bin/env bash # Delete all generated content, leaving only this script + reset() { echo "Are you sure you want to delete all blog entries? Please write \"Yes, I am!\" " read -r line if [[ $line == "Yes, I am!" ]]; then - rm .*.html ./*.html ./*.css ./*.rss &> /dev/null + rm .*.html ./*.html ./*.css ./*.rss &>/dev/null echo echo "Deleted all posts, stylesheets and feeds." echo "Kept your old '.backup.tar.gz' just in case, please delete it manually if needed." else echo "Phew! You dodged a bullet there. Nothing was modified." fi -} \ No newline at end of file +} diff --git a/lib/tags_in_post.sh b/lib/tags_in_post.sh index 35c6eda..d01fb2d 100644 --- a/lib/tags_in_post.sh +++ b/lib/tags_in_post.sh @@ -3,7 +3,9 @@ # Finds all tags referenced in one post. # Accepts either filename as first argument, or post content at stdin # Prints one line with space-separated tags to stdout + +declare template_tags_line_header + tags_in_post() { - declare template_tags_line_header sed -n "/^

$template_tags_line_header/{s/^

$template_tags_line_header//;s/<[^>]*>//g;s/[ ,]\+/ /g;p;}" "$1" | tr ', ' ' ' -} \ No newline at end of file +} diff --git a/lib/test_markdown.sh b/lib/test_markdown.sh deleted file mode 100644 index 1ad49ef..0000000 --- a/lib/test_markdown.sh +++ /dev/null @@ -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=$'

line 1

\n\n

line 2

' - local c2=$'

line 1

\n

line 2

' - # shellcheck disable=SC22350 # must enforce order of operations - [[ "$m1" == "$c1" ]] || [[ "$m2" == "$c2" ]] - else - return 1 - fi -} \ No newline at end of file diff --git a/lib/twitter_card.sh b/lib/twitter_card.sh index 3366cb7..57cab42 100644 --- a/lib/twitter_card.sh +++ b/lib/twitter_card.sh @@ -4,20 +4,23 @@ # # $1 the post file # $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() { - 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 echo "" echo "" echo "" # Twitter truncates at 70 char - description=$(grep -v "^

$template_tags_line_header" "$1" | sed -e 's/<[^>]*>//g' | head -c 250 | tr '\n' ' ' | sed "s/\"/'/g") + description=$(grep -v "^

$template_tags_line_header" "$1" | sed -e 's/<[^>]*>//g' | head -c 250 | tr '\n' ' ' | sed "s/\"/'/g") echo "" image=$(sed -n 's/.*$template_comments $template_twitter_button " echo " 

" - return; - else - echo "

$template_comments "; + return + else + echo "

$template_comments " fi echo "$template_twitter_button " echo "

" -} \ No newline at end of file +} diff --git a/lib/usage.sh b/lib/usage.sh index a5e00bd..48476e0 100644 --- a/lib/usage.sh +++ b/lib/usage.sh @@ -1,8 +1,10 @@ #!/usr/bin/env bash +# Displays the help + declare global_software_name declare global_software_version -# Displays the help + usage() { echo "$global_software_name $global_software_version" echo "usage: $0 command [filename]" diff --git a/lib/write_entry.sh b/lib/write_entry.sh index 26d7cd1..b39fcc0 100644 --- a/lib/write_entry.sh +++ b/lib/write_entry.sh @@ -2,26 +2,28 @@ # Manages the creation of the text file and the parsing to html file # also the drafts + declare template_tags_line_header declare global_url declare convert_filename + write_entry() { f=$2 - extension=${f##*.} - if [[ $extension != md ]]; then - f=${f}.md - fi - if [[ -n $f ]]; thenq + + if [[ -n $f ]]; then + extension=${f##*.} + if [[ $extension != md ]]; then + f=${f}.md + fi if [[ -f $f ]]; then echo "The file $f doesn't exist" delete_includes exit fi else - TMPFILE=.entry-$RANDOM.$fmt - echo -e "Title on this line\n" >> "$TMPFILE" - - cat << EOF >> "$TMPFILE" + TMPFILE=.entry-$RANDOM.md + echo -e "Title on this line\n" >>"$TMPFILE" + cat <>"$TMPFILE" The rest of the text file is a **Markdown** blog post. The process will continue as soon as you exit your editor. @@ -37,12 +39,10 @@ EOF $EDITOR "$TMPFILE" - html_from_md=$(mrkdwn "$TMPFILE") - - parse_file "$html_from_md" + html_from_md=$(make_html "$TMPFILE") + parse_file "$html_from_md" rm "$html_from_md" - chmod 644 "$filename" [[ -n $preview_url ]] || preview_url=$global_url 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 $title ]] || title=$RANDOM - draft=drafts/$title.$fmt + draft=drafts/$title.md mv "$TMPFILE" "$draft" chmod 600 "$draft" rm "$filename" @@ -84,4 +84,4 @@ EOF relevant_posts="$(posts_with_tags $relevant_tags) $filename" rebuild_tags "$relevant_posts" "$relevant_tags" fi -} \ No newline at end of file +} diff --git a/tildelog.sh b/tildelog.sh index 091e168..0735956 100755 --- a/tildelog.sh +++ b/tildelog.sh @@ -1,12 +1,18 @@ #!/usr/bin/env bash +# shellcheck disable=SC1091 +# shellcheck disable=SC2034 + # TildeLog, a not-so-simple blog/gemlog/phlog system made for tilde.team # By Josemar Lohn -# +# # Heavily based on BashBlog, by Carlos Fenollosa -### BEGIN SOURCEFILES -> DO NOT REMOVE THIS LINE +md2html_awk=$( DO NOT REMOVE THIS LINE source ./lib/all_posts.sh source ./lib/all_tags.sh source ./lib/create_css.sh @@ -26,7 +32,7 @@ source ./lib/list_tags.sh source ./lib/make_gemini.sh source ./lib/make_gophermap.sh source ./lib/make_rss.sh -source ./lib/mrkdwn.sh +source ./lib/make_html.sh source ./lib/parse_file.sh source ./lib/posts_with_tags.sh source ./lib/rebuild_all_entries.sh @@ -34,7 +40,6 @@ source ./lib/rebuild_index.sh source ./lib/rebuild_tags.sh source ./lib/reset.sh source ./lib/tags_in_post.sh -source ./lib/test_markdown.sh source ./lib/twitter_card.sh source ./lib/usage.sh source ./lib/write_entry.sh @@ -47,4 +52,4 @@ source ./lib/write_entry.sh # do_main "$@" -# vim: set shiftwidth=4 tabstop=4 expandtab: \ No newline at end of file +# vim: set shiftwidth=4 tabstop=4 expandtab: