From a900c42fa33b6013ee8d1b3bb778096a6e505105 Mon Sep 17 00:00:00 2001 From: Christophe Demko Date: Fri, 15 Apr 2016 19:22:33 +0200 Subject: [PATCH] Use sectioning in metadata and + instead of # in sectioning --- pandoc_numbering.py | 24 ++++++++--------- tests/test_numbering.py | 55 +++++++-------------------------------- tests/test_referencing.py | 4 +-- 3 files changed, 23 insertions(+), 60 deletions(-) diff --git a/pandoc_numbering.py b/pandoc_numbering.py index dba3406..5c4c2d4 100644 --- a/pandoc_numbering.py +++ b/pandoc_numbering.py @@ -17,6 +17,7 @@ count = {} information = {} headers = [0, 0, 0, 0, 0, 0] +headerRegex = '(?P
(?P(-\.)*)(\+\.)*)' def removeAccents(string): nfkd_form = unicodedata.normalize('NFKD', string) @@ -32,6 +33,7 @@ def toIdentifier(string): return string def numbering(key, value, format, meta): + global headerRegex if key == 'Header': [level, [id, classes, attributes], content] = value if 'unnumbered' not in classes: @@ -43,7 +45,7 @@ def numbering(key, value, format, meta): if length >= 3 and value[-2] == Space() and value[-1]['t'] == 'Str': last = value[-1]['c'] - match = re.match('^((?P
(?P(-\.)*)(#\.)*)#)(?P[a-zA-Z][\w.-]*:)?(?P[a-zA-Z][\w:.-]*)?$', last) + match = re.match('^' + headerRegex + '#(?P[a-zA-Z][\w.-]*:)?(?P[a-zA-Z][\w:.-]*)?$', last) if match: # Is it a Para and the last element is an identifier beginning with '#' @@ -96,7 +98,7 @@ def numbering(key, value, format, meta): else: tag = basicCategory + match.group('name') - # Replace the '#.#...#' by the category count (omitting the hidden part) + # Replace the '-.-.+.+...#' by the category count (omitting the hidden part) value[-1]['c'] = '.'.join(map(str, headers[levelInf:levelSup] + [number])) # Prepare the final text @@ -198,6 +200,7 @@ def hasCiteShortCut(meta): def getDefaultLevels(category, meta): + global headerRegex if not hasattr(getDefaultLevels, 'value'): getDefaultLevels.value = {} if category not in getDefaultLevels.value: @@ -205,16 +208,13 @@ def getDefaultLevels(category, meta): levelSup = 0 if 'pandoc-numbering' in meta and \ meta['pandoc-numbering']['t'] == 'MetaMap' and \ - 'categories' in meta['pandoc-numbering']['c'] and\ - meta['pandoc-numbering']['c']['categories']['t'] == 'MetaMap' and\ - category in meta['pandoc-numbering']['c']['categories']['c'] and\ - meta['pandoc-numbering']['c']['categories']['c'][category]['t'] == 'MetaInlines': - if len(meta['pandoc-numbering']['c']['categories']['c'][category]['c']) == 1 and\ - meta['pandoc-numbering']['c']['categories']['c'][category]['c'][0]['t'] == 'Str': - match = re.match( - '^(?P
(?P(-\.)*)(#\.)*)$', - meta['pandoc-numbering']['c']['categories']['c'][category]['c'][0]['c'] - ) + 'sectioning' in meta['pandoc-numbering']['c'] and\ + meta['pandoc-numbering']['c']['sectioning']['t'] == 'MetaMap' and\ + category in meta['pandoc-numbering']['c']['sectioning']['c'] and\ + meta['pandoc-numbering']['c']['sectioning']['c'][category]['t'] == 'MetaInlines': + if len(meta['pandoc-numbering']['c']['sectioning']['c'][category]['c']) == 1 and\ + meta['pandoc-numbering']['c']['sectioning']['c'][category]['c'][0]['t'] == 'Str': + match = re.match('^' + headerRegex + '$', meta['pandoc-numbering']['c']['sectioning']['c'][category]['c'][0]['c']) if match: # Compute the levelInf and levelSup values levelInf = len(match.group('hidden')) // 2 diff --git a/tests/test_numbering.py b/tests/test_numbering.py index 513b67d..22ebdc5 100644 --- a/tests/test_numbering.py +++ b/tests/test_numbering.py @@ -76,7 +76,7 @@ def test_numbering_title(): def test_numbering_level(): init() - src = Para([Str(u'Exercise'), Space(), Str(u'#.#.#')]) + src = Para([Str(u'Exercise'), Space(), Str(u'+.+.#')]) dest = Para([Span([u'exercise:0.0.1', [], []], [Strong([Str(u'Exercise'), Space(), Str(u'0.0.1')])])]) assert pandoc_numbering.numbering(src['t'], src['c'], '', {}) == dest @@ -87,12 +87,12 @@ def test_numbering_level(): src = Header(2, [u'first-section', [], []], [Str(u'First'), Space(), Str('section')]) pandoc_numbering.numbering(src['t'], src['c'], '', {}) - src = Para([Str(u'Exercise'), Space(), Str(u'#.#.#')]) + src = Para([Str(u'Exercise'), Space(), Str(u'+.+.#')]) dest = Para([Span( [u'exercise:1.1.1', [], []], [Strong([Str(u'Exercise'), Space(), Str(u'1.1.1')])])]) assert pandoc_numbering.numbering(src['t'], src['c'], '', {}) == dest - src = Para([Str(u'Exercise'), Space(), Str(u'#.#.#')]) + src = Para([Str(u'Exercise'), Space(), Str(u'+.+.#')]) dest = Para([Span([u'exercise:1.1.2', [], []], [Strong([Str(u'Exercise'), Space(), Str(u'1.1.2')])])]) assert pandoc_numbering.numbering(src['t'], src['c'], '', {}) == dest @@ -100,7 +100,7 @@ def test_numbering_level(): src = Header(2, [u'second-section', [], []], [Str(u'Second'), Space(), Str('section')]) pandoc_numbering.numbering(src['t'], src['c'], '', {}) - src = Para([Str(u'Exercise'), Space(), Str(u'#.#.#')]) + src = Para([Str(u'Exercise'), Space(), Str(u'+.+.#')]) dest = Para([Span([u'exercise:1.2.1', [], []], [Strong( [Str(u'Exercise'), Space(), Str(u'1.2.1')])])]) assert pandoc_numbering.numbering(src['t'], src['c'], '', {}) == dest @@ -111,7 +111,7 @@ def test_numbering_unnumbered(): src = Header(1, [u'unnumbered-chapter', [u'unnumbered'], []], [Str(u'Unnumbered'), Space(), Str('chapter')]) pandoc_numbering.numbering(src['t'], src['c'], '', {}) - src = Para([Str(u'Exercise'), Space(), Str(u'#.#')]) + src = Para([Str(u'Exercise'), Space(), Str(u'+.#')]) dest = Para([Span([u'exercise:0.1', [], []], [Strong([Str(u'Exercise'), Space(), Str(u'0.1')])])]) assert pandoc_numbering.numbering(src['t'], src['c'], '', {}) == dest @@ -140,7 +140,7 @@ def test_numbering_hidden(): assert pandoc_numbering.numbering(src['t'], src['c'], '', {}) == dest - src = Para([Str(u'Exercise'), Space(), Str(u'#.#')]) + src = Para([Str(u'Exercise'), Space(), Str(u'+.#')]) dest = Para([Span([u'exercise:2.2', [], []], [Strong([Str(u'Exercise'), Space(), Str(u'2.2')])])]) assert pandoc_numbering.numbering(src['t'], src['c'], '', {}) == dest @@ -159,18 +159,18 @@ def test_numbering_sharp_sharp(): assert src == dest -def test_numbering_categories(): +def test_numbering_sectioning(): init() meta = { 'pandoc-numbering': { 't': 'MetaMap', 'c': { - 'categories': { + 'sectioning': { 't': 'MetaMap', 'c': { 'exercise': { - 'c': [{'c': '-.#.', 't': 'Str'}], + 'c': [{'c': '-.+.', 't': 'Str'}], 't': 'MetaInlines' } } @@ -196,40 +196,3 @@ def test_numbering_categories(): assert src == dest -def test_numbering_categories_error(): - init() - - meta = { - 'pandoc-numbering': { - 't': 'MetaMap', - 'c': { - 'categories': { - 't': 'MetaMap', - 'c': { - 'exercise': { - 't': 'MetaMap', - 'c': {'inf': {'t': 'MetaString', 'c': 'error'}, 'sup': {'t': 'MetaString', 'c': 'error'}} - } - } - } - } - } - } - src = Header(1, [u'first-chapter', [], []], [Str(u'First'), Space(), Str('chapter')]) - pandoc_numbering.numbering(src['t'], src['c'], '', meta) - - src = Header(1, [u'second-chapter', [], []], [Str(u'Second'), Space(), Str('chapter')]) - pandoc_numbering.numbering(src['t'], src['c'], '', meta) - - src = Header(2, [u'first-section', [], []], [Str(u'First'), Space(), Str('section')]) - pandoc_numbering.numbering(src['t'], src['c'], '', meta) - - src = Header(2, [u'second-section', [], []], [Str(u'Second'), Space(), Str('section')]) - pandoc_numbering.numbering(src['t'], src['c'], '', meta) - - src = Para([Str(u'Exercise'), Space(), Str(u'#')]) - dest = Para([Str(u'Exercise'), Space(), Str(u'1')]) - pandoc_numbering.numbering(src['t'], src['c'], '', meta) - - assert src == dest - diff --git a/tests/test_referencing.py b/tests/test_referencing.py index 3adbdea..a541a14 100644 --- a/tests/test_referencing.py +++ b/tests/test_referencing.py @@ -66,7 +66,7 @@ def test_referencing_prefix_single(): src = Header(1, [u'first-chapter', [], []], [Str(u'First'), Space(), Str('chapter')]) pandoc_numbering.numbering(src['t'], src['c'], u'', {}) - src = Para([Str(u'Exercise'), Space(), Str(u'#.#ex:')]) + src = Para([Str(u'Exercise'), Space(), Str(u'+.#ex:')]) pandoc_numbering.numbering(src['t'], src['c'], u'', {}) src = json.loads(json.dumps(createLink(['', [], []], [Str(u'exercise'), Space(), Str(u'#')], [u'#ex:1.1', u'']))) @@ -114,7 +114,7 @@ def test_referencing_headers(): src = Header(1, [u'first-chapter', [], []], [Str(u'First'), Space(), Str('chapter')]) pandoc_numbering.numbering(src['t'], src['c'], u'', {}) - src = Para([Str(u'Theorem'), Space(), Str(u'#.#theorem:first')]) + src = Para([Str(u'Theorem'), Space(), Str(u'+.#theorem:first')]) pandoc_numbering.numbering(src['t'], src['c'], u'', {}) src = json.loads(json.dumps(createLink(['', [], []], [], [u'#theorem:first', u''])))