|
@ -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 typing import Union, Callable |
|
|
from types import ModuleType |
|
|
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 |
|
|
# This class is basically an extension to panflute's doc, this is why metadata |
|
|
# is read directly from it. |
|
|
# 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: |
|
|
class Context: |
|
|
parent: Union["Context", None] |
|
|
parent: Union["Context", None] |
|
|
_commands: dict[str, Union[CommandCallable, None]] |
|
|
_commands: dict[str, Union[CommandCallable, None]] |
|
@ -29,6 +37,9 @@ class Context: |
|
|
dir: str |
|
|
dir: str |
|
|
filename: 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): |
|
|
def __init__(self, doc: Doc, path: str, parent: Union['Context', None]=None, trusted: bool=True): |
|
|
self.parent = parent |
|
|
self.parent = parent |
|
|
self._commands = {} |
|
|
self._commands = {} |
|
@ -39,6 +50,8 @@ class Context: |
|
|
self.filename = os.path.basename(path) |
|
|
self.filename = os.path.basename(path) |
|
|
if self.get_metadata("flags", immediate=True) is None: |
|
|
if self.get_metadata("flags", immediate=True) is None: |
|
|
self.set_metadata("flags", {}) |
|
|
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]: |
|
|
def get_command(self, command: str) -> Union[CommandCallable, None]: |
|
|
if command in self._commands: |
|
|
if command in self._commands: |
|
|