def diff_toc(older_version, newer_version, old_toc, diff, from_version): #We work around Subparts in the TOC for now. compiled_toc = extract_sections(old_toc) for node in (v['node'] for v in diff.values() if v['op'] == 'added'): if len(node['label']) == 2 and node['title']: element = { 'label': node['title'], 'index': node['label'], 'section_id': '-'.join(node['label']), 'op': 'added' } data = {'index': node['label'], 'title': node['title']} TableOfContentsLayer.section(element, data) TableOfContentsLayer.appendix_supplement(element, data) compiled_toc.append(element) modified, deleted = modified_deleted_sections(diff) for el in compiled_toc: if not 'Subpart' in el['index']: el['url'] = reverse_chrome_diff_view( el['section_id'], older_version, newer_version, from_version) # Deleted first, lest deletions in paragraphs affect the section if tuple(el['index']) in deleted and 'op' not in el: el['op'] = 'deleted' if tuple(el['index']) in modified and 'op' not in el: el['op'] = 'modified' return sort_toc(compiled_toc)
def diff_toc(versions, old_toc, diff): # We work around Subparts in the TOC for now. compiled_toc = extract_sections(old_toc) for node in (v['node'] for v in diff.values() if v['op'] == 'added'): if len(node['label']) == 2 and node['title']: element = { 'label': node['title'], 'index': node['label'], 'section_id': '-'.join(node['label']), 'op': 'added' } data = {'index': node['label'], 'title': node['title']} TableOfContentsLayer.section(element, data) TableOfContentsLayer.appendix_supplement(element, data) compiled_toc.append(element) modified, deleted = modified_deleted_sections(diff) for el in compiled_toc: if 'Subpart' not in el['index'] and 'Subjgrp' not in el['index']: el['url'] = reverse_chrome_diff_view(el['section_id'], *versions) # Deleted first, lest deletions in paragraphs affect the section if tuple(el['index']) in deleted and 'op' not in el: el['op'] = 'deleted' if tuple(el['index']) in modified and 'op' not in el: el['op'] = 'modified' return sorted(compiled_toc, key=normalize_toc)
def test_section_with_thin_space(self): toc = TableOfContentsLayer(None) el = {} toc.section(el, {'index': ['1', '2'], 'title': u'§ 1.2 - Awesome'}) self.assertEqual(el, { 'is_section': True, 'is_section_span': False, 'section_id': '1-2', 'label': '1.2', 'sub_label': 'Awesome' })
def test_section_span(self): toc = TableOfContentsLayer(None) el = {} toc.section(el, { 'index': ['1', '2'], 'title': u'§§ 1.2-6 - This is a span' }) self.assertEqual(el, { 'is_section': True, 'is_section_span': True, 'section_id': '1-2', 'label': '1.2-6', 'sub_label': 'This is a span' })
def test_section(self): toc = TableOfContentsLayer(None) el = {} toc.section(el, {'index': ['1']}) self.assertEqual({}, el) toc.section(el, {'index': ['1', '2', '3']}) self.assertEqual({}, el) toc.section(el, {'index': ['1', 'B']}) self.assertEqual({}, el) toc.section(el, {'index': ['1', 'Interpretations']}) self.assertEqual({}, el) toc.section(el, {'index': ['1', '2'], 'title': u'§ 1.2 - Awesome'}) self.assertEqual(el, { 'is_section': True, 'is_section_span': False, 'section_id': '1-2', 'label': '1.2', 'sub_label': 'Awesome' }) toc.section(el, {'index': ['2', '1'], 'title': u'§ 2.1 Sauce'}) self.assertEqual(el, { 'is_section': True, 'is_section_span': False, 'section_id': '2-1', 'label': '2.1', 'sub_label': 'Sauce' })
def test_section(self): toc = TableOfContentsLayer(None) el = {} toc.section(el, {'index': ['1']}) self.assertEqual({}, el) toc.section(el, {'index': ['1', '2', '3']}) self.assertEqual({}, el) toc.section(el, {'index': ['1', 'B']}) self.assertEqual({}, el) toc.section(el, {'index': ['1', 'Interpretations']}) self.assertEqual({}, el) toc.section(el, {'index': ['1', '2'], 'title': '1.2 - Awesome'}) self.assertEqual(el, { 'is_section': True, 'section_id': '1-2', 'label': '1.2', 'sub_label': 'Awesome' }) toc.section(el, {'index': ['2', '1'], 'title': '2.1Sauce'}) self.assertEqual(el, { 'is_section': True, 'section_id': '2-1', 'label': '2.1', 'sub_label': 'Sauce' })