boh 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #!/bin/sh
  2. boh_help() {
  3. echo "Usage: boh COMMAND [options...]"
  4. echo "Transform your markdown documents into html"
  5. echo "Options:"
  6. echo " -s, --source [DIR] Source directory (defaults to ./)"
  7. echo " -d, --destination [DIR] Destination directory (defaults to ./_site)"
  8. echo " -l, --layouts [DIR] Layouts directory (defaults to ./_layouts)"
  9. echo "Commands:"
  10. echo " build, b Build your pages"
  11. echo " serve, server, s Serve your pages locally"
  12. echo " new Creates boh basic stuff"
  13. }
  14. boh_new() {
  15. mkdir $destination_dir $layouts_dir
  16. cat <<EOF > "$layouts_dir"/default.html
  17. <!doctype html>
  18. <html lang="en">
  19. <head>
  20. <meta charset="utf-8">
  21. <title>\$title</title>
  22. </head>
  23. <body>
  24. \$body
  25. </body>
  26. </html>
  27. EOF
  28. }
  29. boh_serve() {
  30. cd $destination_dir
  31. python2 -m SimpleHTTPServer
  32. }
  33. boh_check() {
  34. if [ ! -d $layouts_dir ];then
  35. >&2 echo "'$layouts_dir' directory not found. Maybe you should 'boh new'"
  36. exit 1
  37. fi
  38. type markdown &> /dev/null || {
  39. >&2 echo "'markdown' is not installed"
  40. exit 1
  41. }
  42. type envsubst &> /dev/null || {
  43. >&2 echo "'envsubst' is not installed, install 'gettext' package"
  44. exit 1
  45. }
  46. }
  47. # Parse the yaml frontmatter and returns a string ready for eval
  48. # First sed extract the frontmatter
  49. # Second sed removes frontmatter's delimiter
  50. # Third sed transform yaml in shell evaluable code (lol: asd -> lol="asd")
  51. # Kudos to stackoverflow users for this magic
  52. parse_frontmatter(){
  53. frontmatter=`cat $1 | sed -n '1{/^---/{:a N;/\n---/!ba;p}}' | sed '1d;$d' | sed -e 's/:[^:\/\/]/="/g;s/$/"/g;s/ *=/=/g'`
  54. echo $frontmatter
  55. }
  56. boh_build() {
  57. echo "Building all the things..."
  58. rm -rf $destination_dir
  59. mkdir $destination_dir
  60. type boh_pre_build &> /dev/null && { boh_pre_build; }
  61. # extra "/" is used to increment the final count by 1
  62. cut_at=`echo "/"$source_dir | fold -w 1 | grep -c /`
  63. for file in `find $source_dir -type f -name "*.md"`; do
  64. layout="default"
  65. title=`echo $file | rev | cut -d / -f 1 | rev | sed s/.md//`
  66. # sed removes the yaml frontmatter
  67. body=`cat $file | sed '1{/^---/{:a N;/\n---/!ba;d}}' | markdown`
  68. newpath="$destination_dir/"`echo $file | rev | cut -d . -f 2 | rev | cut -d / -f $cut_at-`".html"
  69. frontmatter=`parse_frontmatter $file`
  70. if [ "$frontmatter" ];then
  71. eval $frontmatter
  72. fi
  73. mkdir -p "${newpath%/*}"
  74. export title body
  75. envsubst < "$layouts_dir/$layout.html" > "$newpath"
  76. unset title body
  77. done
  78. type boh_post_build &> /dev/null && { boh_post_build; }
  79. echo "Yay!"
  80. }
  81. ## defaults
  82. source_dir="./"
  83. destination_dir="./_site"
  84. layouts_dir="./_layouts"
  85. command=$1
  86. shift
  87. for arg in "$@"
  88. do
  89. case $arg in
  90. -s=*|--source=*)
  91. source_dir="${arg#*=}"
  92. source_dir="`eval echo ${source_dir//>}`"
  93. shift
  94. ;;
  95. -d=*|--destination=*)
  96. destination_dir="${arg#*=}"
  97. destination_dir="`eval echo ${destination_dir//>}`"
  98. shift
  99. ;;
  100. -l=*|--layouts=*)
  101. layouts_dir="${arg#*=}"
  102. layouts_dir="`eval echo ${layouts_dir//>}`"
  103. shift
  104. ;;
  105. *)
  106. ;;
  107. esac
  108. done
  109. case $command in
  110. "build"|"b")
  111. boh_check
  112. if [ -f $source_dir"/boh.config" ];then
  113. source $source_dir"/boh.config"
  114. fi
  115. boh_build
  116. ;;
  117. "new")
  118. boh_new
  119. ;;
  120. "serve"|"server"|"s")
  121. boh_serve
  122. ;;
  123. *)
  124. boh_help
  125. ;;
  126. esac