def test_appstream_convert(self): markup = """ <p>CHANGES IN THIS RELEASE</p> <p>Version 11.8.55.3510</p> <p>[Important updates]</p> <p></p> <ul> <li>Nothing.</li> <li></li> </ul> <p>[New functions & enhancements]</p> <ul> <li>Nothing.</li> <li>Nothing more.</li> </ul> <p>[Problem fixes]</p> <ul> <li>Nothing.</li> </ul> """ markdown = _markdown_from_root( ET.fromstring('<xxx>' + markup + '</xxx>')) print('`' + markdown + '`') # convert from markdown back to XML root = _xml_from_markdown(markdown) xml = ET.tostring(root, pretty_print=True) print('`' + xml.decode() + '`') # show problems for problem in _get_update_description_problems(root): print(' * %s' % problem.description)
def _node_validate_text(node, minlen=0, maxlen=0, nourl=False, allow_none=False): """ Validates the style """ # unwrap description if node.tag == 'description': try: text = _markdown_from_root(node) except KeyError as e: raise MetadataInvalid(e) else: text = node.text if text: text = text.strip() for tag in ['<p>', '<li>', '<ul>', '<ol>']: if text.find(tag) != -1: raise MetadataInvalid( '{} cannot specify markup tag {}'.format( node.tag, tag)) # invalid length if not text: if allow_none: return None raise MetadataInvalid('{} has no value'.format(node.tag)) # some tags can be split for multiple models if node.tag in ['name']: textarray = text.split('/') else: textarray = [text] for textsplit in textarray: if minlen and len(textsplit) < minlen: raise MetadataInvalid('<{}> is too short: {}/{}'.format( node.tag, len(textsplit), minlen)) if maxlen and len(textsplit) > maxlen: raise MetadataInvalid('<{}> is too long: {}/{}'.format( node.tag, len(textsplit), maxlen)) # contains hyperlink if nourl: for urlhandler in ['http://', 'https://', 'ftp://']: if text.find(urlhandler) != -1: raise MetadataInvalid( '{} cannot contain a hyperlink: {}'.format(node.tag, text)) return text
def test_markdown_from_root(self): xml = """<desc><p>Firmware</p><p>Awesome</p></desc>""" root = ET.fromstring(xml) md = _markdown_from_root(root) self.assertEqual(md, "Firmware\n\nAwesome") xml = """<desc><ul><li>Firmware</li><li>Awesome</li></ul></desc>""" root = ET.fromstring(xml) md = _markdown_from_root(root) self.assertEqual(md, "* Firmware\n * Awesome") xml = """<desc><unknown>Firmware</unknown></desc>""" root = ET.fromstring(xml) with self.assertRaises(KeyError): _markdown_from_root(root) xml = """<desc><li>Firmware</li></desc>""" root = ET.fromstring(xml) with self.assertRaises(KeyError): _markdown_from_root(root) xml = """<desc><ul><p>Firmware</p></ul></desc>""" root = ET.fromstring(xml) with self.assertRaises(KeyError): _markdown_from_root(root) xml = """<desc><ul><li><li>Firmware</li></li></ul></desc>""" root = ET.fromstring(xml) with self.assertRaises(KeyError): _markdown_from_root(root) xml = """<desc><p>Firmware<li>Awesome</li></p></desc>""" root = ET.fromstring(xml) with self.assertRaises(KeyError): _markdown_from_root(root)