jrnl/jrnl/plugins/template_exporter.py
Micah Jerome Ellison c5729420a4
Comply with GPL by acknowledging all authors and including license info in each source file (#1121)
* Update authors to "jrnl contributors" to comply with GPL3
* Include jrnl email address with contributors
* Include GPL notice in jrnl --version
* Apply consistent copyright and license to all Python files
* Add copyright and license to documentation
* Add copyright and license to docs theme
* Wiping poetry cache to try to resolve a test issue
* Testing with Python 3.9.0 in attempt to bypass GitHub Actions failure in 3.9.1
* make format
* Exclude Windows Python 3.9 build which is failing due to a GitHub Actions problem
* Modify testing to get around this 3.9 issue...
* Fix exclude
2020-12-19 14:53:15 -08:00

44 lines
1.4 KiB
Python

#!/usr/bin/env python
# encoding: utf-8
# Copyright (C) 2012-2021 jrnl contributors
# License: https://www.gnu.org/licenses/gpl-3.0.html
from glob import glob
import os
from .template import Template
from .text_exporter import TextExporter
class GenericTemplateExporter(TextExporter):
"""This Exporter can convert entries and journals into text files."""
@classmethod
def export_entry(cls, entry):
"""Returns a string representation of a single entry."""
vars = {"entry": entry, "tags": entry.tags}
return cls.template.render_block("entry", **vars)
@classmethod
def export_journal(cls, journal):
"""Returns a string representation of an entire journal."""
vars = {"journal": journal, "entries": journal.entries, "tags": journal.tags}
return cls.template.render_block("journal", **vars)
def __exporter_from_file(template_file):
"""Create a template class from a file"""
name = os.path.basename(template_file).replace(".template", "")
template = Template.from_file(template_file)
return type(
str(f"{name.title()}Exporter"),
(GenericTemplateExporter,),
{"names": [name], "extension": template.extension, "template": template},
)
__all__ = []
# Factory pattern to create Exporter classes for all available templates
for template_file in glob("jrnl/templates/*.template"):
__all__.append(__exporter_from_file(template_file))