def test_pseudo_form_field_number_of_underscores(self): self.assertIn( '<span class="regdown-form_extend">__' '<span></span></span>', regdown('Field: __')) self.assertIn( '<span class="regdown-form_extend">_______' '<span></span></span>', regdown('Field: _______'))
def test_pseudo_form_field_number_of_underscores(self): self.assertIn('<span class="regdown-form_extend">__' '<span></span></span>', regdown('Field: __')) self.assertIn('<span class="regdown-form_extend">_______' '<span></span></span>', regdown('Field: _______'))
def test_tables_extension_exists(self): text = ('First Header | Second Header\n' '------------- | -------------\n' 'Content Cell | Content Cell\n' 'Content Cell | Content Cell\n') self.assertIn('<table>', regdown(text)) self.assertIn('<th>First Header</th>', regdown(text)) self.assertIn('<td>Content Cell</td>', regdown(text))
def test_tables_extension_exists(self): text = ( 'First Header | Second Header\n' '------------- | -------------\n' 'Content Cell | Content Cell\n' 'Content Cell | Content Cell\n' ) self.assertIn('<table>', regdown(text)) self.assertIn('<th>First Header</th>', regdown(text)) self.assertIn('<td>Content Cell</td>', regdown(text))
def test_block_reference(self): contents_resolver = lambda l: '{foo-bar}\n# §FooBar\n\n' render_block_reference = lambda c, **kwargs: \ '<blockquote>{}</blockquote>'.format(regdown(c)) text = 'see(foo-bar)' self.assertIn( '<h1>§FooBar</h1>', regdown(text, contents_resolver=contents_resolver, render_block_reference=render_block_reference))
def test_prefixed_inline_interp_label(self): text = '{31-a-1-Interp-1}\nThis is a paragraph with a label.' text2 = '{31-a-1-i-Interp-1}\nThis is a paragraph with a label.' self.assertEqual( regdown(text), '<p class="regdown-block level-1" id="31-a-1-Interp-1">' 'This is a paragraph with a label.</p>') self.assertEqual( regdown(text2), '<p class="regdown-block level-1" id="31-a-1-i-Interp-1">' 'This is a paragraph with a label.</p>')
def test_prefixed_inline_interp_label(self): text = '{31-a-1-Interp-1}\nThis is a paragraph with a label.' text2 = '{31-a-1-i-Interp-1}\nThis is a paragraph with a label.' self.assertEqual( regdown(text), '<p class="regdown-block level-0" id="31-a-1-Interp-1">' 'This is a paragraph with a label.</p>' ) self.assertEqual( regdown(text2), '<p class="regdown-block level-0" id="31-a-1-i-Interp-1">' 'This is a paragraph with a label.</p>' )
def test_appendix_label(self): text = '{A-2-d}\nThis is a paragraph with a label.' text2 = '{A-2-d-1}\nThis is another paragraph with a label.' text3 = '{A-2-d-1-v}\nThis is yet another paragraph with a label.' self.assertEqual( regdown(text), '<p class="regdown-block level-0" id="A-2-d">' 'This is a paragraph with a label.</p>') self.assertEqual( regdown(text2), '<p class="regdown-block level-1" id="A-2-d-1">' 'This is another paragraph with a label.</p>') self.assertEqual( regdown(text3), '<p class="regdown-block level-2" id="A-2-d-1-v">' 'This is yet another paragraph with a label.</p>')
def test_multi_part_appendix_label(self): text = '{M1-1-a}\nThis is a paragraph with a label.' text2 = '{M1-1-a-1}\nThis is another paragraph with a label.' text3 = '{M1-1-a-1-i}\nThis is yet another paragraph with a label.' self.assertEqual( regdown(text), '<p class="regdown-block level-0" id="M1-1-a">' 'This is a paragraph with a label.</p>') self.assertEqual( regdown(text2), '<p class="regdown-block level-1" id="M1-1-a-1">' 'This is another paragraph with a label.</p>') self.assertEqual( regdown(text3), '<p class="regdown-block level-2" id="M1-1-a-1-i">' 'This is yet another paragraph with a label.</p>')
def test_complex_appendix_label(self): text = '{B-h2-p2}\nThis is a paragraph with a label.' text2 = '{B-h2-p2-1}\nThis is another paragraph with a label.' text3 = '{B-h2-p2-1-i}\nThis is yet another paragraph with a label.' self.assertEqual( regdown(text), '<p class="regdown-block level-0" id="B-h2-p2">' 'This is a paragraph with a label.</p>') self.assertEqual( regdown(text2), '<p class="regdown-block level-1" id="B-h2-p2-1">' 'This is another paragraph with a label.</p>') self.assertEqual( regdown(text3), '<p class="regdown-block level-2" id="B-h2-p2-1-i">' 'This is yet another paragraph with a label.</p>')
def section_page(self, request, section_label): section = self.section_query.get(label=section_label) current_index = self.sections.index(section) context = self.get_context(request) content = regdown(section.contents, url_resolver=get_url_resolver(self), contents_resolver=get_contents_resolver(self), render_block_reference=partial( self.render_interp, context)) context.update({ 'version': self.regulation.effective_version, 'content': content, 'get_secondary_nav_items': get_reg_nav_items, 'next_section': get_next_section(self.sections, current_index), 'previous_section': get_previous_section(self.sections, current_index), 'section': section, 'breadcrumb_items': self.get_breadcrumbs(request, section), }) return TemplateResponse(request, self.template, context)
def test_block_reference_no_contents(self): contents_resolver = lambda l: '' text = 'see(foo-bar)' self.assertEqual( regdown(text, contents_resolver=contents_resolver, render_block_reference=DEFAULT_RENDER_BLOCK_REFERENCE), '')
def test_complicated_inline_interp_label(self): text = '{12-d-Interp-7-ii-c-A-7}\nThis is a paragraph with a label.' self.assertEqual( regdown(text), '<p class="regdown-block level-4" id="12-d-Interp-7-ii-c-A-7">' 'This is a paragraph with a label.</p>' )
def test_get_contents_resolver_reference_doesnt_exist(self): contents_resolver = get_contents_resolver(self.reg_page) result = regdown(self.section_3.contents, contents_resolver=contents_resolver) self.assertEqual( result, '<p class="regdown-block level-0" id="b">Securities credit.</p>')
def test_list_state(self): text = '- {my-label} This is a paragraph in a list.' self.assertEqual( regdown(text), '<ul>\n<li>\n<p class="regdown-block level-1" id="my-label">' 'This is a paragraph in a list.' '</p>\n</li>\n</ul>')
def test_linebreak_label(self): text = '{my-label}\nThis is a paragraph with a label.' self.assertEqual( regdown(text), '<p class="regdown-block level-1" id="my-label">' 'This is a paragraph with a label.</p>' )
def test_even_deeper_inline_interp_label(self): text = '{1-a-Interp-1-i-a}\nThis is a paragraph with a label.' self.assertEqual( regdown(text), '<p class="regdown-block level-2" id="1-a-Interp-1-i-a">' 'This is a paragraph with a label.</p>' )
def test_get_contents_resolver(self): contents_resolver = get_contents_resolver( self.reg_page.regulation.effective_version) result = regdown(self.section_2.contents, contents_resolver=contents_resolver, render_block_reference=DEFAULT_RENDER_BLOCK_REFERENCE) self.assertIn('Interpreting adverse action', result)
def section_page(self, request, date_str=None, section_label=None): """ Render a section of the currently effective regulation """ if date_str is not None: effective_version = self.get_effective_version(request, date_str) section_query = self.get_section_query( effective_version=effective_version ) else: effective_version = self.regulation.effective_version section_query = self.get_section_query() kwargs = {} if date_str is not None: kwargs['date_str'] = date_str try: section = section_query.get(label=section_label) except Section.DoesNotExist: return redirect( self.url + self.reverse_subpage( "index", kwargs=kwargs ) ) sections = list(section_query.all()) current_index = sections.index(section) context = self.get_context( request, section, sections=sections, **kwargs ) content = regdown( section.contents, url_resolver=get_url_resolver(self, date_str=date_str), contents_resolver=get_contents_resolver(effective_version), render_block_reference=partial(self.render_interp, context) ) next_section = get_next_section(sections, current_index) previous_section = get_previous_section(sections, current_index) context.update({ 'version': effective_version, 'section': section, 'content': content, 'get_secondary_nav_items': partial( get_secondary_nav_items, sections=sections, date_str=date_str ), 'next_section': next_section, 'next_url': get_section_url(self, next_section, date_str=date_str), 'previous_section': previous_section, 'previous_url': get_section_url(self, previous_section, date_str=date_str), }) return TemplateResponse( request, self.template, context )
def test_block_reference(self): contents_resolver = lambda l: '{foo-bar}\n# §FooBar\n\n' text = 'see(foo-bar)' self.assertIn( '<h1>§FooBar</h1>', regdown(text, contents_resolver=contents_resolver, render_block_reference=DEFAULT_RENDER_BLOCK_REFERENCE))
def test_get_contents_resolver(self): contents_resolver = get_contents_resolver(self.reg_page) result = regdown( self.section_2.contents, contents_resolver=contents_resolver, render_block_reference=DEFAULT_RENDER_BLOCK_REFERENCE ) self.assertIn('Interpreting adverse action', result)
def test_list_state(self): text = '- {my-label} This is a paragraph in a list.' self.assertEqual( regdown(text), '<ul>\n<li>\n<p class="regdown-block level-1" id="my-label">' 'This is a paragraph in a list.' '</p>\n</li>\n</ul>' )
def test_pseudo_form_field_inside_line(self): text = 'inline______fields______within paragraph' self.assertIn( 'inline<span class="regdown-form">______</span>' 'fields<span class="regdown-form">______</span>' 'within paragraph', regdown(text) )
def test_empty_inline_interp_label(self): text = ('{30-Interp-1}\n' '1. Applicability\n\n' '{30-b-Interp}\n' '#### 30(b) Business Day') self.assertIn( '<div class="regdown-block level-0" id="30-b-Interp"></div>', regdown(text))
def test_nolabel(self): text = 'This is a paragraph without a label.' self.assertEqual( regdown(text), '<p class="regdown-block" ' 'id="e2cb7f25f263e65fc6737e03e0ecb90382398da3966b6da734b451be">' 'This is a paragraph without a label.</p>' )
def test_multiple_linebreaks_label(self): text = '{my-label}\n\nThis is a paragraph with a label.' self.assertEqual( regdown(text), '<div class="regdown-block level-1" id="my-label"></div>\n' '<p class="regdown-block" ' 'id="725445113243d57f132b6408fa8583122d2641e591a9001f04fcde08">' 'This is a paragraph with a label.</p>')
def test_multiple_linebreaks_label(self): text = '{my-label}\n\nThis is a paragraph with a label.' self.assertEqual( regdown(text), '<div class="regdown-block level-1" id="my-label"></div>\n' '<p class="regdown-block" ' 'id="725445113243d57f132b6408fa8583122d2641e591a9001f04fcde08">' 'This is a paragraph with a label.</p>' )
def test_get_contents_resolver_reference_doesnt_exist(self): contents_resolver = get_contents_resolver( self.reg_page.regulation.effective_version) result = regdown(self.section_3.contents, contents_resolver=contents_resolver, render_block_reference=DEFAULT_RENDER_BLOCK_REFERENCE) self.assertEqual( result, '<p class="regdown-block level-0" id="b">Securities credit.</p>')
def test_complex_appendix_label(self): text = '{B-h2-p2}\nThis is a paragraph with a label.' text2 = '{B-h2-p2-1}\nThis is another paragraph with a label.' text3 = '{B-h2-p2-1-i}\nThis is yet another paragraph with a label.' self.assertEqual( regdown(text), '<p class="regdown-block level-0" id="B-h2-p2">' 'This is a paragraph with a label.</p>' ) self.assertEqual( regdown(text2), '<p class="regdown-block level-1" id="B-h2-p2-1">' 'This is another paragraph with a label.</p>' ) self.assertEqual( regdown(text3), '<p class="regdown-block level-2" id="B-h2-p2-1-i">' 'This is yet another paragraph with a label.</p>' )
def test_block_reference_resolver_not_callable(self): text = 'see(foo-bar)' self.assertEqual( regdown( text, contents_resolver=None, render_block_reference=DEFAULT_RENDER_BLOCK_REFERENCE ), '' )
def test_multi_part_appendix_label(self): text = '{M1-1-a}\nThis is a paragraph with a label.' text2 = '{M1-1-a-1}\nThis is another paragraph with a label.' text3 = '{M1-1-a-1-i}\nThis is yet another paragraph with a label.' self.assertEqual( regdown(text), '<p class="regdown-block level-0" id="M1-1-a">' 'This is a paragraph with a label.</p>' ) self.assertEqual( regdown(text2), '<p class="regdown-block level-1" id="M1-1-a-1">' 'This is another paragraph with a label.</p>' ) self.assertEqual( regdown(text3), '<p class="regdown-block level-2" id="M1-1-a-1-i">' 'This is yet another paragraph with a label.</p>' )
def test_appendix_label(self): text = '{A-2-d}\nThis is a paragraph with a label.' text2 = '{A-2-d-1}\nThis is another paragraph with a label.' text3 = '{A-2-d-1-v}\nThis is yet another paragraph with a label.' self.assertEqual( regdown(text), '<p class="regdown-block level-0" id="A-2-d">' 'This is a paragraph with a label.</p>' ) self.assertEqual( regdown(text2), '<p class="regdown-block level-1" id="A-2-d-1">' 'This is another paragraph with a label.</p>' ) self.assertEqual( regdown(text3), '<p class="regdown-block level-2" id="A-2-d-1-v">' 'This is yet another paragraph with a label.</p>' )
def test_block_reference(self): contents_resolver = lambda l: '{foo-bar}\n# §FooBar\n\n' text = 'see(foo-bar)' self.assertIn( '<h1>§FooBar</h1>', regdown( text, contents_resolver=contents_resolver, render_block_reference=DEFAULT_RENDER_BLOCK_REFERENCE ) )
def test_empty_inline_interp_label(self): text = ( '{30-Interp-1}\n' '1. Applicability\n\n' '{30-b-Interp}\n' '#### 30(b) Business Day' ) self.assertIn( '<div class="regdown-block level-2" id="30-b-Interp"></div>', regdown(text) )
def test_block_reference_no_contents(self): contents_resolver = lambda l: '' text = 'see(foo-bar)' self.assertEqual( regdown( text, contents_resolver=contents_resolver, render_block_reference=DEFAULT_RENDER_BLOCK_REFERENCE ), '' )
def test_get_contents_resolver_reference_doesnt_exist(self): contents_resolver = get_contents_resolver(self.reg_page) result = regdown( self.section_3.contents, contents_resolver=contents_resolver, render_block_reference=DEFAULT_RENDER_BLOCK_REFERENCE ) self.assertEqual( result, '<p class="regdown-block level-0" id="b">Securities credit.</p>' )
def test_no_underscore_emphasis(self): self.assertIn('<em>foo</em>', regdown('*foo*')) self.assertIn('<strong>foo</strong>', regdown('**foo**')) self.assertIn('<strong><em>foo</em></strong>', regdown('***foo***')) self.assertNotIn('<em>foo</em>', regdown('_foo_')) self.assertNotIn('<strong>foo</strong>', regdown('__foo__')) self.assertNotIn('<strong><em>foo</em></strong>', regdown('___foo___'))
def test_section_symbol_with_non_breaking_space(self): self.assertIn('§ 1023', regdown('§ 1023')) self.assertIn('§ 1023.4', regdown('§ 1023.4')) self.assertIn('§ 1023.4(a)', regdown('§ 1023.4(a)')) self.assertIn('§ 1023.4(a)', regdown('§\t1023.4(a)')) self.assertIn('§ 1023.4(a)', regdown('§ 1023.4(a)')) self.assertIn('§ 1023.4(a)', regdown('§ 1023.4(a)'))
def render_interp(self, context, raw_contents, **kwargs): template = get_template('regulations3k/inline_interps.html') # Extract the title from the raw regdown section_title_match = re.search(r'#+\s?(?P<section_title>.*)\s', raw_contents) if section_title_match is not None: context.update({'section_title': section_title_match.group(1)}) span = section_title_match.span() raw_contents = raw_contents[:span[0]] + raw_contents[span[1]:] context.update({'contents': regdown(raw_contents)}) context.update(kwargs) return template.render(context)
def extract_graphs(self): """Break out and store a section's paragraphs for indexing.""" part = self.subpart.version.part section_tag = "{}-{}".format(part.part_number, self.label) extractor = regdown.extract_labeled_paragraph paragraph_ids = re.findall(r'[^{]*{(?P<label>[\w\-]+)}', self.contents) created = 0 deleted = 0 kept = 0 exclude_from_deletion = [] known_ids = [] dupes = [] for pid in paragraph_ids: raw_graph = extractor(pid, self.contents, exact=True) markup_graph = regdown.regdown(raw_graph) index_graph = strip_tags(markup_graph).strip() full_id = "{}-{}".format(section_tag, pid) graph, cr = SectionParagraph.objects.get_or_create( paragraph=index_graph, paragraph_id=pid, section=self) if cr: created += 1 else: if full_id in known_ids: dupes.append(full_id) else: known_ids.append(full_id) kept += 1 exclude_from_deletion.append(graph.pk) to_delete = SectionParagraph.objects.filter( section__subpart__version__part=part, section__label=self.label).exclude( pk__in=exclude_from_deletion) deleted += to_delete.count() for graph in to_delete: graph.delete() dupes = sorted(set(dupes)) return { 'section': section_tag, 'created': created, 'deleted': deleted, 'kept': kept, 'dupes': dupes, }
def section_page(self, request, date_str=None, section_label=None): """ Render a section of the currently effective regulation """ if date_str is not None: effective_version = self.get_effective_version(request, date_str) section_query = self.get_section_query( effective_version=effective_version ) else: section_query = self.get_section_query() sections = list(section_query.all()) section = section_query.get(label=section_label) current_index = sections.index(section) context = self.get_context(request, sections=sections) content = regdown( section.contents, url_resolver=get_url_resolver(self), contents_resolver=get_contents_resolver(self), render_block_reference=partial(self.render_interp, context) ) context.update({ 'version': self.regulation.effective_version, 'content': content, 'get_secondary_nav_items': partial( get_reg_nav_items, sections=sections, date_str=date_str ), 'next_section': get_next_section( sections, current_index), 'previous_section': get_previous_section( sections, current_index), 'section': section, 'breadcrumb_items': self.get_breadcrumbs(request, section), 'search_url': (self.get_parent().url + 'search-regulations/results/?regs=' + self.regulation.part_number) }) return TemplateResponse( request, self.template, context)
def render_interp(self, context, raw_contents, **kwargs): template = get_template('regulations3k/inline_interps.html') # Extract the title from the raw regdown section_title_match = re.search( r'#+\s?(?P<section_title>.*)\s', raw_contents ) if section_title_match is not None: context.update({ 'section_title': section_title_match.group(1) }) span = section_title_match.span() raw_contents = raw_contents[:span[0]] + raw_contents[span[1]:] context.update({'contents': regdown(raw_contents)}) context.update(kwargs) return template.render(context)
def section_page(self, request, date_str=None, section_label=None): """ Render a section of the currently effective regulation """ if date_str is not None: effective_version = self.get_effective_version(request, date_str) section_query = self.get_section_query( effective_version=effective_version) else: section_query = self.get_section_query() sections = list(section_query.all()) section = section_query.get(label=section_label) current_index = sections.index(section) context = self.get_context(request, sections=sections) content = regdown(section.contents, url_resolver=get_url_resolver(self), contents_resolver=get_contents_resolver(self), render_block_reference=partial( self.render_interp, context)) context.update({ 'version': self.regulation.effective_version, 'content': content, 'get_secondary_nav_items': partial(get_reg_nav_items, sections=sections, date_str=date_str), 'next_section': get_next_section(sections, current_index), 'previous_section': get_previous_section(sections, current_index), 'section': section, 'breadcrumb_items': self.get_breadcrumbs(request, section), 'search_url': (self.get_parent().url + 'search-regulations/results/?regs=' + self.regulation.part_number) }) return TemplateResponse(request, self.template, context)
def test_pseudo_form_field_inside_line(self): text = 'inline______fields______within paragraph' self.assertIn( 'inline<span class="regdown-form">______</span>' 'fields<span class="regdown-form">______</span>' 'within paragraph', regdown(text))
def test_block_reference_renderer_not_callable(self): text = 'see(foo-bar)' self.assertEqual(regdown(text, render_block_reference=None), '')
def test_block_reference_no_contents(self): contents_resolver = lambda l: '' text = 'see(foo-bar)' self.assertEqual(regdown(text, contents_resolver=contents_resolver), '')
def test_complicated_inline_interp_label(self): text = '{12-d-Interp-7-ii-c-A-7}\nThis is a paragraph with a label.' self.assertEqual( regdown(text), '<p class="regdown-block level-5" id="12-d-Interp-7-ii-c-A-7">' 'This is a paragraph with a label.</p>')
def test_even_deeper_inline_interp_label(self): text = '{1-a-Interp-1-i-a}\nThis is a paragraph with a label.' self.assertEqual( regdown(text), '<p class="regdown-block level-3" id="1-a-Interp-1-i-a">' 'This is a paragraph with a label.</p>')
def test_pseudo_form_field_end_of_line(self): text = 'Form field: ___' self.assertIn('Form field: ' '<span class="regdown-form_extend">___' '<span></span></span>', regdown(text))
def test_linebreak_label(self): text = '{my-label}\nThis is a paragraph with a label.' self.assertEqual( regdown(text), '<p class="regdown-block level-1" id="my-label">' 'This is a paragraph with a label.</p>')
def test_pseudo_form_field_start_of_line(self): text = '__Form Field' self.assertIn('<span class="regdown-form">__</span>Form Field', regdown(text))
def test_pseudo_form_field_end_of_line(self): text = 'Form field: ___' self.assertIn( 'Form field: ' '<span class="regdown-form_extend">___' '<span></span></span>', regdown(text))
def test_nolabel(self): text = 'This is a paragraph without a label.' self.assertEqual( regdown(text), '<p class="regdown-block" ' 'id="e2cb7f25f263e65fc6737e03e0ecb90382398da3966b6da734b451be">' 'This is a paragraph without a label.</p>')