Esempio n. 1
0
def numbered_bullet_without_paragraph():
    return pg.AllOf(
        pg.Ignore(digits),
        pg.Ignore("."),
        pg.Ignore(
            pg.OneOf(" ", "\t")),
        span_text)
Esempio n. 2
0
def bullet_without_paragraph():
    return pg.AllOf(
        pg.Ignore(
            pg.OneOf("*", "+", "-")),
        pg.Ignore(
            pg.OneOf(" ", "\t")),
        span_text)
Esempio n. 3
0
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
Esempio n. 4
0
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)
Esempio n. 5
0
def bullet_with_paragraph():
    return pg.AllOf(
        pg.Ignore(
            pg.OneOf("*", "+", "-")),
        pg.Ignore(
            pg.OneOf(" ", "\t")),
        paragraph)
Esempio n. 6
0
def code():
    return pg.AllOf(
        pg.Ignore("`"),
        pg.Join(
            pg.Many(
                pg.Not("`"))),
        pg.Ignore("`"))
Esempio n. 7
0
def test_match_indented_nested_bullets():
    bullet = pg.NamedPattern(
        'bullet',
        pg.AllOf(
            pg.Ignore(
                pg.Optional(
                    pg.Many("\n"))),
            pg.Ignore("* "),
            pg.Words()))

    @pg.lazy
    def indented_bullets():
        return pg.Indented(
            pg.AllOf(
                bullet,
                pg.Optional(
                    indented_bullets)),
            optional=True)

    data = """
* Line One
* Line Two
"""

    expected = [
        'indented_bullets',
        ['bullet', "Line One"],
        ['indented_bullets',
         ['bullet', "Line Two"]]]

    match, rest = indented_bullets(data, 'indented_bullets')
    assert match == expected
    assert rest == "\n"
Esempio n. 8
0
def link_url():
    return pg.AllOf(
        pg.Ignore("("),
        pg.Join(
            pg.Many(
                pg.Not(")"))),
        pg.Ignore(")"))
Esempio n. 9
0
def test_match_insert():
    insert_a = pg.Insert("a")

    expected = ['insert_a', "a"]

    match, rest = insert_a("", 'insert_a')
    assert match == expected
    assert rest == ""

    joined_lines = pg.AllOf(
        pg.Ignore("\n"),
        pg.Words(),
        pg.Insert(" : "),
        pg.Ignore("\n"),
        pg.Words())

    data = """
flamble
floosit"""

    expected = [
        "joined_lines",
        'flamble',
        " : ",
        'floosit']

    match, rest = joined_lines(data, 'joined_lines')
    assert match == expected
    assert rest == ""
Esempio n. 10
0
def link_text():
    return pg.AllOf(
        pg.Ignore("["),
        pg.Join(
            pg.Many(
                pg.Not("]"))),
        pg.Ignore("]"))
Esempio n. 11
0
def attribute_list():
    return pg.AllOf(
        pg.Ignore("("), attribute,
        pg.Optional(
            pg.Many(
                pg.AllOf(pg.Ignore(","), pg.Ignore(pg.Optional(" ")),
                         attribute))), pg.Ignore(")"))
Esempio n. 12
0
def document():
    return pg.AllOf(
        pg.Optional(pg.Ignore(newlines_or_eof)), pg.Optional(doctype),
        pg.OneOf(
            extends,
            pg.Many(
                pg.AllOf(pg.OneOf(element, code_block, comment),
                         pg.Optional(pg.Ignore(pg.Many("\n")))))))
Esempio n. 13
0
def title_level_2():
    return pg.AllOf(
        pg.Ignore("## "),
        pg.Words(),
        pg.Ignore(
            pg.AllOf(
                pg.Optional(" "),
                pg.Optional("##"),
                "\n")))
Esempio n. 14
0
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"]
Esempio n. 15
0
def test_match_one_of_empty():
    b1 = pg.Ignore("*")
    b2 = pg.Ignore("-")

    bullet = pg.OneOf(b1, b2)

    data = "*"
    expected = ['bullet', ""]
    match, rest = pg.match_one_of(data, bullet, 'bullet')
    assert match == expected
    assert rest == ""
Esempio n. 16
0
def test_match_ignore():
    ignore_a = pg.Ignore("a")

    match, rest = pg.match_ignore("a", ignore_a, "ignore_a")
    assert match == []
    assert rest == ""

    ignore_a = pg.Ignore(pg.AllOf(pg.Optional("#"), "abc"))

    match, rest = pg.match_ignore("#abc", ignore_a, "ignore_a")
    assert match == []
    assert rest == ""

    with py.test.raises(pg.NoPatternFound):
        match, rest = pg.match_ignore("123", ignore_a, "ignore_a")
Esempio n. 17
0
def test_match_one_of():
    asterix = pg.Ignore("*")

    emphasis = pg.AllOf(
        asterix,
        pg.Many(pg.Not("*")),
        asterix)

    phrase = pg.OneOf(
            pg.Words(),
            emphasis)

    expected = ['phrase', "b", "o", "l", "d"]
    match, rest = phrase("*bold*", "phrase")
    assert match == expected
    assert rest == ""

    with py.test.raises(pg.NoPatternFound):
        match, rest = phrase("123", "phrase")

    match, rest = phrase("text", "phrase")
    assert match == ['phrase', "text"]
    assert rest == ""

    # Test match with no name
    match, rest = phrase("text", "")
    assert match == ['', "text"]
    assert rest == ""
Esempio n. 18
0
def test_match_optional():
    optional_a = pg.Optional("a")

    match, rest = optional_a("abc", 'optional_a')
    assert match == ['', "a"]
    assert rest == "bc"

    match, rest = optional_a("bc", 'optional_a')
    assert match == []
    assert rest == "bc"

    letter_a = pg.NamedPattern(
        'letter_a',
        "a")

    optional_a = pg.Optional(letter_a)
    match, rest = optional_a("abc", 'optional_a')
    assert match == ['letter_a', "a"]
    assert rest == "bc"

    optional_ignore = pg.Optional(pg.Ignore("a"))

    match, rest = optional_ignore("abc", 'optional_ignore')
    assert match == []
    assert rest == "bc"
Esempio n. 19
0
def test_indented_bullet():
    paragraph = pg.NamedPattern(
        'paragraph',
        pg.AllOf(
            pg.Ignore(pg.Optional("\n")),
            pg.Words()))

    indented_paragraphs =  pg.Indented(
        pg.Many(paragraph),
        initial_indent="*   ")

    data = """
*   Paragraph One
    Paragraph Two
""".strip()

    expected = [
        'indented_paragraphs',
        ['paragraph',
         "Paragraph One"],
        ['paragraph',
         "Paragraph Two"]]

    match, rest = indented_paragraphs(data, "indented_paragraphs")
    assert match == expected
Esempio n. 20
0
 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))
Esempio n. 21
0
def test_get_current_indentation_initial_indent_with_tabs():
    indented_text = pg.Indented(pg.Words(),
                                initial_indent=pg.AllOf(pg.Ignore("*"), "\t"))

    data = "*\tfoo"
    expected = ("\t", "\tfoo")
    result = pg._get_current_indentation(data, indented_text)
    assert expected == result
Esempio n. 22
0
def test_match_all_of():
    letter_a = pg.NamedPattern(
        'letter_a',
        "a")

    letter_b = pg.NamedPattern(
        'letter_b',
        "b")

    word_ab = pg.AllOf(letter_a, letter_b)

    match, rest = word_ab("ab", "word_ab")
    assert match == ['word_ab', ['letter_a', "a"], ['letter_b', "b"]]
    assert rest == ""

    word_abc = pg.AllOf(letter_a, pg.Words())

    expected = ['word_ab', ['letter_a', "a"], "bc"]
    match, rest = word_abc("abc", "word_ab")
    assert match == expected
    assert rest == ""

    match, rest = word_abc("abc!", "")
    assert match == ['', ['letter_a', "a"], "bc"]
    assert rest == "!"

    emphasis = pg.AllOf(
        pg.Ignore("*"),
        pg.Words(),
        pg.Ignore("*"))

    match, rest = emphasis("*abc*", "emphasis")
    assert match == ['emphasis', "abc"]

    with py.test.raises(pg.NoPatternFound):
        result = word_ab("cab", "word_ab")

    ignore_ab = pg.AllOf(
        pg.Ignore("a"),
        pg.Ignore("b"))

    match, rest = ignore_ab("ab", "ignore_ab")
    assert match == ['ignore_ab', ""]
    assert rest == ""
Esempio n. 23
0
def test_reprs():
    # Pattern matchers
    assert repr(pg.Some("a")) == "<Some pattern='a'>"
    assert repr(pg.Ignore("#")) == "<Ignore pattern=<Text pattern='#'>>"
    assert repr(pg.Not("#")) == "<Not pattern=<Text pattern='#'>>"
    assert repr(pg.Optional("#")) == "<Optional pattern=<Text pattern='#'>>"

    # Option matchers
    assert repr(pg.OneOf("abc", pg.Not("#"))) == "<OneOf options=(<Text pattern='abc'>, <Not pattern=<Text pattern='#'>>)>"
    assert repr(pg.Many("abc", pg.Not("#"))) == "<Many options=(<Text pattern='abc'>, <Not pattern=<Text pattern='#'>>)>"
    assert repr(pg.Words()) == "<Words letters='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz .,'>"
Esempio n. 24
0
def numbered_bullet_with_paragraph():
    return pg.Indented(
        pg.AllOf(
            paragraph,
            pg.Optional(
                pg.AllOf(
                    linebreaks,
                    paragraph))),
        initial_indent=pg.AllOf(
            pg.Optional(
                linebreaks),
            pg.OneOf(
                pg.AllOf(
                    pg.Ignore(digits),
                    pg.Ignore("."),
                    pg.Many('\t')),
                pg.AllOf(
                    digits,
                    ".",
                    " "))))
Esempio n. 25
0
def test_match_many_empty():
    "Test that empty matches don't raise NoPatternFound"
    linebreaks = pg.Many(
        pg.OneOf(
            pg.Ignore(
                "\n")))

    data = "\n\n"
    expected = ['linebreaks', ""]
    match, rest = linebreaks(data, 'linebreaks')
    assert match == expected
    assert rest == ""
Esempio n. 26
0
def test_match_many_complex():
    emphasis = pg.NamedPattern(
        'emphasis',
        pg.AllOf(
            pg.Ignore("*"),
            pg.Words(),
            pg.Ignore("*")))

    words = pg.NamedPattern(
        'words',
        pg.Words())

    body = pg.Many(emphasis, words)

    expected = [
        'body',
        ['words', 'a phrase with '],
         ['emphasis', "bold words"],
         ['words', " in it"]]
    match, rest = body("a phrase with *bold words* in it", 'body')
    assert match == expected
    assert rest == ""
Esempio n. 27
0
 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))
Esempio n. 28
0
def _unordered_list_template(bullet_type, spacing):
    return pg.Indented(
        pg.AllOf(
            bullet_type,
            pg.Many(
                pg.AllOf(
                    pg.Ignore(
                        spacing),
                    pg.OneOf(
                        bullet_type,
                        unordered_list,
                        ordered_list)))),
        optional=True)
Esempio n. 29
0
def horizontal_rule():
    return pg.AllOf(
        pg.OneOf(
            "---",
            "___",
            "***",
            "- - -",
            "_ _ _",
            "* * *"),
        pg.Optional(
            pg.Ignore(
                pg.Many(
                    pg.Not("\n")))))
Esempio n. 30
0
def test_match_all_of():
    def letter_a():
        return "a"

    def letter_b():
        return "b"

    word_ab = pg.AllOf(letter_a, letter_b)

    match, rest = pg.match_all_of("ab", word_ab, "word_ab")
    assert match == ['word_ab', ['letter_a', "a"], ['letter_b', "b"]]
    assert rest == ""

    word_abc = pg.AllOf(letter_a, pg.Words())

    expected = ['word_ab', ['letter_a', "a"], "bc"]
    match, rest = pg.match_all_of("abc", word_abc, "word_ab")
    assert match == expected
    assert rest == ""

    match, rest = pg.match_all_of("abc!", word_abc, "")
    assert match == ['', ['letter_a', "a"], "bc"]
    assert rest == "!"

    emphasis = pg.AllOf(lambda: pg.Ignore("*"), lambda: pg.Words(),
                        lambda: pg.Ignore("*"))

    match, rest = pg.match_all_of("*abc*", emphasis, "emphasis")
    assert match == ['emphasis', "abc"]

    with py.test.raises(pg.NoPatternFound):
        result = pg.match_all_of("cab", word_ab, "word_ab")

    ignore_ab = pg.AllOf(pg.Ignore("a"), pg.Ignore("b"))

    match, rest = pg.match_all_of("ab", ignore_ab, "ignore_ab")
    assert match == ['ignore_ab', ""]
    assert rest == ""