boh 3.5 KB

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