Merge pull request #470 from rachmadaniHaryono/feature/org-mode

buku tags to org mode tags
This commit is contained in:
Mischievous Meerkat 2020-09-20 00:36:34 +05:30 committed by GitHub
commit 94afb01106
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 11 deletions

24
buku
View File

@ -2819,6 +2819,18 @@ PROMPT KEYS:
ConverterResult = TypedDict('ConverterResult', {'data': str, 'count': int}) if TypedDict else Dict[str, Any]
def convert_tags_to_org_mode_tags(tags: str) -> str:
"""convert buku tags to org-mode compatible tags."""
if tags != DELIM:
buku_tags = tags.split(DELIM)[1:-1]
buku_tags = [re.sub(r'[^a-zA-Z0-9_@]', ' ', tag) for tag in buku_tags]
buku_tags = [re.sub(r'\s+', ' ', tag) for tag in buku_tags]
buku_tags = list(sorted(set([x.replace(' ', '_') for x in buku_tags]), reverse=False))
if buku_tags:
return ' :{}:\n'.format(':'.join(buku_tags))
return '\n'
def convert_bookmark_set(
bookmark_set: List[BookmarkVar],
export_type: str) -> ConverterResult: # type: ignore
@ -2858,17 +2870,7 @@ def convert_bookmark_set(
out += '* [[{}][Untitled]]'.format(row[1])
else:
out += '* [[{}][{}]]'.format(row[1], row[2])
if row[3] != DELIM:
# add additional whitespaces for tags that end or start with a colon
tag_string = row[3].replace(',:', ', ,:').replace(':,', ':, ,')
buku_tags = tag_string.split(DELIM)[1:-1]
# if colons are inside a tag, add one additional colon
buku_tags = [re.sub(r'(?<=[\w,\:]):(?=\w)', '::', tag) for tag in buku_tags]
out += ' :{}:\n'.format(':'.join(buku_tags))
else:
out += '\n'
out += convert_tags_to_org_mode_tags(row[3])
count += 1
elif export_type == 'html':
timestamp = str(int(time.time()))

View File

@ -770,3 +770,18 @@ def test_convert_bookmark_set(export_type, exp_res, monkeypatch):
res = convert_bookmark_set(bms, export_type=export_type)
assert res['count'] == 3
assert exp_res == res['data']
@pytest.mark.parametrize('tags,data', [
[',', '\n'],
[',tag1,tag2,', ' :tag1:tag2:\n'],
[',word1 word2,', ' :word1_word2:\n'],
[',word1:word2,', ' :word1_word2:\n'],
[',##tag##,', ' :_tag_:\n'],
[',##tag##,!!tag!!,', ' :_tag_:\n'],
[',home / personal,', ' :home_personal:\n'],
])
def test_convert_tags_to_org_mode_tags(tags, data):
from buku import convert_tags_to_org_mode_tags
res = convert_tags_to_org_mode_tags(tags)
assert res == data