|
| 1 | +################################################################################# |
| 2 | +# Makefile for building the lecture material (website and slides) |
| 3 | +################################################################################# |
| 4 | + |
| 5 | +## Run 'make' or 'make help' to display commonly used targets. "make slides" and |
| 6 | +## "make web" should be the most useful targets (also, targets for individual |
| 7 | +## files also exist but should only be used if you know what you are doing). |
| 8 | +## |
| 9 | +## (a) You can use the toolchain installed in the Docker image "pandoc-lecture", |
| 10 | +## which comes ready to use (no other dependencies). |
| 11 | +## (b) Alternatively, you need to |
| 12 | +## (1) install all tools (Pandoc, Hugo, TexLive) manually to your |
| 13 | +## operating system, and |
| 14 | +## (2) clone the pandoc-lecture repo plus relearn theme locally to a |
| 15 | +## specific location (${HOME}/.local/share/pandoc/): |
| 16 | +## "git clone --depth 1 https://github.com/cagix/pandoc-lecture.git ${HOME}/.local/share/pandoc/" |
| 17 | +## "wget https://github.com/McShelby/hugo-theme-relearn/archive/refs/tags/${RELEARN}.tar.gz" |
| 18 | +## "tar -zxf ${RELEARN}.tar.gz --strip-components 1 -C ${HOME}/.local/share/pandoc/hugo/hugo-theme-relearn/" |
| 19 | +## (Alternatively, just use "make install_scripts_locally" using https://github.com/cagix/pandoc-lecture/) |
| 20 | +## |
| 21 | +## To build the mentioned Docker image or for the required packages for a native |
| 22 | +## installation, see https://github.com/cagix/pandoc-lecture/docker. |
| 23 | +## |
| 24 | +## If you want to use the Docker image to build the lecture material, start the |
| 25 | +## container interactively using "make runlocal" and run the desired Make targets |
| 26 | +## in the interactive container shell. |
| 27 | + |
| 28 | +## NOTE: |
| 29 | +## The pdf slides that can be generated for certain chapters are named by taking |
| 30 | +## the relative path of the respective source file and replacing any '/' with an |
| 31 | +## '_' (e.g. A/B/C -> A_B_C). |
| 32 | +## This must be reversible in order to find the prerequisites for each output |
| 33 | +## file. Therefore, any subdirectory of the $(SRC_DIR) directory must NOT contain |
| 34 | +## any '_'. |
| 35 | + |
| 36 | +#-------------------------------------------------------------------------------- |
| 37 | +# Literature and other sources: Please adjust to your course! |
| 38 | +#-------------------------------------------------------------------------------- |
| 39 | + |
| 40 | +## Readings data template |
| 41 | +READINGS = data/readings.yaml |
| 42 | +BIBTEX = pm.bib |
| 43 | + |
| 44 | +## Metadata (for Pandoc) |
| 45 | +METADATA = metadata.yaml |
| 46 | + |
| 47 | +## Top level directory for source files |
| 48 | +SRC_DIR = . |
| 49 | + |
| 50 | +## local.yaml allows to override settings in hugo_conf.yaml |
| 51 | +HUGO_LOCAL = $(wildcard local.yaml) |
| 52 | + |
| 53 | +## Filename of "landing pages": usually "readme.md" |
| 54 | +INDEX_MD = readme |
| 55 | +ROOT_DOC = $(INDEX_MD).md |
| 56 | + |
| 57 | +#-------------------------------------------------------------------------------- |
| 58 | +# Tools |
| 59 | +#-------------------------------------------------------------------------------- |
| 60 | +## Define tools to process various types of source files. |
| 61 | +## |
| 62 | +## Note: LaTeX needs to be called in the folder of the .tex file to be processed. |
| 63 | +## In the rule that generates images from tex files, the variable "$<" is set to |
| 64 | +## the current .tex file (incl. path in the working directory). Therefore, the |
| 65 | +## working directory for the Docker container is set to the folder of the current |
| 66 | +## .tex file. When called directly, we need to first change-dir to this folder. |
| 67 | +PANDOC = pandoc |
| 68 | +HUGO = hugo |
| 69 | +DOT = dot |
| 70 | +LATEX = cd $(dir $(realpath $<)) && latex |
| 71 | + |
| 72 | +## Where do we find the content from https://github.com/cagix/pandoc-lecture, |
| 73 | +## i.e. the resources for Pandoc and the themes for Hugo? |
| 74 | +## (a) If we run inside the Docker container or inside the GitHub action, |
| 75 | +## we find the files in ${XDG_DATA_HOME}/pandoc/. |
| 76 | +## (b) If we are running locally (native installation), we look for the |
| 77 | +## files at ${HOME}/.local/share/pandoc/. |
| 78 | +## XDG_DATA_HOME can be set externally |
| 79 | +XDG_DATA_HOME ?= $(HOME)/.local/share |
| 80 | +PANDOC_DIRS = --resource-path=".:$(XDG_DATA_HOME)/pandoc/:$(XDG_DATA_HOME)/pandoc/resources/" |
| 81 | +HUGO_DIRS = --themesDir "$(XDG_DATA_HOME)/pandoc/hugo" |
| 82 | + |
| 83 | +## Define options for generating images from ".tex" files |
| 84 | +LATEX_ARGS = -shell-escape |
| 85 | + |
| 86 | +## Define options for generating images from ".dot" files |
| 87 | +DOT_ARGS = -Tpng |
| 88 | + |
| 89 | +## Define options to be used by Pandoc |
| 90 | +PANDOC_ARGS = --metadata-file=$(METADATA) $(PANDOC_DIRS) |
| 91 | + |
| 92 | +## Define options to be used by Hugo |
| 93 | +HUGO_ARGS = --config hugo_conf.yaml,$(HUGO_LOCAL) $(HUGO_DIRS) |
| 94 | + |
| 95 | +#-------------------------------------------------------------------------------- |
| 96 | +# I/O Directories |
| 97 | +#-------------------------------------------------------------------------------- |
| 98 | + |
| 99 | +## Top level directory for temporary files |
| 100 | +TEMP_DIR = temp |
| 101 | +HUGO_TEMP_DIR = resources |
| 102 | +HUGO_LOCK = .hugo_build.lock |
| 103 | +ROOT_DEPS = make.deps |
| 104 | + |
| 105 | +## Output directory generated by Hugo |
| 106 | +WEB_OUTPUT_DIR = docs |
| 107 | + |
| 108 | +## Output directory for generated slides |
| 109 | +SLIDES_OUTPUT_DIR = pdf |
| 110 | + |
| 111 | +#-------------------------------------------------------------------------------- |
| 112 | +# Helper lists |
| 113 | +#-------------------------------------------------------------------------------- |
| 114 | + |
| 115 | +## TeX source and target files |
| 116 | +## uncomment to convert .tex to .png |
| 117 | +TEX_SOURCES = $(shell find $(SRC_DIR) -type f -iname '*.tex') |
| 118 | +TEX_TARGETS = $(patsubst %.tex,%.png, $(TEX_SOURCES)) |
| 119 | + |
| 120 | +## Dot source and target files |
| 121 | +## uncomment to convert .dot to .png |
| 122 | +DOT_SOURCES = $(shell find $(SRC_DIR) -type f -iname '*.dot') |
| 123 | +DOT_TARGETS = $(patsubst %.dot,%.png, $(DOT_SOURCES)) |
| 124 | + |
| 125 | +## Source and target files for slides |
| 126 | +## NOTES: |
| 127 | +## (1) The name for the target pdf file is generated from the relative |
| 128 | +## path under $(SRC_DIR) with '/' substituted by '_'. |
| 129 | +## (2) Directories containing a '.noslides' file will not be considered for slides generation. |
| 130 | +SLIDES_EXCLUDE_DIRS = $(dir $(shell find $(SRC_DIR) -type f -iname '.noslides')) |
| 131 | +SLIDES_MARKDOWN_SOURCES = $(filter-out $(addsuffix %, $(SLIDES_EXCLUDE_DIRS)), $(shell find $(SRC_DIR) -type f -iname '*.md' ! -iname 'readme.md' ! -iname '*index.md')) |
| 132 | +SLIDES_PDF_TARGETS = $(addprefix $(SLIDES_OUTPUT_DIR)/,$(subst /,_, $(patsubst $(SRC_DIR)/%.md,%.pdf, $(SLIDES_MARKDOWN_SOURCES)))) |
| 133 | +## Convenience targets |
| 134 | +SLIDES_SHORT_TARGETS = $(patsubst $(SLIDES_OUTPUT_DIR)/%.pdf,%,$(SLIDES_PDF_TARGETS)) |
| 135 | + |
| 136 | +## Hugo: build a list of dependencies (markdown, images), fill WEB_MARKDOWN_TARGETS and WEB_IMAGE_TARGETS (and WEIGHT) |
| 137 | +WEB_MARKDOWN_TARGETS = |
| 138 | +WEIGHT = |
| 139 | +WEB_IMAGE_TARGETS = |
| 140 | + |
| 141 | +$(ROOT_DEPS): $(ROOT_DOC) |
| 142 | + @$(PANDOC) $(PANDOC_ARGS) -L hugo_makedeps.lua -M prefix=$(TEMP_DIR) -M indexMD=$(INDEX_MD) -f markdown -t markdown $< -o $@ |
| 143 | +-include $(ROOT_DEPS) |
| 144 | + |
| 145 | +#-------------------------------------------------------------------------------- |
| 146 | +# Secondary Expansion |
| 147 | +#-------------------------------------------------------------------------------- |
| 148 | + |
| 149 | +## Enable secondary expansion for subsequent targets. This allows the use |
| 150 | +## of automatic variables like '@' in the prerequisite definitions by |
| 151 | +## expanding twice (e.g. $$(VAR)). For normal variable references (e.g. |
| 152 | +## $(VAR)) the expansion behaviour is unchanged as the second expansion |
| 153 | +## has no effect on an already fully expanded reference. |
| 154 | + |
| 155 | +.SECONDEXPANSION: |
| 156 | + |
| 157 | +#-------------------------------------------------------------------------------- |
| 158 | +# Phony Targets |
| 159 | +#-------------------------------------------------------------------------------- |
| 160 | + |
| 161 | +.DEFAULT_GOAL:=help |
| 162 | + |
| 163 | +##@ Helpers |
| 164 | + |
| 165 | +## Display help |
| 166 | +.PHONY: help |
| 167 | +help: ## Display this help |
| 168 | + @awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m\033[0m\n"} /^[a-zA-Z_-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST) |
| 169 | + |
| 170 | +.PHONY: list_slides |
| 171 | +list_slides: ## List available targets for individual slides |
| 172 | + $(foreach target,$(SLIDES_SHORT_TARGETS), $(info $(target))) |
| 173 | + @: ## Suppress 'Nothing to be done for ...' message |
| 174 | + |
| 175 | +##@ Building |
| 176 | + |
| 177 | +## Start Docker container "pandoc-lecture" into interactive shell |
| 178 | +.PHONY: runlocal |
| 179 | +runlocal: ## Start Docker container "pandoc-lecture" into interactive shell |
| 180 | + docker run --rm -it -v "$(shell pwd):/pandoc" -w "/pandoc" -u "$(shell id -u):$(shell id -g)" --entrypoint "bash" pandoc-lecture |
| 181 | + |
| 182 | +## Make everything |
| 183 | +.PHONY: all |
| 184 | +all: $(ROOT_DEPS) slides web ## Make everything |
| 185 | + |
| 186 | +## Create all slides |
| 187 | +.PHONY: slides |
| 188 | +slides: $(SLIDES_PDF_TARGETS) ## Create all slides |
| 189 | + |
| 190 | +## Generate pdf slides (shortened target name for convenience) |
| 191 | +.PHONY: $(SLIDES_SHORT_TARGETS) |
| 192 | +$(SLIDES_SHORT_TARGETS): $$(patsubst %,$(SLIDES_OUTPUT_DIR)/%.pdf,$$@) |
| 193 | + |
| 194 | +## Create website |
| 195 | +.PHONY: web |
| 196 | +web: $$(WEB_IMAGE_TARGETS) $$(WEB_MARKDOWN_TARGETS) $(READINGS) $(HUGO_LOCAL) ## Create website |
| 197 | + $(HUGO) $(HUGO_ARGS) |
| 198 | + |
| 199 | +## Create website and archive |
| 200 | +.PHONY: web_zip |
| 201 | +web_zip: web ## Create website and archive |
| 202 | + cd $(WEB_OUTPUT_DIR) && rm -rf site.zip && zip -r site.zip * |
| 203 | + |
| 204 | +##@ Cleanup |
| 205 | + |
| 206 | +.PHONY: clean |
| 207 | +clean: ## Clean up intermediate files and directories |
| 208 | + rm -rf $(TEMP_DIR) $(HUGO_TEMP_DIR) $(HUGO_LOCK) $(READINGS) $(ROOT_DEPS) |
| 209 | + |
| 210 | +.PHONY: distclean |
| 211 | +distclean: clean ## Clean up all generated files and directories |
| 212 | + rm -rf $(SLIDES_OUTPUT_DIR) $(WEB_OUTPUT_DIR) |
| 213 | + |
| 214 | +#-------------------------------------------------------------------------------- |
| 215 | +# File Targets |
| 216 | +#-------------------------------------------------------------------------------- |
| 217 | + |
| 218 | +## Canned recipe for creating output folder |
| 219 | +define create-folder |
| 220 | +@mkdir -p $(dir $@) |
| 221 | +endef |
| 222 | + |
| 223 | +## Canned recipe for creating output folder and copy output file |
| 224 | +define create-dir-and-copy |
| 225 | +$(create-folder) |
| 226 | +cp $< $@ |
| 227 | +endef |
| 228 | + |
| 229 | +## Hugo: Create readings data template |
| 230 | +$(READINGS): $(BIBTEX) |
| 231 | + $(PANDOC) -s -f biblatex -t markdown $< -o $@ |
| 232 | + |
| 233 | +## Aux: Create images from tex files |
| 234 | +$(TEX_TARGETS): %.png: %.tex |
| 235 | + $(LATEX) $(LATEX_ARGS) $(notdir $<) |
| 236 | + |
| 237 | +## Aux: Create images from dot files |
| 238 | +$(DOT_TARGETS): %.png: %.dot |
| 239 | + $(DOT) $(DOT_ARGS) $< -o $@ |
| 240 | + |
| 241 | +## Hugo: Copy image files to $(TEMP_DIR) |
| 242 | +$(WEB_IMAGE_TARGETS): |
| 243 | + $(create-dir-and-copy) |
| 244 | + |
| 245 | +## Hugo: Process markdown with pandoc (preprocessing for hugo) |
| 246 | +$(WEB_MARKDOWN_TARGETS): |
| 247 | + $(create-folder) |
| 248 | + $(PANDOC) $(PANDOC_ARGS) -L hugo_rewritelinks.lua -s -d hugo -M weight=$(WEIGHT) -M indexMD=$(INDEX_MD) -M prefix=$(TEMP_DIR) $< -o $@ |
| 249 | +# $(PANDOC) $(PANDOC_ARGS) -d hugo -M weight=$(WEIGHT) -M indexMD=$(INDEX_MD) -M prefix=$(TEMP_DIR) $< -o $@ |
| 250 | + |
| 251 | +## Slides: Generate pdf slides |
| 252 | +## Folder structure and names: path/name.md, path/<images>/, path_name.pdf |
| 253 | +$(SLIDES_PDF_TARGETS): $$(patsubst $(SLIDES_OUTPUT_DIR)/%.pdf,$(SRC_DIR)/%.md, $$(subst _,/,$$@)) |
| 254 | + $(create-folder) |
| 255 | + $(PANDOC) $(PANDOC_ARGS) -d beamer $< -o $@ |
0 commit comments