~stormdragon2976/bashtml

5d45b823719b11c77dbdeeadec1476da497a8bc4 — Storm Dragon 2 years ago a626279
Code fixes based on shellcheck recomendations.
1 files changed, 26 insertions(+), 26 deletions(-)

M bashtml.sh
M bashtml.sh => bashtml.sh +26 -26
@@ 71,7 71,7 @@ init() {
  local dependencies=(
    "markdown" # tested with discount, other markdown providers may require changing the code.
  )
  for i in ${dependencies[@]} ; do
  for i in "${dependencies[@]}" ; do
    command -v "$i" &> /dev/null || {
      echo "Please install $i before using $scriptName.";
      exit 1;


@@ 95,7 95,7 @@ init() {
    bashtml-plugins
    bashtml-scripts
  )
  for i in ${directories[@]} ; do
  for i in "${directories[@]}" ; do
    [[ -d "$i" ]] || {
      mkdir "$i";
      declare -f "initialize_$i" &> /dev/null && "initialize_$i";


@@ 118,7 118,7 @@ robots_txt() {
    /.bashtmlrc
    '/*.md'
  )
  for i in ${disallowed[@]} ; do
  for i in "${disallowed[@]}" ; do
    grep -qx "Disallow: $i" robots.txt || echo "Disallow: $i" >> robots.txt
  done
}


@@ 293,12 293,12 @@ EOF

run_plugins() {
    # $1 is the type of plugin to run. 
    local plugins=($(find bashtml-plugins/ -iname "*.$1" 2> /dev/null))
    mapfile -t plugins < <(find bashtml-plugins/ -iname "*.$1" 2> /dev/null)
    # We are done with $1, so remove it 
    shift
    unset i
    for i in ${plugins[@]} ; do
        ./$i ${@}
    for i in "${plugins[@]}" ; do
        ./"$i" "${@}"
    done
}



@@ 335,7 335,7 @@ sync_to_server() {
        rsyncArgs+=("--exclude \"${settings[rsyncExcludes]// /\" --exclude \"}\"")
    fi
    # Sync the files to the server with the given settings.
    eval "rsync ${rsyncArgs[@]} . ${settings[rsyncUser]}@${settings[rsyncAddress]}"
    eval "rsync ${rsyncArgs[*]} . ${settings[rsyncUser]}@${settings[rsyncAddress]}"
    # Postsync plugins
    run_plugins postsync
}


@@ 346,7 346,7 @@ local i
# Find the next available post number
local lastPost=0
local postNumber=0
for i in *.${settings[pageExtension]} ; do
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


@@ 363,13 363,13 @@ cat << EOF > "$htmlFile"
<meta name="application-name" content="$scriptName" data-postnumber="$postNumber">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
  $( # Meta plugins.
  run_plugins meta $fileName)
  run_plugins meta "${fileName}")
<title>${settings[blogName]}</title>
<link rel="stylesheet" type="text/css" href="bashtml-css/${settings[css]}">
</head>
<body>
  $( # Pre header plugins.
  run_plugins preheader $fileName)
  run_plugins preheader "${fileName}")
<header>
<h1><a href="${settings[blogURL]}">${settings[blogName]}</a></h1>
<p>


@@ 377,7 377,7 @@ ${settings[blogDescription]}
</p>
</header>
  $( # Post header plugins.
  run_plugins postheader $fileName)
  run_plugins postheader "${fileName}")
<!-- END OF HEADER -->
EOF
}


@@ 399,11 399,11 @@ add_footer()
  fi
cat << EOF >> "$htmlFile"
  $( # Plugin placed just above footer.
    run_plugins prefooter $fileName)
    run_plugins prefooter "${fileName}")
<!-- START OF FOOTER -->
<footer>
  $( # Plugin placed inside footer.
    run_plugins footer $fileName)
    run_plugins footer "${fileName}")
<h3>$license $author</h3>
<p>
Powered by <a href="https://gitlab.com/stormdragon2976/bashtml">${scriptName} version ${scriptVersion}</a>


@@ 419,7 419,7 @@ echo "</p>"
fi)
</footer>
  $( # Plugin placed just below footer.
    run_plugins postfooter $fileName)
    run_plugins postfooter "${fileName}")
</body>
</html>
EOF


@@ 438,10 438,10 @@ build_pages() {
  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
  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
    [[ $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


@@ 450,11 450,11 @@ build_pages() {
    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)"
  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
  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


@@ 519,7 519,7 @@ new_post() {
    echo "<hr>" >> "$htmlFile"
    echo "<h4>Tags</h4>" >> "$htmlFile"
    echo "<p>" >> "$htmlFile"
    for i in ${tags[@]} ; do
    for i in "${tags[@]}" ; do
      echo "<a href=\"${settings[gnuSocialURL]}/search/notice?q=$i\">$i</a>," >> "$htmlFile"
    done
    echo "</p>" >> "$htmlFile"


@@ 534,16 534,16 @@ new_post() {
    echo "$title created successfully."
    # Plugins called after article is created.
    # Contains file name and page title.
    run_plugins post "$fileName" "$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."
    echo "Your file is saved at ${htmlFile}. Please place it manually."
  fi
  if [[ ! -e "${pageName}.md" ]]; then
    cp -- "$mdFile" "${pageName}.md"
    cp -- "${mdFile}" "${pageName}.md"
  else
    echo "A file named ${pageName}.md already exists."
    echo "Your file is saved at $mdFile. Please place it manually."
    echo "Your file is saved at ${mdFile}. Please place it manually."
  fi
}



@@ 560,7 560,7 @@ rebuild_site() {
        # 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/^\([[:space:][:alnum:]]\)/## \1/' -e '1s/^#\+/##/' "$mdFile"
        sed -i -e "1s/\([^a-zA-Z0-9]*\)\(.*\)/## [\2]($fileName)/" -e '1s/^#\+/##/' "$mdFile"
        # Add the header:
        add_header "$htmlFile"
        markdown "$mdFile" >> "$htmlFile"


@@ 581,7 581,7 @@ rebuild_site() {
            echo "<hr>" >> "$htmlFile"
            echo "<h4>Tags</h4>" >> "$htmlFile"
            echo "<p>" >> "$htmlFile"
            for i in ${tags[@]} ; do
            for i in "${tags[@]}" ; do
                echo "<a href=\"${settings[gnuSocialURL]}/search/notice?q=$i\">$i</a>," >> "$htmlFile"
            done
    echo "</p>" >> "$htmlFile"