boh 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #!/bin/bash
  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 boh_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. boh_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_markdown() {
  66. cmark || Markdown.pl
  67. }
  68. boh_build() {
  69. echo "Building all the things..."
  70. rm -rf $destination_dir/*
  71. mkdir -p "$destination_dir"
  72. type boh_pre_build &> /dev/null && { boh_pre_build; }
  73. # extra "/" is used to increment the final count by 1
  74. cut_at=$(echo "/$source_dir" | fold -w 1 | grep -c /)
  75. find "$source_dir" -type f -name "*.md" | while read file
  76. do
  77. layout="default"
  78. title=$(echo "$file" | rev | cut -d / -f 1 | rev | sed s/.md//)
  79. # sed removes the yaml frontmatter
  80. body=$(cat "$file" | sed '1{/^---/{:a N;/\n---/!ba;d}}' | boh_markdown 2>/dev/null)
  81. newpath="$destination_dir/"$(echo "$file" | rev | cut -d . -f 2- | rev | cut -d / -f $cut_at-)".html"
  82. frontmatter=$(boh_parse_frontmatter "$file")
  83. if [ "$frontmatter" ];then
  84. eval "$frontmatter"
  85. fi
  86. mkdir -p "${newpath%/*}"
  87. export title body
  88. envsubst < "$layouts_dir/$layout.html" > "$newpath"
  89. unset title body
  90. done
  91. type boh_post_build &> /dev/null && { boh_post_build; }
  92. echo "Yay!"
  93. }
  94. ## defaults
  95. source_dir="./"
  96. destination_dir="./_site"
  97. layouts_dir="./_layouts"
  98. command=$1
  99. shift
  100. for arg in "$@"
  101. do
  102. case $arg in
  103. -s=*|--source=*)
  104. source_dir="${arg#*=}"
  105. source_dir="$(eval echo ${source_dir//>})"
  106. shift
  107. ;;
  108. -d=*|--destination=*)
  109. destination_dir="${arg#*=}"
  110. destination_dir="$(eval echo ${destination_dir//>})"
  111. shift
  112. ;;
  113. -l=*|--layouts=*)
  114. layouts_dir="${arg#*=}"
  115. layouts_dir="$(eval echo ${layouts_dir//>})"
  116. shift
  117. ;;
  118. *)
  119. ;;
  120. esac
  121. done
  122. if [ -f "$source_dir/boh.config" ];then
  123. source "$source_dir/boh.config"
  124. fi
  125. case $command in
  126. "build"|"b")
  127. boh_check
  128. boh_build
  129. ;;
  130. "new")
  131. boh_new
  132. ;;
  133. "serve"|"server"|"s")
  134. boh_serve
  135. ;;
  136. "publish")
  137. type boh_publish &> /dev/null && { boh_publish; }
  138. ;;
  139. *)
  140. boh_help
  141. ;;
  142. esac