Skip to content

Commit 56f2412

Browse files
authored
Initial stab at building a tags index (#1)
* Initial stab at building a tags index * Revise with Kirby's feedback
1 parent 3af834c commit 56f2412

File tree

5 files changed

+485
-3
lines changed

5 files changed

+485
-3
lines changed

Makefile

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1+
PYTHON=python
12
GIT_BRANCH=`git rev-parse --abbrev-ref HEAD`
23

3-
.PHONY: all serve help
4+
.PHONY: build serve help
45

56
help: ## Show this help message
67
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-20s\033[0m %s\n", $$1, $$2}'
78

8-
all: ## Build the documentation under build/<git branch>
9+
build: ## Build the documentation under build/<git branch>
910
hugo -d build/$(GIT_BRANCH)
11+
$(PYTHON) -B tools/genindex.py --out build/$(GIT_BRANCH)/tags.json --config config.toml content/tutorials/
1012

1113
serve: ## Host the documentation on port 1313
1214
hugo serve

config.toml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,17 @@ baseurl = "http://example.org/"
22
languageCode = "en-us"
33
title = "MongoDB Tutorials"
44
theme = "mongodb"
5+
6+
[taxonomies]
7+
category = ""
8+
tags = ""
9+
10+
[tags]
11+
python = "language"
12+
ruby = "language"
13+
compass = "product"
14+
bi = "product"
15+
mongodb = "product"
16+
driver = "product"
17+
beginner = "level"
18+
crud = "topic"

content/index.mmark

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
+++
22
date = "2017-02-13T12:12:50-05:00"
33
title = "index"
4-
tags = []
54
+++
65

76
# MongoDB Tutorials

tools/genindex.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#!/usr/bin/env python
2+
import argparse
3+
import json
4+
import os
5+
import re
6+
import sys
7+
import toml
8+
import logging
9+
10+
logger = logging.getLogger('genindex')
11+
PAT_HEADMATTER = re.compile(r'^\+\+\+\n(.+)\n\+\+\+', re.DOTALL)
12+
13+
14+
def process_file(path):
15+
with open(path, 'r') as f:
16+
rawdata = f.read()
17+
18+
match = PAT_HEADMATTER.match(rawdata)
19+
if not match:
20+
raise ValueError('Couldn\'t find headmatter')
21+
22+
data = toml.loads(match.group(1))
23+
title = data['title']
24+
slug = data.get('slug', os.path.splitext(os.path.basename(path))[0])
25+
tags = data['tags']
26+
27+
return (slug, title, tags)
28+
29+
30+
def main(args):
31+
parser = argparse.ArgumentParser(description=__doc__)
32+
parser.add_argument('source', help='Source directory under which to find pages.')
33+
parser.add_argument('--out', metavar='PATH', help='Path in which to save the tag index.')
34+
parser.add_argument('--config', metavar='PATH', help='Path to the project configuration file.')
35+
args = parser.parse_args()
36+
37+
logging.basicConfig()
38+
data = []
39+
40+
with open(args.config, 'r') as f:
41+
tag_manifest = toml.load(f).get('tags', {})
42+
43+
error = False
44+
for root, _, files in os.walk(args.source):
45+
for filename in files:
46+
filename = os.path.join(root, filename)
47+
try:
48+
slug, title, tags = process_file(filename)
49+
except ValueError:
50+
logger.exception('Error processing %s', filename)
51+
continue
52+
53+
for tag in tags:
54+
if not tag in tag_manifest:
55+
logger.fatal('Unknown tag "%s" in %s', tag, filename)
56+
error = True
57+
58+
data.append((slug, title, tags))
59+
60+
if error:
61+
sys.exit(1)
62+
63+
with open(args.out, 'w') as f:
64+
json.dump({
65+
'tags': tag_manifest,
66+
'pages': data
67+
}, f)
68+
69+
if __name__ == '__main__':
70+
main(sys.argv)

0 commit comments

Comments
 (0)