From 628ff5a93b8b6e19d3bd3b47e819eba92bad3c29 Mon Sep 17 00:00:00 2001 From: kez Date: Mon, 5 Dec 2022 13:35:55 -0800 Subject: [PATCH] Move file generator templates to their own file --- .gitignore | 3 ++ models/sortby_resources.ini | 20 ------- .../__init__.py} | 51 +++++------------- .../file_templates.py | 54 +++++++++++++++++++ packages/community-generator/setup.py | 2 +- 5 files changed, 70 insertions(+), 60 deletions(-) delete mode 100644 models/sortby_resources.ini rename packages/community-generator/{lektor_community_generator.py => lektor_community_generator/__init__.py} (92%) create mode 100644 packages/community-generator/lektor_community_generator/file_templates.py diff --git a/.gitignore b/.gitignore index 6f71a7ea..64e53d8a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ +# this file is auto-generated on each build +models/sortby_resources.ini + assets/static/css node_modules diff --git a/models/sortby_resources.ini b/models/sortby_resources.ini deleted file mode 100644 index b6dadebc..00000000 --- a/models/sortby_resources.ini +++ /dev/null @@ -1,20 +0,0 @@ -[model] -name = Community Resources -label = {{ this.title }} -inherits = page - -[fields.sortby_resources] -label = Sortby Resources -type = text - -[fields.current_topic] -label = Current topic filter -type = text - -[fields.current_lang] -label = Current language filter -type = text - -[fields.current_author] -label = Current author filter -type = text diff --git a/packages/community-generator/lektor_community_generator.py b/packages/community-generator/lektor_community_generator/__init__.py similarity index 92% rename from packages/community-generator/lektor_community_generator.py rename to packages/community-generator/lektor_community_generator/__init__.py index a160fc53..80b5d379 100644 --- a/packages/community-generator/lektor_community_generator.py +++ b/packages/community-generator/lektor_community_generator/__init__.py @@ -7,6 +7,8 @@ from urllib.parse import quote_plus from lektor.pluginsystem import Plugin +from .file_templates import contents_lr_tmpl, sortby_resources_ini + # lektor's slugify removes characters that *should* be url-safe, so let's make our own def slugify(it: str): @@ -32,38 +34,6 @@ class CommunityGeneratorPlugin(Plugin): name = 'community-generator' description = u'Generate content files for community training resource page' - contents_lr_tmpl = ''' -section: {section} ---- -_model: sortby_resources ---- -section_id: {section_id} ---- -color: {color} ---- -_template: {template} ---- -title: {title} ---- -subtitle: {subtitle} ---- -cta: {cta} ---- -html: {html} ---- -sortby_resources: {sortby_resources} ---- -current_topic: {current_topic} ---- -current_lang: {current_lang} ---- -current_author: {current_author} ---- -body: - -{body} -''' - def _generate_file_helper(self, url_path: str, content: str) -> None: """Generate a contents.lr file from a URL path and content string. @@ -147,6 +117,9 @@ body: def generate_files(self): """Generate contents.lr files for the sortby fields: topics, languages, author.""" + with open(os.path.join(self.env.project.tree, 'models', 'sortby_resources.ini'), 'w') as f: + f.write(sortby_resources_ini) + with open(os.path.join(self.env.project.tree, 'databags', 'community-training-materials.json'), 'r') as f: self.resources = json.load(f) @@ -155,7 +128,7 @@ body: authors = self._get_authors() # lektor won't render a page if it doesn't have a parent contents.lr - self._generate_file_helper(f'training/resources/sortby', self._format_default(self.contents_lr_tmpl)) + self._generate_file_helper(f'training/resources/sortby', self._format_default(contents_lr_tmpl)) # loop through topics for topic in topics: @@ -164,12 +137,12 @@ body: if topic != 'none': topic_resources = dict(filter(lambda resource: topic in resource[1].get('topics', []), topic_resources.items())) - self._generate_file_helper(f'training/resources/sortby/{slugify(topic)}', self._format_default(self.contents_lr_tmpl)) - self._generate_file_helper(f'training/resources/sortby/{slugify(topic)}/none', self._format_default(self.contents_lr_tmpl)) + self._generate_file_helper(f'training/resources/sortby/{slugify(topic)}', self._format_default(contents_lr_tmpl)) + self._generate_file_helper(f'training/resources/sortby/{slugify(topic)}/none', self._format_default(contents_lr_tmpl)) self._generate_file_helper( f'training/resources/sortby/{slugify(topic)}/none/none', self._format_default( - self.contents_lr_tmpl, + contents_lr_tmpl, section='Training', color='primary', template='layout.html', @@ -189,11 +162,11 @@ Or, if you want to teach your community about Tor, these training materials are if language_code != 'none': language_resources = dict(filter(lambda resource: language_code in resource[1]['languages'], language_resources.items())) - self._generate_file_helper(f'training/resources/sortby/{slugify(topic)}/{language_code}', self._format_default(self.contents_lr_tmpl)) + self._generate_file_helper(f'training/resources/sortby/{slugify(topic)}/{language_code}', self._format_default(contents_lr_tmpl)) self._generate_file_helper( f'training/resources/sortby/{slugify(topic)}/{language_code}/none', self._format_default( - self.contents_lr_tmpl, + contents_lr_tmpl, section='Training', color='primary', template='layout.html', @@ -217,7 +190,7 @@ Or, if you want to teach your community about Tor, these training materials are self._generate_file_helper( f'training/resources/sortby/{slugify(topic)}/{language_code}/{slugify(author_name)}', self._format_default( - self.contents_lr_tmpl, + contents_lr_tmpl, section='Training', color='primary', template='layout.html', diff --git a/packages/community-generator/lektor_community_generator/file_templates.py b/packages/community-generator/lektor_community_generator/file_templates.py new file mode 100644 index 00000000..d618bdc5 --- /dev/null +++ b/packages/community-generator/lektor_community_generator/file_templates.py @@ -0,0 +1,54 @@ +sortby_resources_ini = \ +'''[model] +name = Community Resources +label = {{ this.title }} +inherits = page + +[fields.sortby_resources] +label = Sortby Resources +type = text + +[fields.current_topic] +label = Current topic filter +type = text + +[fields.current_lang] +label = Current language filter +type = text + +[fields.current_author] +label = Current author filter +type = text +''' + +contents_lr_tmpl = \ +'''section: {section} +--- +_model: sortby_resources +--- +section_id: {section_id} +--- +color: {color} +--- +_template: {template} +--- +title: {title} +--- +subtitle: {subtitle} +--- +cta: {cta} +--- +html: {html} +--- +sortby_resources: {sortby_resources} +--- +current_topic: {current_topic} +--- +current_lang: {current_lang} +--- +current_author: {current_author} +--- +body: + +{body} +''' diff --git a/packages/community-generator/setup.py b/packages/community-generator/setup.py index a73f32bb..98517edb 100644 --- a/packages/community-generator/setup.py +++ b/packages/community-generator/setup.py @@ -9,7 +9,7 @@ with io.open('README.md', 'rt', encoding="utf8") as f: _description_re = re.compile(r'description\s+=\s+(?P.*)') -with open('lektor_community_generator.py', 'rb') as f: +with open('lektor_community_generator/__init__.py', 'rb') as f: description = str(ast.literal_eval(_description_re.search( f.read().decode('utf-8')).group(1)))