Skip to content

Commit

Permalink
Implementing external jq
Browse files Browse the repository at this point in the history
  • Loading branch information
fadado committed Apr 16, 2017
1 parent 4d1d532 commit d75b7b2
Show file tree
Hide file tree
Showing 9 changed files with 234 additions and 294 deletions.
136 changes: 19 additions & 117 deletions docs/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ endif
endif

# ======================================================================
-include $(Metadata)/phase_1.make
-include $(Metadata)/phase1.make
#
# Load site global parameters. If `phase_1.make` not exists it is built when
# Load site global parameters. If `phase1.make` not exists it is built when
# restarting with a rule defined in `config.make`.
#
# Exported variables:
Expand All @@ -39,163 +39,65 @@ endif
# Styles
# ======================================================================

# ======================================================================
include make.d/config.make
#
# Build files derived from user defined configuration:
# config.{yaml,json} => config.json => site.json => phase_1.make
#
# Imported variables:
# Metadata
# Version
# Exported rules for:
# $(Metadata)
# $(Metadata)/config.json
# $(Metadata)/site.json
# $(Metadata)/phase_1.make
# ======================================================================

# Warning:
#
# If `__phase_1` is not defined because `phase_1.make` does not exists, after
# this point most of the file is ignored, but `phase_1.make` is built because a
# If `__phase_1` is not defined because `phase1.make` does not exists, after
# this point most of the file is ignored, but `phase1.make` is built because a
# rule exists in the file `config.make`. Then this `Makefile` is restarted,
# `MAKE_RESTARTS` will be equal to 1, `phase_1.make` is now loaded and
# `__phase_1` is defined.
# `MAKE_RESTARTS` is be equal to 1, `phase1.make` is now loaded and
# `__phase_1` is defined. Equivalent situation happens in all phases.

ifdef __phase_1 # not defined in a fresh start
ifdef __phase_1

# ======================================================================
-include $(Metadata)/phase_2.make
-include $(Metadata)/phase2.make
#
# Build metadata from filesystem introspection.
# Load variables for several pathnames.
#
# Exported variables:
# __phase_2
# DestinationPages
# DestinationPaths
# HomePage
# MetadataPages
# MetadataPaths
# Exported dependencies:
# $(Metadata)/pages/.../page.json: | $$(dir $$@)
# ======================================================================

# ======================================================================
include make.d/pathnames.make
#
# ???????'
#
# Imported variables:
# Content
# Destination
# Metadata
# Exported rules for:
# $(Destination)
# $(Destination)/.../
# $(Metadata)/.../
# all:: $(DestinationPages)
# Additional dependencies defined:
# $(Destination)/.../page.html => $(Metadata)/pages/.../page.json
# ======================================================================

#ifdef __phase_2
ifdef __phase_2

# ======================================================================
include make.d/front-matter.make
#
# Collect metadata from front-matter sections.
#
# Imported variables:
# Content
# Metadata
# MetadataPages
# Exported rules for:
# $(Metadata)/pages/.../page.json
# $(Metadata)/nodes/.../section.json
# $(Metadata)/pages.json
# $(Metadata)/nodes.json
# $(Metadata)/sections.json
# ======================================================================

# ======================================================================
-include $(Metadata)/phase_3.make
-include $(Metadata)/phase3.make
#
# Load rules for pages and nodes. If not exists is built when restarting with
# rule defined in `pages.make`.
#
# Exported variables:
# __phase_3
# Exported rules for:
# rules for all HTML files
# dependencies and rules for all HTML files
# ======================================================================

# ======================================================================
include make.d/pages.make
#
# Define standard rules and rules for HTML pages and nodes.
#
# Imported variables:
# Assets
# Destination
# Layouts
# Metadata
# JQTFLAGS
# Exported rules for:
# $(Metadata)/phase_3.make
# all:: # assets
# build
# clean::
# clobber::
# xbuild
# xxbuild
# ======================================================================

# ======================================================================
include make.d/sitemap.make
#
# Define rules for sitemap.
#
# Imported variables:
# Destination
# Metadata
# Exported rules for:
# $(Destination)/sitemap.*
# all:: $(Destination)/sitemap.*
# ======================================================================
ifdef __phase_3

# ======================================================================
include make.d/sitemap.make
include make.d/styles.make
#
# Build CSS main stylesheet.
#
# Imported variables:
# Destination
# Blocks
# Styles
# Exported rules for:
# $(Destination)/styles.css
# all:: $(Destination)/styles.css
# ======================================================================

# ======================================================================
include make.d/tools.make
#
# Tools independent of any target.
#
# Parameters:
# vnudir
# Imported variables:
# Destination
# Exported rules for:
# help
# valid
# lint
# ======================================================================

########################################################################
# Specific rules for this web site
########################################################################

ifdef __phase_3
# Call jqt with user defined flags
JQT = jqt $(JQTFLAGS)

# Options for `jqt`.
JQTFLAGS += \
Expand Down Expand Up @@ -270,7 +172,7 @@ clean::
@rm -f /tmp/help

endif # __phase_3
#endif # __phase_2
endif # __phase_2
endif # __phase_1

# vim:ai:sw=8:ts=8:noet:fileencoding=utf8:syntax=make
48 changes: 11 additions & 37 deletions docs/make.d/config.make
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
# Version
# Exported rules for:
# $(Metadata)
# $(Metadata)/
# $(Metadata)/config.json
# $(Metadata)/site.json
# $(Metadata)/phase_1.make
Expand Down Expand Up @@ -54,47 +55,20 @@ endif
#
# Create `$(Metadata)/site.json` from `$(Metadata)/config.json`.
#

# Add some new members to `$(Metadata)/config.json` and delete `.defaults`.
define m_SITE_JSON.jq :=
del(.defaults) \
| . + { \
Destination: (.Destination // "./_site"), \
Assets: (.Assets // "./assets"), \
Blocks: (.Blocks // "./blocks"), \
Content: (.Content // "./content"), \
Data: (.Data // "./data"), \
Layouts: (.Layouts // "./layouts"), \
Styles: (.Styles // "./styles"), \
Version: (.Version // "$(Version)") \
}
endef

# Global configuration file with some added members.
$(Metadata)/site.json: $(Metadata)/config.json
$(Metadata)/site.json: $(Metadata)/config.json make.d/config.make
$(info ==> $@)
@jq --sort-keys '$(m_SITE_JSON.jq)' < $< > $@
@jq --sort-keys \
--from-file make.d/phase0.jq \
--arg Version $(Version) \
< $< > $@

#
# Create `$(Metadata)/phase_1.make` from `$(Metadata)/site.json`.
# Create `$(Metadata)/phase1.make` from `$(Metadata)/site.json`.
#

# Format members as make variables.
define PHASE_1.jq :=
"__phase_1 := 1", \
"Assets := " + .Assets, \
"Blocks := " + .Blocks, \
"Content := " + .Content, \
"Data := " + .Data, \
"Destination := " + .Destination, \
"Layouts := " + .Layouts, \
"Styles := " + .Styles, \
"# vim:syntax=make"
endef

# Makefile to be included in `Makefile`.
$(Metadata)/phase_1.make: $(Metadata)/site.json make.d/config.make
$(Metadata)/phase1.make: $(Metadata)/site.json make.d/config.make make.d/phase1.jq
$(info ==> $@)
@jq --raw-output '$(PHASE_1.jq)' < $< > $@
@jq --raw-output \
--from-file make.d/phase1.jq \
< $< > $@

# vim:ai:sw=8:ts=8:noet:fileencoding=utf8:syntax=make
74 changes: 51 additions & 23 deletions docs/make.d/front-matter.make
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,57 @@
# Collect metadata from front-matter sections.
#
# Imported variables:
# Content
# Metadata
# DestinationPages
# Exported rules for:
# $(Metadata)/pages/.../page.json
# $(Metadata)/nodes/.../section.json
# $(Metadata)/pages.json
# $(Metadata)/nodes.json
# $(Metadata)/sections.json
# ??

########################################################################
# Collect metadata for pages
# Extra dependencies
########################################################################

# Add prerequisites to default goal
all:: $(DestinationPages)

$(DestinationPages): $(Destination)/%.html : $(Metadata)/pages/%.json
$(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 ==> $@)
@mkdir --parents $@ >/dev/null 2>&1 || true

# Directories starting at `$(Metadata)/pages/`.
$(MetadataPaths):
$(info ==> $@)
@mkdir --parents $@ >/dev/null 2>&1 || true

$(DestinationPages): | $$(dir $$@)
$(MetadataPages): | $$(dir $$@)

########################################################################
# Group metadata
########################################################################

$(Metadata)/pages.json: $(MetadataPages)
$(info ==> $@)
@jq --slurp '.' $^ > $@

$(Metadata)/sections.json: $(Metadata)/pages.json
$(info ==> $@)
@jq '[.[].section] | unique | map(select(.))' < $< > $@

########################################################################
# TODO: phase21.jq !!!!!!!!!!!
########################################################################

# `$(Metadata)/pages/path/to/page.json` => `path/to/page`
Expand Down Expand Up @@ -85,7 +125,7 @@ define f_PAGE_JSON.jq =
endef

# Extract YAML front matter.
define f_extract_front_matter =
define EXTRACT_FRONT_MATTER =
sed -n -e '1d' \
-e '/^---$$/q' \
-e '/^\.\.\.$$/q' \
Expand All @@ -95,24 +135,12 @@ endef

# Build metadata for pages.
$(MetadataPages): $(Metadata)/config.json
$(MetadataPages): $(Metadata)/pages/%.json : $(Content)/%.md
$(MetadataPages): $(Metadata)/pages/%.json : $(Content)/%.md # TODO: any accepted extension
$(info ==> $@)
@$(f_extract_front_matter) < $< \
@$(EXTRACT_FRONT_MATTER) < $< \
| yaml2json \
| jq --sort-keys \
--slurpfile config $(Metadata)/config.json \
'$(call f_PAGE_JSON.jq,$<,$@)' > $@

########################################################################
# Group metadata
########################################################################

$(Metadata)/pages.json: $(MetadataPages)
$(info ==> $@)
@jq --slurp '.' $^ > $@

$(Metadata)/sections.json: $(Metadata)/pages.json
$(info ==> $@)
@jq '[.[].section] | unique | map(select(.))' < $< > $@

# vim:ai:sw=8:ts=8:noet:fileencoding=utf8:syntax=make
Loading

0 comments on commit d75b7b2

Please sign in to comment.