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.

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

def condition(self, block):<br />

if not self.first: return False<br />

self.first = False<br />

return HeadingRule.condition(self, block)<br />

class ListItemRule(Rule):<br />

"""<br />

A list item is a paragraph that begins with a hyphen. As part of<br />

the formatting, the hyphen is removed.<br />

"""<br />

type = 'listitem'<br />

def condition(self, block):<br />

return block[0] == '-'<br />

def action(self, block, handler):<br />

handler.start(self.type)<br />

handler.feed(block[1:].strip())<br />

handler.end(self.type)<br />

return 1<br />

class ListRule(ListItemRule):<br />

"""<br />

A list begins between a block that is not a list item and a<br />

subsequent list item. It ends after the last consecutive list<br />

item.<br />

"""<br />

type = 'list'<br />

inside = False<br />

def condition(self, block):<br />

return True<br />

def action(self, block, handler):<br />

if not self.inside and ListItemRule.condition(self, block):<br />

handler.start(self.type)<br />

self.inside = True<br />

elif self.inside and not ListItemRule.condition(self, block):<br />

handler.end(self.type)<br />

self.inside = False<br />

return False<br />

class ParagraphRule(Rule):<br />

"""<br />

A paragraph is simply a block that isn't covered by any of the<br />

other rules.<br />

"""<br />

type = 'paragraph'<br />

def condition(self, block):<br />

return True

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

Saved successfully!

Ooh no, something went wrong!