From e5107ee40df3c7a8d2467bd7c746a8da6c8bdac1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20D=C3=B6ring?= Date: Fri, 16 Jan 2015 15:50:55 +0100 Subject: [PATCH] Improve site generator to use simple file metadata in capital words at the top of the MD file. Detect menu entry automatically based on path --- CONTRIBUTING.md | 4 +++- build/buildsite.py | 39 +++++++++++++++++++++++++++++---------- build/footer.inc | 2 +- build/header.inc | 24 ++++++++++++------------ guides/text/index.md | 9 +++------ 5 files changed, 48 insertions(+), 30 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 860313e..2d320fa 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -1,4 +1,6 @@ -# Guidelines for contributing +TITLE: Guidelines for contributing +DESCRIPTION: How to contribute to Darwin Core + Changes to the standard are subject to the [change policy](change_policy.html). Here's how you can contribute: diff --git a/build/buildsite.py b/build/buildsite.py index c3415e3..4b8053a 100755 --- a/build/buildsite.py +++ b/build/buildsite.py @@ -15,7 +15,10 @@ import datetime as dt BASE="http://rs.gbif.org" + ROOT = abspath(join(os.curdir, os.pardir)) +PARAMS_MD = re.compile("^([A-Z0-9_]+)\s*\:\s*(.*)$", re.MULTILINE) +MENU_PARAMS_HTML = re.compile("\$MENU[A-Z0-9_/]+", re.MULTILINE) def traverse(): print """building site starting at %s""" % ROOT @@ -38,19 +41,35 @@ def parse(level, fn, header, footer): mdFile = fn + ".md" htmlFile = fn + ".html" print """PARSE %s -> %s""" % (mdFile,htmlFile) - with codecs.open(mdFile, 'r', 'utf-8') as f: - html = mistune.markdown(f.read()) + (md, metadata)=parseMetadata(mdFile) + print metadata + body = mistune.markdown(md) + path=fn[len(ROOT):] with codecs.open(htmlFile, 'w', 'utf-8') as f: - f.write(buildInc(header, level)) - f.write(html) - f.write(buildInc(footer, level)) + f.write(processMetadata(header, level, path, metadata)) + f.write(processMetadata(body, level, path, metadata)) + f.write(processMetadata(footer, level, path, metadata)) -def buildInc(html, level): - return html.replace("$BASE", "../"*level)\ - .replace("$DATE", str(dt.date.today()))\ - .replace("$DESCRIPTION", "")\ - .replace("$TITLE", "my title") +def parseMetadata(mdFile): + metadata={} + with codecs.open(mdFile, 'r', 'utf-8') as f: + md = f.read().strip() + metadata["LAST_MODIFIED"]=str(dt.date.today()) + metadata["TITLE"]="" + metadata["DESCRIPTION"]="" + for m in PARAMS_MD.finditer(md): + metadata[m.group(1)]=m.group(2).strip() + md=PARAMS_MD.sub("", md) + return (md, metadata) +def processMetadata(html, level, path, metadata): + html=html.replace("$BASE", "../"*level) + # replace menu vars + html=html.replace("$MENU"+path.upper(), "active") + html=MENU_PARAMS_HTML.sub("", html) + for k, v in metadata.iteritems(): + html=html.replace("$"+k, v) + return html def anchorLinks(x): if x is None: diff --git a/build/footer.inc b/build/footer.inc index 0bcd943..a5eb053 100644 --- a/build/footer.inc +++ b/build/footer.inc @@ -10,7 +10,7 @@
diff --git a/build/header.inc b/build/header.inc index 1c48147..287e308 100644 --- a/build/header.inc +++ b/build/header.inc @@ -6,7 +6,7 @@ - + @@ -37,27 +37,27 @@ diff --git a/guides/text/index.md b/guides/text/index.md index 2305afe..8bc057a 100644 --- a/guides/text/index.md +++ b/guides/text/index.md @@ -1,10 +1,7 @@ -dcterms.title: Darwin Core text guide -dcterms.description: Guidelines for implementing Darwin Core in Text files. -dcterms.modified: 2015-01-14 +TITLE: Darwin Core text guide +DESCRIPTION: Guidelines for implementing Darwin Core in Text files. +LAST_MODIFIED: 2015-01-14 ---- - -# Darwin Core text guide ## 1. Introduction