From b416c7d130f17ac3191d2ef39492cd7c4f70bef7 Mon Sep 17 00:00:00 2001 From: Radek Czajka Date: Thu, 15 Sep 2011 16:56:10 +0200 Subject: [PATCH] function to extract annotations from html --- librarian/html.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/librarian/html.py b/librarian/html.py index 5f832e3..1c141f3 100644 --- a/librarian/html.py +++ b/librarian/html.py @@ -263,3 +263,15 @@ def add_table_of_contents(root): root.insert(0, toc) + +def extract_annotations(html_path): + """For each annotation, yields a tuple: anchor, text, html.""" + parser = etree.HTMLParser(encoding='utf-8') + tree = etree.parse(html_path, parser) + for footnote in tree.find('//*[@id="footnotes"]').findall('div'): + anchor = footnote.find('a[@href]').get('href') + del footnote[:2] + text_str = etree.tostring(footnote, method='text', encoding='utf-8') + html_str = etree.tostring(footnote, method='html', encoding='utf-8') + yield anchor, text_str, html_str + -- 2.20.1