From: Waylan L. <wa...@gm...> - 2008-11-17 14:56:05
|
On Mon, Nov 17, 2008 at 2:17 AM, Yuri Takhteyev <qar...@gm...> wrote: >> Meh. I'v thought about this before and even wondered why you used >> classes back when I started my first extension, but the class gives us >> so much more. Look at the InlinePatterns. Many of them are subclasses >> that override only part of the functionality. In fact, a few don't >> even have a run method, they just override an auxiliary method. >> Actually, there are quite a few auxiliary methods that would now >> litter the markdown namespace unnecessarily. Using classes keeps >> things neat, compartmentalized, and DRY. > > Ok, a variation on this idea: each odict would be allowed to take as > entries any callable objects - either a function or an instance of a > callable class would do. We can stick with classes for our > implementations, but let the users extend the functionality by adding > either instances or functions. > That would be a simple change - just rename all 'run' methods to '__call__' in the code and adjust the calling code accordingly. I have some reservations[^1], but also see it as slightly more pythonic. Whatever. If we did do this, I would suggest adding a utility function to add a regex pattern to a function: def makePattern(regex, callable): return callable.pattern = regex That may not be exactly what we want, but you get the idea. Then, one could do: md.inlinePatterns.add("footnote", makePattern(FOOTNOTE_RE, handleFootnotePattern), "<reference") Of course, this would not be necessary for the callable classes (as they already do this internally), just functions. But, that way the odict would only ever contain one object each (no tuples), and the regex would still be mapped to the pattern/processor. [^1]: My concern is that some newcomers to python may see the callable classes as a higher barrier of entry as they aren't up on the interworkings of python. That also means that our documentation may have to spend at least a little time educating people about python itself. That said, it is the python way, so it can't be all that bad. -- ---- Waylan Limberg wa...@gm... |