def test_jinja_environment_custom_options(self): """jinja_environment properly applies custom jinja options""" self.settings['jinja']['trim_blocks'] = True env = jinja_environment( user_templates=self.settings['paths']['templates root'], settings=self.settings) self.assertTrue(env.trim_blocks)
def test_jinja_environment_defaults(self): """jinja_environment results contains expected default options In particular: environment.auto_reload should be False environment.globals should contain 'settings' """ env = jinja_environment( user_templates=self.settings['paths']['templates root'], settings=self.settings) self.assertFalse(env.auto_reload) self.assertTrue('settings' in env.globals)
def test_jinja_environment_default_templates(self): """jinja_environment includes default templates in search path majestic supplies some default templates that the user is not expected to create. These are stored in the majestic directory and the folder containing them should be included in the search path passed to the jinja loader. """ env = jinja_environment( user_templates=self.settings['paths']['templates root'], settings=self.settings) self.assertIn(str(MAJESTIC_DIR.joinpath('default_templates')), env.loader.searchpath)
def process_blog(*, settings, write_only_new=True, posts=True, pages=True, index=True, archives=True, feeds=True, sitemap=True, extensions=True): """Create output files from the blog's source By default, create the entire blog. Certain parts can be disabled by setting their corresponding parameter to False. By default, only Pages and Posts that are considered new (by checking content.is_new) are written out. This can be overridden by passing False to write_only_new. Sitemap can be created by itself but will raise if the first index, or any of the page or post output files don't exist. This is because the sitemap depends on knowing the mtime of those files on disk. If extensions is False, posts and pages are not processed with any extension modules present in the extensions directory. """ content_dir = Path(settings['paths']['content root']) posts_dir = content_dir.joinpath(settings['paths']['posts subdir']) pages_dir = content_dir.joinpath(settings['paths']['pages subdir']) env = jinja_environment(user_templates=settings['paths']['templates root'], settings=settings) post_filenames = markdown_files(posts_dir) page_filenames = markdown_files(pages_dir) posts_list = [] pages_list = [] for class_, file_list, obj_list in [(Post, post_filenames, posts_list), (Page, page_filenames, pages_list)]: for fn in file_list: try: obj_list.append(class_.from_file(fn, settings)) except DraftError: print('{file} is marked as a draft'.format(file=fn), file=sys.stderr) posts_list.sort(reverse=True) pages_list.sort() objects_to_write = [] extensions_loaded = False if extensions: extensions_dir = Path(settings['paths']['extensions root']) if extensions_dir.exists(): modules = load_extensions(extensions_dir) extensions_loaded = True processed = apply_extensions(modules=modules, stage=ExtensionStage.posts_and_pages, pages=pages_list, posts=posts_list, settings=settings) posts_list = processed['posts'] pages_list = processed['pages'] objects_to_write.extend(processed['new_objects']) content_objects = [] if posts: content_objects.extend(posts_list) if pages: content_objects.extend(pages_list) if write_only_new: content_objects = [c for c in content_objects if c.is_new] objects_to_write.extend(content_objects) if index: indexes = Index.paginate_posts(posts=posts_list, settings=settings) objects_to_write.extend(indexes) if archives: objects_to_write.append(Archives(posts=posts_list, settings=settings)) if feeds: objects_to_write.append(RSSFeed(posts=posts_list, settings=settings)) objects_to_write.append(JSONFeed(posts=posts_list, settings=settings)) if extensions_loaded: processed = apply_extensions(modules=modules, stage=ExtensionStage.objects_to_write, objects=objects_to_write, settings=settings) objects_to_write = processed['objects'] for obj in objects_to_write: obj.render_to_disk(environment=env, build_date=datetime.now(tz=pytz.utc), all_posts=posts_list, all_pages=pages_list) if sitemap: if index: front_page = indexes[0] else: # Create dummy front so the sitemap can be generated by itself front_page = Index(page_number=1, posts=[], settings=settings) if not Index.output_path.exists(): Index.output_path.touch() content_list = posts_list + pages_list + [front_page] sitemap = Sitemap(content=content_list, settings=settings) sitemap.render_to_disk(environment=env)
def test_jinja_environment_absolute_urls_filter(self): """jinja_environment adds absolute_urls as a custom filter""" env = jinja_environment( user_templates=self.settings['paths']['templates root'], settings=self.settings) self.assertEqual(env.filters['absolute_urls'], absolute_urls)
def test_jinja_environment_rfc822_filter(self): """jinja_environment adds rfc822_date as a custom filter""" env = jinja_environment( user_templates=self.settings['paths']['templates root'], settings=self.settings) self.assertEqual(env.filters['rfc822_date'], rfc822_date)
def test_jinja_environment_basic(self): """jinja_environment returns Environment with expected templates""" env = jinja_environment( user_templates=self.settings['paths']['templates root'], settings=self.settings) self.assertEqual(self.jinja_env.list_templates(), env.list_templates())