commit 76b963bc358c0e32a63a8b8cae743ed48186139a Author: Alex Palaistras Date: Sat Mar 25 15:15:00 2023 +0000 Initial commit of entire workspace This includes a workable Makefile, able to generate readable ePub documents for all existing content. diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a7a3288 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +# Ignore output directories. +build/ +tmp/ diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..0a7fadc --- /dev/null +++ b/LICENSE @@ -0,0 +1,19 @@ +Copyright (c) 2023 Alex Palaistras + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..66ae652 --- /dev/null +++ b/Makefile @@ -0,0 +1,96 @@ +# 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 diff --git a/README.md b/README.md new file mode 100644 index 0000000..dfd5b0c --- /dev/null +++ b/README.md @@ -0,0 +1,12 @@ +# The Stanford Encyclopedia of Philosophy in ePub + +Generate ePub documents for entries in the Stanford Encyclopedia of Philosophy. + +## Building + +Simply run `make` to build all articles in the SEP as separate ePub documents. Pass the +`ENABLE_KOBO=1` option for building Kobo ePub files instead (denoted by the `.kepub.epub` extension). + +## License + +All code in this repository is covered by the terms of the MIT License, the full text of which can be found in the LICENSE file.