Skip to content

Instantly share code, notes, and snippets.

@sirjofri sirjofri/Makefile
Last active Feb 23, 2019

Embed
What would you like to do?
static site generator-chan

static site generator-chan

This static site generator provides a Makefile for building websites with multiple languages and sites. New: markdown support, sitemap generation

How I Work

Most of the time I have 3 directories:

  • frags contains static content like header.inc, footer.inc, ...
  • sites contains the content files like index.de.inc, index.en.inc, ... (notice the language IDs) New: index.en.md
  • conf contains setting files (aka sed scripts) for changing data in the static content files (index.de.sed, index.en.sed, ...)

Getting Started

Use the shell script initialize.sh to create a basic web page with almost no content.

Examples

The language tag of header and footer is optional. Every (LANG) in the BEFORE and AFTER variable (config.mk) is replaced by the correct language tag.

frags/header.de.inc

<!doctype html>
<html lang="@lang@">
<head>
	<title>@page_title@</title>
	<meta charset="utf-8">
</head>
<body>
<p>Deutscher Anfang</p>

frags/header.en.inc

<!doctype html>
<html lang="@lang@">
<head>
	<title>@page_title@</title>
	<meta charset="utf-8">
</head>
<body>
<p>English Beginning</p>

frags/footer.inc

</body>
</html>

sites/index.de.inc

<h1>Deutscher Content</h1>

sites/index.en.md

English Content
===============

conf/de.sed

s/@lang@/de/g

conf/en.sed

s/@lang@/en/g

conf/index.de.sed

s/@page_title@/Deutscher Titel/g

conf/index.en.sed

s/@page_title@/English Title/g

Markdown Support

With this version the Makefile provides markdown support. Just use filenames like index.en.md instead of index.en.inc inside your sites directory and use make.

You want to use another markdown parser? No problem: Adjust the config.mk.

.htaccess Recommendation

RewriteEngine On

RewriteRule ^initialize.sh$ - [NC,F]
RewriteRule ^config.mk$ - [NC,F]
RewriteRule ^Makefile$ - [NC,F]
RewriteRule ^Readme.md$ - [NC,F]
RewriteRule ^*.inc$ - [NC,F]
RewriteRule ^*.md$ - [NC,F]
# Where are your sites stored?
SITESPATH=sites
# Where are your site configurations?
CONFPATH=conf
# What should be included before and after the site? (Order! Use (LANG) for the language code)
BEFORE="frags/header.(LANG).inc"
AFTER="frags/footer.(LANG).inc"
# What markdown parser do you use? (default: markdown)
MARKDOWN=markdown
# What is your URL? This is used for sitemap URLs (with ending /)
URL=https://example.com/
# What command(s) should be run before anything else?
PRECMD=
#!/bin/sh
source ./config.mk
mkdir -p frags
mkdir -p $SITESPATH
mkdir -p $CONFPATH
echo " ✓ Created directories"
cat <<EOF > frags/header.de.inc
<!doctype html>
<html lang="@lang@">
<head>
<title>@page_title@</title>
<meta charset="utf-8">
</head>
<body>
<p>Deutscher Anfang</p>
EOF
cat <<EOF > frags/header.en.inc
<!doctype html>
<html lang="@lang@">
<head>
<title>@page_title@</title>
<meta charset="utf-8">
</head>
<body>
<p>English Beginning</p>
EOF
cat <<EOF > frags/footer.inc
</body>
</html>
EOF
cat <<EOF > sites/index.de.inc
<h1>Deutscher Content</h1>
EOF
cat <<EOF > sites/index.en.md
English Content
===============
EOF
cat <<EOF > conf/de.sed
s/@lang@/de/g
EOF
cat <<EOF > conf/en.sed
s/@lang@/en/g
EOF
cat <<EOF > conf/index.de.sed
s/@page_title@/Deutscher Titel/g
EOF
cat <<EOF > conf/index.en.sed
s/@page_title@/English Title/g
EOF
echo " ✓ Created files"
include config.mk
SITES=$(sort $(notdir $(basename $(wildcard $(SITESPATH)/*.*.inc))))
MKDFILES=$(sort $(basename $(wildcard $(SITESPATH)/*.*.md)))
LANG=$(subst .,,$(suffix $(SITES)))
all:
@$(PRECMD)
@make -s markdown
@make -s html
@make -s sitemap.xml
html: $(join $(addsuffix /,$(LANG)),$(addsuffix .html,$(basename $(SITES))))
define TEMPLATE =
$(1): $(subst (LANG),$(subst /,,$(dir $(1))),$(BEFORE)) $(SITESPATH)/$(basename $(notdir $(1))).$(subst /,,$(dir $(1))).inc $(CONFPATH)/$(basename $(notdir $(1))).$(subst /,,$(dir $(1))).sed $(CONFPATH)/$(subst /,,$(dir $(1))).sed $(subst (LANG),$(subst /,,$(dir $(1))),$(AFTER))
@mkdir -p $$(dir $$@)
@cat $$(filter-out %.sed,$$^) $$(foreach sedfile,$$(filter %.sed,$$^),| sed -f $$(sedfile)) > $$@
@echo " ✓ Created $$@"
endef
$(foreach site,$(join $(addsuffix /,$(LANG)),$(addsuffix .html,$(basename $(SITES)))),$(eval $(call TEMPLATE,$(site))))
markdown: $(addsuffix .inc,$(MKDFILES))
define INC_TEMPLATE =
$(1): $(addsuffix .md,$(basename $(1)))
@$(MARKDOWN) $$< > $$@
@echo " ✓ Created $$@"
endef
$(foreach mkdsite,$(addsuffix .inc,$(MKDFILES)),$(eval $(call INC_TEMPLATE,$(mkdsite))))
sitemap.xml:
@echo -e "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd\">" > $@
@{ $(foreach lang,$(sort $(LANG)),ls -l --time-style=+%Y-%m-%d $(lang)/*.html;) } | \
awk '/^-rw/ { printf "<url><loc>$(URL)%s</loc><lastmod>%s</lastmod></url>\n", $$7, $$6; }' | sed -e 's/.html//g' | sed -e 's/index//g' >> $@
@echo "</urlset>" >> $@
@echo " ✓ Created $@"
cleanbuild:
@rm -Rf $(sort $(LANG)) sitemap.xml
@make -Bs
.PHONY: all html markdown cleanbuild
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.