| 1 | from pathlib import Path |
| 2 | import re |
| 3 | import runpy |
| 4 | import shutil |
| 5 | from tempfile import TemporaryDirectory |
| 6 | import unittest |
| 7 | import xml.etree.ElementTree as ET |
| 8 | |
| 9 | from native_xml import ns |
| 10 | |
| 11 | ROOT = Path(__file__).resolve().parent.parent |
| 12 | FIXTURE = ROOT / 'corpus/word-lists' |
| 13 | compare = runpy.run_path(str(ROOT / 'tools/verify-document.py'))['compare'] |
| 14 | |
| 15 | |
| 16 | class WordListsTest(unittest.TestCase): |
| 17 | def test_lists_pasted_from_word_open_in_onenote_as_its_own_paste_gave_them(self): |
| 18 | read = FIXTURE / 'cold/read' |
| 19 | with TemporaryDirectory() as temporary: |
| 20 | copy = Path(temporary) / 'read' |
| 21 | shutil.copytree(read, copy) |
| 22 | compare(FIXTURE / 'candidate', copy) |
| 23 | page = ET.parse(read / 'page-000.xml').getroot() |
| 24 | lines = [] |
| 25 | |
| 26 | def walk(children, depth): |
| 27 | for element in children.findall('one:OE', ns): |
| 28 | bullet = element.find('one:List/one:Bullet', ns) |
| 29 | number = element.find('one:List/one:Number', ns) |
| 30 | marker = ('bullet %s ' % bullet.get('bullet') if bullet is not None |
| 31 | else number.get('text') + ' ' if number is not None else '') |
| 32 | text = re.sub(r'<[^>]*>', '', element.find('one:T', ns).text or '') |
| 33 | lines.append(' ' * depth + marker + text) |
| 34 | for nested in element.findall('one:OEChildren', ns): |
| 35 | walk(nested, depth + 1) |
| 36 | |
| 37 | outline = page.findall('one:Outline', ns)[-1] |
| 38 | for children in outline.findall('one:OEChildren', ns): |
| 39 | walk(children, 0) |
| 40 | self.assertEqual(lines, [ |
| 41 | 'bullet 1 Bullet one', |
| 42 | ' bullet 0 Bullet nested', |
| 43 | ' bullet 13 Bullet deeper', |
| 44 | 'bullet 1 Bullet two', |
| 45 | 'Plain paragraph', |
| 46 | '1. Number one', |
| 47 | ' a. Number nested', |
| 48 | ' i. Number deeper', |
| 49 | '2. Number two', |
| 50 | 'Last plain', |
| 51 | ]) |
| 52 | |
| 53 | |
| 54 | if __name__ == '__main__': |
| 55 | unittest.main() |