2017-01-12 21:45:49 +01:00
|
|
|
from functools import reduce
|
2017-01-12 16:48:09 +01:00
|
|
|
|
2017-01-14 11:09:42 +01:00
|
|
|
from src.markdown.links import MarkdownLinks
|
|
|
|
|
|
|
|
from src.markdown.headers import MarkdownHeader
|
|
|
|
from src.markdown.simplestyle import MarkdownItalic, MarkdownBold, MarkdownStrikeThrough
|
|
|
|
|
|
|
|
|
2017-01-12 16:48:09 +01:00
|
|
|
class MarkdownConverter:
|
|
|
|
|
2017-01-12 21:45:49 +01:00
|
|
|
def __init__(self, file):
|
|
|
|
self.file = file
|
2017-01-14 11:09:42 +01:00
|
|
|
self.converters = (
|
2017-01-14 20:14:18 +01:00
|
|
|
# TODO auto-discover these. How do I do that, without interfaces?
|
2017-01-14 11:09:42 +01:00
|
|
|
MarkdownHeader(),
|
|
|
|
MarkdownLinks(),
|
|
|
|
MarkdownItalic(),
|
|
|
|
MarkdownBold(),
|
|
|
|
MarkdownStrikeThrough()
|
|
|
|
)
|
2017-01-12 21:45:49 +01:00
|
|
|
|
|
|
|
def convert(self):
|
|
|
|
converted = []
|
|
|
|
with open(self.file, 'r') as file:
|
|
|
|
for line in file:
|
|
|
|
converted.append(self.convert_line(line))
|
|
|
|
|
|
|
|
return "\n".join(converted)
|
|
|
|
|
|
|
|
def convert_line(self, line):
|
|
|
|
convertfns = map(lambda converter: converter.convert, self.converters)
|
|
|
|
massconvert = reduce(lambda red1, red2: lambda text: red1(red2(line)), convertfns, lambda text: text)
|
|
|
|
return massconvert(line)
|