From f6efa946c20bdd5c9fee3eb22aca9c752c47fd8e Mon Sep 17 00:00:00 2001 From: Flyounet Date: Fri, 20 Nov 2015 11:13:42 +0100 Subject: [PATCH 01/12] Baker now creates a Draft dir and generate post for drafted post. --- baker | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/baker b/baker index bf1f5a3..a3e07e2 100755 --- a/baker +++ b/baker @@ -10,6 +10,9 @@ readonly POST_DIR=post # OUTPUT_DIR stores all compiled html readonly OUTPUT_DIR=out +# DRAFT_DIR stores all compiled html +readonly DRAFT_DIR=draft + # LAYOUT_DIR stores all layout markdown files readonly LAYOUT_DIR=layout @@ -279,11 +282,15 @@ usage() { case "$1" in bake) + rm -rf "$DRAFT_DIR" + mkdir -p "$DRAFT_DIR" rm -rf "$OUTPUT_DIR" mkdir -p "$OUTPUT_DIR" [[ -d "$POST_DIR" ]] || usage [[ -d "$PUBLIC_DIR" ]] && cp -r "$PUBLIC_DIR"/. "$OUTPUT_DIR" + [[ -d "$PUBLIC_DIR" ]] && cp -r "$PUBLIC_DIR"/. "$DRAFT_DIR" + touch "$DRAFT_DIR/index.html" readarray -t posts < <(find "$POST_DIR" -name '*.md' | sort -r) @@ -291,7 +298,9 @@ bake) time for post in "${posts[@]}"; do id="$(basename "$post" .md)" # skip drafts - [[ "$(header draft < "$post")" == false ]] || continue + if [ "$(header draft < "$post")" != false ]; then + render_file "$post" > "$DRAFT_DIR/$id.html"; continue; + fi echo "$id" render_file "$post" > "$OUTPUT_DIR/$id.html" & From 6e973bf6b3884ba1ff337a2dff5774479720c6c3 Mon Sep 17 00:00:00 2001 From: Flyounet Date: Fri, 20 Nov 2015 11:18:40 +0100 Subject: [PATCH 02/12] =?UTF-8?q?For=20people=20like=20me=20(who's=20speak?= =?UTF-8?q?ing=20french),=20accent=20are=20important.=20Previously=20the?= =?UTF-8?q?=20slug=20function=20didn't=20handled=20accent=20:=20echo=20'Ba?= =?UTF-8?q?ker=20est=20tr=C3=A8s=20bien'=20|=20tr=20-cs=20'[:alnum:]\n'=20?= =?UTF-8?q?-=20|=20tr=20'[:upper:]'=20'[:lower:]'=20|=20sed=20's|^-*||;s|-?= =?UTF-8?q?*$||'=20baker-est-tr-s-bien?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Now it handles them : echo 'Baker est très bien' | iconv -f utf8 -t ascii//TRANSLIT | tr -cs '[:alnum:]\n' - | tr '[:upper:]' '[:lower:]' | sed 's|^-*||;s|-*$||' baker-est-tres-bien --- baker | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/baker b/baker index a3e07e2..e520935 100755 --- a/baker +++ b/baker @@ -59,7 +59,7 @@ body() { # slug creates a friendly URL like 'hello-world' slug() { - tr -cs '[:alnum:]\n' - | tr '[:upper:]' '[:lower:]' | sed 's|^-*||;s|-*$||' + iconv -f utf8 -t ascii//TRANSLIT | tr -cs '[:alnum:]\n' - | tr '[:upper:]' '[:lower:]' | sed 's|^-*||;s|-*$||' } # From 602ace3e567861f381b67314c4867af60239ee2e Mon Sep 17 00:00:00 2001 From: Flyounet Date: Fri, 20 Nov 2015 11:30:41 +0100 Subject: [PATCH 03/12] Starting to implement a sort of Prev/Next button for each post. The idea is when you're on a Post, you can go to the older or the newer. If the there is no newer or no older, you go to the index. --- layout/post.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/layout/post.md b/layout/post.md index 23f19d9..e5a926f 100644 --- a/layout/post.md +++ b/layout/post.md @@ -12,6 +12,8 @@ From f76bb1804d1c210523001f30b804acf45c3de7db Mon Sep 17 00:00:00 2001 From: Flyounet Date: Fri, 20 Nov 2015 12:21:52 +0100 Subject: [PATCH 04/12] When posts are baked, they now points to older are newer posts. --- baker | 36 +++++++++++++++++++++++++---- post/2015-11-20-welcome-to-baker.md | 11 +++++++++ 2 files changed, 43 insertions(+), 4 deletions(-) create mode 100644 post/2015-11-20-welcome-to-baker.md diff --git a/baker b/baker index e520935..2506261 100755 --- a/baker +++ b/baker @@ -293,22 +293,50 @@ bake) touch "$DRAFT_DIR/index.html" readarray -t posts < <(find "$POST_DIR" -name '*.md' | sort -r) - + echo "Rendering Posts..." >&2 idx=0 + id_all=0 time for post in "${posts[@]}"; do + idx_prev=1;idx_next=1 id="$(basename "$post" .md)" # skip drafts if [ "$(header draft < "$post")" != false ]; then - render_file "$post" > "$DRAFT_DIR/$id.html"; continue; + (( id_all++ )) + POST_PREV='index.html';POST_NEXT='index.html'; render_file "$post" > "$DRAFT_DIR/$id.html"; continue; + fi + if [ ${idx} -eq 0 ]; then # It's the first post, so the newer one is index + id_prev=0 + else # search for the previous post not draft + while [ -f "${posts[$((id_all-idx_prev))]}" -a "$(header draft < "${posts[$((id_all-idx_prev))]}" 2>/dev/null)" != false ]; do + (( idx_prev++ )) + if [ ${idx_prev} -gt ${id_all} ]; then idx_prev=${id_all}; break; fi + done 2>/dev/null + fi + # look for the next older post not draft + while [ -f "${posts[$((id_all+idx_next))]}" -a "$(header draft < "${posts[$((id_all+idx_next))]}" 2>/dev/null)" != false ]; do + (( idx_next++ )) + done 2>/dev/null + + if [ ${idx} -eq 0 -o $((idx_all-idx_prev)) -eq 0 -a ${id_all} -eq 0 ]; then + POST_PREV="index.html" + else + POST_PREV="$( basename "${posts[$((id_all-idx_prev))]}" )" + POST_PREV="${POST_PREV%*.md}.html" fi + POST_NEXT="$( basename "${posts[$((id_all+idx_next))]}" )" + [[ -z "${POST_PREV:=}" ]] && POST_PREV='index.html' + [[ -z "${POST_NEXT:=}" ]] && POST_NEXT='index.md' + echo "$idx - $idx_next - $id [Prev:${POST_PREV}][Next:${POST_NEXT}]" + POST_NEXT="${POST_NEXT%*.md}.html" - echo "$id" render_file "$post" > "$OUTPUT_DIR/$id.html" & declare "posts_${idx}_id"="$id" export_headers "$post" "posts_${idx}_" + POST_PREV="$id.html" + (( id_all++ )) (( idx++ )) done - + echo "Rendering Index..." >&2 render_file "$LAYOUT_DIR/index.md" > "$OUTPUT_DIR/index.html" & wait ;; diff --git a/post/2015-11-20-welcome-to-baker.md b/post/2015-11-20-welcome-to-baker.md new file mode 100644 index 0000000..7ba852f --- /dev/null +++ b/post/2015-11-20-welcome-to-baker.md @@ -0,0 +1,11 @@ +--- +title: Welcome to Baker! +date: 2015-11-20T11:06:07Z +layout: post +draft: false +summary: This is your second post. +--- + +With the **Markdown** syntax, expect more readbility. Be prepared to start blogging! + + From addea7c1c10e76065ed9502dc609f3c87c1864ed Mon Sep 17 00:00:00 2001 From: Flyounet Date: Fri, 20 Nov 2015 13:35:59 +0100 Subject: [PATCH 05/12] Baker now list all posts with their status --- baker | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/baker b/baker index 2506261..9319a1b 100755 --- a/baker +++ b/baker @@ -274,6 +274,7 @@ usage() { baker post [title] draft a post bake ship all posts + list list all posts with state EOF exit 1 } @@ -359,6 +360,23 @@ post) echo "$post_file" [[ "$EDITOR" ]] && $EDITOR "$post_file" ;; +list) + idx=0 + readarray -t posts < <(find "$POST_DIR" -name '*.md' | sort -r) + ( + echo "id title status" + for post in "${posts[@]}"; do + id="$(basename "$post" .md)" + # skip drafts + if [ "$(header draft < "$post")" == 'false' ]; then + echo "${idx} $id [Published]" + else + echo "${idx} $id [Draft]" + fi + ((idx++)) + done + ) | column -t + ;; *) usage ;; From 3d7a671f6b9613526945db34e52598bbb6c06dd3 Mon Sep 17 00:00:00 2001 From: Flyounet Date: Fri, 20 Nov 2015 13:41:58 +0100 Subject: [PATCH 06/12] Baker now is able to easily change the status of a post --- baker | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/baker b/baker index 9319a1b..1afb59c 100755 --- a/baker +++ b/baker @@ -377,6 +377,28 @@ list) done ) | column -t ;; +tog) + _fname='' + readarray -t posts < <(find "$POST_DIR" -name '*.md' | sort -r) + if [ -z "${2:-}" ]; then + usage + elif [ -z "${2//[0-9]/}" ]; then + if [ ${2} -lt ${#posts[@]} ]; then + _fname="${posts[${2}]}" + fi + elif [ -f "${POST_DIR}/${2}" -o -f "${POST_DIR}/${2}.md" ]; then + _fname="${POST_DIR}/${2//\.md/}.md" + fi + if [ -z "${_fname}" ]; then + echo "This post doesn't exist" + exit 1 + fi + if [ "$(header draft < "${_fname}")" == 'false' ]; then + sed -i -e 's;^[[:space:]]*draft:[[:space:]]*false[[:space:]]*$;draft: true;g' "${_fname}" + else + sed -i -e 's;^[[:space:]]*draft:[[:space:]]*.*[[:space:]]*$;draft: false;g' "${_fname}" + fi + ;; *) usage ;; From 1e7637460c5c3fab7eca3346ef9dc99e0437962e Mon Sep 17 00:00:00 2001 From: Flyounet Date: Fri, 20 Nov 2015 13:44:29 +0100 Subject: [PATCH 07/12] Adding help for toggle --- baker | 1 + 1 file changed, 1 insertion(+) diff --git a/baker b/baker index 1afb59c..19c7202 100755 --- a/baker +++ b/baker @@ -275,6 +275,7 @@ usage() { post [title] draft a post bake ship all posts list list all posts with state + toggle [post] toggle the draft status of a post EOF exit 1 } From c523dc06c7a35bdf6cc7975a3c71718ab95a96ac Mon Sep 17 00:00:00 2001 From: Flyounet Date: Fri, 20 Nov 2015 13:45:04 +0100 Subject: [PATCH 08/12] Changed tog by toggle in the switch to follow help --- baker | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/baker b/baker index 19c7202..41c7864 100755 --- a/baker +++ b/baker @@ -378,7 +378,7 @@ list) done ) | column -t ;; -tog) +toggle) _fname='' readarray -t posts < <(find "$POST_DIR" -name '*.md' | sort -r) if [ -z "${2:-}" ]; then From e4b8a0428a62a3759b9c231dbb7baee2561b27fc Mon Sep 17 00:00:00 2001 From: Flyounet Date: Fri, 20 Nov 2015 15:45:22 +0100 Subject: [PATCH 09/12] Adding info about toggle in the README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 1d8190a..19175a4 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ 1. Give it a title: `./baker post I love pizza so much!` This command will create a markdown file that has the slug `i-love-pizza-so-much` in the `post` directory. If the `$EDITOR` environment variable is set, it will open up the post markdown file with the editor. -2. Change `draft` from `true` to `false` to publish the post. +2. Change `draft` from `true` to `false` to publish the post (or use the `bake toggle id`). 3. Bake all posts: `./baker bake` From cf64ab2afa5bfb5fa8626f0b0213f76fd8b73ae7 Mon Sep 17 00:00:00 2001 From: Flyounet Date: Fri, 20 Nov 2015 15:48:49 +0100 Subject: [PATCH 10/12] Typo error in the README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 19175a4..32b24f2 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ 1. Give it a title: `./baker post I love pizza so much!` This command will create a markdown file that has the slug `i-love-pizza-so-much` in the `post` directory. If the `$EDITOR` environment variable is set, it will open up the post markdown file with the editor. -2. Change `draft` from `true` to `false` to publish the post (or use the `bake toggle id`). +2. Change `draft` from `true` to `false` to publish the post (or use the `./baker toggle id`). 3. Bake all posts: `./baker bake` From 64341d8faf12cf96390e6e30a11dd8bd35d390b5 Mon Sep 17 00:00:00 2001 From: Flyounet Date: Fri, 20 Nov 2015 18:03:51 +0100 Subject: [PATCH 11/12] The actions : post, list and toggle are now functions. It will be easier to do changes if needed. --- baker | 119 +++++++++++++++++++++++++++++++++------------------------- 1 file changed, 67 insertions(+), 52 deletions(-) diff --git a/baker b/baker index 41c7864..7d6acc8 100755 --- a/baker +++ b/baker @@ -266,6 +266,70 @@ render_file() { echo "$content" } +# post_toggle toggles the status of a post (either use ID or post name) +post_toggle() { + local _fname='' + if [ -z "${1:-}" ]; then + usage + elif [ -z "${1//[0-9]/}" ]; then + readarray -t posts < <(find "$POST_DIR" -name '*.md' | sort -r) + if [ ${1} -lt ${#posts[@]} ]; then + _fname="${posts[${1}]}" + fi + elif [ -f "${POST_DIR}/${1}" -o -f "${POST_DIR}/${1}.md" ]; then + _fname="${POST_DIR}/${1//\.md/}.md" + fi + if [ -z "${_fname}" ]; then + echo "This post doesn't exist" + exit 1 + fi + if [ "$(header draft < "${_fname}")" == 'false' ]; then + sed -i -e 's;^[[:space:]]*draft:[[:space:]]*false[[:space:]]*$;draft: true;g' "${_fname}" + else + sed -i -e 's;^[[:space:]]*draft:[[:space:]]*.*[[:space:]]*$;draft: false;g' "${_fname}" + fi +} + +# post_list list the id title and status of posts +post_list() { + local idx=0 + readarray -t posts < <(find "$POST_DIR" -name '*.md' | sort -r) + ( + echo "id title status" + for post in "${posts[@]}"; do + id="$(basename "$post" .md)" + # skip drafts + if [ "$(header draft < "$post")" == 'false' ]; then + echo "${idx} $id [Published]" + else + echo "${idx} $id [Draft]" + fi + ((idx++)) + done + ) | column -t +} + +# post_post creates a post (in the POST_DIR) with the minimum information +post_post() { + readonly title="${@}" + [[ "$title" ]] || usage + + mkdir -p "$POST_DIR" || ( echo "Can't create '$POST_DIR'" >&2; exit 1 ) + readonly post_file="$POST_DIR/$(date +%Y-%m-%d)-$(slug <<< "$title").md" + cat > "$post_file" <<-EOF + --- + title: $title + date: $(date -u +%FT%TZ) + layout: post + draft: true + summary: + --- + EOF + + echo "$post_file" + [[ "$EDITOR" ]] && $EDITOR "$post_file" +} + # # usage # @@ -343,62 +407,13 @@ bake) wait ;; post) - readonly title="${@:2}" - [[ "$title" ]] || usage - - mkdir -p "$POST_DIR" - readonly post_file="$POST_DIR/$(date +%Y-%m-%d)-$(slug <<< "$title").md" - cat > "$post_file" <<-EOF - --- - title: $title - date: $(date -u +%FT%TZ) - layout: post - draft: true - summary: - --- - EOF - - echo "$post_file" - [[ "$EDITOR" ]] && $EDITOR "$post_file" + post_post "${@:2}" ;; list) - idx=0 - readarray -t posts < <(find "$POST_DIR" -name '*.md' | sort -r) - ( - echo "id title status" - for post in "${posts[@]}"; do - id="$(basename "$post" .md)" - # skip drafts - if [ "$(header draft < "$post")" == 'false' ]; then - echo "${idx} $id [Published]" - else - echo "${idx} $id [Draft]" - fi - ((idx++)) - done - ) | column -t + post_list ;; toggle) - _fname='' - readarray -t posts < <(find "$POST_DIR" -name '*.md' | sort -r) - if [ -z "${2:-}" ]; then - usage - elif [ -z "${2//[0-9]/}" ]; then - if [ ${2} -lt ${#posts[@]} ]; then - _fname="${posts[${2}]}" - fi - elif [ -f "${POST_DIR}/${2}" -o -f "${POST_DIR}/${2}.md" ]; then - _fname="${POST_DIR}/${2//\.md/}.md" - fi - if [ -z "${_fname}" ]; then - echo "This post doesn't exist" - exit 1 - fi - if [ "$(header draft < "${_fname}")" == 'false' ]; then - sed -i -e 's;^[[:space:]]*draft:[[:space:]]*false[[:space:]]*$;draft: true;g' "${_fname}" - else - sed -i -e 's;^[[:space:]]*draft:[[:space:]]*.*[[:space:]]*$;draft: false;g' "${_fname}" - fi + post_toggle "${@:2}" ;; *) usage From 5a5c09e785b9e09121f7f1eaa8eee08165c5814a Mon Sep 17 00:00:00 2001 From: Flyounet Date: Fri, 20 Nov 2015 18:14:47 +0100 Subject: [PATCH 12/12] The actione : bake is now a function post_bake. --- baker | 41 +++++++++++++++++++++++------------------ 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/baker b/baker index 7d6acc8..ae21304 100755 --- a/baker +++ b/baker @@ -330,24 +330,8 @@ post_post() { [[ "$EDITOR" ]] && $EDITOR "$post_file" } -# -# usage -# -usage() { - cat <<-EOF - baker - post [title] draft a post - bake ship all posts - list list all posts with state - toggle [post] toggle the draft status of a post - EOF - exit 1 -} - -(( $# == 0 )) && usage - -case "$1" in -bake) +# post_bake it's where the magic start +post_bake() { rm -rf "$DRAFT_DIR" mkdir -p "$DRAFT_DIR" rm -rf "$OUTPUT_DIR" @@ -405,6 +389,27 @@ bake) echo "Rendering Index..." >&2 render_file "$LAYOUT_DIR/index.md" > "$OUTPUT_DIR/index.html" & wait +} + +# +# usage +# +usage() { + cat <<-EOF + baker + post [title] draft a post + bake ship all posts + list list all posts with state + toggle [post] toggle the draft status of a post + EOF + exit 1 +} + +(( $# == 0 )) && usage + +case "$1" in +bake) + post_bake ;; post) post_post "${@:2}"