By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,275 Members | 1,745 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,275 IT Pros & Developers. It's quick & easy.

Pyparsing Question.

P: n/a
Ant
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,

Nov 22 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Ant wrote:
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
Nov 22 '06 #2

P: n/a
"Ant" <an****@gmail.comwrote in message
news:11**********************@f16g2000cwb.googlegr oups.com...
>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
Nov 22 '06 #3

P: n/a
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.
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,

Nov 23 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.