class Handler: """ An object that handles method calls from the Parser. The Parser will call the start() and end() methods at the beginning of each block, with the proper block name as a parameter. The sub() method will be used in regular expression substitution. When called with a name such as 'emphasis', it will return a proper substitution function. """ def callback(self, prefix, name, *args): method = getattr(self, prefix + name, None) if callable(method): return method(*args) def start(self, name): self.callback('start_', name) def end(self, name): self.callback('end_', name) def sub(self, name): def substitution(match): result = self.callback('sub_', name, match) if result is None: match.group(0) return result return substitution class HTMLRenderer(Handler): """ A specific handler used for rendering HTML. The methods in HTMLRenderer are accessed from the superclass Handler's start(), end(), and sub() methods. They implement basic markup as used in HTML documents. """ def start_document(self): print('
') def end_paragraph(self): print('
') def start_heading(self): print('