~stormdragon2976/bashtml

3f5863f3f434a0333082f7b1291dd0fd3d2b7d2a — Storm Dragon 1 year, 5 months ago 5d45b82
A little bit of code cleanup. Fixed spacing because I changed my vim configuration and some of the spaces were in the old style.
1 files changed, 253 insertions(+), 240 deletions(-)

M bashtml.sh
M bashtml.sh => bashtml.sh +253 -240
@@ 2,11 2,11 @@

# Global variables section. Anything use through the whole script will be initialized here.
command=(
  h # help
  n # New post
  p # New post
  r # Rebuild site
  s # sync to server using rsync
    h # help
    n # New post
    p # New post
    r # Rebuild site
    s # sync to server using rsync
)
htmlFile="$(mktemp -t html.XXXXXXXXXXXX)"
ifs="$IFS"


@@ 16,24 16,24 @@ scriptName="BasHTML"
scriptVersion="0.90"
  
cleanup() {
  # This runs any time the script closes.
  # Called from the trap statement in the main body.
  local i
  for i in "$mdFile" "$htmlFile" ; do
    [[ -f "$i" ]] && rm -f "$i"
  done
# Return  IFS to its propper state
  # I dont' actually think this is necessary but better safe than sorry
  IFS="$ifs"
    # This runs any time the script closes.
    # Called from the trap statement in the main body.
    local i
    for i in "$mdFile" "$htmlFile" ; do
        [[ -f "$i" ]] && rm -f "$i"
    done
     Return  IFS to its propper state
    # I dont' actually think this is necessary but better safe than sorry
    IFS="$ifs"
}
  
configuration() {
  # Loads or creates configuration file, .bashtmlrc.
  [[ -f ".bashtmlrc" ]] && {
    source ".bashtmlrc";
    return;
  }
  # No configuration file was detected, so write it
    # Loads or creates configuration file, .bashtmlrc.
    if [[ -f ".bashtmlrc" ]]; then
        source ".bashtmlrc";
        return;
    fi
    # No configuration file was detected, so write it
    # The next part of this function is left-aligned easier with cat EOF.
cat << EOF > .bashtmlrc
declare -Ag settings=(


@@ 58,74 58,79 @@ declare -Ag settings=(
[rsyncUser]="" # User rsync should sync as, e.g. http
)
EOF
  # Exit so configuration can be customized.
  echo ".bashtmlrc configuration has been created. Please edit it to your liking and run ${0##*/} again." | fold -s
  exit 0
    # Exit so configuration can be customized.
    echo ".bashtmlrc configuration has been created. Please edit it to your liking and run ${0##*/} again." | fold -s
    exit 0
}

init() {
  # setup for  a new site
  # This is ran each time bashtml starts, to check dependancies and directory structures.
  # Check installed dependancies
  local i
  local dependencies=(
    "markdown" # tested with discount, other markdown providers may require changing the code.
  )
  for i in "${dependencies[@]}" ; do
    command -v "$i" &> /dev/null || {
      echo "Please install $i before using $scriptName.";
      exit 1;
    }
  done
  # Base64 encode author's email address if set
  # If already encoded to base64, the last character will be =
  if [[ -n "${settings[email]}" ]]; then
    if [[ "${settings[email]:$((${#settings[email]} - 1))}" != '=' ]]; then
      # Update the variable both in the current script and the settings file
      sed -i "s/\[email\]=\"\(.*\)\"\(.*\)/[email]=\"$(echo "${settings[email]}" | base64)\"\2/"  ".bashtmlrc"
      settings[email]="$(echo "${settings[email]}" | base64)"
      # Remove the old script, if any.
      rm bashtml-scripts/email.js &> /dev/null
      initialize_bashtml-scripts
    # setup for  a new site
    # This is ran each time bashtml starts, to check dependancies and directory structures.
    # Check installed dependancies
    local i
    local dependencies=(
        "markdown" # tested with discount, other markdown providers may require changing the code.
    )
    for i in "${dependencies[@]}" ; do
        command -v "$i" &> /dev/null || {
            echo "Please install $i before using $scriptName.";
            exit 1;
        }
    done
    # Base64 encode author's email address if set
    # If already encoded to base64, the last character will be =
    if [[ -n "${settings[email]}" ]]; then
        if [[ "${settings[email]:$((${#settings[email]} - 1))}" != '=' ]]; then
            # Update the variable both in the current script and the settings file
            sed -i "s/\[email\]=\"\(.*\)\"\(.*\)/[email]=\"$(echo "${settings[email]}" | base64)\"\2/"  ".bashtmlrc"
            settings[email]="$(echo "${settings[email]}" | base64)"
            # Remove the old script, if any.
            rm bashtml-scripts/email.js &> /dev/null
            initialize_bashtml-scripts
        fi
    fi
  fi
  # create directories
  local directories=(
    bashtml-css
    bashtml-plugins
    bashtml-scripts
  )
  for i in "${directories[@]}" ; do
    [[ -d "$i" ]] || {
      mkdir "$i";
      declare -f "initialize_$i" &> /dev/null && "initialize_$i";
    }
  done
  # Write robots.txt file
  robots_txt
    # create directories
    local directories=(
        bashtml-css
        bashtml-plugins
        bashtml-scripts
    )
    for i in "${directories[@]}" ; do
        if ! [[ -d "$i" ]]; then
            mkdir "$i"
            declare -f "initialize_$i" &> /dev/null && "initialize_$i"
        fi
    done
    # Write robots.txt file
    robots_txt
}

robots_txt() {
  # If robots.txt doesn't exist, create it.
  [[ -f robots.txt ]] || touch robots.txt
  # Add all spiders if it's not found on first line
  [[ "$(head -1 robots.txt)" == 'User-agent: *' ]] || sed -i -e '/U#ser-agent:/d' -e '1s/^/User-agent: *\n/' robots.txt
    # If robots.txt doesn't exist, create it.
    if ! [[ -f robots.txt ]]; then
        touch robots.txt
    fi
    # Add all spiders if it's not found on first line
    if [[ "$(head -1 robots.txt)" != 'User-agent: *' ]]; then
        sed -i -e '/U#ser-agent:/d' -e '1s/^/User-agent: *\n/' robots.txt
    fi
  # Now add non-allowed directories and files.
  local disallowed=(
    /bashtml-css/
    /bashtml-plugins/
    /bashtml-scripts/
    /.bashtmlrc
    '/*.md'
  )
  for i in "${disallowed[@]}" ; do
    grep -qx "Disallow: $i" robots.txt || echo "Disallow: $i" >> robots.txt
  done
    local disallowed=(
        /bashtml-css/
        /bashtml-plugins/
        /bashtml-scripts/
        /.bashtmlrc
        '/*.md'
    )
    for i in "${disallowed[@]}" ; do
        grep -qx "Disallow: $i" robots.txt || echo "Disallow: $i" >> robots.txt
    done
}

initialize_bashtml-scripts() {
# Create email protection script
if [[ -n "${settings[email]}" ]]; then
    # Create email protection script
    if [[ -n "${settings[email]}" ]]; then
    # Heredocument left aligned
cat << EOF > "bashtml-scripts/email.js"
//	  <!--
decodeBase64 = function(s) {


@@ 141,12 146,14 @@ decodeBase64 = function(s) {
document.write('<a href="mailto://' + decodeBase64('${settings[email]}') + '">contact ${settings[author]} at ' + decodeBase64('${settings[email]}') + '</a>');
	  //-->
EOF
fi
    # Heredocument end
    fi
}

initialize_bashtml-css() {
#write  default html file.
# http://matthewjamestaylor.com/blog/perfect-full-page.htm
    # write  default html file.
    # http://matthewjamestaylor.com/blog/perfect-full-page.htm
# Heredocument left aligned
cat << EOF > bashtml-css/default.css
	/* <!-- */
	/* General styles */


@@ 289,6 296,7 @@ cat << EOF > bashtml-css/default.css
	}
	/* --> */
EOF
    # Heredocument end
}

run_plugins() {


@@ 341,19 349,19 @@ sync_to_server() {
}

add_header() {
# This function is left aligned so the cat command can be ended with EOF
local i
# Find the next available post number
local lastPost=0
local postNumber=0
for i in *."${settings[pageExtension]}" ; do
  lastPost=$(grep -m1 -- '<meta name="application-name" content=".*" data-postnumber=".*">$' "$i" 2> /dev/null | tr -cd '[:digit:]')
  if [[ $lastPost -gt $postNumber ]]; then
    postNumber=$lastPost
  fi
done
# Increment postNumber for the next available slot.
((postNumber++))
    local i
    # Find the next available post number
    local lastPost=0
    local postNumber=0
    for i in *."${settings[pageExtension]}" ; do
        lastPost=$(grep -m1 -- '<meta name="application-name" content=".*" data-postnumber=".*">$' "$i" 2> /dev/null | tr -cd '[:digit:]')
        if [[ $lastPost -gt $postNumber ]]; then
            postNumber=$lastPost
        fi
    done
    # Increment postNumber for the next available slot.
    ((postNumber++))
    # Heredocument left aligned
cat << EOF > "$htmlFile"
<!DOCTYPE html>
<html>


@@ 380,23 388,24 @@ ${settings[blogDescription]}
  run_plugins postheader "${fileName}")
<!-- END OF HEADER -->
EOF
    # Heredocument end
}

add_footer()
{
  # Set license and author info
  local author
  local license
  if [[ -n "${settings[authorURL]}" ]]; then
    author="<a href=\"${settings[authorURL]}\">${settings[author]}</a>"
  else
    author="${settings[author]}"
  fi
  if [[ -n "${settings[licenseURL]}" ]]; then
    license="<a href=\"${settings[licenseURL]}\">${settings[license]}</a>"
  else
    license="${settings[license]}"
  fi
add_footer() {
    # Set license and author info
    local author
    local license
    if [[ -n "${settings[authorURL]}" ]]; then
        author="<a href=\"${settings[authorURL]}\">${settings[author]}</a>"
    else
        author="${settings[author]}"
    fi
    if [[ -n "${settings[licenseURL]}" ]]; then
        license="<a href=\"${settings[licenseURL]}\">${settings[license]}</a>"
    else
        license="${settings[license]}"
    fi
    # Heredocument left aligned
cat << EOF >> "$htmlFile"
  $( # Plugin placed just above footer.
    run_plugins prefooter "${fileName}")


@@ 423,128 432,131 @@ fi)
</body>
</html>
EOF
    # Heredocument end
}

build_pages() {
  # Set the filename to index.ext so that plugins that depend on it will work.
  fileName="index.${settings[pageExtension]}"
  # Start the index file
  add_header "index"
  # Remove the postnumber from the index.
  # It is added during the header section.
  sed -i -- '/<meta name="application-name" content=".*" data-postnumber=".*">$/d' "$htmlFile"
  # Generate the index page
  local i
  local lastPost=1 # placeholder for last published postnumber
  local lastNumber=1 # Keep track of the last highest number.
  local pages # A list of pages to add to index.
  for i in *".${settings[pageExtension]}" ; do
    lastNumber=$(grep -m1 -- '<meta name="application-name" content=".*" data-postnumber=".*">$' "$i" | tr -cd '[:digit:]')
    lastNumber=${lastNumber:-0}
    [[ $lastNumber -gt $lastPost ]] && lastPost=$lastNumber
  done
  # Now, lastNumber is repurposed to be the last number included on the index.
  if [[ $(($lastPost - ${settings[maxIndex]})) -le 0 ]]; then
    lastNumber=1
  else
    lastNumber=$(($lastPost - ${settings[maxIndex]}))
  fi
  # Generate the list of pages to be included on the index.
  for i in $(seq ${lastPost} -1 ${lastNumber}) ; do
    pages[${#pages[@]}]="$(grep -H "^<meta name=\"application-name\" content=\".*\" data-postnumber=\"$i\">$" *".${settings[pageExtension]}" | cut -d: -f1)"
  done
  # Build the actual index's posts.
  for i in "${pages[@]}" ; do
    sed -n -e '/^<!-- END OF HEADER -->$/,/<!-- START OF FOOTER -->$/p' "$i" | sed '/^<!-- \(END OF HEADER\|START OF FOOTER\) -->$/d' >> "$htmlFile"
  done
  # Decrement lastNumber, then see if it is greater than 0
((lastNumber--))
  if [[ $lastNumber -gt 0 ]]; then
    # Create links to older posts
    local articleFileName
    echo "<h6>Older Posts</h6>" >> "$htmlFile"
    echo "<p><ul>" >> "$htmlFile"
    for i in $(seq $lastNumber -1 1) ; do
      # This makes links to the older articles
      articleFileName="$(grep -H -- "^<meta name=\"application-name\" content=\".*\" data-postnumber=\"$i\">$" *.${settings[pageExtension]})"
      # Extract the file name by removing everything after the first :
      articleFileName="${articleFileName%%:*}"
      echo "<li><a href=\"$articleFileName\">$(grep -m1 '^<h2>.*</h2>$' "$articleFileName" | cut -d '>' -f3 | cut -d '<' -f1)</a></li>" >> "$htmlFile"
    # Set the filename to index.ext so that plugins that depend on it will work.
    fileName="index.${settings[pageExtension]}"
    # Start the index file
    add_header "index"
    # Remove the post number from the index.
    # It is added during the header section.
    sed -i -- '/<meta name="application-name" content=".*" data-postnumber=".*">$/d' "$htmlFile"
    # Generate the index page
    local i
    local lastPost=1 # placeholder for last published postnumber
    local lastNumber=1 # Keep track of the last highest number.
    local pages # A list of pages to add to index.
    for i in *".${settings[pageExtension]}" ; do
        lastNumber=$(grep -m1 -- '<meta name="application-name" content=".*" data-postnumber=".*">$' "$i" | tr -cd '[:digit:]')
        lastNumber=${lastNumber:-0}
        if [[ $lastNumber -gt $lastPost ]]; then
            lastPost=$lastNumber
        fi
    done
    # Now, lastNumber is repurposed to be the last number included on the index.
    if [[ $(($lastPost - ${settings[maxIndex]})) -le 0 ]]; then
        lastNumber=1
    else
        lastNumber=$(($lastPost - ${settings[maxIndex]}))
    fi
    # Generate the list of pages to be included on the index.
    for i in $(seq ${lastPost} -1 ${lastNumber}) ; do
        pages[${#pages[@]}]="$(grep -H "^<meta name=\"application-name\" content=\".*\" data-postnumber=\"$i\">$" *".${settings[pageExtension]}" | cut -d: -f1)"
    done
    # Build the actual index's posts.
    for i in "${pages[@]}" ; do
        sed -n -e '/^<!-- END OF HEADER -->$/,/<!-- START OF FOOTER -->$/p' "$i" | sed '/^<!-- \(END OF HEADER\|START OF FOOTER\) -->$/d' >> "$htmlFile"
    done
    echo "</ul></p>" >> "$htmlFile"
  fi
  # Add the footer.
  add_footer "index"
  # Move indexpage into place
  cp -- "$htmlFile" "index.${settings[pageExtension]}"
    # Decrement lastNumber, then see if it is greater than 0
    ((lastNumber--))
    if [[ $lastNumber -gt 0 ]]; then
        # Create links to older posts
        local articleFileName
        echo "<h6>Older Posts</h6>" >> "$htmlFile"
        echo "<p><ul>" >> "$htmlFile"
        for i in $(seq $lastNumber -1 1) ; do
            # This makes links to the older articles
            articleFileName="$(grep -H -- "^<meta name=\"application-name\" content=\".*\" data-postnumber=\"$i\">$" *.${settings[pageExtension]})"
            # Extract the file name by removing everything after the first :
            articleFileName="${articleFileName%%:*}"
            echo "<li><a href=\"$articleFileName\">$(grep -m1 '^<h2>.*</h2>$' "$articleFileName" | cut -d '>' -f3 | cut -d '<' -f1)</a></li>" >> "$htmlFile"
        done
        echo "</ul></p>" >> "$htmlFile"
    fi
    # Add the footer.
    add_footer "index"
    # Move indexpage into place
    cp -- "$htmlFile" "index.${settings[pageExtension]}"
}

new_post() {
  # create templet
  local templateText="$(echo -e "Title Goes Here\n\nArticle text here in markdown format.\n\nTags: optional, seperate, with, commas")"
  echo "$templateText" > "$mdFile"
  ${settings[editor]} "$mdFile"
  # compare the file with the template text and abandon if they are equal.
  [[ "$(<"$mdFile")" == "$templateText" ]] && {
    echo "Post abandoned ."
    exit 0
  }
  # Extract the title, the first line, to pass to plugins
  local title="$(head -1 "$mdFile" | sed 's/^[# ]*//')"
  # Page name is the title, with dashes instead of spaces, spaces compressed to only 1 at a time, and no punctuation.
  local pageName="$(echo "$title" | sed -e 's/^[[:space:]]\+//' -e 's/[[:space:]]\+$//' -e 's/[[:punct:]]//g' -e 's/[[:space:]]\+/-/g')"
  # Page names should also be completely lowercase.
  pageName="${pageName,,}"
  # Get the final file name to pass to plugins.
  fileName="${pageName}.${settings[pageExtension]}"
  # make sure the first line (title) is a heading level 2.
  sed -i -e "1s/\([^a-zA-Z0-9]*\)\(.*\)/## [\2]($fileName)/" -e '1s/^#\+/##/' "$mdFile"
  # Add the header:
  add_header "$htmlFile"
  markdown "$mdFile" >> "$htmlFile"
  # Add the date. Note the html file is edited because it is created in the step above.
  # Slashes have to be escaped in html tags to not clash with sed. Of course I could have used something else like ; but I didn't.
  # Also, converted the title into a link to the page itself.
  sed -i "0,/<h2 id=\".*\">/{s/<h2 id=\".*\">\(.*\)<\/h2>$/<h2><a href=\"${pageName}.${settings[pageExtension]}\">\1<\/a><\/h2>\n<p>Posted $(date "+${settings[dateFormat]}") by ${settings[author]}<\/p>\n/}" "$htmlFile"
  # Create an array from the posts tags in the markdown file.
  # We need 2 sed statements, because the -e thing works on the whole input, not the previous result.
  local tags=($(sed -n 's/^Tags: \+\(.*\)/\1/p' "$mdFile" | sed 's/, */ /g'))
  if [[ ${#tags[@]} -ge 1 && -n "${settings[gnuSocialURL]}" ]]; then
    # Remove the tags line from the html file.
    # This needs to be the last occurrence, and I haven't found an easy solution to do this with sed.
    # So, I am thinking to use grep to get the last line number, then delete that line
    local i=$(grep -n "^<p>Tags: .*</p>$" "$htmlFile" | tail -1 | cut -d: -f1)
    sed -i "${i}d"  "$htmlFile"
    # Add tags back as links to the tags directory
    echo "<hr>" >> "$htmlFile"
    echo "<h4>Tags</h4>" >> "$htmlFile"
    echo "<p>" >> "$htmlFile"
    for i in "${tags[@]}" ; do
      echo "<a href=\"${settings[gnuSocialURL]}/search/notice?q=$i\">$i</a>," >> "$htmlFile"
    done
    echo "</p>" >> "$htmlFile"
  fi
  # Add the footer:
  add_footer
  # Move files into place
  # Make sure a file of the same name does not exist already
  if [[ ! -e "${pageName}.${settings[pageExtension]}" ]]; then
    cp -- "$htmlFile" "${pageName}.${settings[pageExtension]}"
    build_pages
    echo "$title created successfully."
    # Plugins called after article is created.
    # Contains file name and page title.
    run_plugins post "${fileName}" "${title}"
  else
    echo "A file named ${pageName}.${settings[pageExtension]} already exists."
    echo "Your file is saved at ${htmlFile}. Please place it manually."
  fi
  if [[ ! -e "${pageName}.md" ]]; then
    cp -- "${mdFile}" "${pageName}.md"
  else
    echo "A file named ${pageName}.md already exists."
    echo "Your file is saved at ${mdFile}. Please place it manually."
  fi
    # create templet
    local templateText="$(echo -e "Title Goes Here\n\nArticle text here in markdown format.\n\nTags: optional, seperate, with, commas")"
    echo "$templateText" > "$mdFile"
    ${settings[editor]} "$mdFile"
    # compare the file with the template text and abandon if they are equal.
    if [[ "$(<"$mdFile")" == "$templateText" ]]; then
        echo "Post abandoned ."
        exit 0
    fi
    # Extract the title, the first line, to pass to plugins
    local title="$(head -1 "$mdFile" | sed 's/^[# ]*//')"
    # Page name is the title, with dashes instead of spaces, spaces compressed to only 1 at a time, and no punctuation.
    local pageName="$(echo "$title" | sed -e 's/^[[:space:]]\+//' -e 's/[[:space:]]\+$//' -e 's/[[:punct:]]//g' -e 's/[[:space:]]\+/-/g')"
    # Page names should also be completely lowercase.
    pageName="${pageName,,}"
    # Get the final file name to pass to plugins.
    fileName="${pageName}.${settings[pageExtension]}"
    # make sure the first line (title) is a heading level 2.
    sed -i -e "1s/\(.*\)/## [\1]($fileName)/" -e '1s/^#\+/##/' "$mdFile"
    # Add the header:
    add_header "$htmlFile"
    markdown "$mdFile" >> "$htmlFile"
    # Add the date. Note the html file is edited because it is created in the step above.
    # Slashes have to be escaped in html tags to not clash with sed. Of course I could have used something else like ; but I didn't.
    # Also, converted the title into a link to the page itself.
    sed -i "0,/<h2 id=\".*\">/{s/<h2 id=\".*\">\(.*\)<\/h2>$/<h2><a href=\"${pageName}.${settings[pageExtension]}\">\1<\/a><\/h2>\n<p>Posted $(date "+${settings[dateFormat]}") by ${settings[author]}<\/p>\n/}" "$htmlFile"
    # Create an array from the posts tags in the markdown file.
    # We need 2 sed statements, because the -e thing works on the whole input, not the previous result.
    local tags=($(sed -n 's/^Tags: \+\(.*\)/\1/p' "$mdFile" | sed 's/, */ /g'))
    if [[ ${#tags[@]} -ge 1 && -n "${settings[gnuSocialURL]}" ]]; then
        # Remove the tags line from the html file.
        # This needs to be the last occurrence, and I haven't found an easy solution to do this with sed.
        # So, I am thinking to use grep to get the last line number, then delete that line
        local i=$(grep -n "^<p>Tags: .*</p>$" "$htmlFile" | tail -1 | cut -d: -f1)
        sed -i "${i}d"  "$htmlFile"
        # Add tags back as links to the tags directory
        echo "<hr>" >> "$htmlFile"
        echo "<h4>Tags</h4>" >> "$htmlFile"
        echo "<p>" >> "$htmlFile"
        for i in "${tags[@]}" ; do
            echo "<a href=\"${settings[gnuSocialURL]}/search/notice?q=$i\">$i</a>," >> "$htmlFile"
        done
        echo "</p>" >> "$htmlFile"
    fi
    # Add the footer:
    add_footer
    # Move files into place
    # Make sure a file of the same name does not exist already
    if [[ ! -e "${pageName}.${settings[pageExtension]}" ]]; then
        cp -- "$htmlFile" "${pageName}.${settings[pageExtension]}"
        build_pages
        echo "$title created successfully."
        # Plugins called after article is created.
        # Contains file name and page title.
        run_plugins post "${fileName}" "${title}"
    else
        echo "A file named ${pageName}.${settings[pageExtension]} already exists."
        echo "Your file is saved at ${htmlFile}. Please place it manually."
    fi
    if [[ ! -e "${pageName}.md" ]]; then
        cp -- "${mdFile}" "${pageName}.md"
    else
        echo "A file named ${pageName}.md already exists."
        echo "Your file is saved at ${mdFile}. Please place it manually."
    fi
}

rebuild_site() {


@@ 584,7 596,7 @@ rebuild_site() {
            for i in "${tags[@]}" ; do
                echo "<a href=\"${settings[gnuSocialURL]}/search/notice?q=$i\">$i</a>," >> "$htmlFile"
            done
    echo "</p>" >> "$htmlFile"
            echo "</p>" >> "$htmlFile"
        fi
        # Add the footer:
        add_footer


@@ 593,10 605,10 @@ rebuild_site() {
    # This prevents removal of markdown file.
    htmlFile="$(mktemp -t html.XXXXXXXXXXXX)"
    mdFile="$(mktemp -t html.XXXXXXXXXXXX)"
build_pages
    build_pages
}

#When the script closes, make sure everything is cleaned up.
# When the script closes, make sure everything is cleaned up.
trap cleanup EXIT
# Load user settings.
configuration


@@ 604,24 616,25 @@ configuration
init
# getopts does not take long arguments, so we have to convert them to short
for i in "$@"; do
  shift
  case "$i" in
    "--help") set -- "$@" "-h";;
    "--"new|post)   set -- "$@" "-p";;
    "--rebuild")   set -- "$@" "-r";;
    "--sync")   set -- "$@" "-s";;
    *)        set -- "$@" "$i"
  esac
    shift
    case "$i" in
        "--help") set -- "$@" "-h";;
        "--"new|post)   set -- "$@" "-p";;
        "--rebuild")   set -- "$@" "-r";;
        "--sync")   set -- "$@" "-s";;
        *)        set -- "$@" "$i"
    esac
done
# Short flags for recognized arguments along with what they do are in the commands array
# See the top of the file, where all global variables are declared.
# Because command is an array, we have to strip the spaces for getopts.
while getopts $(echo "${command[@]}" | tr -d '[:space:]') i ; do
  case "$i" in 
    h) show_help;;
    n|p) new_post;;
    s) sync_to_server;;
    r) rebuild_site;;
  esac
    case "$i" in 
        h) show_help;;
        n|p) new_post;;
        s) sync_to_server;;
        r) rebuild_site;;
    esac
done

exit 0