kaveh

a simple website generator
git clone git://mahdi.pw/kaveh.git
Log | Files | Refs | README | LICENSE

kaveh (4290B)


      1 #!/bin/sh
      2 #
      3 # This is a simple statis website generator script.
      4 #
      5 KVH_COPYRIGHT="Copyright (C) 2021-2022 Mahdi Mirzade"
      6 KVH_VERSION="0.1"
      7 KVH_SRC="${PWD}/src"
      8 KVH_DST="${PWD}/out"
      9 
     10 # Default page values
     11 KVH_PAGE_NAME="Kaveh"
     12 KVH_PAGE_ICON="\/logo.png"
     13 KVH_PAGE_DESC="Kaveh is a simple website generator, it aims to generate static html files from markdown files, no databases and bloat-web, just texts."
     14 
     15 while getopts :vi:o: flag; do
     16     case "${flag}" in
     17         i) KVH_SRC=${OPTARG};;
     18         o) KVH_DST=${OPTARG};;
     19         v)
     20             printf "\n"
     21             printf " _  __               _      \tKaveh v%s\n" "${KVH_VERSION}"
     22             printf "| |/ /__ ___   _____| |__   \t%s\n" "${KVH_COPYRIGHT}"
     23             printf "| ' // _\` \ \ / / _ \ '_ \ \t\n"
     24             printf "| . \ (_| |\ V /  __/ | | | \tThis program may be freely redistributed under\n"
     25             printf "|_|\_\__,_| \_/ \___|_| |_| \tthe terms of the MIT License.\n"
     26             printf "\n"
     27             exit
     28         ;;
     29         *)
     30             printf "%s [-v] [-i SOURCE_DIR] [-o OUTPUT_DIR]\n" "$0"
     31             exit
     32         ;;
     33     esac
     34 done
     35 
     36 getvalue() { sed -e "/.*<\!---.*KVH.*/,/--->/!d" "$1" | sed -n "/^$2/I p" | sed 's/^.*: //'; }
     37 md2html() { markdown "$1" | sed -e '/.*<\!---.*KVH.*/,/--->/d'; }
     38 
     39 # Generate html files in source from markdowns.
     40 find "${KVH_SRC}" -name "*.md" | while read FILE; do
     41     FILE_NAME="${FILE##$KVH_SRC/}"
     42     FILE_DIR="$(dirname "${FILE_NAME}")"
     43 
     44     [ -d "${KVH_DST}/${FILE_DIR}" ] \
     45         || mkdir -p "${KVH_DST}/${FILE_DIR}"
     46 
     47     PAGE_ICON="$(getvalue "${FILE}" "icon")"
     48     PAGE_NAME="$(getvalue "${FILE}" "title")"
     49     PAGE_DESC="$(getvalue "${FILE}" "description")"
     50 
     51     [ "${PAGE_ICON}" ] || PAGE_ICON="${KVH_PAGE_ICON}"
     52     [ "${PAGE_NAME}" ] || PAGE_NAME="${KVH_PAGE_NAME}"
     53     [ "${PAGE_DESC}" ] || PAGE_DESC="${KVH_PAGE_DESC}"
     54 
     55     printf "%s\n" \
     56         "$(sed '0,/^__HTML TEMPLATE__$/d' "$0" | sed -e '/PAGE_DATA/,//d')" \
     57         "$(md2html "${FILE}")" \
     58         "$(sed '0,/^__HTML TEMPLATE__$/d' "$0" | sed -e '0,/%PAGE_DATA%/d')" \
     59     | sed   -e '/^\s*$/d' \
     60             -e "s/%PAGE_TITLE%/${PAGE_NAME}/g" \
     61             -e "s/%PAGE_DESC%/${PAGE_DESC}/g" \
     62             -e "s/%PAGE_ICON%/${PAGE_ICON}/g" \
     63             -e "s/%KVH_VERSION%/${KVH_VERSION}/g" \
     64     > "${KVH_DST}/${FILE_NAME%.md}.html"
     65 
     66     # Minify html files: (just removing characters within tags, not effecting JS,CSS)
     67     #sed -i ':a;N;$!ba;s/>\s*</></g' "${KVH_DST}/${FILE_NAME%.md}.html"
     68 
     69 done
     70 
     71 # Copy the rest files/assets into destination.
     72 find "${KVH_SRC}" -type f | while read FILE; do
     73     [ "${FILE#*.}" = "md" ] || continue;    # Skip *.md files.
     74     FILE_NAME="${FILE##$KVH_SRC/}"
     75     FILE_DIR="$(dirname "$FILE")"
     76     [ -d "${FILE_DIR}" ] || mkdir -p "${KVH_DST}/${FILE_DIR}"
     77     [ -e "${FILE}" ] || cp "${FILE}" "${KVH_DST}/${FILE_NAME}"
     78 done
     79 
     80 exit $?
     81 
     82 __HTML TEMPLATE__
     83 <!DOCTYPE html>
     84 <html lang=en>
     85 <head>
     86 <meta charset="UTF-8">
     87 <meta name="viewport" content="width=device-width, initial-scale=1">
     88 <meta name="generator" content="Kaveh v%KVH_VERSION%" />
     89 <meta name="description" content="%PAGE_DESC%">
     90 <link rel="icon" href="%PAGE_ICON%">
     91 <title>%PAGE_TITLE%</title>
     92 <style>
     93     html {
     94         overflow-y: scroll;
     95     }
     96     body {
     97 	background-color: #fff;
     98 	color: #000;
     99         margin: 0 auto;
    100         padding: 1ex;
    101 	max-width: 80ex;
    102 	font-family: sans-serif;
    103     }
    104     /* Center Images */
    105     img {
    106         display: block;
    107         margin: 0 auto;
    108     }
    109     pre {
    110         margin: 0;
    111     }
    112     code {
    113     	background-color: #eee;
    114     	border: 3px solid #aaa;
    115     	display: block;
    116     	font-family: monospace;
    117     	overflow-x: auto;
    118     	padding: 5px;
    119     	word-wrap: normal;
    120 	white-space: pre-wrap;
    121     }
    122     h1,h2,h3 {
    123         line-height:1.2
    124     }
    125     @media print{
    126         body{
    127             max-width:none
    128         }
    129     }
    130     @media (prefers-color-scheme: dark){
    131         body {color:#fff;background-color:#000}
    132 	code {border-color:#222;background-color:#111;}
    133         a:link {color:#cdf}
    134         a:hover, a:visited:hover {color:#def}
    135         a:visited {color:#dcf}
    136     }
    137 </style>
    138 </head>
    139 <body>
    140 <div>
    141 %PAGE_DATA%
    142 </div>
    143 <footer style="text-align: right;">
    144 <p><small>Powered by <a href="https://github.com/MahdiMirzade/Kaveh">Kaveh</a></small></p>
    145 </footer>
    146 </body>
    147 </html>
    148