aboutsummaryrefslogtreecommitdiffstats
path: root/generate_html_cs.py
diff options
context:
space:
mode:
authorMatěj Cepl <mcepl@redhat.com>2013-07-04 11:42:44 +0200
committerMatěj Cepl <mcepl@redhat.com>2013-07-04 12:08:49 +0200
commit148601911795a79ae99f222b9c1e7fee0b3501f9 (patch)
tree48e385dae82c97f570f53944d09a689f3888324f /generate_html_cs.py
parent3e1ebaaf407f61bb5fb6b3e052a2c42111c42ac0 (diff)
downloadhesla-148601911795a79ae99f222b9c1e7fee0b3501f9.tar.gz
Add parsing of holidays, week, and sunday readings.
Fixes #87
Diffstat (limited to 'generate_html_cs.py')
-rwxr-xr-xgenerate_html_cs.py98
1 files changed, 69 insertions, 29 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 <L> elements, make the text out of them (with
+ <BR> 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 += "<br>\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):
<OT>
<S b="Ps" ch="91" v="9"/>
<L>V Hospodinu je tvé útočiště.</L>
<SL>Žalm 91,9</SL>
</OT>
"""
+ 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'] += "<br>\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 <LOSUNG>
-# elements, then <DAY> 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 <DAY> 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 <DAY> element.
+
+ Example:
+ <DAY d="1" m="1" type="holiday" name="Nový Rok ">
+ <S b="Kol" ch="3" v="17"/>
+ <L>Všechno, cokoli mluvíte nebo děláte,</L>
+ <L>čiňte ve jménu Pána Ježíše a skrze něho děkujte Bohu Otci.</L>
+ <SL>Koloským 3,17</SL>
+ </DAY>
+
+ <DAY d="17" m="2" type="sunday" name="Invocavit"
+ meaning="Vzývati mne bude a vyslyším jej. Žalm 91,15" ord="7">
+ <S b="1J" ch="3" v="8"/>
+ <L>Proto se zjevil Syn Boží, aby zmařil činy ďáblovy.</L>
+ <SL>1.Janova 3,8b</SL>
+ </DAY>
+
+ <DAY d="13" m="1" type="week" name="Alianční modlitební týden ">
+ </DAY>
+ """
+ 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)