lpschedule-generator

libreplanet schedule generator - ricketyspace.net/lpschedule-generator
git clone git://git.ricketyspace.net/lpschedule-generator.git
Log | Files | Refs

commit aac9e52d138fef142b67a85921bff643060cf277
parent a28e07096198b52da4e1e1411d1f761d411e0db1
Author: rsiddharth <rsd@gnu.org>
Date:   Tue, 15 Dec 2015 20:50:39 -0500

HTMLRender -> RenderHTML + changes to it.

`RenderHTML` (was `HTMLRender`) now uses `PackagLoader` instead of
FileSystemLoader and the template are loaded from the templates/
directory.

Diffstat:
lps_gen.py | 38++++++++++++++++++++++----------------
templates/lp-sch-2016.jinja2 | 67+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
tests/test_lps_gen.py | 6++++++
3 files changed, 95 insertions(+), 16 deletions(-)

diff --git a/lps_gen.py b/lps_gen.py @@ -19,14 +19,15 @@ # <http://www.gnu.org/licenses/>. 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 @@ -0,0 +1,67 @@ +{# -*- mode: jinja2; -*- #} + +{# macros start #} + +{# make day header macro #} +{% macro mk_day_header(day) %} + <header> + <hgroup> + <h2>{{ day }}</h2> + </hgroup> + </header> +{% endmacro %} + +{# make timeslot header macro #} +{% macro mk_timeslot_header(timeslot) %} + <header> + <hgroup> + <h2>{{ timeslot }}</h2> + </hgroup> + </header> +{% endmacro %} + +{# make session header macro #} +{% macro mk_session_header(session) %} + <header> + <hgroup> + <h2>{{ session }}</h2> + </hgroup> + </header> +{% endmacro %} + +{# desc macro #} +{% macro desc(disc_list) %} + {% for desc_p in disc_list %} + <p>{{ desc_p }}</p> + {% endfor %} +{% endmacro %} + +{# populate sessions macro #} +{% macro populate_sessions(sessions, day_index, timeslot_index) %} + {% for session, session_info in sessions.iteritems() %} {# session start #} + <section> <!-- day-{{ day_index }}-timeslot-{{ timeslot_index }}-session-{{ loop.index }} start --> + {{ mk_session_header(session) }} + <p>{{ session_info['speaker'] }}</p> + <p>{{ session_info['room'] }}</p> + {{ desc(session_info['desc']) }} + </section> <!-- day-{{ day_index }}-timeslot-{{ timeslot_index }}-session-{{ loop.index }} end --> + {% endfor %} {# session end #} +{% endmacro %} + +{# populate timeslots macro #} +{% macro populate_timeslots(timeslots, day_index) %} + {% for timeslot, sessions in timeslots.iteritems() %} {# timeslot start #} + <article> <!-- day-{{ day_index }}-timeslot-{{ loop.index }} start --> + {{ mk_timeslot_header(timeslot) }} + {{ populate_sessions(sessions, day_index, loop.index) }} + </article> <!-- day-{{ day_index }}-timeslot-{{ loop.index }} end --> + {% endfor %} {# timeslot start #} +{% endmacro %} + +{# lp 2016 template start #} +{% for day, timeslots in schedule.iteritems() %} {# day start #} + <article> <!-- day-{{ loop.index }} start --> + {{ mk_day_header(day) }} + {{ populate_timeslots(timeslots, loop.index) }} + </article> <!-- day-{{ loop.index }} end --> +{% endfor %} {# day loop end #} diff --git 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