16.01.2014 Views

Beginning Python - From Novice to Professional

Beginning Python - From Novice to Professional

Beginning Python - From Novice to Professional

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

408 CHAPTER 20 ■ PROJECT 1: INSTANT MARKUP<br />

self.addFilter(r'\*(.+?)\*', 'emphasis')<br />

self.addFilter(r'(http://[\.a-zA-Z/]+)', 'url')<br />

self.addFilter(r'([\.a-zA-Z]+@[\.a-zA-Z]+[a-zA-Z]+)', 'mail')<br />

handler = HTMLRenderer()<br />

parser = BasicTextParser(handler)<br />

parser.parse(sys.stdin)<br />

You can see the result of running the program on the sample text in Figure 20-2.<br />

Figure 20-2. The second attempt at generating a Web page<br />

The second implementation is clearly more complicated and extensive than the first<br />

version. The added complexity is well worth the effort because the resulting program is much<br />

more flexible and extensible. Adapting it <strong>to</strong> new input and output formats is merely a matter of<br />

subclassing and initializing the existing classes, rather than rewriting everything from scratch,<br />

as you would have had <strong>to</strong> do in the first pro<strong>to</strong>type.<br />

Further Exploration<br />

Several expansions are possible for this program. Here are some possibilities:<br />

• Add support for tables. Find all aligning left word borders and split the block in<strong>to</strong> columns.<br />

• Add support for interpreting all uppercase words as emphasis. (To do this properly, you will<br />

need <strong>to</strong> take in<strong>to</strong> account acronyms, punctuations, names, and other capitalized words.)

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!