|
@ -1,4 +1,4 @@ |
|
|
from panflute import Doc, Element, Div, Span, Header |
|
|
from panflute import Doc, Element, Div, Span, Header, Figure |
|
|
|
|
|
|
|
|
from typing import Union, Callable |
|
|
from typing import Union, Callable |
|
|
from types import ModuleType |
|
|
from types import ModuleType |
|
@ -19,12 +19,21 @@ CommandCallable = Callable[[Command, 'Context', 'NOPProcessor'], list[Element]] |
|
|
# |
|
|
# |
|
|
# 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: |
|
|
def default_section_number_generator(e: Header, context: 'Context') -> list[Union[str, int]]: |
|
|
l = e.level |
|
|
l = e.level |
|
|
context.section_counters[l-1] += 1 |
|
|
section_counters = context.get_data("section_counters") |
|
|
for i in range(l, len(context.section_counters)): |
|
|
section_counters[l-1] += 1 |
|
|
context.section_counters[i] = 0 |
|
|
for i in range(l, len(section_counters)): |
|
|
return ".".join(map(str, context.section_counters[:l])) |
|
|
section_counters[i] = 0 |
|
|
|
|
|
return list(section_counters[:l]) |
|
|
|
|
|
|
|
|
|
|
|
def default_figure_number_generator(e: Figure, context: 'Context') -> Union[str, int]: |
|
|
|
|
|
figure_type = e.attributes.get("type", "img") |
|
|
|
|
|
figure_counters = context.get_data("figure_counters") |
|
|
|
|
|
figure_counters.setdefault(figure_type, 0) |
|
|
|
|
|
figure_counters[figure_type] += 1 |
|
|
|
|
|
return figure_counters[figure_type] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Context: |
|
|
class Context: |
|
@ -58,8 +67,11 @@ class Context: |
|
|
self.add_dep(path) |
|
|
self.add_dep(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 |
|
|
if not parent: |
|
|
self.section_counters = [0 for i in range(6)] |
|
|
self.set_data('section_number_generator', default_section_number_generator) |
|
|
|
|
|
self.set_data('figure_number_generator', default_figure_number_generator) |
|
|
|
|
|
self.set_data('section_counters', [0 for i in range(6)]) |
|
|
|
|
|
self.set_data('figure_counters', {}) |
|
|
|
|
|
|
|
|
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: |
|
|