Skip to content

Commit 6c780d6

Browse files
committed
fixes, esp. with numbered section headings and config
1 parent 579a3a7 commit 6c780d6

4 files changed

Lines changed: 271 additions & 95 deletions

File tree

‎flm/counter.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,8 +480,8 @@ def __init__(self, format_num, prefix_display=None,
480480
'format_num',
481481
'prefix_display', 'delimiters', 'join_spec',
482482
'name_in_link', 'repeat_numprefix_in_range',
483-
'subnums_format_nums',
484483
'counter_formatter_id',
484+
'subnums_format_nums',
485485
)
486486

487487
def format_number(self, n, numprefix=None, subnums=None):

‎flm/feature/headings.py‎

Lines changed: 93 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
)
88

99
from .. import flmspecinfo
10-
from ..counter import build_counter_formatter
10+
from ..counter import build_counter_formatter, CounterFormatter
1111

1212
from ._base import Feature
1313

@@ -139,7 +139,7 @@ def recompose_pure_latex(self, node, recomposer):
139139
return s
140140

141141

142-
sec_default_counter_formatter_spec = {
142+
_default_counter_formatter_spec = {
143143
'format_num': { 'template': '${arabic}' },
144144
'prefix_display': {
145145
'singular': '§ ',
@@ -150,6 +150,49 @@ def recompose_pure_latex(self, node, recomposer):
150150
}
151151

152152

153+
_default_section_commands_by_level = {
154+
1: dict(cmdname=r"section"),
155+
2: dict(cmdname=r"subsection"),
156+
3: dict(cmdname=r"subsubsection"),
157+
4: dict(cmdname=r"paragraph", inline=True),
158+
5: dict(cmdname=r"subparagraph", inline=True),
159+
6: dict(cmdname=r"subsubparagraph", inline=True),
160+
}
161+
_default_section_numbering_by_level = {
162+
1: dict(
163+
counter_formatter={'template': '${arabic}'},
164+
numprefix=None,
165+
heading_joiner='. '
166+
),
167+
2: dict(
168+
counter_formatter={'template': '${arabic}'},
169+
numprefix='${section}.',
170+
heading_joiner='. '
171+
),
172+
3: dict(
173+
counter_formatter={'template': '${arabic}'},
174+
numprefix='${subsection}.',
175+
heading_joiner='. '
176+
),
177+
4: dict(
178+
counter_formatter={'template': '${alph}'},
179+
numprefix=None,
180+
heading_joiner='. '
181+
),
182+
5: dict(
183+
counter_formatter={'template': '${alph}'},
184+
numprefix='${paragraph}.',
185+
heading_joiner='. '
186+
),
187+
6: dict(
188+
counter_formatter={'template': '${alph}'},
189+
numprefix='${subparagraph}.',
190+
heading_joiner='. '
191+
),
192+
}
193+
194+
195+
153196
class FeatureHeadings(Feature):
154197
r"""
155198
Add support for headings via LaTeX commands, including ``\section``,
@@ -186,27 +229,32 @@ def initialize(
186229
else:
187230
self.section_numbering_by_level = self.feature.section_numbering_by_level
188231

232+
logger.debug(
233+
"Initializing FeatureHeadings.RenderManager; using:\n"
234+
" numbering_section_depth=%r\n"
235+
" section_commands_by_level=%r\n"
236+
" section_numbering_by_level=%r\n",
237+
self.numbering_section_depth,
238+
self.feature.section_commands_by_level,
239+
self.section_numbering_by_level
240+
)
241+
189242
if counter_formatter is not None:
190243
self.counter_formatter = build_counter_formatter(
191244
counter_formatter,
192-
sec_default_counter_formatter_spec,
245+
_default_counter_formatter_spec,
193246
counter_formatter_id='section',
194247
)
195248
else:
196249
self.counter_formatter = self.feature.counter_formatter
197250

198-
logger.debug(
199-
"Initialize FeatureHeadings.RenderManager; using:\n"
200-
" numbering_section_depth=%r\n"
201-
" section_commands_by_level=%r\n"
202-
" section_numbering_by_level=%r\n"
203-
" counter_formatter=%r",
204-
self.numbering_section_depth, self.feature.section_commands_by_level,
205-
self.section_numbering_by_level, self.counter_formatter)
206-
207251
self.section_counter_ifaces = {}
208-
last_counter_name = None
252+
209253
if self.numbering_section_depth is not False:
254+
255+
last_counter_name = None
256+
base_counter_formatter_spec = self.counter_formatter.asdict()
257+
210258
for j in sorted(self.section_numbering_by_level.keys(), key=int):
211259
if (self.numbering_section_depth is not True
212260
and j > self.numbering_section_depth):
@@ -232,18 +280,30 @@ def initialize(
232280
'reset_at': number_within_reset_at,
233281
'numprefix': numbering_info.numprefix,
234282
}
283+
284+
# Prepare the counter_formatter for this section level.
285+
sec_counter_formatter = build_counter_formatter(
286+
numbering_info.counter_formatter,
287+
base_counter_formatter_spec,
288+
# for hyper-references, etc.: keep 'section' for all
289+
# levels for internal counter formatter id.
290+
counter_formatter_id=self.counter_formatter.counter_formatter_id,
291+
)
292+
235293
counter_iface = numbering.get_document_render_counter(
236-
self.render_context, counter_name, self.counter_formatter,
294+
self.render_context, counter_name, sec_counter_formatter,
237295
always_number_within=always_number_within
238296
)
239297
self.section_counter_ifaces[j] = counter_iface
298+
240299
last_counter_name = counter_name
241-
300+
242301
logger.debug(
243302
"Set up counter interfaces: %r",
244303
self.section_counter_ifaces
245304
)
246305

306+
247307
def new_heading(self, node, heading_level,
248308
labels, heading_content_nodelist,
249309
skip_numbering=False,
@@ -374,64 +434,36 @@ class SectionNumberingInfo:
374434
the parent counter is determined automatically (no parent counter for
375435
top-level heading and last numbered heading level for sub-headings).
376436
"""
377-
def __init__(self, format_num, numprefix=None, heading_joiner=' ',
378-
number_within_reset_at=True):
437+
def __init__(self,
438+
numprefix=None,
439+
heading_joiner=' ',
440+
number_within_reset_at=True,
441+
counter_formatter=None,):
379442
super().__init__()
380-
self.format_num = format_num
381443
self.numprefix = numprefix
382444
self.heading_joiner = heading_joiner
383445
self.number_within_reset_at = number_within_reset_at
446+
self.counter_formatter = counter_formatter
384447

385448
def __repr__(self):
386449
return (
387-
f"{self.__class__.__name__}(format_num={repr(self.format_num)}, "
450+
f"{self.__class__.__name__}("
388451
f"numprefix={repr(self.numprefix)}, "
389452
f"heading_joiner={repr(self.heading_joiner)}, "
390453
f"number_within_reset_at={repr(self.number_within_reset_at)}"
454+
f"counter_formatter={repr(self.counter_formatter)}"
455+
f")"
391456
)
392457

393-
394458
feature_default_config = {
395-
'counter_formatter': sec_default_counter_formatter_spec,
459+
'counter_formatter': _default_counter_formatter_spec,
396460
'section_commands_by_level': {
397-
1: dict(cmdname=r"section"),
398-
2: dict(cmdname=r"subsection"),
399-
3: dict(cmdname=r"subsubsection"),
400-
4: dict(cmdname=r"paragraph", inline=True),
401-
5: dict(cmdname=r"subparagraph", inline=True),
402-
6: dict(cmdname=r"subsubparagraph", inline=True),
461+
'$no-merge': True,
462+
**_default_section_commands_by_level
403463
},
404464
'section_numbering_by_level': {
405-
1: dict(
406-
format_num={'template': '${arabic}'},
407-
numprefix=None,
408-
heading_joiner='. '
409-
),
410-
2: dict(
411-
format_num={'template': '${arabic}'},
412-
numprefix='${section}.',
413-
heading_joiner='. '
414-
),
415-
3: dict(
416-
format_num={'template': '${arabic}'},
417-
numprefix='${subsection}.',
418-
heading_joiner='. '
419-
),
420-
4: dict(
421-
format_num={'template': '${alph}'},
422-
numprefix=None,
423-
heading_joiner='. '
424-
),
425-
5: dict(
426-
format_num={'template': '${alph}'},
427-
numprefix='${paragraph}.',
428-
heading_joiner='. '
429-
),
430-
6: dict(
431-
format_num={'template': '${alph}'},
432-
numprefix='${subparagraph}.',
433-
heading_joiner='. '
434-
),
465+
'$no-merge': True,
466+
**_default_section_numbering_by_level
435467
}
436468
}
437469

@@ -445,15 +477,14 @@ def __init__(
445477
super().__init__()
446478

447479
if section_commands_by_level is None:
448-
section_commands_by_level = self.feature_default_config['section_commands_by_level']
480+
section_commands_by_level = _default_section_commands_by_level
449481
if section_numbering_by_level is None:
450-
section_numbering_by_level = \
451-
self.feature_default_config['section_numbering_by_level']
482+
section_numbering_by_level = _default_section_numbering_by_level
452483
if counter_formatter is None:
453-
counter_formatter = self.feature_default_config['counter_formatter']
484+
counter_formatter = _default_counter_formatter_spec
454485
counter_formatter = build_counter_formatter(
455486
counter_formatter,
456-
sec_default_counter_formatter_spec,
487+
_default_counter_formatter_spec,
457488
counter_formatter_id='section',
458489
)
459490
self.counter_formatter = counter_formatter

0 commit comments

Comments
 (0)