First tests

This commit is contained in:
Josemar Lohn 2021-02-10 20:48:40 +00:00
parent adfc0fdde1
commit 26468aec34
10 changed files with 87 additions and 100 deletions

View File

@ -1,16 +1,16 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# Create an index page with all the posts # Create an index page with all the posts
declare archive_index
declare template_archive_index_page
declare template_archive_title
declare date_allposts_header
declare date_format
declare date_locale
declare index_file
declare global_title
declare global_author
all_posts() { all_posts() {
declare archive_index
declare template_archive_index_page
declare template_archive_title
declare date_allposts_header
declare date_format
declare date_locale
declare index_file
declare global_title
declare global_author
echo -n "Creating an index page with all the posts " echo -n "Creating an index page with all the posts "
contentfile=$archive_index.$RANDOM contentfile=$archive_index.$RANDOM
while [[ -f $contentfile ]]; do while [[ -f $contentfile ]]; do

View File

@ -1,17 +1,17 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# Create an index page with all the tags # Create an index page with all the tags
declare tags_index
declare template_tags_posts
declare template_tags_posts_singular
declare template_tags_posts_2_4
declare template_tags_title
declare prefix_tags
declare index_file
declare template_archive_index_page
declare global_author
declare global_title
all_tags() { all_tags() {
declare tags_index
declare template_tags_posts
declare template_tags_posts_singular
declare template_tags_posts_2_4
declare template_tags_title
declare prefix_tags
declare index_file
declare template_archive_index_page
declare global_author
declare global_title
echo -n "Creating an index page with all the tags " echo -n "Creating an index page with all the tags "
contentfile=$tags_index.$RANDOM contentfile=$tags_index.$RANDOM
while [[ -f $contentfile ]]; do while [[ -f $contentfile ]]; do

View File

@ -12,14 +12,14 @@
# $4 title for the html header # $4 title for the html header
# $5 original blog timestamp # $5 original blog timestamp
# $6 post author # $6 post author
declare body_begin_file
declare date_inpost
declare date_locale
declare date_format
declare date_format_timestamp
declare global_url
declare body_end_file
create_html_page() { create_html_page() {
declare body_begin_file
declare date_inpost
declare date_locale
declare date_format
declare date_format_timestamp
declare global_url
declare body_end_file
content=$1 content=$1
filename=$2 filename=$2
index=$3 index=$3

View File

@ -1,21 +1,21 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# generate headers, footers, etc # generate headers, footers, etc
declare global_author
declare global_author_url
declare global_description
declare global_email
declare global_feedburner
declare global_license
declare global_title
declare global_url
declare index_file
declare header_file
declare css_include
declare template_subscribe_browser_button
declare blog_feed
declare footer_file
create_includes() { create_includes() {
declare global_author
declare global_author_url
declare global_description
declare global_email
declare global_feedburner
declare global_license
declare global_title
declare global_url
declare index_file
declare header_file
declare css_include
declare template_subscribe_browser_button
declare blog_feed
declare footer_file
{ {
echo "<h1 class=\"nomargin\"><a class=\"ablack\" href=\"$global_url/$index_file\">$global_title</a></h1>" echo "<h1 class=\"nomargin\"><a class=\"ablack\" href=\"$global_url/$index_file\">$global_title</a></h1>"
echo "<div id=\"description\">$global_description</div>" echo "<div id=\"description\">$global_description</div>"

View File

@ -1,8 +1,8 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# Detects if GNU date is installed # Detects if GNU date is installed
declare date_format_full
date_version_detect() { date_version_detect() {
declare date_format_full
if ! stat -c"%U" /dev/null >/dev/null 2>&1 ; then if ! stat -c"%U" /dev/null >/dev/null 2>&1 ; then
# BSD environment # BSD environment
if command -v gdate >/dev/null 2>&1 ; then if command -v gdate >/dev/null 2>&1 ; then

View File

@ -37,19 +37,19 @@ do_main() {
if [[ $1 == edit ]]; then if [[ $1 == edit ]]; then
if (($# < 2)) || [[ ! -f ${!#} ]]; then if (($# < 2)) || [[ ! -f ${!#} ]]; then
echo "Please enter a valid .md or .html file to edit" echo "Please enter a valid .md file to edit"
exit exit
fi fi
fi fi
# Test for existing html files # Test for existing html files
if ls ./*.html &> /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
fi fi
# 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

View File

@ -4,5 +4,6 @@
# #
# $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
} }

View File

@ -9,6 +9,8 @@
# 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
is_boilerplate_file() { is_boilerplate_file() {
return 1 ### Ugly Hack to Temporally disable the verification
declare non_blogpost_files declare non_blogpost_files
declare index_file declare index_file
declare archive_index declare archive_index

View File

@ -1,13 +1,14 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# Displays a list of the posts # Displays a list of the posts
declare date_format
declare date_locale
list_posts() { list_posts() {
declare date_format ls ./*.md &> /dev/null
declare date_locale (($? != 0)) &&
if ls ./*.html > /dev/null; then echo "No posts yet. Use '$0 post' to create one" &&
echo "No posts yet. Use 'bb.sh post' to create one" && return return
fi
lines="" lines=""
n=1 n=1
while IFS='' read -r i; do while IFS='' read -r i; do
@ -15,7 +16,6 @@ list_posts() {
line="$n # $(get_post_title "$i") # $(LC_ALL=$date_locale date -r "$i" +"$date_format")" line="$n # $(get_post_title "$i") # $(LC_ALL=$date_locale date -r "$i" +"$date_format")"
lines+=$line\\n lines+=$line\\n
n=$(( n + 1 )) n=$(( n + 1 ))
done < <(ls -t ./*.html) done < <(ls -t ./*.md)
echo -e "$lines" | column -t -s "#" echo -e "$lines" | column -t -s "#"
} }

View File

@ -2,46 +2,26 @@
# 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 global_url
declare convert_filename
write_entry() { write_entry() {
declare template_tags_line_header
declare global_url
declare convert_filename
declare save_markdown
test_markdown && fmt=md || fmt=html
f=$2 f=$2
[[ $2 == -html ]] && fmt=html && f=$3 extension=${f##*.}
if [[ $extension != md ]]; then
if [[ -n $f ]]; then f=${f}.md
TMPFILE=$f fi
if [[ ! -f $TMPFILE ]]; then if [[ -n $f ]]; thenq
echo "The file doesn't exist" if [[ -f $f ]]; then
echo "The file $f doesn't exist"
delete_includes delete_includes
exit exit
fi fi
# guess format from TMPFILE
extension=${TMPFILE##*.}
[[ $extension == md || $extension == html ]] && fmt=$extension
# but let user override it (`bb.sh post -html file.md`)
[[ $2 == -html ]] && fmt=html
# Test if Markdown is working before re-posting a .md file
if [[ $extension == md ]]; then
if test_markdown; then
echo "Markdown is not working, please edit HTML file directly."
exit
fi
fi
else else
TMPFILE=.entry-$RANDOM.$fmt TMPFILE=.entry-$RANDOM.$fmt
echo -e "Title on this line\n" >> "$TMPFILE" echo -e "Title on this line\n" >> "$TMPFILE"
[[ $fmt == html ]] && cat << EOF >> "$TMPFILE" cat << EOF >> "$TMPFILE"
<p>The rest of the text file is an <b>html</b> blog post. The process will continue as soon
as you exit your editor.</p>
<p>$template_tags_line_header keep-this-tag-format, tags-are-optional, example</p>
EOF
[[ $fmt == md ]] && 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.
@ -54,21 +34,29 @@ EOF
filename="" filename=""
while [[ $post_status != "p" && $post_status != "P" ]]; do while [[ $post_status != "p" && $post_status != "P" ]]; do
[[ -n $filename ]] && rm "$filename" # Delete the generated html file, if any [[ -n $filename ]] && rm "$filename" # Delete the generated html file, if any
$EDITOR "$TMPFILE" $EDITOR "$TMPFILE"
if [[ $fmt == md ]]; then
html_from_md=$(mrkdwn "$TMPFILE") html_from_md=$(mrkdwn "$TMPFILE")
parse_file "$html_from_md"
rm "$html_from_md" parse_file "$html_from_md"
else rm "$html_from_md"
parse_file "$TMPFILE" # this command sets $filename as the html processed file
fi
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"
echo -n "[P]ost this entry, [E]dit again, [D]raft for later? (p/E/d) " echo -n "[P]ost this entry, [E]dit again, [D]raft for later, [C]ancel and quit? (p/e/d/c) "
read -r post_status read -r post_status
#Case user cancelled
if [[ $post_status == c || $post_status == C ]]; then
rm "$TMPFILE"
delete_includes
echo "Your draft was erased. Nothing was changed in your blog."
exit
fi
#Case User choose post
if [[ $post_status == d || $post_status == D ]]; then if [[ $post_status == d || $post_status == D ]]; then
mkdir -p "drafts/" mkdir -p "drafts/"
chmod 700 "drafts/" chmod 700 "drafts/"
@ -87,11 +75,7 @@ EOF
fi fi
done done
if [[ $fmt == md && -n $save_markdown ]]; then mv "$TMPFILE" "${filename%%.*}.md"
mv "$TMPFILE" "${filename%%.*}.md"
else
rm "$TMPFILE"
fi
chmod 644 "$filename" chmod 644 "$filename"
echo "Posted $filename" echo "Posted $filename"
relevant_tags=$(tags_in_post "$filename") relevant_tags=$(tags_in_post "$filename")