-
Notifications
You must be signed in to change notification settings - Fork 10
/
pandoc-wrapfig.py
executable file
·73 lines (66 loc) · 3.39 KB
/
pandoc-wrapfig.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
"""Pandoc filter to allow variable wrapping of LaTeX/pdf documents
through the wrapfig package.
Simply add a " {?}" tag to the end of the caption for the figure, where
? is an integer specifying the width of the wrap in inches. 0 will cause
the width of the figure to be used. Optionally precede ? with a
character in the set {l,r,i,o} to set wrapfig's placement parameter; the
default is 'l'. Optionally follow ? with a '-' and another width
specification to set wrapfig's overhang parameter and push the figure
that far into the margin.
"""
from pandocfilters import toJSONFilter, Image, RawInline, stringify
import re, sys
FLAG_PAT = re.compile(
'.*\{(\w?)(\d+\.?\d?(cm|in|pt)?)-?((\d+\.?\d?(cm|in|pt))?)?\,*(\d*)\}')
def wrapfig(key, val, fmt, meta):
if key == 'Image':
attrs, caption, target = val
if FLAG_PAT.match(stringify(caption)):
# Strip tag
where = FLAG_PAT.match(caption[-1]['c']).group(1)
overhang = FLAG_PAT.match(caption[-1]['c']).group(4)
overhang = overhang if not overhang else '[%s]' % overhang
size = FLAG_PAT.match(caption[-1]['c']).group(2)
lines = FLAG_PAT.match(caption[-1]['c']).group(7)
stripped_caption = caption[:-2]
if fmt == 'latex':
if where == 'm': # produce a marginfigure
latex_begin = r'\begin{marginfigure}'
latex_end = r'}\end{marginfigure}'
if len(stripped_caption) > 0:
latex_fig = r'\includegraphics{' + target[0] \
+ '}\caption{'
return [RawInline(fmt, latex_begin + latex_fig)] \
+ stripped_caption + [RawInline(fmt, latex_end)]
else:
latex_fig = r'\includegraphics{' + target[0] \
+ '}'
return [RawInline(fmt, latex_begin + latex_fig)] \
+ [RawInline(fmt, latex_end)]
else: # produce a wrapfigure
if len(lines) > 0:
latex_begin = r'\begin{wrapfigure}[' + lines \
+ ']{%s}%s{' % (where, overhang) + size + '}'
else:
latex_begin = \
r'\begin{wrapfigure}{%s}%s{' % (where, overhang) \
+ size + '}'
if len(stripped_caption) > 0:
latex_fig = r'\centering\includegraphics{' + target[0] \
+ '}\caption{'
latex_end = r'}\end{wrapfigure}'
return [RawInline(fmt, latex_begin + latex_fig)] \
+ stripped_caption + [RawInline(fmt, latex_end)]
else:
latex_fig = r'\centering\includegraphics{' + target[0] \
+ '}'
latex_end = r'\end{wrapfigure}'
return [RawInline(fmt, latex_begin + latex_fig)] \
+ [RawInline(fmt, latex_end)]
else:
return Image(attrs, stripped_caption, target)
if __name__ == '__main__':
toJSONFilter(wrapfig)
sys.stdout.flush() # Should fix issue #1 (pipe error)