From 148601911795a79ae99f222b9c1e7fee0b3501f9 Mon Sep 17 00:00:00 2001 From: Matěj Cepl Date: Thu, 4 Jul 2013 11:42:44 +0200 Subject: Add parsing of holidays, week, and sunday readings. Fixes #87 --- generate_html_cs.py | 98 +++++++++++++++++++++++++++++++++++++---------------- hesla.appcache | 4 ++- hesla.webapp | 2 +- screen.css | 3 ++ templates/base.html | 37 +++++++++++++------- 5 files changed, 101 insertions(+), 43 deletions(-) diff --git a/generate_html_cs.py b/generate_html_cs.py index 2251af8..e06d2b5 100755 --- a/generate_html_cs.py +++ b/generate_html_cs.py @@ -57,21 +57,44 @@ def csv2dict(filename): return out_dict +def parse_verses(lines): + """ + Having list of elements, make the text out of them (with +
elements where necessary). + """ + out = "" + logging.debug("lines = %s", lines) + logging.debug("lines = len %s", len(lines)) + if len(lines) > 0: + out = lines[0].text + if len(lines) > 1: + for line in lines[1:]: + logging.debug("lines = %s", lines) + logging.debug("another line = %s", line.text) + if line.text is not None: + out += "
\n" + line.text + logging.debug("body = %s", out) + return out + + def parse_body(elem): """Parse one verse element - Example could be (or NT instead of OT): + Example could be (or NT instead of OT, or DAY): V Hospodinu je tvé útočiště. Žalm 91,9 """ + if len(list(elem)) == 0: + return None + wword = { 'text': '' } source_elem = elem.find('S') - logging.debug("elem = %s", source_elem) - logging.debug("elem = attrib %s", source_elem.attrib) + logging.debug("S elem = %s", source_elem) + logging.debug("S elem = attrib %s", source_elem.attrib) # biblical reference (computer readable) wword['ref_id'] = { @@ -85,18 +108,7 @@ def parse_body(elem): wword['int_ref_id']['book'] = book_abbrs[wword['ref_id']['book']] # text of the verse - verses = list(elem.getiterator("L")) - logging.debug("verses = %s", verses) - logging.debug("verses = len %s", len(verses)) - if len(verses) > 0: - wword['text'] = verses[0].text - if len(verses) > 1: - for line in verses[1:]: - logging.debug("verses = %s", verses) - logging.debug("another line = %s", line.text) - if line.text is not None: - wword['text'] += "
\n" + line.text - logging.debug("body = %s", wword['text']) + wword['text'] = parse_verses(list(elem.getiterator("L"))) # references ref = elem.find("SL") @@ -147,19 +159,45 @@ def parse_losung(elem): return date_id, out -# TODO: instead of having list, have rather a dictionary with keys being -# dates; then we can have two-pass parsing, once we would parse -# elements, then ones and particular weekly/monthly/anniversary -# readings to the right place. -# -# * What should be the key of the dictionary? (year,month,day) tuple? -# * First step should be just refactoring of the current code to use -# dictionary. -# -# For it is necessary to distinguish, based on 'type' attribute -# ('week', 'sunday', 'holiday', 'month') and put generated text to the -# right property of the day object (and then in the template to the -# right place) + +def parse_day(elem, whole_dict): + """ + Parsse element. + + Example: + + + Všechno, cokoli mluvíte nebo děláte, + čiňte ve jménu Pána Ježíše a skrze něho děkujte Bohu Otci. + Koloským 3,17 + + + + + Proto se zjevil Syn Boží, aby zmařil činy ďáblovy. + 1.Janova 3,8b + + + + + """ + date_id = (cur_year, int(elem.attrib["m"]), int(elem.attrib["d"]),) + out = whole_dict[date_id] + + if elem.attrib['type'] == "holiday": + out['holy_name'] = elem.attrib['name'].strip() + out['holy_text'] = parse_body(elem) + elif elem.attrib['type'] == "sunday": + out['sun_name'] = elem.attrib['name'].strip() + if 'meaning' in elem.attrib: + out['sun_mean'] = elem.attrib['meaning'].strip() + out['sun_ord'] = int(elem.attrib['ord']) + out['sun_text'] = parse_body(elem) + elif elem.attrib['type'] == "week": + out['week_title'] = elem.attrib['name'].strip() + else: + raise ValueError("Unknown DAY type = %s", elem.attrib['type']) def parse_file(filename): @@ -172,11 +210,13 @@ def parse_file(filename): key, text = parse_losung(los) article_dict[key] = text + for los in tree.getiterator("DAY"): + parse_day(los, article_dict) + article_list = [] article_keys = sorted(article_dict.keys()) for key in article_keys: article_list.append(article_dict[key]) - # FIXME does Jinja2 somehow sort dictionary if it is on the input? return template.render(articles=article_list) diff --git a/hesla.appcache b/hesla.appcache index ab9974c..7822747 100644 --- a/hesla.appcache +++ b/hesla.appcache @@ -1,7 +1,9 @@ CACHE MANIFEST -# v56 - 2012-12-31 +# v57 - 2013-07-04 hesla.js icon-128.png +icon-30.png +icon-60.png index.html index_de.html screen.css diff --git a/hesla.webapp b/hesla.webapp index 233d82d..b89a6f7 100644 --- a/hesla.webapp +++ b/hesla.webapp @@ -1,5 +1,5 @@ { - "version": "0.2.1", + "version": "0.3.1", "name": "Hesla", "description": "Display today's Hesla (Losungen, Watchwords) from Bible.", "icons": { diff --git a/screen.css b/screen.css index 3b656c6..3f26113 100644 --- a/screen.css +++ b/screen.css @@ -4,6 +4,9 @@ body { h1 { font-size: 1em; } +h2 { + font-size: 0.8em; +} article { display: none; margin: 6px; diff --git a/templates/base.html b/templates/base.html index db30760..eebb8c0 100644 --- a/templates/base.html +++ b/templates/base.html @@ -1,7 +1,16 @@ -{%- macro bible_url(ref, mod) -%} +{%- macro bible_url(ref, bib_mod) -%} http://www.crosswire.org/study/passagestudy.jsp?key={{ ref.book }}+{{ ref.chapter }}%3A{{ ref.verse }}&mod={{ bib_mod }} {%- endmacro -%} +{%- macro verses(word) -%} +

{{ word.text }}

+

{% if word.int_ref_id -%} + + {{ word.ref }} + + {%- else %} + {{ word.ref }} + {%- endif %}

{%- endmacro -%} @@ -15,19 +24,23 @@ {% for art in articles %} -

{{ art.date_full }}{%- - if art.sunday %}
{{ art.sunday }}{%- endif %}

- {% for wword in art.watchwords %}

{{ wword.text }}

-

{% if wword.int_ref_id -%} - - {{ wword.ref }} - - {%- else %} - {{ wword.ref }} - {%- endif %}

{% endfor %} +

{%- + if art.week_title %}{{ art.week_title }}
{%- + endif %}{{ art.date_full }}{%- + if art.holy_name %}
{{ art.holy_name }}

{%- + if art.holy_text %}{{ + verses(art.holy_text)}}
{% endif %}{% + else %}{%- endif %}{%- + if art.sun_name %}

{{ art.sun_name }}{%- + if art.sun_mean %}: {{ art.sun_mean }}{%- endif %}

{%- + endif %}{%- if art.sun_text %}{{ + verses(art.sun_text)}}
{%- endif %}
+ + {% for wword in art.watchwords %}{{ verses(wword) }}{% endfor %} {%- if art.readings %}
    {% for read in art.readings %}
  • {{ read }}
  • {% endfor %} -
{% endif %} + +{% endif %}
{%- endfor %}