Skip to content

Commit

Permalink
Merge 5cdb188 into 002d57c
Browse files Browse the repository at this point in the history
  • Loading branch information
jefelino committed Jul 19, 2020
2 parents 002d57c + 5cdb188 commit b77fb2e
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 69 deletions.
28 changes: 5 additions & 23 deletions pandoc_latex_environment.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
Pandoc filter for adding LaTeX environement on specific div
"""

from pandocfilters import toJSONFilters, stringify, RawInline, Para
from pandocfilters import toJSONFilters, stringify, RawBlock, Para

import re

Expand All @@ -21,7 +21,7 @@ def environment(key, value, format, meta):
# Is the classes correct?
if currentClasses >= definedClasses:
if id != '':
label = '\\label{' + id + '}'
label = '\n\\label{' + id + '}'
else:
label = ''

Expand All @@ -31,28 +31,10 @@ def environment(key, value, format, meta):
else:
title = ''

# fix an empty block not rendering any output
if len(content) == 0:
content = [Para([])]

newconts = []
pos = 0
last = len(content)
for node in content:
replacement = node['c']
pos += 1
if pos == 1:
replacement = [RawInline('tex', '\\begin{' + environment + '}' + title + '\n' + label)] + replacement
if pos == last:
replacement = replacement + [RawInline('tex', '\n\\end{' + environment + '}')]
newconts.append(
{
't': node['t'],
'c': replacement
}
)
before = RawBlock('tex', '\\begin{' + environment + '}' + title + label)
after = RawBlock('tex', '\\end{' + environment + '}')

value[1] = newconts
value[1] = [before] + content + [after]
break

def getDefined(meta):
Expand Down
90 changes: 44 additions & 46 deletions tests/test_div.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,23 +74,22 @@ def test_div():
],
[]
],
[
[
{
't': 'RawBlock',
'c': ['tex', '\\begin{test}']
},
{
't': 'Plain',
'c': [
{
't': 'RawInline',
'c': ['tex', '\\begin{test}\n']
},
{
'c': 'content',
't': 'Str'
},
{
't': 'RawInline',
'c': ['tex', '\n\\end{test}']
}
]
{
'c': 'content',
't': 'Str'
}],
't': 'Plain'
},
{
't': 'RawBlock',
'c': ['tex', '\\end{test}']
}
]
)))
Expand Down Expand Up @@ -230,6 +229,7 @@ def test_div_with_id():
}
]
)))

dest = json.loads(json.dumps(Div(
[
'identifier',
Expand All @@ -239,23 +239,22 @@ def test_div_with_id():
],
[]
],
[
[
{
't': 'RawBlock',
'c': ['tex', '\\begin{test}\n\\label{identifier}']
},
{
't': 'Plain',
'c': [
{
't': 'RawInline',
'c': ['tex', '\\begin{test}\n\\label{identifier}']
},
{
'c': 'content',
't': 'Str'
},
{
't': 'RawInline',
'c': ['tex', '\n\\end{test}']
}
]
{
'c': 'content',
't': 'Str'
}],
't': 'Plain'
},
{
't': 'RawBlock',
'c': ['tex', '\\end{test}']
}
]
)))
Expand Down Expand Up @@ -322,6 +321,7 @@ def test_div_with_title():
}
]
)))

dest = json.loads(json.dumps(Div(
[
'',
Expand All @@ -333,28 +333,26 @@ def test_div_with_title():
['title', 'theTitle']
]
],
[
[
{
't': 'RawBlock',
'c': ['tex', '\\begin{test}[theTitle]']
},
{
't': 'Plain',
'c': [
{
't': 'RawInline',
'c': ['tex', '\\begin{test}[theTitle]\n']
},
{
'c': 'content',
't': 'Str'
},
{
't': 'RawInline',
'c': ['tex', '\n\\end{test}']
}
]
{
'c': 'content',
't': 'Str'
}],
't': 'Plain'
},
{
't': 'RawBlock',
'c': ['tex', '\\end{test}']
}
]
)))

pandoc_latex_environment.environment(src['t'], src['c'], 'latex', meta)

print(json.loads(json.dumps(src)))
assert json.loads(json.dumps(src)) == dest

0 comments on commit b77fb2e

Please sign in to comment.