|
|
@ -1,4 +1,4 @@ |
|
|
|
from panflute import Doc, Element, Div, Span |
|
|
|
from panflute import Doc, Element, Div, Span, Header |
|
|
|
|
|
|
|
from typing import Union, Callable |
|
|
|
from types import ModuleType |
|
|
@ -20,6 +20,14 @@ CommandCallable = Callable[[Command, 'Context'], list[Element]] # This is here b |
|
|
|
# |
|
|
|
# This class is basically an extension to panflute's doc, this is why metadata |
|
|
|
# is read directly from it. |
|
|
|
def default_number_generator(e: Header, context: 'Context') -> str: |
|
|
|
l = e.level |
|
|
|
context.section_counters[l-1] += 1 |
|
|
|
for i in range(l, len(context.section_counters)): |
|
|
|
context.section_counters[i] = 0 |
|
|
|
return ".".join(map(str, context.section_counters[:l])) |
|
|
|
|
|
|
|
|
|
|
|
class Context: |
|
|
|
parent: Union["Context", None] |
|
|
|
_commands: dict[str, Union[CommandCallable, None]] |
|
|
@ -29,6 +37,9 @@ class Context: |
|
|
|
dir: str |
|
|
|
filename: str |
|
|
|
|
|
|
|
section_counters: list[int] |
|
|
|
number_generator: Callable[[Header, 'Context'], str] |
|
|
|
|
|
|
|
def __init__(self, doc: Doc, path: str, parent: Union['Context', None]=None, trusted: bool=True): |
|
|
|
self.parent = parent |
|
|
|
self._commands = {} |
|
|
@ -39,6 +50,8 @@ class Context: |
|
|
|
self.filename = os.path.basename(path) |
|
|
|
if self.get_metadata("flags", immediate=True) is None: |
|
|
|
self.set_metadata("flags", {}) |
|
|
|
self.number_generator = default_number_generator |
|
|
|
self.section_counters = [0 for i in range(6)] |
|
|
|
|
|
|
|
def get_command(self, command: str) -> Union[CommandCallable, None]: |
|
|
|
if command in self._commands: |
|
|
|