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

View File

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