Alex Palaistras
76b963bc35
This includes a workable Makefile, able to generate readable ePub documents for all existing content.
97 lines
3.4 KiB
Makefile
97 lines
3.4 KiB
Makefile
# Makefile for Plato - The Stanford Encyclopedia of Philosophy, in ePub
|
|
# Use this to build a new version of the ePub document from remote sources.
|
|
|
|
# Document generation options.
|
|
NAME := Stanford Encyclopedia of Philosophy
|
|
BASE_URL := https://plato.stanford.edu
|
|
|
|
# Build-time dependencies.
|
|
AWK = $(call find-cmd,awk)
|
|
GREP = $(call find-cmd,grep)
|
|
PANDOC = $(call find-cmd,pandoc) $(if $(VERBOSE),--verbose)
|
|
WGET = $(call find-cmd,wget) $(if $(VERBOSE),,--quiet)
|
|
KEPUBIFY = $(call find-cmd,kepubify) $(if $(VERBOSE),--verbose)
|
|
|
|
# Directory aliases.
|
|
ROOTDIR := $(dir $(realpath $(firstword $(MAKEFILE_LIST))))
|
|
BUILDDIR := $(ROOTDIR)build/
|
|
TMPDIR := $(ROOTDIR)tmp/
|
|
|
|
# Default Makefile options.
|
|
VERBOSE :=
|
|
ENABLE_KOBO :=
|
|
|
|
## Build ePub documents via Pandoc.
|
|
build: $(addprefix $(BUILDDIR),$(addsuffix $(if $(ENABLE_KOBO),.kepub).epub,$(basename $(notdir $(wildcard $(TMPDIR)entries/*.md)))))
|
|
|
|
## Remove all temporary files.
|
|
clean:
|
|
$Q rm --recursive --force $(if $(VERBOSE),--verbose) $(TMPDIR)
|
|
|
|
## Remove all temporary and build files.
|
|
purge:
|
|
$Q rm --recursive --force $(if $(VERBOSE),--verbose) $(BUILDDIR) $(TMPDIR)
|
|
|
|
$(BUILDDIR)%.kepub.epub: $(BUILDDIR)%.epub
|
|
$Q $(KEPUBIFY) --inplace --smarten-punctuation --output $@ $<
|
|
|
|
$(BUILDDIR)%.epub: $(TMPDIR)entries/%.md
|
|
$Q install -d $(@D)
|
|
$Q cd $(TMPDIR)entries/$* && $(PANDOC) --from markdown --to epub --mathml $< > $@
|
|
|
|
$(TMPDIR)entries/%.md: $(TMPDIR)entries/%/index.html
|
|
$Q install -d $(@D)
|
|
$Q $(AWK) "$$EXTRACT_ARTICLE_META" $< > $@
|
|
$Q $(PANDOC) --from html+tex_math_single_backslash+tex_math_double_backslash --to markdown $< | $(AWK) "$$EXTRACT_ARTICLE" >> $@
|
|
|
|
$(TMPDIR)%.html:
|
|
$Q install -d $(@D)
|
|
$Q $(WGET) --no-clobber --page-requisites --accept html,png,jpg,jpeg,gif,svg --no-directories --directory-prefix $(@D) $(BASE_URL)/$*.html
|
|
|
|
$(TMPDIR)table-of-contents.md: $(TMPDIR)contents.html
|
|
$Q install -d $(@D)
|
|
$Q $(PANDOC) --from html --to markdown $< | $(AWK) "$$EXTRACT_TOC" > $@
|
|
|
|
.PHONY: build clean purge
|
|
|
|
$(TMPDIR)make.depend: $(TMPDIR)table-of-contents.md
|
|
@printf "build: $<" > $@
|
|
@for i in `$(GREP) -Po '\[.+?\]\(\Kentries/[^/]+' $<`; do printf " $(TMPDIR)$${i}.md"; done >> $@
|
|
|
|
# Conditional command echo control.
|
|
Q := $(if $(VERBOSE),,@)
|
|
|
|
# Find and return full path to command by name, or throw error if none can be found in PATH.
|
|
# Example use: $(call find-cmd,ls)
|
|
find-cmd = $(or $(firstword $(wildcard $(addsuffix /$(1),$(subst :, ,$(PATH))))),$(error "Command '$(1)' not found in PATH"))
|
|
|
|
# Awk script for extracting bare table-of-contents body from Markdown source.
|
|
define EXTRACT_TOC
|
|
$$0 ~ "::: {#toc-wrapper}" {s=1; next}
|
|
$$0 ~ "Projected Table of Contents" {s=0}
|
|
$$1 != ":::" && s == 1 {print}
|
|
endef
|
|
export EXTRACT_TOC
|
|
|
|
# Awk script for extracting article metadata from HTML source.
|
|
define EXTRACT_ARTICLE_META
|
|
BEGIN {FS="\""; print "---"}
|
|
$$2 == "citation_title" {printf "title: \"%s\"\n", $$4; next}
|
|
$$2 == "citation_author" {if (c==0) {print "creator:"; c=1}; printf "- role: author\n text: \"%s\"\n", $$4; next}
|
|
END {print "publisher: \"$(NAME)\""; print "belongs-to-collection: \"$(NAME)\""; print "---"}
|
|
endef
|
|
export EXTRACT_ARTICLE_META
|
|
|
|
# Awk script for extracting bare aricle body from Markdown source.
|
|
define EXTRACT_ARTICLE
|
|
$$0 ~ "::: {#article-content}" {s=1; next}
|
|
$$0 ~ "::: {#toc}" {s=0; next}
|
|
$$0 ~ "::: {#main-text}" {s=1; next}
|
|
$$0 ~ "::: {#academic-tools}" {s=0; next}
|
|
$$1 != ":::" && s == 1 {print}
|
|
endef
|
|
export EXTRACT_ARTICLE
|
|
|
|
# Dependency includes.
|
|
include $(TMPDIR)make.depend
|