converter.py
1.56 KB
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
'ODT to markdown conversion'
import subprocess
import os
from cleaner import Cleaner
class Converter():
'ODT to Markdown converter'
def __init__(self, destination):
self.destination = destination
def convert(self, path):
'Convert file or folder'
if os.path.isdir(path):
for filename in os.listdir(path):
self._convert_filename(os.path.join(path, filename))
else:
self._convert_filename(path)
def _convert_filename(self, filename):
'Convert a single file based on extension'
if not os.path.isfile(filename):
return
ext = os.path.splitext(filename)[1]
functions = {'.odt': self._convert_odt, '.md': self._clean_md}
if ext in functions:
functions[ext](filename)
def _convert_odt(self, filename):
'Convert a single file to Markdown'
output = self._output_file(filename, 'md')
subprocess.call(['pandoc', filename, '-o', output])
self._clean_md(output)
def _clean_md(self, filename):
'Correct Markdown file'
cleaner = Cleaner()
output = self._output_file(filename, 'md', 'clean')
cleaner.clean(filename, output)
def _output_file(self, path, extension, folder=''):
'Create output file path'
folder = folder or extension
basefile = os.path.splitext(os.path.basename(path))[0]
outdir = f'{self.destination}/{folder}'
if not os.path.isdir(outdir):
os.makedirs(outdir)
return f'{outdir}/{basefile}.{extension}'