def test_parse_one_of(): emphasis = pg.NamedPattern( 'emphasis', pg.AllOf( pg.Ignore("*"), pg.Words(), pg.Ignore("*"))) words = pg.NamedPattern( 'words', pg.Words()) phrase = pg.NamedPattern( 'phrase', pg.OneOf( words, emphasis)) expected = ['phrase', ['emphasis', "bold words"]] result = pg.parse_string("*bold words*", phrase) assert result == expected expected = ['phrase', ['words', "normal words"]] result = pg.parse_string("normal words", phrase) assert result == expected
def test_parse_many(): emphasis = pg.NamedPattern( 'emphasis', pg.AllOf( pg.Ignore("*"), pg.Words(), pg.Ignore("*"))) words = pg.NamedPattern( 'words', pg.Words()) phrase = pg.NamedPattern( 'phrase', pg.OneOf( words, emphasis)) body = pg.NamedPattern( 'body', pg.Many(phrase)) expected = ['body', ['phrase', ['words', 'a phrase with ']], ['phrase', ['emphasis', "bold words"]], ['phrase', ['words', " in it"]]] result = pg.parse_string("a phrase with *bold words* in it", body) assert result == expected with py.test.raises(pg.NoPatternFound): result = pg.parse_string("123", body)
def test_parse_many(): def emphasis(): return pg.AllOf( lambda: pg.Ignore("*"), lambda: pg.Words(), lambda: pg.Ignore("*")) def words(): return pg.Words() def phrase(): return pg.OneOf( words, emphasis) def body(): return pg.Many(phrase) expected = ['body', ['phrase', ['words', 'a phrase with ']], ['phrase', ['emphasis', "bold words"]], ['phrase', ['words', " in it"]]] result = pg.parse_string("a phrase with *bold words* in it", body) assert result == expected with py.test.raises(pg.NoPatternFound): result = pg.parse_string("123", body)
def test_parse_many(): def emphasis(): return pg.AllOf(lambda: pg.Ignore("*"), lambda: pg.Words(), lambda: pg.Ignore("*")) def words(): return pg.Words() def phrase(): return pg.OneOf(words, emphasis) def body(): return pg.Many(phrase) expected = [ 'body', ['phrase', ['words', 'a phrase with ']], ['phrase', ['emphasis', "bold words"]], ['phrase', ['words', " in it"]] ] result = pg.parse_string("a phrase with *bold words* in it", body) assert result == expected with py.test.raises(pg.NoPatternFound): result = pg.parse_string("123", body)
def test_escaped(): html_text = pg.NamedPattern( 'html_text', pg.Many( pg.Words( pg.Words.letters+"</>"))) escaped_text = pg.NamedPattern( 'escaped_text', pg.Escaped(html_text)) data = """<p>Some Text</p>""" expected = [ 'escaped_text', ['html_text', "<p>Some Text</p>"]] result = pg.parse_string(data, escaped_text) assert expected == result html_text = pg.Many( pg.Words( pg.Words.letters+"</>")) escaped_text = pg.NamedPattern( 'escaped_text', pg.Escaped(html_text)) expected = [ 'escaped_text', "<p>Some Text</p>"] result = pg.parse_string(data, escaped_text) assert expected == result
def test_parse_string_b(): letter_b = pg.NamedPattern("letter_b", "b") result = pg.parse_string("b", letter_b) assert result == ['letter_b', "b"] with py.test.raises(pg.NoPatternFound): result = pg.parse_string("c", letter_b)
def test_not(): def not_a(): return pg.Not("a") result = pg.parse_string("bc", not_a) assert result == ['not_a', 'b'] with py.test.raises(pg.NoPatternFound): result = pg.parse_string("a", not_a)
def test_parse_string_b(): def letter_b(): return "b" result = pg.parse_string("b", letter_b) assert result == ['letter_b', "b"] with py.test.raises(pg.NoPatternFound): result = pg.parse_string("c", letter_b)
def test_not(): not_a = pg.NamedPattern( 'not_a', pg.Not("a")) result = pg.parse_string("bc", not_a) assert result == ['not_a', 'b'] with py.test.raises(pg.NoPatternFound): result = pg.parse_string("a", not_a)
def test_parse_string_a(): letter_a = pg.NamedPattern("letter_a", "a") result = pg.parse_string("a", letter_a) assert result == ['letter_a', "a"] result = pg.parse_string("ab", letter_a) assert result == ['letter_a', "a"] with py.test.raises(pg.NoPatternFound): result = pg.parse_string("c", letter_a)
def test_parse_string_some_a(): def word_a(): return pg.Some('a') result = pg.parse_string("aa", word_a) assert result == ['word_a', "aa"] result = pg.parse_string("aaa", word_a) assert result == ['word_a', "aaa"] with py.test.raises(pg.NoPatternFound): result = pg.parse_string("caa", word_a)
def test_parse_string_a(): def letter_a(): return "a" result = pg.parse_string("a", letter_a) assert result == ['letter_a', "a"] result = pg.parse_string("ab", letter_a) assert result == ['letter_a', "a"] with py.test.raises(pg.NoPatternFound): result = pg.parse_string("c", letter_a)
def test_indented(): def list_item(): return pg.AllOf(pg.Ignore(pg.Optional(pg.Many("\n"))), pg.Ignore("* "), pg.Words()) def nested_list(): return pg.AllOf( pg.Ignore(pg.Optional(pg.Many("\n"))), pg.Indented(pg.AllOf(list_item, pg.Optional(pg.Many(list_item, nested_list))), optional=True)) data = """ * A bullet """ expected = ['nested_list', ['list_item', "A bullet"]] result = pg.parse_string(data, nested_list) assert expected == result data = """ * A bullet * A bullet in a sublist """ expected = [ 'nested_list', ['list_item', "A bullet"], ['nested_list', ['list_item', "A bullet in a sublist"]] ] result = pg.parse_string(data, nested_list) assert expected == result data = """ * A bullet * A bullet in a sublist * Another bullet in a sublist * Another bullet in the first list """ expected = [ 'nested_list', ['list_item', "A bullet"], [ 'nested_list', ['list_item', "A bullet in a sublist"], ['list_item', "Another bullet in a sublist"] ], ['list_item', "Another bullet in the first list"] ] result = pg.parse_string(data, nested_list) assert expected == result
def test_parse_words(): body = pg.NamedPattern( 'body', pg.Words()) result = pg.parse_string("The confused dog jumped over the fox", body) assert result == ['body', "The confused dog jumped over the fox"]
def test_parse_ignore(): def emphasis(): return pg.AllOf(lambda: pg.Ignore("*"), lambda: pg.Words(), lambda: pg.Ignore("*")) result = pg.parse_string("*bold words*", emphasis) assert result == ['emphasis', "bold words"]
def test_numbered_bullet(): def numbered_bullet(): return pg.AllOf(pg.Ignore(pg.Optional(pg.Many("\n"))), pg.Ignore(pg.Words(letters="1234567890")), pg.Ignore(". "), pg.Words()) def ordered_list(): return pg.AllOf( pg.Ignore(pg.Optional(pg.Many("\n"))), pg.Indented(pg.AllOf( numbered_bullet, pg.Optional(pg.Many(numbered_bullet, ordered_list))), optional=True)) data = """ 1. A numbered bullet 2. A bullet in a sublist 3. Another bullet in a sublist 4. Another bullet in the first list """ expected = [ 'ordered_list', ['numbered_bullet', "A numbered bullet"], [ 'ordered_list', ['numbered_bullet', "A bullet in a sublist"], ['numbered_bullet', "Another bullet in a sublist"] ], ['numbered_bullet', "Another bullet in the first list"] ] result = pg.parse_string(data, ordered_list) assert expected == result
def test_parse_string_some_aab(): word_a = pg.NamedPattern("word_a", pg.Some('a')) letter_b = pg.NamedPattern("letter_b", "b") word_aab = pg.NamedPattern( 'word_aab', pg.AllOf(word_a, letter_b)) expected = ['word_aab', ['word_a', "aa"], ['letter_b', "b"]] result = pg.parse_string("aab", word_aab) assert result == expected with py.test.raises(pg.NoPatternFound): result = pg.parse_string("caab", word_aab)
def test_parse_string_some_aab(): def word_a(): return pg.Some('a') def letter_b(): return "b" def word_aab(): return pg.AllOf(word_a, letter_b) expected = ['word_aab', ['word_a', "aa"], ['letter_b', "b"]] result = pg.parse_string("aab", word_aab) assert result == expected with py.test.raises(pg.NoPatternFound): result = pg.parse_string("caab", word_aab)
def test_parse_one_of(): def emphasis(): return pg.AllOf(lambda: pg.Ignore("*"), lambda: pg.Words(), lambda: pg.Ignore("*")) def words(): return pg.Words() def phrase(): return pg.OneOf(words, emphasis) expected = ['phrase', ['emphasis', "bold words"]] result = pg.parse_string("*bold words*", phrase) assert result == expected expected = ['phrase', ['words', "normal words"]] result = pg.parse_string("normal words", phrase) assert result == expected
def test_parse_ignore(): def emphasis(): return pg.AllOf( lambda: pg.Ignore("*"), lambda: pg.Words(), lambda: pg.Ignore("*")) result = pg.parse_string("*bold words*", emphasis) assert result == ['emphasis', "bold words"]
def test_parse_ignore(): emphasis = pg.NamedPattern( 'emphasis', pg.AllOf( pg.Ignore("*"), pg.Words(), pg.Ignore("*"))) result = pg.parse_string("*bold words*", emphasis) assert result == ['emphasis', "bold words"]
def test_optional(): def optional_a(): return pg.Optional("a") result = pg.parse_string("a", optional_a) expected = ['', "a"] assert expected == result def optional_a(): return pg.Optional("a") def letters(): return pg.Words() def body(): return pg.AllOf(optional_a, letters) result = pg.parse_string("abc", body) expected = ['body', "a", ['letters', "bc"]] assert expected == result
def test_parse_one_of(): def emphasis(): return pg.AllOf( lambda: pg.Ignore("*"), lambda: pg.Words(), lambda: pg.Ignore("*")) def words(): return pg.Words() def phrase(): return pg.OneOf( words, emphasis) expected = ['phrase', ['emphasis', "bold words"]] result = pg.parse_string("*bold words*", phrase) assert result == expected expected = ['phrase', ['words', "normal words"]] result = pg.parse_string("normal words", phrase) assert result == expected
def test_optional(): optional_a = pg.NamedPattern( 'optional_a', pg.Optional("a")) result = pg.parse_string("a", optional_a) expected = ['', "a"] assert expected == result letters = pg.NamedPattern( 'letters', pg.Words()) body = pg.NamedPattern( 'body', pg.AllOf(optional_a, letters)) result = pg.parse_string("abc", body) expected = [ 'body', "a", ['letters', "bc"]] assert expected == result
def test_optional(): def optional_a(): return pg.Optional("a") result = pg.parse_string("a", optional_a) expected = ['', "a"] assert expected == result def optional_a(): return pg.Optional("a") def letters(): return pg.Words() def body(): return pg.AllOf(optional_a, letters) result = pg.parse_string("abc", body) expected = [ 'body', "a", ['letters', "bc"]] assert expected == result
def test_numbered_bullet(): def numbered_bullet(): return pg.AllOf( pg.Ignore( pg.Optional( pg.Many("\n"))), pg.Ignore(pg.Words(letters="1234567890")), pg.Ignore(". "), pg.Words()) def ordered_list(): return pg.AllOf( pg.Ignore( pg.Optional( pg.Many("\n"))), pg.Indented( pg.AllOf( numbered_bullet, pg.Optional( pg.Many( numbered_bullet, ordered_list))), optional=True)) data = """ 1. A numbered bullet 2. A bullet in a sublist 3. Another bullet in a sublist 4. Another bullet in the first list """ expected = [ 'ordered_list', ['numbered_bullet', "A numbered bullet"], ['ordered_list', ['numbered_bullet', "A bullet in a sublist"], ['numbered_bullet', "Another bullet in a sublist"]], ['numbered_bullet', "Another bullet in the first list"]] result = pg.parse_string(data, ordered_list) assert expected == result
def test_unknown_matcher(): def unknown(): return 1 with py.test.raises(pg.UnknownMatcherType): result = pg.parse_string("", unknown)
def test_parse_words(): def body(): return pg.Words() result = pg.parse_string("The confused dog jumped over the fox", body) assert result == ['body', "The confused dog jumped over the fox"]
def test_indented(): list_item = pg.NamedPattern( 'list_item', pg.AllOf( pg.Ignore( pg.Optional( pg.Many("\n"))), pg.Ignore("* "), pg.Words())) @pg.lazy def nested_list(): return pg.AllOf( pg.Ignore( pg.Optional( pg.Many("\n"))), pg.Indented( pg.AllOf( list_item, pg.Optional( pg.Many( list_item, nested_list))), optional=True)) data = """ * A bullet """ expected = [ 'nested_list', ['list_item', "A bullet"]] result = pg.parse_string(data, nested_list) assert expected == result data = """ * A bullet * A bullet in a sublist """ expected = [ 'nested_list', ['list_item', "A bullet"], ['nested_list', ['list_item', "A bullet in a sublist"]]] result = pg.parse_string(data, nested_list) assert expected == result data = """ * A bullet * A bullet in a sublist * Another bullet in a sublist * Another bullet in the first list """ expected = [ 'nested_list', ['list_item', "A bullet"], ['nested_list', ['list_item', "A bullet in a sublist"], ['list_item', "Another bullet in a sublist"]], ['list_item', "Another bullet in the first list"]] result = pg.parse_string(data, nested_list) assert expected == result
def parse(text, pattern=body): if not text.endswith("\n\n"): text = text + "\n\n" return pg.parse_string(text, pattern)
def to_html(text, pattern=body): if not text.endswith("\n\n"): text = text + "\n\n" return htmlise(pg.parse_string(text, pattern)).strip()