From 1fd04ad03d910c5aef520449604a657a177dfcb9 Mon Sep 17 00:00:00 2001 From: MinchinWeb Date: Sun, 9 May 2021 16:29:21 -0600 Subject: [PATCH] Convert "short" and "pretty" (and "default") formaters to plugins further to https://github.com/jrnl-org/jrnl/pull/1177 --- docs/plugins.md | 11 +++++++ features/plugins.feature | 56 ++++++++++++++++++--------------- jrnl/jrnl.py | 8 ++--- jrnl/plugins/exporter/pretty.py | 20 ++++++++++++ jrnl/plugins/exporter/short.py | 20 ++++++++++++ 5 files changed, 85 insertions(+), 30 deletions(-) create mode 100644 jrnl/plugins/exporter/pretty.py create mode 100644 jrnl/plugins/exporter/short.py diff --git a/docs/plugins.md b/docs/plugins.md index b728ca48..b8ffede0 100644 --- a/docs/plugins.md +++ b/docs/plugins.md @@ -146,6 +146,17 @@ Some implementation notes: *jrnl* will call **export_entry()** on each entry in turn and then concatenate the results together. +### Special Exporters + +There are a few "special" exporters, in that they are called by *jrnl* in +situations other than a traditional export. They are: + +- **short** -- called by `jrnl --short`. Displays each entry on a single line. + The default is to print the timestamp of the entry, followed by the title. + The built-in (default) plugin is at `jrnl.plugins.exporter.short`. +- **default** -- called when a different format is not specified. The built-in + (default) plugin is at `jrnl.plugins.exporter.pretty`. + ## Development Tips - editable installs (`pip install -e ...`) don't seem to play nice with diff --git a/features/plugins.feature b/features/plugins.feature index 49957e55..298f03d8 100644 --- a/features/plugins.feature +++ b/features/plugins.feature @@ -10,18 +10,21 @@ Feature: Functionality of Importer and Exporter Plugins Examples: | plugin_name | version | source | type | filename | - | jrnl | v.2.8.1 | plugins | importer | jrnl | - | boxed | v.2.8.1 | plugins | exporter | fancy | - | dates | v.2.8.1 | plugins | exporter | dates | - | fancy | v.2.8.1 | plugins | exporter | fancy | - | json | v.2.8.1 | plugins | exporter | json | - | markdown | v.2.8.1 | plugins | exporter | markdown | - | md | v.2.8.1 | plugins | exporter | markdown | - | tags | v.2.8.1 | plugins | exporter | tag | - | text | v.2.8.1 | plugins | exporter | text | - | txt | v.2.8.1 | plugins | exporter | text | - | xml | v.2.8.1 | plugins | exporter | xml | - | yaml | v.2.8.1 | plugins | exporter | yaml | + | jrnl | v2.8.1 | plugins | importer | jrnl | + | boxed | v2.8.1 | plugins | exporter | fancy | + | dates | v2.8.1 | plugins | exporter | dates | + | default | v2.8.1 | plugins | exporter | pretty | + | fancy | v2.8.1 | plugins | exporter | fancy | + | json | v2.8.1 | plugins | exporter | json | + | markdown | v2.8.1 | plugins | exporter | markdown | + | md | v2.8.1 | plugins | exporter | markdown | + | pretty | v2.8.1 | plugins | exporter | pretty | + | short | v2.8.1 | plugins | exporter | short | + | tags | v2.8.1 | plugins | exporter | tag | + | text | v2.8.1 | plugins | exporter | text | + | txt | v2.8.1 | plugins | exporter | text | + | xml | v2.8.1 | plugins | exporter | xml | + | yaml | v2.8.1 | plugins | exporter | yaml | @skip_only_with_external_plugins Scenario Outline: List external plugin names in --version @@ -31,21 +34,24 @@ Feature: Functionality of Importer and Exporter Plugins And The output should contain " : from jrnl..." Examples: | plugin_name | version | source | type | filename | - | jrnl | v.2.8.1 | plugins | importer | jrnl | + | jrnl | v2.8.1 | plugins | importer | jrnl | | json | v1.0.0 | contrib | importer | json | - | boxed | v.2.8.1 | plugins | exporter | fancy | - | dates | v.2.8.1 | plugins | exporter | dates | - | fancy | v.2.8.1 | plugins | exporter | fancy | + | boxed | v2.8.1 | plugins | exporter | fancy | + | dates | v2.8.1 | plugins | exporter | dates | + | default | v2.8.1 | plugins | exporter | pretty | + | fancy | v2.8.1 | plugins | exporter | fancy | | json | v1.0.0 | contrib | exporter | json | - | markdown | v.2.8.1 | plugins | exporter | markdown | - | md | v.2.8.1 | plugins | exporter | markdown | - | tags | v.2.8.1 | plugins | exporter | tag | - | text | v.2.8.1 | plugins | exporter | text | - | txt | v1.0.0 | contrib | exporter | rot13 | - | xml | v.2.8.1 | plugins | exporter | xml | - | yaml | v.2.8.1 | plugins | exporter | yaml | + | markdown | v2.8.1 | plugins | exporter | markdown | + | md | v2.8.1 | plugins | exporter | markdown | + | pretty | v2.8.1 | plugins | exporter | pretty | | rot13 | v1.0.0 | contrib | exporter | rot13 | + | short | v2.8.1 | plugins | exporter | short | + | tags | v2.8.1 | plugins | exporter | tag | | testing | v0.0.1 | contrib | exporter | testing | + | text | v2.8.1 | plugins | exporter | text | + | txt | v1.0.0 | contrib | exporter | rot13 | + | xml | v2.8.1 | plugins | exporter | xml | + | yaml | v2.8.1 | plugins | exporter | yaml | @skip_only_with_external_plugins Scenario Outline: Do not list overridden plugin names in --version @@ -56,5 +62,5 @@ Feature: Functionality of Importer and Exporter Plugins Examples: | plugin_name | version | source | type | filename | - | json | v.2.8.1 | plugins | exporter | json | - | txt | v.2.8.1 | plugins | exporter | text | + | json | v2.8.1 | plugins | exporter | json | + | txt | v2.8.1 | plugins | exporter | text | diff --git a/jrnl/jrnl.py b/jrnl/jrnl.py index e50eee59..a344e30a 100644 --- a/jrnl/jrnl.py +++ b/jrnl/jrnl.py @@ -324,10 +324,7 @@ def _delete_search_results(journal, old_entries, **kwargs): def _display_search_results(args, journal, **kwargs): if args.short or args.export == "short": - print(journal.pprint(short=True)) - - elif args.export == "pretty": - print(journal.pprint()) + print(plugins.meta.get_exporter("short").export(journal)) elif args.tags: print(plugins.meta.get_exporter("tags").export(journal)) @@ -339,4 +336,5 @@ def _display_search_results(args, journal, **kwargs): exporter = plugins.meta.get_exporter(kwargs["config"]["display_format"]) print(exporter.export(journal, args.filename)) else: - print(journal.pprint()) + # print(journal.pprint()) + print(plugins.meta.get_exporter("default").export(journal)) diff --git a/jrnl/plugins/exporter/pretty.py b/jrnl/plugins/exporter/pretty.py new file mode 100644 index 00000000..e44eb872 --- /dev/null +++ b/jrnl/plugins/exporter/pretty.py @@ -0,0 +1,20 @@ +#!/usr/bin/env python +# encoding: utf-8 +# Copyright (C) 2012-2021 jrnl contributors +# License: https://www.gnu.org/licenses/gpl-3.0.html + +from jrnl.plugins.base import BaseExporter + +from ... import __version__ + + +class Exporter(BaseExporter): + """Pretty print journal""" + + names = ["pretty", "default"] + extension = "txt" + version = __version__ + + @classmethod + def export_journal(cls, journal): + return journal.pprint() diff --git a/jrnl/plugins/exporter/short.py b/jrnl/plugins/exporter/short.py new file mode 100644 index 00000000..c9c21a8a --- /dev/null +++ b/jrnl/plugins/exporter/short.py @@ -0,0 +1,20 @@ +#!/usr/bin/env python +# encoding: utf-8 +# Copyright (C) 2012-2021 jrnl contributors +# License: https://www.gnu.org/licenses/gpl-3.0.html + +from jrnl.plugins.base import BaseExporter + +from ... import __version__ + + +class Exporter(BaseExporter): + """Short export -- i.e. single line date and title""" + + names = ["short"] + extension = "txt" + version = __version__ + + @classmethod + def export_journal(cls, journal): + return journal.pprint(short=True)