From 117cf5e542d23c286823461b770d7d6e5f20f4bc Mon Sep 17 00:00:00 2001 From: rachmadaniHaryono Date: Sat, 19 Sep 2020 19:20:03 +0800 Subject: [PATCH 1/3] chg: dev: org mode tag convertion --- buku | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/buku b/buku index 35934a1..f50dbaf 100755 --- a/buku +++ b/buku @@ -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())) From 55d048460e26d22419d43f5bea2379dae0d67a02 Mon Sep 17 00:00:00 2001 From: rachmadaniHaryono Date: Sat, 19 Sep 2020 19:21:18 +0800 Subject: [PATCH 2/3] new: test: convert_tags_to_org_mode_tags --- tests/test_buku.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/test_buku.py b/tests/test_buku.py index ab41e3f..cc88118 100644 --- a/tests/test_buku.py +++ b/tests/test_buku.py @@ -770,3 +770,17 @@ 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'], +]) +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 From 6eba4f44456a19d1a88c694a3b27ecd1e52ecb4c Mon Sep 17 00:00:00 2001 From: rachmadaniHaryono Date: Sat, 19 Sep 2020 19:24:40 +0800 Subject: [PATCH 3/3] new: test: issue test case --- tests/test_buku.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_buku.py b/tests/test_buku.py index cc88118..fbeb17a 100644 --- a/tests/test_buku.py +++ b/tests/test_buku.py @@ -779,6 +779,7 @@ def test_convert_bookmark_set(export_type, exp_res, monkeypatch): [',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