2019-01-06 15:57:15 -08:00
|
|
|
from datetime import datetime
|
|
|
|
import re
|
|
|
|
from typing import List, Optional
|
|
|
|
|
2022-12-19 17:23:32 -08:00
|
|
|
DEFAULT_TEMPLATE = "\\%title: [[%:link|%:description]]\n%:tags\n%U\n\n\n%:link\n%:initial\n"
|
2019-01-13 07:34:13 -08:00
|
|
|
|
2020-04-12 08:10:27 -07:00
|
|
|
# TODO reuse inorganic/orgparse??
|
2019-01-06 15:57:15 -08:00
|
|
|
def date2org(t: datetime) -> str:
|
|
|
|
return t.strftime("%Y-%m-%d %a")
|
|
|
|
|
|
|
|
def datetime2orgtime(t: datetime) -> str:
|
|
|
|
return t.strftime("%H:%M")
|
|
|
|
|
|
|
|
def datetime2org(t: datetime) -> str:
|
|
|
|
return date2org(t) + " " + datetime2orgtime(t)
|
|
|
|
|
2019-01-13 07:34:13 -08:00
|
|
|
|
|
|
|
def empty(s) -> bool:
|
|
|
|
return s is None or len(s.strip()) == 0
|
|
|
|
|
|
|
|
|
2020-04-12 08:10:27 -07:00
|
|
|
# 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
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2019-01-13 07:34:13 -08:00
|
|
|
def as_org(
|
|
|
|
url: str,
|
|
|
|
title: str,
|
|
|
|
selection: str,
|
|
|
|
comment: str,
|
|
|
|
tags: List[str],
|
|
|
|
org_template: str,
|
2020-04-12 08:10:27 -07:00
|
|
|
config: Optional[Config] = None,
|
2019-01-06 15:57:15 -08:00
|
|
|
):
|
2019-01-13 07:34:13 -08:00
|
|
|
"""
|
|
|
|
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.
|
|
|
|
|
|
|
|
Additionally, supports :selection, :comment and :tags expansions.
|
|
|
|
|
|
|
|
You can look at `test_templates` for some specific examples.
|
|
|
|
"""
|
2022-12-19 00:26:29 -08:00
|
|
|
# py_template = re.sub(r'%[:]?([?\w]+)', r'{\1}', org_template)
|
|
|
|
py_template = re.sub(r'[^\\]%[:]?([?\w]+)', r'{\1}', org_template)
|
2019-01-13 07:34:13 -08:00
|
|
|
|
|
|
|
NOW = datetime.now()
|
|
|
|
org_date = date2org(NOW)
|
|
|
|
org_datetime = datetime2org(NOW)
|
|
|
|
|
|
|
|
tags_s = '' if len(tags) == 0 else (':' + ':'.join(tags) + ':')
|
|
|
|
|
2020-04-12 08:10:27 -07:00
|
|
|
if config is None:
|
|
|
|
config = DefaultConfig()
|
|
|
|
|
2019-01-13 07:34:13 -08:00
|
|
|
# TODO tabulate selection and comments?
|
|
|
|
# TODO not sure, maybe add as org quote?
|
|
|
|
parts = []
|
|
|
|
if not empty(selection):
|
2020-04-12 08:10:27 -07:00
|
|
|
parts.extend(config.format_selection(selection))
|
2019-01-13 07:34:13 -08:00
|
|
|
if not empty(comment):
|
2020-04-12 08:10:27 -07:00
|
|
|
parts.extend(config.format_comment(comment))
|
2019-01-13 07:34:13 -08:00
|
|
|
initial = '\n'.join(parts)
|
|
|
|
|
|
|
|
res = py_template.format(
|
|
|
|
t=f'<{org_date}>',
|
|
|
|
u=f'[{org_date}]',
|
|
|
|
T=f'<{org_datetime}>',
|
|
|
|
U=f'[{org_datetime}]',
|
|
|
|
description=title,
|
|
|
|
link=url,
|
|
|
|
|
|
|
|
initial=initial,
|
|
|
|
i=initial,
|
|
|
|
|
|
|
|
tags=tags_s,
|
|
|
|
selection=selection,
|
|
|
|
comment=comment,
|
|
|
|
annotation=f'[[{url}][{title}]]', # https://orgmode.org/manual/capture-protocol.html
|
|
|
|
**{'?': ''}, # just ignore it
|
|
|
|
)
|
|
|
|
return res
|
|
|
|
|
|
|
|
# TODO escaping?
|
|
|
|
# just checks it's not crashing for now
|
|
|
|
def test_templates():
|
|
|
|
url = 'https://whatever'
|
|
|
|
title = 'hello'
|
|
|
|
selection = """some
|
|
|
|
selected
|
|
|
|
text
|
|
|
|
"""
|
|
|
|
comment = 'fafewfewf'
|
|
|
|
tags = ['aba', 'caba']
|
|
|
|
|
|
|
|
# https://orgmode.org/guide/Capture-templates.html
|
|
|
|
org_templates = [
|
|
|
|
DEFAULT_TEMPLATE,
|
|
|
|
"* %? [[%:link][%:description]] \nCaptured On: %U",
|
|
|
|
"* %:annotation",
|
|
|
|
"* %U %:description :protocol: \n %:link \n%:initial \n\n",
|
|
|
|
"* %t captured stuff %:tags \n ",
|
|
|
|
"\n** Selection\n%:selection\n** Comment\n%:comment\n",
|
|
|
|
"""* TODO [#A] %:link
|
|
|
|
** Selection
|
|
|
|
%:selection
|
|
|
|
** Comment
|
|
|
|
%:comment
|
|
|
|
""",
|
|
|
|
"""* %T %:link %:description
|
|
|
|
#+BEGIN_QUOTE
|
|
|
|
%:selection
|
|
|
|
#+END_QUOTE
|
|
|
|
""",
|
2019-01-06 15:57:15 -08:00
|
|
|
]
|
2019-01-13 07:34:13 -08:00
|
|
|
for org_template in org_templates:
|
|
|
|
res = as_org(
|
|
|
|
url,
|
|
|
|
title,
|
|
|
|
selection,
|
|
|
|
comment,
|
|
|
|
tags,
|
|
|
|
org_template=org_template
|
2019-01-06 15:57:15 -08:00
|
|
|
|
2019-01-13 07:34:13 -08:00
|
|
|
)
|