1
|
|
|
#!/usr/bin/env python |
2
|
|
|
# encoding: utf-8 |
3
|
|
|
|
4
|
|
|
from glob import glob |
5
|
|
|
import os |
6
|
|
|
|
7
|
|
|
from .template import Template |
8
|
|
|
from .text_exporter import TextExporter |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
class GenericTemplateExporter(TextExporter): |
12
|
|
|
"""This Exporter can convert entries and journals into text files.""" |
13
|
|
|
|
14
|
|
|
@classmethod |
15
|
|
|
def export_entry(cls, entry): |
16
|
|
|
"""Returns a string representation of a single entry.""" |
17
|
|
|
vars = {"entry": entry, "tags": entry.tags} |
18
|
|
|
return cls.template.render_block("entry", **vars) |
19
|
|
|
|
20
|
|
|
@classmethod |
21
|
|
|
def export_journal(cls, journal): |
22
|
|
|
"""Returns a string representation of an entire journal.""" |
23
|
|
|
vars = {"journal": journal, "entries": journal.entries, "tags": journal.tags} |
24
|
|
|
return cls.template.render_block("journal", **vars) |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
def __exporter_from_file(template_file): |
28
|
|
|
"""Create a template class from a file""" |
29
|
|
|
name = os.path.basename(template_file).replace(".template", "") |
30
|
|
|
template = Template.from_file(template_file) |
31
|
|
|
return type( |
32
|
|
|
str(f"{name.title()}Exporter"), |
33
|
|
|
(GenericTemplateExporter,), |
34
|
|
|
{"names": [name], "extension": template.extension, "template": template}, |
35
|
|
|
) |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
__all__ = [] |
39
|
|
|
|
40
|
|
|
# Factory pattern to create Exporter classes for all available templates |
41
|
|
|
for template_file in glob("jrnl/templates/*.template"): |
42
|
|
|
__all__.append(__exporter_from_file(template_file)) |
43
|
|
|
|