Skip to content

Commit

Permalink
Migrated generation to jq
Browse files Browse the repository at this point in the history
  • Loading branch information
fadado committed Apr 16, 2017
1 parent d75b7b2 commit 211de32
Show file tree
Hide file tree
Showing 12 changed files with 152 additions and 140 deletions.
4 changes: 2 additions & 2 deletions docs/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 7 additions & 7 deletions docs/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions docs/make.d/config.make
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
107 changes: 23 additions & 84 deletions docs/make.d/front-matter.make
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 ==> $@)
Expand All @@ -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' \
Expand All @@ -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
7 changes: 6 additions & 1 deletion docs/make.d/pathnames.make
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,19 @@
#
# Build metadata from filesystem introspection.
#
# Imported variables:
# Content
# Destination
# Metadata
#
# Exported rules for:
# $(Metadata)/phase2.make

########################################################################
# 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 \
Expand Down
14 changes: 7 additions & 7 deletions docs/make.d/phase0.jq
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
36 changes: 21 additions & 15 deletions docs/make.d/phase2.jq
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
69 changes: 69 additions & 0 deletions docs/make.d/phase21.jq
Original file line number Diff line number Diff line change
@@ -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
26 changes: 10 additions & 16 deletions docs/make.d/prelude.make
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Loading

0 comments on commit 211de32

Please sign in to comment.