diff --git a/htmlcc/emitter/__init__.py b/htmlcc/emitter/__init__.py index daad541..5198572 100644 --- a/htmlcc/emitter/__init__.py +++ b/htmlcc/emitter/__init__.py @@ -45,6 +45,16 @@ class Emitter: raise NotImplementedError() + def start_block(self, block_name: str) -> None: + """Called at {% block ... %}""" + + self.emit('void ' + block_name + '(void) {') + + def end_block(self) -> None: + """Called at {% endblock %}""" + + self.emit('}') + def start_page(self, page_name: str) -> None: """Called at {% page ... %}""" diff --git a/htmlcc/parser.py b/htmlcc/parser.py index 13bcc10..8e7435a 100644 --- a/htmlcc/parser.py +++ b/htmlcc/parser.py @@ -27,7 +27,7 @@ class Parser: self.cur_line_contains_handlebars = False self.cur_line_contains_nonblank_literal = False self.cur_line_leading_ws = '' # Buffer for leading whitespace - self.in_page = False + self.in_html = False def parse(self) -> None: """Parse the entire file""" @@ -70,15 +70,24 @@ class Parser: command = s[len('{%'):-len('%}')].strip() - if command.startswith('page '): + if command.startswith('block '): + # {% block ... %} + block_name = command[len('block '):].strip() + self.emitter.start_block(block_name) + self.in_html = True + elif command == 'endblock': + # {% endblock %} + self.emitter.end_block() + self.in_html = False + elif command.startswith('page '): # {% page ... %} page_name = command[len('page '):].strip() self.emitter.start_page(page_name) - self.in_page = True + self.in_html = True elif command == 'endpage': # {% endpage %} self.emitter.end_page() - self.in_page = False + self.in_html = False else: raise SyntaxError(f'Unknown command "{command}"') @@ -134,7 +143,7 @@ class Parser: s = self.buffer[:stop_reading_idx] self.buffer = self.buffer[stop_reading_idx:] - if not self.in_page: + if not self.in_html: # Not in a page block! if s.isspace(): # Suppress whitespace outside page blocks