Skip to content

Instantly share code, notes, and snippets.

@noonworks
Last active January 10, 2017 11:07
Show Gist options
  • Save noonworks/12bd07181d7923b968964c9f3f39565b to your computer and use it in GitHub Desktop.
Save noonworks/12bd07181d7923b968964c9f3f39565b to your computer and use it in GitHub Desktop.
/_drafts/%.mdを、/%.htmlに変換する。テンプレートファイル_template._templateは、変換するmdごとに、同じフォルダにあればそれを、なければその上のフォルダを探してそれを、そこにもなければさらに上を……と探索する。
####################
# markdown to html
# (For Windows)
####################
DRAFTS_DIR_NAME := _drafts
SITE_ROOT_DIR := $(shell cd)
# template file
TEMPLATE_EXT := _template
TEMPLATE_FILE := _template.$(TEMPLATE_EXT)
# markdown source files
SOURCE_MD := $(subst $(SITE_ROOT_DIR)\,,$(shell dir /A-D /B /S $(DRAFTS_DIR_NAME)\*.md))
# target html files
DEST_HTML := $(subst $(DRAFTS_DIR_NAME)\,,$(SOURCE_MD:.md=.html))
# vpath for markdown source files
vpath %.md $(DRAFTS_DIR_NAME)
####################
# functions
####################
# Get parent folder
Parent = $(subst $(SITE_ROOT_DIR)\,,$(subst /,\,$(dir $(abspath $(subst \,/,$(1))))))
# Return file path if it exsists, return empty if not
GetExistFile = $(if $(shell dir /A-D /B $(1) 2>NUL),$(1),)
# Return directory path if it exsists, return empty if not
GetExistFolder = $(if $(shell dir /AD $(1) 2>NUL),$(1),)
# Make template file path from base path
MakeTempPath = $(DRAFTS_DIR_NAME)\$(call Parent,$(1))$(TEMPLATE_FILE)
# Find template path from html file path
FindTemplate = $(if $(1),$(if $(call GetExistFile,$(call MakeTempPath,$(1))),$(call MakeTempPath,$(1)),$(call FindTemplate,$(call Parent,$(1)))),)
# Filter markdown source file from prerequisite list
PickMD = $(subst /,\,$(firstword $(filter %.md,$(1))))
# Filter template file from prerequisite list
PickTMP = $(subst /,\,$(firstword $(filter %.$(TEMPLATE_EXT),$(1))))
####################
# rules
####################
.PHONY: all
all: ${DEST_HTML}
.SECONDEXPANSION:
%.html: %.md $$(call FindTemplate,$$@)
@echo Build $@
-@mkdir $(call Parent,$@)
@nkf -w8 $(call PickMD,$^) | pandoc --template $(call PickTMP,$^) -f markdown+east_asian_line_breaks -o $@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment