Pyparsing Question.

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</strong> and 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,
 
S

Stefan Behnel

Ant said:
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
 
P

Paul McGuire

Ant said:
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</strong> and 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
 
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,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top