feat(convert): ajoute la conversion des footnotes

This commit is contained in:
François Poulain 2020-08-09 12:14:48 +02:00
parent e4816bc2ee
commit 9360cc134e
1 changed files with 41 additions and 1 deletions

View File

@ -164,6 +164,44 @@ def filter_html(html):
return html
def footnotes(html):
bracket_pattern = re.compile(r'\[\s*(/?)\s*(fn|footnote)\s*(\s[^\]]*)?\]')
html = bracket_pattern.sub(r'<\1\2\3>', html)
soup = bs(html, 'html.parser')
index = 1
seen_values = []
for fn in soup.find_all(['fn', 'footnote']):
if 'value' in fn.attrs and fn.attrs['value'] in seen_values:
value = fn.attrs['value']
fn.clear()
elif 'value' in fn.attrs and fn.attrs['value']:
value = fn.attrs['value']
seen_values.append(value)
else:
value = ""
seen_values.append(str(index))
index += 1
if value:
spip_fn = fn.wrap(soup.new_tag('spip:fn', value=value))
else:
spip_fn = fn.wrap(soup.new_tag('spip:fn'))
spip_fn.fn.unwrap()
html = str(soup)
# Spip a eu la bonne idée de choisir les crochets et chevrons
# pour indiquer les réfs.
spip_fn_open_pattern = re.compile(r'<spip:fn>')
spip_fn_close_pattern = re.compile(r'</spip:fn>')
spip_fn_value_pattern = re.compile(r'<spip:fn value="([^"]+)">')
html = spip_fn_open_pattern.sub(r'[[', html)
html = spip_fn_close_pattern.sub(r']]', html)
html = spip_fn_value_pattern.sub(r'[[<\1>', html)
return html
def sanitarize_html(html, node_fmt):
html = strong_to_dl(html)
@ -176,7 +214,9 @@ def sanitarize_html(html, node_fmt):
else:
raise NotImplementedError("Ce node est dans un format inconnu.")
return str(bs(html, 'html.parser'))
html = footnotes(html)
return html
def convert_node(node, options):