chg: dev: org mode tag convertion

This commit is contained in:
rachmadaniHaryono 2020-09-19 19:20:03 +08:00
parent 24600085fc
commit 117cf5e542

24
buku
View File

@ -2819,6 +2819,18 @@ PROMPT KEYS:
ConverterResult = TypedDict('ConverterResult', {'data': str, 'count': int}) if TypedDict else Dict[str, Any] 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( def convert_bookmark_set(
bookmark_set: List[BookmarkVar], bookmark_set: List[BookmarkVar],
export_type: str) -> ConverterResult: # type: ignore export_type: str) -> ConverterResult: # type: ignore
@ -2858,17 +2870,7 @@ def convert_bookmark_set(
out += '* [[{}][Untitled]]'.format(row[1]) out += '* [[{}][Untitled]]'.format(row[1])
else: else:
out += '* [[{}][{}]]'.format(row[1], row[2]) out += '* [[{}][{}]]'.format(row[1], row[2])
out += convert_tags_to_org_mode_tags(row[3])
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'
count += 1 count += 1
elif export_type == 'html': elif export_type == 'html':
timestamp = str(int(time.time())) timestamp = str(int(time.time()))