add Config to allow for more flexible formatting

This commit is contained in:
Dima Gerasimov 2020-04-12 16:10:27 +01:00
parent 3327e40f09
commit 6b6020598b
2 changed files with 37 additions and 9 deletions

View file

@ -8,7 +8,7 @@ from pathlib import Path
import re import re
from typing import List, Optional 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_PATH_VAR = 'GRASP_CAPTURE_PATH'
CAPTURE_TEMPLATE_VAR = 'GRASP_CAPTURE_TEMPLATE' CAPTURE_TEMPLATE_VAR = 'GRASP_CAPTURE_TEMPLATE'

View file

@ -4,6 +4,7 @@ from typing import List, Optional
DEFAULT_TEMPLATE = "* %U %:description %:tags\n%:link\n%:initial\n" DEFAULT_TEMPLATE = "* %U %:description %:tags\n%:link\n%:initial\n"
# TODO reuse inorganic/orgparse??
def date2org(t: datetime) -> str: def date2org(t: datetime) -> str:
return t.strftime("%Y-%m-%d %a") return t.strftime("%Y-%m-%d %a")
@ -18,6 +19,35 @@ def empty(s) -> bool:
return s is None or len(s.strip()) == 0 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( def as_org(
url: str, url: str,
title: str, title: str,
@ -25,6 +55,7 @@ def as_org(
comment: str, comment: str,
tags: List[str], tags: List[str],
org_template: 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. 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) + ':') tags_s = '' if len(tags) == 0 else (':' + ':'.join(tags) + ':')
if config is None:
config = DefaultConfig()
# TODO tabulate selection and comments? # TODO tabulate selection and comments?
# TODO not sure, maybe add as org quote? # TODO not sure, maybe add as org quote?
parts = [] parts = []
if not empty(selection): if not empty(selection):
parts.extend([ parts.extend(config.format_selection(selection))
'Selection:',
selection,
])
if not empty(comment): if not empty(comment):
parts.extend([ parts.extend(config.format_comment(comment))
'Comment:',
comment
])
initial = '\n'.join(parts) initial = '\n'.join(parts)
res = py_template.format( res = py_template.format(