Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Work on EOAtable
  • Loading branch information
Klaus Thoden committed Aug 31, 2018
1 parent e89dc78 commit 91db5d1
Showing 1 changed file with 50 additions and 68 deletions.
118 changes: 50 additions & 68 deletions tei2imxml.py
Expand Up @@ -565,74 +565,52 @@ def transform_body(xml_tree, cited_data, publang):
##########
eoa_tables = xml_tree.xpath("//t:table", namespaces=NS_MAP)

# turn
# <table rows="4" cols="4" rend="blank" xml:id="sec3table1">
# <head>This is a table</head>
# <row role="label">
# <cell role="label">Heading 1</cell>
# <cell role="label">Heading <hi rend="bold">2</hi></cell>
# <cell role="label">Heading 3</cell>
# <cell role="label">Heading 4</cell>
# </row>
# <row role="data">
# <cell role="data">Here</cell>
# <cell role="data">you</cell>
# <cell role="data">may</cell>
# <cell role="data">find</cell>
# </row>
# <row role="data">
# <cell role="data">some</cell>
# <cell role="data">data</cell>
# <cell role="data">spread</cell>
# <cell role="data">over</cell>
# </row>
# <row role="data">
# <cell role="data">the</cell>
# <cell role="data">table</cell>
# <cell role="data">in</cell>
# <cell role="data">cells</cell>
# </row>
# </table>

# into

# <EOAtable>
# <EOAtablelabel>sec3table1</EOAtablelabel>
# <EOAtablecaption>This is a table</EOAtablecaption>
# <EOAtablecolumns>L2.3cmL2.3cmL2.3cmL2.3cm</EOAtablecolumns>
# <table id-text="1" id="uid38" place="sec3table1" rend="display">
# <row>
# <cell><tableheader>TRUE</tableheader>Heading 1</cell>
# <cell>Heading <hi rend="bold">2</hi></cell>
# <cell>Heading 3</cell>
# <cell>Heading 4</cell>
# </row>
# <row>
# <cell>Here</cell>
# <cell>you</cell>
# <cell>may</cell>
# <cell>find</cell>
# </row>
# <row>
# <cell>some</cell>
# <cell>data</cell>
# <cell>spread</cell>
# <cell>over</cell>
# </row>
# <row>
# <cell>the</cell>
# <cell>table</cell>
# <cell>in</cell>
# <cell>cells</cell>
# </row>
# </table>
# </EOAtable>

for table in eoa_tables:
table.tag = "EOAtable"
table_id = (table.get("{http://www.w3.org/XML/1998/namespace}id"))
tablechildren = table.findall("t:row", namespaces=NS_MAP)
table_caption = table.find("t:head", namespaces=NS_MAP)
number_of_cells = len(table.findall("t:row[1]/t:cell", namespaces=NS_MAP))

if table_caption is not None:
# print(etree.tostring(table))
table_id = table.attrib["{http://www.w3.org/XML/1998/namespace}id"]
# table_id = table.get("{httbp://www.w3.org/XML/1998/namespace}id")
table.clear()
table_label = etree.Element("EOAtablelabel")
table_label.text = table_id
table.append(table_label)
table_caption.tag = "EOAtablecaption"
table.tag = "EOAtable"
else:
table.tag = "EOAtablenonumber"
table.clear()
# not sure if this is evaluated later.
table_label = etree.SubElement(table, "EOAtablecolumns").text = "L3cm" * number_of_cells

table_label = etree.SubElement(table, "EOAtablelabel").text = table_id
if table_caption is not None:
table.insert(1, table_caption)
else:
pass

real_table_element = etree.SubElement(table, "table")
real_table_element.set("place", table_id)
real_table_element.set("rend", "display")
# how are those two constructed?
real_table_element.set("id-text", "1")
real_table_element.set("id", "uid38")

for row in tablechildren:
if row.get("role") == "label":
tableheader = etree.Element("tableheader")
tableheader.text = "TRUE"
# <tableheader>TRUE</tableheader>
row.insert(0, tableheader)
else:
pass
del row.attrib["role"]
cells = row.findall("t:cell", namespaces=NS_MAP)
for cell in cells:
del cell.attrib["role"]
real_table_element.append(row)

##############
# References #
Expand Down Expand Up @@ -747,6 +725,10 @@ def update_ids(xml_tree):

xmlReferences = xml_tree.findall(".//EOAref")
logging.debug("Found %d references", len(xmlReferences))
all_ids = xml_tree.xpath('//*[@xml:id]')
for xmlid in all_ids:
print(xmlid, "all ids")

for xmlReference in xmlReferences:
eoa_reference = xmlReference.find("ref")

Expand All @@ -759,8 +741,8 @@ def update_ids(xml_tree):
# else:
corresponding_eoa_id_element = xml_tree.xpath("//*[@xml:id='{}']".format(label_text))
logging.debug("The corresponding id element is %s", corresponding_eoa_id_element)
# if len(corresponding_eoa_id_element) == 0:
if corresponding_eoa_id_element is None:
# if corresponding_eoa_id_element is None:
if len(corresponding_eoa_id_element) == 0:
print("There seems to be no corresponding xml:id for %s. Exiting." % label_text)
sys.exit()
elif len(corresponding_eoa_id_element) > 1:
Expand Down

0 comments on commit 91db5d1

Please sign in to comment.