From aac9e52d138fef142b67a85921bff643060cf277 Mon Sep 17 00:00:00 2001 From: rsiddharth Date: Tue, 15 Dec 2015 20:50:39 -0500 Subject: HTMLRender -> RenderHTML + changes to it. `RenderHTML` (was `HTMLRender`) now uses `PackagLoader` instead of FileSystemLoader and the template are loaded from the templates/ directory. --- lps_gen.py | 38 ++++++++++++++----------- templates/lp-sch-2016.jinja2 | 67 ++++++++++++++++++++++++++++++++++++++++++++ tests/test_lps_gen.py | 6 ++++ 3 files changed, 95 insertions(+), 16 deletions(-) create mode 100644 templates/lp-sch-2016.jinja2 diff --git a/lps_gen.py b/lps_gen.py index 9476e75..478d4ef 100644 --- a/lps_gen.py +++ b/lps_gen.py @@ -19,14 +19,15 @@ # . import json -import jinja2 from argparse import ArgumentParser from collections import OrderedDict from os import path +from jinja2 import Environment, PackageLoader from mistune import Renderer, Markdown + # Python dictionary that will contain the lp schedule. lps_dict = OrderedDict() @@ -62,6 +63,10 @@ class LPSRenderer(Renderer): def header(self, text, level, raw=None): global lps_dict + utf8_text = text + + # jinja2 will encode text back to utf8. + text = text.decode('utf8') if level == 2: # Add new day. @@ -79,7 +84,7 @@ class LPSRenderer(Renderer): # to 0. self.no_paragraph = 0 - return super(LPSRenderer, self).header(text, level, raw) + return super(LPSRenderer, self).header(utf8_text, level, raw) def paragraph(self, text): @@ -87,6 +92,9 @@ class LPSRenderer(Renderer): p = super(LPSRenderer, self).paragraph(text) + # jinja2 will encode text back to utf8. + text = text.decode('utf8') + if self.no_paragraph == 0: # Speaker if len(text.split(', ')) == 1: @@ -133,18 +141,19 @@ class LPSMarkdown(Markdown): html = super(LPSMarkdown, self).parse(text) return lps_dict -def HTMLRender(_dict): - """Render html from a dictionary and returns a string. - Uses jinja2 and HTML template file. - """ - templateLoader = jinja2.FileSystemLoader( searchpath="./" ) - templateEnv = jinja2.Environment( loader=templateLoader ) - TEMPLATE_FILE = "tests/files/index.html" - template = templateEnv.get_template( TEMPLATE_FILE ) - _data = {'res':_dict} - lps_output = template.render( _data ) - return lps_output +def RenderHTML(lps_dict, year): + """Renders LP schedule in HTML from a python dictionary. + + Returns the HTML as a string. + """ + env = Environment(loader=PackageLoader('lps_gen', + 'templates'), + trim_blocks=True, lstrip_blocks=True) + template = env.get_template('lp-sch-%s.jinja2' % year) + + return template.render(schedule=lps_dict) + def main(): parser = ArgumentParser() @@ -156,9 +165,6 @@ def main(): markdown = LPSMarkdown() lps_dict = markdown(lps_md_content) - print HTMLRender(lps_dict) - - #print json.dumps(lps_dict, indent=4) if __name__ == "__main__": diff --git a/templates/lp-sch-2016.jinja2 b/templates/lp-sch-2016.jinja2 new file mode 100644 index 0000000..fbffc0a --- /dev/null +++ b/templates/lp-sch-2016.jinja2 @@ -0,0 +1,67 @@ +{# -*- mode: jinja2; -*- #} + +{# macros start #} + +{# make day header macro #} +{% macro mk_day_header(day) %} +
+
+

{{ day }}

+
+
+{% endmacro %} + +{# make timeslot header macro #} +{% macro mk_timeslot_header(timeslot) %} +
+
+

{{ timeslot }}

+
+
+{% endmacro %} + +{# make session header macro #} +{% macro mk_session_header(session) %} +
+
+

{{ session }}

+
+
+{% endmacro %} + +{# desc macro #} +{% macro desc(disc_list) %} + {% for desc_p in disc_list %} +

{{ desc_p }}

+ {% endfor %} +{% endmacro %} + +{# populate sessions macro #} +{% macro populate_sessions(sessions, day_index, timeslot_index) %} + {% for session, session_info in sessions.iteritems() %} {# session start #} +
+ {{ mk_session_header(session) }} +

{{ session_info['speaker'] }}

+

{{ session_info['room'] }}

+ {{ desc(session_info['desc']) }} +
+ {% endfor %} {# session end #} +{% endmacro %} + +{# populate timeslots macro #} +{% macro populate_timeslots(timeslots, day_index) %} + {% for timeslot, sessions in timeslots.iteritems() %} {# timeslot start #} +
+ {{ mk_timeslot_header(timeslot) }} + {{ populate_sessions(sessions, day_index, loop.index) }} +
+ {% endfor %} {# timeslot start #} +{% endmacro %} + +{# lp 2016 template start #} +{% for day, timeslots in schedule.iteritems() %} {# day start #} +
+ {{ mk_day_header(day) }} + {{ populate_timeslots(timeslots, loop.index) }} +
+{% endfor %} {# day loop end #} diff --git a/tests/test_lps_gen.py b/tests/test_lps_gen.py index 1394dde..2b89414 100644 --- a/tests/test_lps_gen.py +++ b/tests/test_lps_gen.py @@ -154,6 +154,12 @@ class TestLpsGen(object): i = i + 1 + def test_RenderHTML(self): + """Testing `RenderHTML` function + """ + print RenderHTML(self.lps_dict, '2016') + + def teardown(self): """Cleans up things after each test in this class.""" pass -- cgit v1.2.3