#!/bin/sh -e ROOT=$PWD txt2html() { printf '
\n'

    # Convert [0-9] to links
    sed -E 's|(.*[a-z].*)\[([0-9].*)\]|\1[\2]|g' "$1" |

    # Add span id to sections
    sed -E 's|^\[([0-9].*)\]|[\1]|' |

    # Make links clickable
    sed -E "s|(http[s]?://[^[:space:]\)]*)|\1|g"

    printf '
\n' } gettitle() { unset title case "$(sed -n 2p "$1")" in =*=|-*-) title="$(sed -n 1p "$1")"; esac file=${1##*/} file=${file%.*} printf '%s\n' "${title:-$file}${2:+ | Carbs Linux}" } genpages() ( cd src || return 1 find . ! -name .git | while read -r file; do # If this is a directory, create the directory on the destination and # return. [ -d "$file" ] && { mkdir -p "../docs/${file#./}" continue } # We are treating markdown/plaintext files differently. case "$file" in *.md|*.txt) tohtml "$file" > "../docs/${file%.*}.html" cp "$file" "../docs/${file%.*}.txt" ;; # If this is not a markdown/txt file copy as-is. *) cp "$file" "../docs/$file" esac done ) tohtml() { srcfile=${1#.} srcfile=${srcfile%.*}.txt case "${1##*.}" in md) markdown "$1" ;; txt) txt2html "$1" ;; esac | sed '/{{ CONTENT }}/r /dev/stdin' "$ROOT/templates/template.html" | sed "s#{{ TITLE }}#$(gettitle "$1" html)#" | sed '/{{ CONTENT }}/d' | sed "s|{{ SRC }}|$srcfile|" } blog_index() { cat templates/blog-index > src/blog/index.md find src/blog -type f ! -name index.md | sort -r | while read -r post ; do postdate="${post##*/}"; postdate="${postdate%%.*}" printf '* %s - [%s](%s)\n' \ "$(date --date="$postdate" +%b\ %d\ %Y)" \ "$(gettitle "$post")" \ "$(printf "$post" | sed 's#src/blog/##;s/.md/.html/')" done >> src/blog/index.md } site_index() { cp index/index.md src/index.md :> src/news.md i=0 find index -name '*.news' | sort -r | while read -r news; do [ "$(( i += 1 ))" -lt 6 ] && { printf '\n' cat "$news" } >> src/index.md [ "$i" -eq 1 ] || printf '\n' >> src/news.md cat "$news" >> src/news.md done } genrss() { find "$1" -type f ! -name index.md | sort -r | while read -r post ; do postdate="${post##*/}" ; postdate="${postdate%.*}" cat < $(gettitle "$post") $(date --date="$postdate" +%a,\ %d\ %b\ %Y) Cem Keylan https://carbslinux.org/${3:-blog/$(printf "${post##*/}" | sed 's/.md/.html/')} $(grep -v '^Title: ' "$post" | markdown -f cdata) EOF done | sed '/{{ CONTENT }}/r /dev/stdin' "$ROOT/templates/rss.xml" | sed '/{{ CONTENT }}/d' | sed "s|{{ SRC }}|${2:-rss}.xml|" | sed "s|{{ DATE }}|$(date -u "+%a %b %d %Y %H:00")|" } main() { # Recreate docs directory rm -rf docs; mkdir -p docs # Generate the indexes for blog and the wiki site_index; blog_index # Generate rss feeds genrss index news index.html > src/news.xml genrss src/blog > src/rss.xml # Generate pages genpages } main "$@"