From 117cf5e542d23c286823461b770d7d6e5f20f4bc Mon Sep 17 00:00:00 2001 From: rachmadaniHaryono Date: Sat, 19 Sep 2020 19:20:03 +0800 Subject: [PATCH] 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()))