Skip to content

Commit

Permalink
Getting xml element children in Python 3.9
Browse files Browse the repository at this point in the history
  • Loading branch information
jchalupka-pan committed Feb 17, 2021
1 parent 7064548 commit 08ce6f1
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions mdv/markdownviewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,14 @@ def breakpoint():
else:
unescape = HTMLParser().unescape()


from xml.etree.ElementTree import Element

if getattr(Element, 'getchildren', None) is None:
get_element_children = lambda el: el
else:
get_element_children = lambda el: el.getchildren()

is_app = 0

def_enc_set = False
Expand Down Expand Up @@ -857,7 +865,7 @@ def replace_links(el, html):
if len(parts) == 1:
return None, html
links_list, cur_link = [], 0
links = [l for l in el.getchildren() if 'href' in l.keys()]
links = [l for l in get_element_children(el) if 'href' in l.keys()]
if not len(parts) == len(links) + 1:
# there is an html element within which we don't support,
# e.g. blockquote
Expand Down Expand Up @@ -918,18 +926,18 @@ def formatter(el, out, hir=0, pref='', parent=None):
import pdb
pdb.set_trace()
# for c in el.getchildren()[0].getchildren(): print c.text, c
# for c in get_element_children(get_element_children(el)[0]): print c.text, c
print('---------')
print(el, el.text)
print('---------')
"""
if el.tag == 'br':
out.append('\n')
return
# for c in el.getchildren(): print c.text, c
# for c in get_element_children(el): print c.text, c
links_list, is_txt_and_inline_markup = None, 0
if el.tag == 'blockquote':
for el1 in el.getchildren():
for el1 in get_element_children(el):
iout = []
formatter(el1, iout, hir + 2, parent=el)
pr = col(bquote_pref, H1)
Expand Down Expand Up @@ -1065,7 +1073,7 @@ def formatter(el, out, hir=0, pref='', parent=None):
# nr for ols:
if is_txt_and_inline_markup:
if el.tag == 'li':
childs = el.getchildren()
childs = get_element_children(el)
for nested in 'ul', 'ol':
if childs and childs[-1].tag == nested:
ul = childs[-1]
Expand Down Expand Up @@ -1095,10 +1103,10 @@ def fmt(cell, parent):

t = []
for he_bo in 0, 1:
for Row in el[he_bo].getchildren():
for Row in get_element_children(el[he_bo]):
row = []
t.append(row)
for cell in Row.getchildren():
for cell in get_element_children(Row):
row.append(fmt(cell, row))
cols = term_columns
# good ansi handling:
Expand Down

0 comments on commit 08ce6f1

Please sign in to comment.