Connecting Tech Pros Worldwide Forums | Help | Site Map

Pyparsing Question.

Ant
Guest
 
Posts: n/a
#1: Nov 22 '06
I have a home-grown Wiki that I created as an excercise, with it's own
wiki markup (actually just a clone of the Trac wiki markup). The wiki
text parser I wrote works nicely, but makes heavy use of regexes, tags
and stacks to parse the text. As such it is a bit of a mantainability
nightmare - adding new wiki constructs can be a bit painful.

So I thought I'd look into the pyparsing module, but can't find a
simple example of processing random text. For example, I want to parse
the following:

"Some random text and '''some bold text''' and some more random text"

into:

"Some random text and <strong>some bold text</strongand some more
random text"

I have the following as a starting point:

from pyparsing import *

def parse(text):
italics = QuotedString(quoteChar="''")

parser = Optional(italics)

parsed_text = parser.parseString(text)


print parse("Test this is '''bold''' but this is not.")

So if you could provide a bit of a starting point, I'd be grateful!

Cheers,


Stefan Behnel
Guest
 
Posts: n/a
#2: Nov 22 '06

re: Pyparsing Question.


Ant wrote:
Quote:
So I thought I'd look into the pyparsing module, but can't find a
simple example of processing random text.
Have you looked at the examples on the pyparsing web page?

Stefan
Paul McGuire
Guest
 
Posts: n/a
#3: Nov 22 '06

re: Pyparsing Question.


"Ant" <antroy@gmail.comwrote in message
news:1164188646.047093.182390@f16g2000cwb.googlegr oups.com...
Quote:
>I have a home-grown Wiki that I created as an excercise, with it's own
wiki markup (actually just a clone of the Trac wiki markup). The wiki
text parser I wrote works nicely, but makes heavy use of regexes, tags
and stacks to parse the text. As such it is a bit of a mantainability
nightmare - adding new wiki constructs can be a bit painful.
>
So I thought I'd look into the pyparsing module, but can't find a
simple example of processing random text. For example, I want to parse
the following:
>
"Some random text and '''some bold text''' and some more random text"
>
into:
>
"Some random text and <strong>some bold text</strongand some more
random text"
>
I have the following as a starting point:
>
from pyparsing import *
>
def parse(text):
italics = QuotedString(quoteChar="''")
>
parser = Optional(italics)
>
parsed_text = parser.parseString(text)
>
>
print parse("Test this is '''bold''' but this is not.")
>
So if you could provide a bit of a starting point, I'd be grateful!
>
Cheers,
>
Ant,

Welcome to pyparsing! The simplest way to implement a markup processor in
pyparsing is to define the grammar of the markup, attach a parse action to
each markup type to convert the original markup to the actual results, and
then use transformString to run through the input and do the conversion.
This discussion topic has some examples:
http://pyparsing.wikispaces.com/message/view/home/31853.

-- Paul


Ant
Guest
 
Posts: n/a
#4: Nov 23 '06

re: Pyparsing Question.


Quote:
Welcome to pyparsing! The simplest way to implement a markup processor in
pyparsing is to define the grammar of the markup, attach a parse action to
each markup type to convert the original markup to the actual results, and
then use transformString to run through the input and do the conversion.
This discussion topic has some examples:
http://pyparsing.wikispaces.com/message/view/home/31853.
Thanks for the pointers - I had a look through the examples on the
pyparsing website, but none seemed to show a simple example of this
kind of thing. The discussion topic you noted above is exactly the sort
of thing I was after!

Cheers,

Closed Thread


Similar Python bytes