Skip to content

Commit

Permalink
Process citedRange with inline markup
Browse files Browse the repository at this point in the history
  • Loading branch information
kthoden committed Mar 2, 2020
1 parent ad520bc commit bcadf50
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 11 deletions.
44 changes: 33 additions & 11 deletions src/tei2imxml.py
Original file line number Diff line number Diff line change
Expand Up @@ -764,15 +764,24 @@ def handle_refs_default(ref):
else:
pass

cited_range_children = False

if len(cited_range) > 0:
if cited_range[0].text is not None and cited_range[0].get("from") is not None:
has_content = libeoaconvert.has_text_or_children(cited_range[0])

if has_content and cited_range[0].get("from") is not None:
logging.error("You must not use 'from' attribute and text in citedRange at the same time. Exiting.")
sys.exit(1)
elif cited_range[0].text is not None:
# might contain markup!
pagerange = ", {}".format(cited_range[0].text)
# clear the text
cited_range[0].text = ""
elif has_content:
if len(cited_range[0].getchildren()) > 0:
cited_range_children = True
cited_range[0].tag = "tagtobestripped"
pagerange = f""", {etree.tostring(cited_range[0]).decode("utf-8").strip()}"""
print("pagerange", pagerange)
else:
pagerange = ", {}".format(cited_range[0].text)
# clear the text
cited_range[0].text = ""
elif cited_range[0].get("from") is not None:
pagerange_start = cited_range[0].get("from")
pagerange_end = cited_range[0].get("to")
Expand All @@ -781,18 +790,27 @@ def handle_refs_default(ref):

if cite_render == 'year':
try:
formatted_citation = cited_data[citekey][1] + pagerange
if cited_range_children:
formatted_citation = etree.fromstring(f"<tagtobestripped>{cited_data[citekey][1]}{pagerange}</tagtobestripped>")
else:
formatted_citation = cited_data[citekey][1] + pagerange
except KeyError:
logging.error("Citekey %s was not found in the references. Exiting." % citekey)
sys.exit(1)
else:
try:
formatted_citation = cited_data[citekey][0] + pagerange
if cited_range_children:
formatted_citation = etree.fromstring(f"<tagtobestripped>{cited_data[citekey][0]}{pagerange}</tagtobestripped>")
else:
formatted_citation = cited_data[citekey][0] + pagerange
except KeyError:
logging.error("Citekey %s was not found in the references. Exiting." % citekey)
sys.exit(1)

sanitized_citation_string = sanitize_data_string(formatted_citation)
if cited_range_children:
pass
else:
sanitized_citation_string = sanitize_data_string(formatted_citation)

if olddesign == True:
""" <span rel="popover" class="citation" citekey="DastonGalison_2010"
Expand All @@ -815,9 +833,13 @@ def handle_refs_default(ref):
citation.set("class", "publications-popup-text")
citation.set("citekey", citekey)

citation.set("data-title", sanitized_citation_string)
citation.set("data-title", sanitized_citation_string.strip())
citation.set("data-content", cited_data[citekey][3])
citation.text = sanitized_citation_string

if cited_range_children:
citation.append(formatted_citation)
else:
citation.text = sanitized_citation_string

#############
# Footnotes #
Expand Down
10 changes: 10 additions & 0 deletions src/utils/libeoaconvert.py
Original file line number Diff line number Diff line change
Expand Up @@ -535,3 +535,13 @@ def format_date(accessed_date, language):

return accessed_string
# def format_date ends here


def has_text_or_children(cr):
"""Check whether an element contains text or further elements"""
if cr.text or len(cr.getchildren()) > 0:
htoc = True
else:
htoc = False
return htoc
# def has_text_or_children ends here

0 comments on commit bcadf50

Please sign in to comment.