Skip to content

Commit

Permalink
successful porting, much revisioning and porting ahead, successful EP…
Browse files Browse the repository at this point in the history
…UB2 creation
  • Loading branch information
SavinaRoja committed Apr 18, 2014
1 parent a2ed11d commit 4d705e3
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 51 deletions.
3 changes: 2 additions & 1 deletion src/openaccess_epub/package/__init__.py
Expand Up @@ -110,12 +110,13 @@ def process(self, article):
#Entry for the biblio content document
biblio_idref = 'biblio-{0}-xhtml'.format(dash_doi)
if self.article.back is not None:
#TODO: May have to the same here as done for the tables
if self.article.back.findall('.//ref'):
self.spine_list.append(spine_item(biblio_idref, True))

#Entry for the tables content document
tables_idref = 'tables-{0}-xhtml'.format(dash_doi)
if self.article.document.findall('.//table'):
if self.article.publisher.has_out_of_flow_tables():
self.spine_list.append(spine_item(tables_idref, False))

self.acquire_metadata()
Expand Down
36 changes: 31 additions & 5 deletions src/openaccess_epub/publisher/__init__.py
Expand Up @@ -148,8 +148,6 @@ def __init__(self, article):
self.biblio_fragment = 'biblio.{0}.xhtml'.format(article_doi) + '#{0}'
self.tables_fragment = 'tables.{0}.xhtml'.format(article_doi) + '#{0}'

self.html_tables = []

self.epub2_support = False
self.epub3_support = False
self.epub_default = 2
Expand Down Expand Up @@ -648,8 +646,8 @@ def depth_headings(self, document):

def recursive_traverse(element, depth=0):
for div in element.findall('div'):
label = element.find('label')
title = element.find('title')
label = div.find('label')
title = div.find('title')
if label is not None:
#If there is a label, but it is empty
if len(label) == 0 and label.text is None:
Expand All @@ -675,5 +673,33 @@ def recursive_traverse(element, depth=0):
recursive_traverse(div, depth=depth + 1)

body = document.getroot().find('body')
recursive_traverse(body)
recursive_traverse(body, depth=1)

def has_out_of_flow_tables(self):
"""
Returns True if the article has out-of-flow tables, indicates separate
tables document.
This method is used to indicate whether rendering this article's content
will result in the creation of out-of-flow HTML tables. This method has
a base class implementation representing a common logic; if an article
has a graphic(image) representation of a table then the HTML
representation will be placed out-of-flow if it exists, if there is no
graphic(image) represenation then the HTML representation will be placed
in-flow.
Returns
-------
bool
True if there are out-of-flow HTML tables, False otherwise
"""
if self.article.body is None:
return False
for table_wrap in self.article.body.findall('.//table-wrap'):
alternatives = table_wrap.find('alternatives')
graphic = table_wrap.find('graphic') or alternatives.find('graphic')
table = table_wrap.find('table') or alternatives.find('table')
if graphic is not None and table is not None:
return True
return False

87 changes: 42 additions & 45 deletions src/openaccess_epub/publisher/plos.py
Expand Up @@ -1299,27 +1299,6 @@ def convert_ref_list_elements(self):
ref_p.text = str(etree.tostring(ref, method='text', encoding='utf-8'), encoding='utf-8')
replace(ref, ref_p)

@Publisher.special2
@Publisher.special3
def convert_graphic_elements(self):
"""
This is a method for the odd special cases where <graphic> elements are
standalone, or rather, not a part of a standard graphical element such
as a figure or a table. This method should always be employed after the
standard cases have already been handled.
"""
for graphic in self.main.getroot().findall('.//graphic'):
graphic.tag = 'img'
graphic.attrib['alt'] = 'unowned-graphic'
ns_xlink_href = ns_format(graphic, 'xlink:href')
if ns_xlink_href in graphic.attrib:
xlink_href = graphic.attrib[ns_xlink_href]
file_name = xlink_href.split('.')[-1] + '.png'
img_dir = 'images-' + self.doi_suffix()
img_path = '/'.join([img_dir, file_name])
graphic.attrib['src'] = img_path
remove_all_attributes(graphic, exclude=['id', 'class', 'alt', 'src'])

@Publisher.special2
@Publisher.special3
def convert_table_wrap_elements(self):
Expand All @@ -1330,28 +1309,20 @@ def convert_table_wrap_elements(self):
The 'id' attribute is treated as mandatory by this method.
"""
for table_wrap in self.main.getroot().findall('.//table-wrap'):
#TODO: Address table uncommenting, for now this is not workable
#for child in tab.childNodes:
# if child.nodeType == 8:
# uncomment(child)

#Create a div for all of the table stuff
table_div = etree.Element('div')
table_div.attrib['id'] = table_wrap.attrib['id']
table_div = etree.Element('div', {'id': table_wrap.attrib['id']})

#Get the optional label and caption
label = table_wrap.find('label')
caption = table_wrap.find('caption')
#Check for the alternatives element
alternatives = table_wrap.find('alternatives')
#Look for the graphic node, under table-wrap and alternatives
graphic = table_wrap.find('graphic')
if graphic is None and alternatives is not None:
graphic = alternatives.find('graphic')
#Look for the table node, under table-wrap and alternatives
table = table_wrap.find('table')
if table is None and alternatives is not None:
table = alternatives.find('table')
if graphic is None:
if alternatives is not None:
graphic = alternatives.find('graphic')
if table is None:
if alternatives is not None:
table = alternatives.find('table')

#Handling the label and caption
if label is not None and caption is not None:
Expand Down Expand Up @@ -1390,7 +1361,7 @@ def convert_table_wrap_elements(self):
xlink_href = ns_format(graphic, 'xlink:href')
graphic_xlink_href = graphic.attrib[xlink_href]
file_name = graphic_xlink_href.split('.')[-1] + '.png'
img_dir = 'images-' + self.doi_frag
img_dir = 'images-' + self.doi_suffix()
img_path = '/'.join([img_dir, file_name])
#Create the new img element
img_element = etree.Element('img', {'alt': 'A Table',
Expand All @@ -1403,33 +1374,59 @@ def convert_table_wrap_elements(self):
#The label attribute is just a means of transmitting some
#plaintext which will be used for the labeling in the html
#tables file
div = etree.SubElement(self.tables.find('body'),
'div',
{'id': table_wrap.attrib['id']})

if label is not None:
#Serialize the text, set as label attribute
table.attrib['label'] = str(etree.tostring(label, method='text', encoding='utf-8'), encoding='utf-8')
table.attrib['id'] = table_wrap.attrib['id']
bold_label = etree.SubElement(div, 'b')
append_all_below(bold_label, label)
#Add the table to the tables list
self.html_tables.append(table)
div.append(deepcopy(table))
#Also add the table's foot if it exists
table_wrap_foot = table_wrap.find('table-wrap-foot')
if table_wrap_foot is not None:
self.html_tables.append(table_wrap_foot)
table_wrap_foot.tag = 'div'
table_wrap_foot.attrib['class'] = 'table-wrap-foot'
div.append(table_wrap_foot)
#Create a link to the html version of the table
html_table_link = etree.Element('a')
html_table_link.attrib['href'] = self.tab_frag.format(table_wrap.attrib['id'])
html_table_link.attrib['href'] = self.tables_fragment.format(table_wrap.attrib['id'])
html_table_link.text = 'Go to HTML version of this table'
#Add this to the table div
table_div.append(html_table_link)
remove(table)

elif table is not None: # Table only
#Simply append the table to the table div
table_div.append(table)
elif graphic is None and table is None:
print('Encountered table-wrap element with neither graphic nor table. Exiting.')
sys.exit(1)
sys.exit('Encountered table-wrap element with neither graphic nor table. Exiting.')

#Replace the original table-wrap with the newly constructed div
replace(table_wrap, table_div)

@Publisher.special2
@Publisher.special3
def convert_graphic_elements(self):
"""
This is a method for the odd special cases where <graphic> elements are
standalone, or rather, not a part of a standard graphical element such
as a figure or a table. This method should always be employed after the
standard cases have already been handled.
"""
for graphic in self.main.getroot().findall('.//graphic'):
graphic.tag = 'img'
graphic.attrib['alt'] = 'unowned-graphic'
ns_xlink_href = ns_format(graphic, 'xlink:href')
if ns_xlink_href in graphic.attrib:
xlink_href = graphic.attrib[ns_xlink_href]
file_name = xlink_href.split('.')[-1] + '.png'
img_dir = 'images-' + self.doi_suffix()
img_path = '/'.join([img_dir, file_name])
graphic.attrib['src'] = img_path
remove_all_attributes(graphic, exclude=['id', 'class', 'alt', 'src'])

@Publisher.maker2
@Publisher.maker3
def make_biblio(self):
Expand Down

0 comments on commit 4d705e3

Please sign in to comment.