Skip to content

Commit

Permalink
Merge pull request #586 from jerome-poisson/SD-5475_2
Browse files Browse the repository at this point in the history
[NITF] [FORMATTER] fixed bad handling of root element
  • Loading branch information
jerome-poisson committed Sep 26, 2016
2 parents cc00473 + fd6bd41 commit a7f9258
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
30 changes: 17 additions & 13 deletions superdesk/publish/formatters/nitf_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,21 +147,25 @@ def html2nitf(self, html_elem, root_elem=True):
"""convert HTML elements to NITF compatible elements
:param ET.Element: HTML to clean/transform
:param bool: True if its the main element (i.e. must no be deleted)
:return ET.Element: NITF compliant element
:param bool: True if its the main element (must be a <div>)
:return ET.Element: <div> element with NITF compliant children
"""
if html_elem.tag not in self.HTML2NITF:
if root_elem:
html_elem.tag = 'p'
else:
raise ValueError("Unhandled HTML element")
else:
if root_elem:
assert html_elem.tag == 'div'
# we change children of root element in place
for c in html_elem:
self.html2nitf(c, root_elem=False)
return html_elem

try:
nitf_map = self.HTML2NITF[html_elem.tag]
nitf_elem = nitf_map.get('nitf')
if nitf_elem is not None:
if nitf_elem == '':
raise ValueError("Element need to be removed")
html_elem.tag = nitf_elem
except KeyError:
raise ValueError("Unhandled HTML element")
nitf_elem = nitf_map.get('nitf')
if nitf_elem is not None:
if nitf_elem == '':
raise ValueError("Element need to be removed")
html_elem.tag = nitf_elem

html_elem.attrib.update(nitf_map.get('attrib', {}))

Expand Down
8 changes: 4 additions & 4 deletions tests/publish/nitf_formatter_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,15 @@ def test_formatter(self):

def test_html2nitf(self):
html = etree.fromstring(
'<p><strong>this text should be <i>modified</i></strong> so '
'<div><p><strong>this text should be <i>modified</i></strong> so '
'<span>[this should not be removed]</span> unkown <em unknown_attribute="toto">'
'elements</em> and <a bad_attribute="to_remove">attributes</a> are <h6>'
'removed</h6></p>')
'removed</h6></p></div>')

nitf = self.formatter.html2nitf(html)
expected = ('<p><em class="bold">this text should be <em class="italic">modified</em>'
expected = ('<div><p><em class="bold">this text should be <em class="italic">modified</em>'
'</em> so [this should not be removed] unkown <em class="italic">elements</em>'
' and <a>attributes</a> are <hl2>removed</hl2></p>')
' and <a>attributes</a> are <hl2>removed</hl2></p></div>')
self.assertEqual(etree.tostring(nitf, 'unicode'), expected)

def test_company_codes(self):
Expand Down

0 comments on commit a7f9258

Please sign in to comment.