def create_jinja_env(output_dir, template_dirs=None): """ Create and return the Jinja2 Environment object. Args: output_dir: a path-like object. """ if template_dirs is None: template_dirs = [] env = Environment( loader=FileSystemLoader(template_dirs), autoescape=jinja2.select_autoescape(['html', 'xml']), # Enable the expression-statement extension: # http://jinja.pocoo.org/docs/2.10/templates/#expression-statement extensions=['jinja2.ext.do'], # Remove excess whitespace with lstrip_blocks and trim_blocks. lstrip_blocks=True, trim_blocks=True, ) # Using a Namespace object lets us change the context inside a # template, e.g. by calling "{% set options.lang = lang %}" from # within a with block. Doing this lets us access the option values # from within a custom filter, without having to pass the option # values explicitly. options = Namespace() # Jinja requires you to set using index rather than attribute notation. options['output_dir'] = Path(output_dir) # Initialize with a default of English. options['lang'] = ENGLISH_LANG env.globals.update( options=options, create_pdf=templating.create_pdf, create_tsv_files=templating.create_tsv_files, create_xlsx=templating.create_xlsx, subtemplate=templating.subtemplate, SHASUMS_PATH=SHA256SUMS_FILENAME, ) filters = dict( output_file_uri=templating.output_file_uri, format_date=templating.format_date, format_date_medium=templating.format_date_medium, secure_hash=templating.secure_hash, translate=templating.translate, to_json=templating.to_json, to_xml=templating.to_xml, to_xml_attr=templating.to_xml_attr, format_number=utils.format_number, format_percent=utils.format_percent, format_percent2=utils.format_percent2, ) tests = {} env.filters.update(filters) env.tests.update(tests) return env
def test_format_date_medium(self): day = date(2018, 2, 5) cases = [ ('en', 'Feb 5, 2018'), ('es', '5 feb. 2018'), ('tl', 'Peb 5, 2018'), ('zh', '2018年2月5日'), ] for lang, expected in cases: with self.subTest(lang=lang): context = {'options': Namespace(lang=lang)} actual = templating.format_date_medium(context, day) self.assertEqual(actual, expected)
def test_get_home_href(self): cases = [ (('summary.html', 'en'), 'index.html'), (('details/contest.html', 'en'), '../index.html'), (('details/contest.html', 'es'), '../index-es.html'), ] for (rel_path, lang), expected in cases: with self.subTest(rel_path=rel_path, lang=lang): context = { 'default_rel_path': Path(rel_path), 'options': Namespace(lang=lang), } actual = templating.get_home_href(context) self.assertEqual(type(actual), PosixPath) self.assertEqual(str(actual), expected)
def test_make_lang_path(self): options = Namespace(lang='zh') context = {'options': options} cases = [ ('en', 'index.html', Path('index.html')), ('es', 'index.html', Path('index-es.html')), # Test defaulting to the context's language. (None, 'index.html', Path('index-zh.html')), # Test some files in a subdirectory. ('en', 'details/contest-1.html', Path('details/contest-1.html')), ('es', 'details/contest-1.html', Path('details/contest-1-es.html')), ] for lang, default_path, expected in cases: with self.subTest(lang=lang): actual = utils.make_lang_path(default_path, context=context, lang=lang) self.assertEqual(actual, expected)
def test_get_relative_href(self): cases = [ (('summary.html', 'new.html', 'en'), 'new.html'), # Test a rel_path "above" the top level. (('summary.html', '../new.html', 'en'), '../new.html'), # Test a current page down one level. (('details/contest.html', 'new.html', 'en'), '../new.html'), # Test a rel_path "above" the top level. (('details/contest.html', '../new.html', 'en'), '../../new.html'), ] for (current_path, rel_path, lang), expected in cases: with self.subTest(rel_path=rel_path, lang=lang): context = { 'default_rel_path': Path(current_path), 'options': Namespace(lang=lang), } actual = templating.get_relative_href(context, rel_path, lang=lang) self.assertEqual(type(actual), PosixPath) self.assertEqual(str(actual), expected)
def create_jinja_env(output_dir, template_dirs=None, deterministic=None, gzip_path=None, skip_pdf=False): """ Create and return the Jinja2 Environment object. Args: output_dir: a path-like object. deterministic: for deterministic PDF generation. Defaults to False. gzip_path: the path the tar.gz file will be written to. skip_pdf: whether to skip PDF generation. Defaults to False. """ if template_dirs is None: template_dirs = [] env = Environment( loader=FileSystemLoader(template_dirs), autoescape=jinja2.select_autoescape(['html', 'xml']), # Enable the expression-statement extension: # http://jinja.pocoo.org/docs/2.10/templates/#expression-statement extensions=['jinja2.ext.do'], # Remove excess whitespace with lstrip_blocks and trim_blocks. lstrip_blocks=True, trim_blocks=True, ) # Using a Namespace object lets us change the context inside a # template, e.g. by calling "{% set options.lang = lang %}" from # within a with block. Doing this lets us access the option values # from within a custom filter, without having to pass the option # values explicitly. options = Namespace() # Jinja requires you to set using index rather than attribute notation. options['output_dir'] = Path(output_dir) # Initialize with a default of English. options['lang'] = ENGLISH_LANG options['deterministic'] = deterministic options['skip_pdf'] = skip_pdf env.globals.update(options=options, current_page_link=templating.current_page_link, create_pdf=templating.create_pdf, create_tsv_files=templating.create_tsv_files, create_xlsx=templating.create_xlsx, get_relative_href=templating.get_relative_href, home_href=templating.get_home_href, make_translator=templating.make_translator, subtemplate=templating.subtemplate, # Convert the Path objects to strings. gzip_path=str(gzip_path), shasums_path=str(SHASUMS_PATH), ) filters = dict( output_file_uri=templating.output_file_uri, format_date=templating.format_date, format_date_medium=templating.format_date_medium, format_datetime=templating.format_datetime, secure_hash=templating.secure_hash, translate=templating.translate, default_contest_path=templating.default_contest_path, format_number=utils.format_number, compute_percent=utils.compute_percent, format_percent=utils.format_percent, format_percent2=utils.format_percent2, nobreak=utils.make_non_breaking, to_element_id=templating.to_element_id, to_fragment=utils.to_fragment, ) tests = {} env.filters.update(filters) env.tests.update(tests) return env