def test_unit_filter(): grepped_segments = main.main(Args("all", None, "structure", "true"), [ "Line 1", "Line 2", ]) assert sorted(grepped_segments) == sorted([ Expansion(0, 0, "Line 1"), Expansion(1, 0, "Line 2"), ])
def test_links_and_grep(): greppedlines = main.main(Args("links", "cat|dog", "structure", "true"), [ "# Heading 1", "[https://cat.cat](Wow I like felines)", "# Heading 2", "[https://dog.dog](Wow I like canines)notalink[https://reptiles.reptiles](wow lizards amiright)", ]) assert sorted(greppedlines) == sorted([ Expansion(1, 0, "[https://cat.cat](Wow I like felines)"), Expansion(3, 0, "[https://dog.dog](Wow I like canines)") ])
def test_headings_in_codeblock(): greppedlines = main.main(Args("headings", None, "structure", "true"), [ "# heading 1", "```", "# heading in code block", "```", "## heading 2", ]) assert sorted(greppedlines) == sorted([ Expansion(0, 0, "# heading 1"), Expansion(4, 0, "## heading 2"), ])
def under_heading_expansion(match: FoundSegment, filelines: List[str]) -> Optional[Expansion]: # Move up to the closet instance of the given heading level line_number = match.line_number while get_heading_level( line_number, filelines) != heading_level and line_number != 0: line_number -= 1 if line_number == 0 and get_heading_level(line_number, filelines) != heading_level: return None starting_line = line_number return_lines = [] return_lines.append( filelines[line_number] ) # The current line (a heading or the first line of the file) is always added line_number += 1 while line_number < len(filelines) and (get_heading_level( line_number, filelines) or heading_level) >= heading_level: return_lines.append(filelines[line_number]) line_number += 1 return Expansion(starting_line, 0, "\n".join(return_lines))
def test_section1_full_file(): text = [ "# Heading 1", "Line 1", "Line 2", "Line 3", "## Heading 2", "line 5", ] grep = main.main(Args("all", None, "section1", "true"), text) assert sorted(grep) == sorted([Expansion(0, 0, "\n".join(text))])
def test_structure_vs_line(): text = [ "# Heading 1", "[https://cat.cat](Wow I like felines) this part is after the link! [https://dog.dog](This is a second link!)" ] structure_grep = main.main(Args("links", None, "structure", "true"), text) line_grep = main.main(Args("links", None, "line", "true"), text) assert sorted(structure_grep) == sorted([ Expansion(1, 0, "[https://cat.cat](Wow I like felines)"), Expansion(1, 67, "[https://dog.dog](This is a second link!)") ]) assert sorted( line_grep ) == sorted([ # Only returns one result, since duplicate lines Expansion( 1, 0, "[https://cat.cat](Wow I like felines) this part is after the link! [https://dog.dog](This is a second link!)" ), ])
def test_section2_from_section_title(): text = [ "# Heading 1", "Line 1", "Line 2", "Line 3", "## Heading 2", "line 5", ] grep = main.main(Args("heading2", None, "section2", "true"), text) assert sorted(grep) == sorted( [Expansion(4, 0, "\n".join(["## Heading 2", "line 5"]))])
def test_codeblock_expansion(): codeblock = [ "This place is not a place of honour", "No highly esteemed deed is commemorated here" ] text = [ "# Heading", "Paragraph", "```poem", *codeblock, "```", "Ending paragraph", ] print("\n".join(text)) grep = main.main(Args("insidecodeblock", "honour", "codeblock", "false"), text) assert len(grep) == 1 assert grep[0] == Expansion(3, 0, "\n".join(codeblock))
def codeblock_expansion(match: FoundSegment, filelines: List[str]) -> Expansion: line_number = match.line_number while filelines[line_number][0:3] != "```": line_number -= 1 if line_number < 0: return None # No codeblock start is above this block line_number += 1 # Move under the codeblock start top_line = line_number while filelines[line_number][0:3] != "```": line_number += 1 if line_number >= len(filelines): return None # The codeblock never ends TODO: Throw error on invalid markdown line_number -= 1 # Move above the codeblock end bottom_line = line_number return Expansion(top_line, 0, "\n".join(filelines[top_line:bottom_line + 1]))
def structure_expansion(match: FoundSegment, filelines: List[str]) -> Expansion: return Expansion(match.line_number, match.line_position, match.text)
def line_expansion(match: FoundSegment, filelines: List[str]) -> Expansion: return Expansion(match.line_number, 0, filelines[match.line_number])