From e906aaef99d896d833b9740ada49b29f50e70776 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Thu, 7 Apr 2016 21:47:53 +0100 Subject: [PATCH] Load project details into default context --- project/common/data.py | 32 +++++++++++++++++++++++++++++++- project/settings.py | 2 +- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/project/common/data.py b/project/common/data.py index 1c395fb..596510d 100644 --- a/project/common/data.py +++ b/project/common/data.py @@ -1,8 +1,38 @@ import os.path import yaml +from glob import glob +from project.pages.utils import get_title_from_markdown, parse_content def generate_config(base_dir): default = yaml.load(open(os.path.join(base_dir, 'data/context.yml'))) or {} page = yaml.load(open(os.path.join(base_dir, 'data/page_context.yml'))) or {} - return default, page + switcher = yaml.load(open(os.path.join(base_dir, 'data/path_switch.yml'))) or {} + + default['projects'] = generate_projects(base_dir) + for i in range(len(default['projects'])): + project = default['projects'][i] + if project['path'] in page: # If there's a custom config + default['projects'][i] = dict(project, **page[project['path']]) + default['projects'][i]['url'] = '/' + project['path'] + return default, page, switcher + + +def generate_projects(base_dir): + projects_path = os.path.join(base_dir, 'templates/projects') + files = [] + for path in glob(projects_path + '/*.*'): + filename = path.replace(projects_path, '') + if filename == '/all.html': + continue + with open(path) as f: + if filename.split('.')[1] == 'md': + parsed_content = parse_content(f.read(), filename.split('.')[1]) + filename = get_title_from_markdown(parsed_content) + else: + filename = filename.split('.')[0] + files.append({ + "name": filename, + "path": 'projects' + path.replace(projects_path, '').split('.')[0] + }) + return files diff --git a/project/settings.py b/project/settings.py index 717f2c6..31cfb79 100644 --- a/project/settings.py +++ b/project/settings.py @@ -99,4 +99,4 @@ WORDPRESS_URL = "realorangeone.wordpress.com" # Generate config data from project.common.data import generate_config -DEFAULT_CONTEXT, PAGE_CONTEXT = generate_config(BASE_DIR) +DEFAULT_CONTEXT, PAGE_CONTEXT, PAGE_SWITCH = generate_config(BASE_DIR)