forked from jblomo/datamining290
-
Notifications
You must be signed in to change notification settings - Fork 8
/
org2md.py
153 lines (125 loc) · 5.31 KB
/
org2md.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
"""
python org2md.py test.org test.markdown && sed -e "/<\!--markdown goes here-->/r test.markdown" < slide_template.html | sed -e "s/<\!--markdown goes here-->//" > test.html && fmdiff test.markdown 2014-02-06-Preprocessing.markdown
python org2md.py test.org ../test.markdown
"""
import re
import sys
HEADER = '''name: inverse
layout: true
class: left, top, inverse
'''
LINES_TO_ELIMINATE = set([
r'''#+STYLE: <link rel="stylesheet" type="text/css" href="production/common.css" />''',
r'''#+STYLE: <link rel="stylesheet" type="text/css" href="production/screen.css" media="screen" />''',
r'''#+STYLE: <link rel="stylesheet" type="text/css" href="production/projection.css" media="projection" />''',
r'''#+STYLE: <link rel="stylesheet" type="text/css" href="production/color-blue.css" media="projection" />''',
r'''#+STYLE: <link rel="stylesheet" type="text/css" href="production/presenter.css" media="presenter" />''',
r'''#+STYLE: <link href='http://fonts.googleapis.com/css?family=Lobster+Two:700|Yanone+Kaffeesatz:700|Open+Sans' rel='stylesheet' type='text/css'>''',
r'''#+BEGIN_HTML''',
r'''<script type="text/javascript" src="production/org-html-slideshow.js"></script>''',
r'''#+END_HTML''',
r'''# Local Variables:''',
r'''# org-export-html-style-include-default: nil''',
r'''# org-export-html-style-include-scripts: nil''',
r'''# buffer-file-coding-system: utf-8-unix''',
r'''# End:''',
])
def count_of_leading_char(s, leading_char):
index = 0
while index < len(s) and s[index] == leading_char:
index += 1
return index
def main(input_filename, output_filename):
todo = []
slide_number = 0
under_heading = False
with open(output_filename, 'wb') as output_file:
output_file.write(HEADER)
with open(input_filename, 'rb') as input_file:
for line in input_file:
line = line[:-1] # remove \n
if line in LINES_TO_ELIMINATE:
continue
if under_heading and line.strip():
output_file.write('\n')
# Checks for upcoming slide
if ':animate:' in line:
todo.append('next slide is animated')
if ':two_col:' in line:
todo.append('next slide is two column')
# Is this a new slide?
if ':slide:' in line:
slide_number += 1
todo.append('Slide %d' % slide_number)
todo.append(' main')
output_file.write('---\n\n')
line = line.replace(':slide:', '')
# Is this notes?
if ':notes:' in line:
todo.append(' notes')
output_file.write('\n???\n\n')
line = line.replace(':notes:', '')
# Is this a heading?
if line.startswith('*'):
level = count_of_leading_char(line, '*')
line = (level * '#') + line[level:]
under_heading = True
else:
under_heading = False
# Normalize indentation (should be an even number of spaces)
if count_of_leading_char(line, ' ') % 2:
line = line[1:]
# Definitions
if ' :: ' in line:
todo.append(' a series of sections will work better for some definitions')
line = line.replace(' :: ', ': ')
# Images
line = re.sub(
r'''\[\[(file:)?([^\]]+)\]\]''',
r'''<img src="\2"/>''',
line
)
# Links
old_line = line
line = re.sub(
r'''\[\[([^]]+)\]\[([^]]+)\]\]''',
r'''[\2](\1)''',
line
)
if old_line != line:
todo.append(' check link')
# Block code
line = re.sub(
r'''#\+begin_src *''',
'```',
line
)
line = line.replace('#+end_src', '```')
# Inline code
line = re.sub(
r''' =([^ =].*?[^ ])=''',
r''' ```\1```''',
line
)
# Jim --> Jimmy
line = line.replace('Jim', 'Jimmy')
line = line.replace('jim', 'jimmy')
line = line.replace('Blomo', 'Retzlaff')
line = line.replace('jblomo', 'jretz')
# Table
first_bar = line.find('|')
if first_bar >= 0:
if line.find('|', first_bar + 1):
todo.append(' table')
# Unknown construct
if line.startswith('#+'):
print 'Unknown construct:', line
output_file.write(line.rstrip())
output_file.write('\n')
todo.append('Headings are the right level?')
output_file.write('\n---\n\n')
for line in todo:
output_file.write(line)
output_file.write('\n')
if __name__ == '__main__':
main(*sys.argv[1:])