zerver/lib/bugdown/fenced_code.py: Add BaseHandler.

Add a class 'BaseHandler' and make it a base class of OuterHandler,
QuoteHandler and CodeHandler.  This will help annotate some functions
and improve type checking.
This commit is contained in:
Eklavya Sharma 2016-06-16 21:34:53 +05:30 committed by Tim Abbott
parent 142bcadb68
commit 598fb1ff28
1 changed files with 14 additions and 6 deletions

View File

@ -126,16 +126,23 @@ class FencedBlockPreprocessor(markdown.preprocessors.Preprocessor):
output = [] # type: List[text_type]
class Record(object):
pass
class BaseHandler(object):
def handle_line(self, line):
# type: (text_type) -> None
raise NotImplementedError()
def done(self):
# type: () -> None
raise NotImplementedError()
processor = self
handlers = []
handlers = [] # type: List[BaseHandler]
def push(handler):
# type: (BaseHandler) -> None
handlers.append(handler)
def pop():
# type: () -> None
handlers.pop()
def check_for_new_fence(output, line):
@ -149,7 +156,7 @@ class FencedBlockPreprocessor(markdown.preprocessors.Preprocessor):
else:
output.append(line)
class OuterHandler(object):
class OuterHandler(BaseHandler):
def __init__(self, output):
# type: (MutableSequence[text_type]) -> None
self.output = output
@ -163,12 +170,13 @@ class FencedBlockPreprocessor(markdown.preprocessors.Preprocessor):
pop()
def generic_handler(output, fence, lang):
# type: (MutableSequence[text_type], text_type, text_type) -> BaseHandler
if lang in ('quote', 'quoted'):
return QuoteHandler(output, fence)
else:
return CodeHandler(output, fence, lang)
class QuoteHandler(object):
class QuoteHandler(BaseHandler):
def __init__(self, output, fence):
# type: (MutableSequence[text_type], text_type) -> None
self.output = output
@ -192,7 +200,7 @@ class FencedBlockPreprocessor(markdown.preprocessors.Preprocessor):
self.output.append('')
pop()
class CodeHandler(object):
class CodeHandler(BaseHandler):
def __init__(self, output, fence, lang):
# type: (MutableSequence[text_type], text_type, text_type) -> None
self.output = output