-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbeamer2revealjs.py
executable file
·85 lines (65 loc) · 2.64 KB
/
beamer2revealjs.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
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Convert "columns" and "block" environments to the correct
fenced divs with attributes (for revealjs slides).
Produces pandoc markdown. DEPRECATED. Use `pandoc_filters/beamer_reveal.py`.
"""
from sys import stdin
import re
import argparse as ap
# See Beamer manual:
COLUMNS_OPTIONS = ['b', 'c', 'onlytextwidth', 't', 'T', 'totalwidth=.*']
# Assume one command per line.
COLUMNS_REGEX = re.compile(r'^\s*\\begin\{columns\}(\[[^[]]+\])?')
COLUMN_REGEX = re.compile(r'^\s*\\begin\{column\}(\[[^[]]+\])?\{(.+)\}')
BLOCK_REGEX = re.compile(r'^\s*\\begin\{(alert|example|)block\}\{(.*)\}')
END_REGEX = re.compile(r'^\s*\\end\{((?:alert|example|)block|columns?)\}')
WIDTH_REGEX = re.compile(r'([0-9]*(?:\.[0-9]*)?)(.*)')
#TODO: notes
def width2html(width_str):
width, unit = WIDTH_REGEX.match(width_str.strip()).groups()
if unit in (r'\textwidth', r'\linewidth'):
width = float(width)*100
unit = r'\%'
return '%g' % width + unit
def process_source(infile):
for line in infile:
if COLUMNS_REGEX.match(line):
options = COLUMNS_REGEX.match(line).groups()
print(r':::::: {.columns options="%s"}' % options)
elif COLUMN_REGEX.match(line):
placement, width_str = COLUMN_REGEX.match(line).groups()
print(r'::: {.column width="%s" placement="%s"}'
% (width2html(width_str), placement))
elif BLOCK_REGEX.match(line):
blocktype, title = BLOCK_REGEX.match(line).groups()
# Convention that slide-level=2
if blocktype == '':
print(r'\subsubsection{%s}' % title)
elif blocktype == 'alert':
print(r'### %s {.alert}' % title)
elif blocktype == 'example':
print(r'### %s {.example}' % title)
else:
raise ValueError('Block type "%s" not understood' % blocktype)
elif END_REGEX.match(line):
environment = END_REGEX.match(line).group(1)
if environment == 'columns':
print(r'::::::')
elif environment == 'column':
print(r':::')
else:
# Block environment begins as a lower level section.
pass
else:
print(line.rstrip())
#if not infile is stdin:
# infile.close()
def main():
parser = ap.ArgumentParser(description=__doc__)
parser.add_argument('infile', nargs='?', default=stdin,
type=ap.FileType('r'))
args = parser.parse_args()
print(process_source(args.infile))
if __name__ == '__main__':
main()