From 211de32923fa1dde1b98e707168f64850415463f Mon Sep 17 00:00:00 2001 From: Joan Josep Ordinas Rosa Date: Sun, 16 Apr 2017 10:35:13 +0200 Subject: [PATCH] Migrated generation to jq --- docs/Makefile | 4 +- docs/config.yaml | 14 ++--- docs/make.d/config.make | 3 +- docs/make.d/front-matter.make | 107 ++++++++-------------------------- docs/make.d/pathnames.make | 7 ++- docs/make.d/phase0.jq | 14 ++--- docs/make.d/phase2.jq | 36 +++++++----- docs/make.d/phase21.jq | 69 ++++++++++++++++++++++ docs/make.d/prelude.make | 26 ++++----- docs/make.d/sitemap.make | 5 +- docs/make.d/styles.make | 1 - docs/make.d/tools.make | 6 +- 12 files changed, 152 insertions(+), 140 deletions(-) create mode 100644 docs/make.d/phase21.jq diff --git a/docs/Makefile b/docs/Makefile index 8a67535..29b7acf 100644 --- a/docs/Makefile +++ b/docs/Makefile @@ -62,8 +62,8 @@ ifdef __phase_1 # DestinationPaths # MetadataPages # MetadataPaths -# Exported dependencies: -# $(Metadata)/pages/.../page.json: | $$(dir $$@) +# Exported rules for: +# $(Metadata)/pages/.../page.json # ====================================================================== include make.d/pathnames.make diff --git a/docs/config.yaml b/docs/config.yaml index f5dc566..c2dd885 100644 --- a/docs/config.yaml +++ b/docs/config.yaml @@ -12,13 +12,13 @@ # Default values: # -# Destination: ./_site -# Assets: ./assets -# Blocks: ./blocks -# Content: ./content -# Data: ./data -# Layouts: ./layouts -# Styles: ./styles +# Destination: _site +# Assets: assets +# Blocks: blocks +# Content: content +# Data: data +# Layouts: layouts +# Styles: styles # Changes from defaults Destination: /tmp/jqt # fast RAM disk diff --git a/docs/make.d/config.make b/docs/make.d/config.make index 4aa20c4..2f581c1 100644 --- a/docs/make.d/config.make +++ b/docs/make.d/config.make @@ -18,8 +18,7 @@ # Metadata directory for all generated metadata and make files ######################################################################## -# Define targets with and without trailing slash. -$(Metadata) $(Metadata)/: +$(Metadata): $(info ==> $@) @mkdir --parents $@ >/dev/null 2>&1 || true diff --git a/docs/make.d/front-matter.make b/docs/make.d/front-matter.make index c665af7..17d709b 100644 --- a/docs/make.d/front-matter.make +++ b/docs/make.d/front-matter.make @@ -4,9 +4,20 @@ # Collect metadata from front-matter sections. # # Imported variables: +# Content +# Destination # DestinationPages +# DestinationPaths +# Layouts +# Metadata +# MetadataPages +# MetadataPaths # Exported rules for: -# ?? +# $(DestinationPaths) +# $(Metadata)/pages.json +# $(Metadata)/sections.json +# $(MetadataPages) +# $(MetadataPaths) ######################################################################## # Extra dependencies @@ -22,12 +33,6 @@ $(DestinationPages): $(Layouts)/default.html # Rules for directories ######################################################################## -# Destination directory. -# Define targets with and without trailing slash. -$(Destination) $(Destination)/: - $(info ==> $@) - @mkdir --parents $@ >/dev/null 2>&1 || true - # Directories starting at `$(Destination)/` $(DestinationPaths): $(info ==> $@) @@ -54,77 +59,9 @@ $(Metadata)/sections.json: $(Metadata)/pages.json @jq '[.[].section] | unique | map(select(.))' < $< > $@ ######################################################################## -# TODO: phase21.jq !!!!!!!!!!! +# Build metadata for pages. ######################################################################## -# `$(Metadata)/pages/path/to/page.json` => `path/to/page` -define f_page_id = -$(subst $(Metadata)/pages/,$(empty),$(basename $1)) -endef - -# `$(Metadata)/pages/path/to/page.json` => `(../)+` -define f_page_base = -$(patsubst ../../../%,%,$(subst $(space),$(empty),$(patsubst %,../,$(subst /,$(space),$1)))) -endef - -# `$(Metadata)/pages/path/to/page.json` => `page` -define f_page_name = -$(basename $(notdir $1)) -endef - -# `$(Metadata)/pages/path/to/page.json` => `path/to/page.html` -define f_page_url = -$(call f_page_id,$1).html -endef - -# `$(Metadata)/pages/path/to/page.json` => `path/to/` -define f_page_path = -$(dir $(call f_page_id,$1)) -endef - -# `$(Metadata)/pages/path/to/page.json` => `path/to` -define f_page_section = -$(if $(filter %index.json,$1),null,"$(patsubst %/,%,$(call f_page_path,$1))") -endef - -# Add members to user defined front-matter at `.`. -# -# Defined by jqt: -# ._content -# ._highlight -# ._toc -# ._source -# ._front_matter -# Defined here (front-matter plus...): -# .page.base -# .page.filename -# .page.id -# .page.path -# .page.section -# .page.source -# .page.url -# -# Parameters: -# $1: $(Content)/%.md -# $2: $(Metadata)/pages/%.json -define f_PAGE_JSON.jq = - . + { \ - base: "$(call f_page_base,$2)", \ - filename: "$(call f_page_name,$2)", \ - id: "$(call f_page_id,$2)", \ - path: "$(call f_page_path,$2)", \ - section: $(call f_page_section,$2), \ - source: "$1", \ - url: "$(call f_page_url,$2)" \ - } as $$page | \ - $$config[0].defaults as $$defaults | \ - reduce $$defaults[] as $$d \ - ({}; if "$(call f_page_id,$2)" | test("^" + $$d.idprefix) \ - then . + $$d.properties \ - else . end) + $$page -endef - -# Extract YAML front matter. define EXTRACT_FRONT_MATTER = sed -n -e '1d' \ -e '/^---$$/q' \ @@ -133,14 +70,16 @@ define EXTRACT_FRONT_MATTER = -e 'p' endef -# Build metadata for pages. +define BUILD_JSON + yaml2json | \ + jq --sort-keys \ + --arg Source $< \ + --arg Target $@ \ + --arg Metadata $(Metadata) \ + --slurpfile config $(Metadata)/config.json \ + --from-file make.d/phase21.jq +endef + $(MetadataPages): $(Metadata)/config.json -$(MetadataPages): $(Metadata)/pages/%.json : $(Content)/%.md # TODO: any accepted extension - $(info ==> $@) - @$(EXTRACT_FRONT_MATTER) < $< \ - | yaml2json \ - | jq --sort-keys \ - --slurpfile config $(Metadata)/config.json \ - '$(call f_PAGE_JSON.jq,$<,$@)' > $@ # vim:ai:sw=8:ts=8:noet:fileencoding=utf8:syntax=make diff --git a/docs/make.d/pathnames.make b/docs/make.d/pathnames.make index f9e5a6a..9c68490 100644 --- a/docs/make.d/pathnames.make +++ b/docs/make.d/pathnames.make @@ -3,6 +3,11 @@ # # Build metadata from filesystem introspection. # +# Imported variables: +# Content +# Destination +# Metadata +# # Exported rules for: # $(Metadata)/phase2.make @@ -10,7 +15,7 @@ # Create makefile defining global parameters about pathnames ######################################################################## -$(Metadata)/phase2.make: make.d/pathnames.make make.d/phase2.jq +$(Metadata)/phase2.make: make.d/pathnames.make make.d/phase2.jq $(Metadata)/phase1.make $(info ==> $@) @find $(Content) -type f -a \ -name '[!_]*.md' -o \ diff --git a/docs/make.d/phase0.jq b/docs/make.d/phase0.jq index f64b876..8b3ec17 100644 --- a/docs/make.d/phase0.jq +++ b/docs/make.d/phase0.jq @@ -5,13 +5,13 @@ del(.defaults) | . + { - Destination: (.Destination // "./_site"), - Assets: (.Assets // "./assets"), - Blocks: (.Blocks // "./blocks"), - Content: (.Content // "./content"), - Data: (.Data // "./data"), - Layouts: (.Layouts // "./layouts"), - Styles: (.Styles // "./styles"), + Destination: (.Destination // "_site"), + Assets: (.Assets // "assets"), + Blocks: (.Blocks // "blocks"), + Content: (.Content // "content"), + Data: (.Data // "data"), + Layouts: (.Layouts // "layouts"), + Styles: (.Styles // "styles"), Version: (.Version // $Version) } diff --git a/docs/make.d/phase2.jq b/docs/make.d/phase2.jq index 3a4259e..2d576bb 100644 --- a/docs/make.d/phase2.jq +++ b/docs/make.d/phase2.jq @@ -10,41 +10,47 @@ def dir: sub("/[^/]+$"; "/") ; -def dpaths($p): +def dpaths($paths): def dpath: - sub("^"+$Content; $Destination) + sub("^" + $Content; $Destination) ; - "DestinationPaths := " + ([$p[] | dpath][1:] | join(" ")) + "DestinationPaths := " + ([$paths[] | dpath] | join(" ")) ; -def mpaths($p): +def mpaths($paths): def mpath: - sub("^"+$Content; $Metadata+"/pages") + sub("^" + $Content; $Metadata + "/pages") ; - "MetadataPaths := " + ([$p[] | mpath] | join(" ")) + "MetadataPaths := " + ([$paths[] | mpath] | join(" ")) ; -def dpages($d): +def dpages($documents): def dpage: - sub("^"+$Content; $Destination) + sub("^" + $Content; $Destination) | sub("\\.(?:markdown|mk?d)$"; ".html") ; - "DestinationPages := " + ([$d[] | dpage] | join(" ")) + "DestinationPages := " + ([$documents[] | dpage] | join(" ")) ; -def mpages($d): +def mpages($documents): def mpage: - sub("^"+$Content; $Metadata+"/pages") + sub("^" + $Content; $Metadata + "/pages") | sub("\\.(?:markdown|mk?d)$"; ".json") ; - [$d[] | mpage] as $json - | "MetadataPages := " + ($json | join(" ")) #+ "\n" - #+ ([$json[] | . + ": | $$(dir $$@)"] | join("\n")) + def mrule: + [ $documents[] + | mpage + ": " + . + "\n" + + "\t$(info ==> $@)\n" + + "\t@$(EXTRACT_FRONT_MATTER) < $< | $(BUILD_JSON) > $@" + ] | join("\n") + ; + [$documents[] | mpage] as $json + | "MetadataPages := " + ($json | join(" ")) + "\n" + mrule ; ######################################################################## -(.[:-1]/"\n") as $documents +(.[:-1] / "\n") as $documents | [$documents[] | dir] | unique as $paths | dpaths($paths) as $DestinationPaths | mpaths($paths) as $MetadataPaths diff --git a/docs/make.d/phase21.jq b/docs/make.d/phase21.jq new file mode 100644 index 0000000..95f4437 --- /dev/null +++ b/docs/make.d/phase21.jq @@ -0,0 +1,69 @@ +# phase21.jq - --arg Source s --arg Target s --arg Metadata s --slurpfile config s +# +# Generate page.json files + +def basename: + sub("\\.[^/]+$"; "") +; + +def dir: + sub("/[^/]+$"; "/") +; + +def notdir: + sub("^.*/"; "") +; + +# `$(Metadata)/pages/path/to/page.json` => `path/to/page` +def page_id: + $Target | basename | sub($Metadata+"/pages/"; "") +; + +# `$(Metadata)/pages/path/to/page.json` => `(../)+` +def page_base: + #$(patsubst ../../../%,%,$(subst $(space),$(empty),$(patsubst %,../,$(subst /,$(space),$1)))) + ("../" * ((($Target / "/") | length) - 3)) // "" +; + +# `$(Metadata)/pages/path/to/page.json` => `page` +def page_name: + $Target | notdir | basename +; + +# `$(Metadata)/pages/path/to/page.json` => `path/to/page.html` +def page_url: + $Target | page_id+".html" +; + +# `$(Metadata)/pages/path/to/page.json` => `path/to/` +def page_path: + $Target | page_id | dir +; + +# `$(Metadata)/pages/path/to/page.json` => `path/to` +def page_section: + $Target + | if test("index.json$") + then null + else page_path[:-1] end +; + +######################################################################## +# +######################################################################## + +. + { + base: page_base, + filename: page_name, + id: page_id, + path: page_path, + section: page_section, + source: $Source, + url: page_url +} + +reduce $config[0].defaults[] as $defaults + ({}; if page_id | test("^" + $defaults.idprefix) + then . + $defaults.properties + else . end) + +# vim:ts=4:sw=4:ai:et:fileencoding=utf8:syntax=jq diff --git a/docs/make.d/prelude.make b/docs/make.d/prelude.make index d81f70d..77b75c8 100644 --- a/docs/make.d/prelude.make +++ b/docs/make.d/prelude.make @@ -3,12 +3,6 @@ # # The project prelude: common module available in all makefile modules # -# Exported macros: -# comma -# empty -# space -# rest -# filename # Debug tool ifdef MAKE_RESTARTS @@ -88,19 +82,19 @@ SHELL := /bin/bash ######################################################################## # Hacks for string manipulation -comma := , -empty := -space := $(empty) $(empty) -tab := $(empty) $(empty) +#!comma := , +#!empty := +#!space := $(empty) $(empty) +#!tab := $(empty) $(empty) # Hack for list manipulation -define rest = -$(wordlist 2,2147483648,$1) -endef +#!define rest = +#!$(wordlist 2,2147483648,$1) +#!endef # File name without extension nor directory -define filename -$(basename $(notdir $1)) -endef +#!define filename +#!$(basename $(notdir $1)) +#!endef # vim:ai:sw=8:ts=8:noet:fileencoding=utf8:syntax=make diff --git a/docs/make.d/sitemap.make b/docs/make.d/sitemap.make index 831ee38..41ef787 100644 --- a/docs/make.d/sitemap.make +++ b/docs/make.d/sitemap.make @@ -8,13 +8,12 @@ # Metadata # Exported rules for: # $(Destination)/sitemap.* -# all:: $(Destination)/sitemap.* ######################################################################## # Sitemap ######################################################################## -define s_sitemap.jq +define SITEMAP.jq $$site[0].baseurl as $$baseurl | \ " \ $@) - @jq --raw-output --slurpfile site $(Metadata)/site.json '$(s_sitemap.jq)' < $< > $@ + @jq --raw-output --slurpfile site $(Metadata)/site.json '$(SITEMAP.jq)' < $< > $@ $(Destination)/sitemap.xml.gz: $(Destination)/sitemap.xml $(info ==> $@) diff --git a/docs/make.d/styles.make b/docs/make.d/styles.make index 9fe4928..02ed07d 100644 --- a/docs/make.d/styles.make +++ b/docs/make.d/styles.make @@ -9,7 +9,6 @@ # Styles # Exported rules for: # $(Destination)/styles.css -# all:: $(Destination)/styles.css # Styles $(Destination)/styles.css: \ diff --git a/docs/make.d/tools.make b/docs/make.d/tools.make index 4a67bc5..c95a960 100644 --- a/docs/make.d/tools.make +++ b/docs/make.d/tools.make @@ -1,7 +1,7 @@ ######################################################################## # tools.make # -# Tools independent of any target. +# Tools. # # Parameters: # vnudir @@ -18,6 +18,7 @@ .PHONY: help +# Warning: `make help` can build several phases. help: @echo 'Usage: make TARGET [parameter=value...]' @echo 'Targets:'; \ @@ -26,7 +27,8 @@ help: | awk '/^[^ \t.%][-A-Za-z0-9_]*:/ { print $$1 }' \ | sort --unique \ | sed 's/:\+$$//' \ - | pr --omit-pagination --indent=4 --width=80 --columns=4 + | pr --omit-pagination --indent=4 --width=80 --columns=4\ + || true ######################################################################## # HTML 5 validation