|
15 | 15 | # |
16 | 16 | # You should have received a copy of the GNU General Public License along with |
17 | 17 | # Wikimedia Developer Portal. If not, see <http://www.gnu.org/licenses/>. |
| 18 | +import os |
| 19 | +from pathlib import Path |
| 20 | + |
| 21 | +import yaml |
| 22 | + |
18 | 23 |
|
19 | 24 | def define_env(env): |
20 | 25 | """Setup local variables, macros, and filters for mkdocs-macros-plugin.""" |
21 | 26 |
|
| 27 | + chatter = env.start_chatting('category') |
| 28 | + root_dir = Path(env.conf.config_file_path).parent |
| 29 | + data_dir = root_dir / "data" |
| 30 | + categories_dir = data_dir / "categories" |
| 31 | + documents_dir = data_dir / "documents" |
| 32 | + |
| 33 | + cat_tree = {} |
| 34 | + for cat in categories_dir.glob("**/*.yaml"): |
| 35 | + with cat.open() as f: |
| 36 | + for doc in yaml.safe_load_all(f): |
| 37 | + if "category" in doc: |
| 38 | + doc["documents"] = [] |
| 39 | + cat_tree[doc["category"]] = doc |
| 40 | + else: |
| 41 | + chatter("{} missing category".format(cat)) |
| 42 | + for document in documents_dir.glob("**/*.yaml"): |
| 43 | + with document.open() as f: |
| 44 | + for doc in yaml.safe_load_all(f): |
| 45 | + for category in doc.get("categories"): |
| 46 | + if category in cat_tree: |
| 47 | + cat_tree[category]["documents"].append(doc) |
| 48 | + else: |
| 49 | + chatter( |
| 50 | + "{}: unknown category '{}'".format(doc, category) |
| 51 | + ) |
| 52 | + env.variables.categories = cat_tree |
| 53 | + |
22 | 54 | @env.macro |
23 | | - def say_hello(s): |
24 | | - return "Hello {}".format(s) |
| 55 | + def category(name): |
| 56 | + """Output a category.""" |
| 57 | + return env.variables.categories.get(name, {}) |
0 commit comments