Skip to content

Commit

Permalink
Correcting bug of using _ for hidden sectioning; change default heade…
Browse files Browse the repository at this point in the history
…rs in the meta
  • Loading branch information
chdemko committed Apr 15, 2016
1 parent 26c4782 commit ea711cc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 22 deletions.
32 changes: 15 additions & 17 deletions pandoc_numbering.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,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<header>(?P<hidden>(_\.)*)(#\.)*)#)(?P<prefix>[a-zA-Z][\w.-]*:)?(?P<name>[a-zA-Z][\w:.-]*)?$', last)
match = re.match('^((?P<header>(?P<hidden>(-\.)*)(#\.)*)#)(?P<prefix>[a-zA-Z][\w.-]*:)?(?P<name>[a-zA-Z][\w:.-]*)?$', last)

if match:
# Is it a Para and the last element is an identifier beginning with '#'
Expand Down Expand Up @@ -201,27 +201,25 @@ def getDefaultLevels(category, meta):
if not hasattr(getDefaultLevels, 'value'):
getDefaultLevels.value = {}
if category not in getDefaultLevels.value:
inf = 0
sup = 0
levelInf = 0
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'] == 'MetaMap':
if 'inf' in meta['pandoc-numbering']['c']['categories']['c'][category]['c'] and\
meta['pandoc-numbering']['c']['categories']['c'][category]['c']['inf']['t'] == 'MetaString':
try:
inf = int(meta['pandoc-numbering']['c']['categories']['c'][category]['c']['inf']['c'])
except ValueError:
pass
if 'sup' in meta['pandoc-numbering']['c']['categories']['c'][category]['c'] and\
meta['pandoc-numbering']['c']['categories']['c'][category]['c']['sup']['t'] == 'MetaString':
try:
sup = int(meta['pandoc-numbering']['c']['categories']['c'][category]['c']['sup']['c'])
except ValueError:
pass
getDefaultLevels.value[category] = [inf, sup]
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<header>(?P<hidden>(-\.)*)(#\.)*)$',
meta['pandoc-numbering']['c']['categories']['c'][category]['c'][0]['c']
)
if match:
# Compute the levelInf and levelSup values
levelInf = len(match.group('hidden')) // 2
levelSup = len(match.group('header')) // 2
getDefaultLevels.value[category] = [levelInf, levelSup]
return getDefaultLevels.value[category]

def pandocVersion():
Expand Down
10 changes: 5 additions & 5 deletions tests/test_numbering.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,20 +122,20 @@ def test_numbering_hidden():
src = Header(1, [u'first-chapter', [], []], [Str(u'First'), Space(), Str('chapter')])
pandoc_numbering.numbering(src['t'], src['c'], '', {})

src = Para([Str(u'Exercise'), Space(), Str(u'_.#exercise:one')])
src = Para([Str(u'Exercise'), Space(), Str(u'-.#exercise:one')])
dest = Para([Span([u'exercise:one', [], []], [Strong([Str(u'Exercise'), Space(), Str(u'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.2', [], []], [Strong([Str(u'Exercise'), Space(), Str(u'2')])])])

assert pandoc_numbering.numbering(src['t'], src['c'], '', {}) == dest

src = Header(1, [u'second-chapter', [], []], [Str(u'Second'), 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:2.1', [], []], [Strong([Str(u'Exercise'), Space(), Str(u'1')])])])

assert pandoc_numbering.numbering(src['t'], src['c'], '', {}) == dest
Expand Down Expand Up @@ -170,8 +170,8 @@ def test_numbering_categories():
't': 'MetaMap',
'c': {
'exercise': {
't': 'MetaMap',
'c': {'inf': {'t': 'MetaString', 'c': '1'}, 'sup': {'t': 'MetaString', 'c': '2'}}
'c': [{'c': '-.#.', 't': 'Str'}],
't': 'MetaInlines'
}
}
}
Expand Down

0 comments on commit ea711cc

Please sign in to comment.