From 6b6020598bb7b34a8e327dcb107703b251eadd08 Mon Sep 17 00:00:00 2001 From: Dima Gerasimov Date: Sun, 12 Apr 2020 16:10:27 +0100 Subject: [PATCH] add Config to allow for more flexible formatting --- server/grasp_server.py | 2 +- server/org_tools.py | 44 ++++++++++++++++++++++++++++++++++-------- 2 files changed, 37 insertions(+), 9 deletions(-) diff --git a/server/grasp_server.py b/server/grasp_server.py index e681aa8..dec0dc5 100755 --- a/server/grasp_server.py +++ b/server/grasp_server.py @@ -8,7 +8,7 @@ from pathlib import Path import re from typing import List, Optional -from org_tools import as_org, empty, DEFAULT_TEMPLATE +from org_tools import as_org, empty, DEFAULT_TEMPLATE, Config CAPTURE_PATH_VAR = 'GRASP_CAPTURE_PATH' CAPTURE_TEMPLATE_VAR = 'GRASP_CAPTURE_TEMPLATE' diff --git a/server/org_tools.py b/server/org_tools.py index e62a6ee..708796e 100644 --- a/server/org_tools.py +++ b/server/org_tools.py @@ -4,6 +4,7 @@ from typing import List, Optional DEFAULT_TEMPLATE = "* %U %:description %:tags\n%:link\n%:initial\n" +# TODO reuse inorganic/orgparse?? def date2org(t: datetime) -> str: return t.strftime("%Y-%m-%d %a") @@ -18,6 +19,35 @@ def empty(s) -> bool: return s is None or len(s.strip()) == 0 +# TODO protocol?? +# TODO put template in config?? +class Config: + @staticmethod + def format_selection(selection: str) -> List[str]: + ... + + @staticmethod + def format_comment(comment: str) -> List[str]: + ... + + +class DefaultConfig(Config): + @staticmethod + def format_selection(selection: str) -> List[str]: + return [ + 'Selection:', + selection, + ] + + + @staticmethod + def format_comment(comment: str) -> List[str]: + return [ + 'Comment:', + comment + ] + + def as_org( url: str, title: str, @@ -25,6 +55,7 @@ def as_org( comment: str, tags: List[str], org_template: str, + config: Optional[Config] = None, ): """ Formats captured results according to org template. Supports all sensible (e.g. non-interactive) template expansions from https://orgmode.org/manual/Template-expansion.html#Template-expansion. @@ -41,19 +72,16 @@ You can look at `test_templates` for some specific examples. tags_s = '' if len(tags) == 0 else (':' + ':'.join(tags) + ':') + if config is None: + config = DefaultConfig() + # TODO tabulate selection and comments? # TODO not sure, maybe add as org quote? parts = [] if not empty(selection): - parts.extend([ - 'Selection:', - selection, - ]) + parts.extend(config.format_selection(selection)) if not empty(comment): - parts.extend([ - 'Comment:', - comment - ]) + parts.extend(config.format_comment(comment)) initial = '\n'.join(parts) res = py_template.format(