Get rid of regex for markdown import.

This commit is contained in:
Arun Prakash Jana 2016-10-23 02:26:27 +05:30
parent 522b35b7ac
commit aad64474bc
3 changed files with 35 additions and 13 deletions

View File

@ -175,7 +175,8 @@ Shell completion scripts for Bash, Fish and Zsh can be found in respective subdi
use --tag to export only specific tags use --tag to export only specific tags
-i, --import file import bookmarks from html file; Firefox, -i, --import file import bookmarks from html file; Firefox,
Google Chrome and IE formats supported Google Chrome and IE formats supported
--markdown use markdown format, works with -e and -i --markdown use markdown with -e and -i
supported format: [title](url), 1 per line
-m, --merge file merge bookmarks from another buku database -m, --merge file merge bookmarks from another buku database
-p, --print [...] show details of bookmark by DB index -p, --print [...] show details of bookmark by DB index
accepts indices and ranges accepts indices and ranges
@ -359,7 +360,6 @@ The last index is moved to the deleted index to keep the DB compact.
15. **Search** bookmarks with **ALL** the keywords `kernel` and `debugging` in URL, title or tags: 15. **Search** bookmarks with **ALL** the keywords `kernel` and `debugging` in URL, title or tags:
$ buku -S kernel debugging $ buku -S kernel debugging
16. **Search** bookmarks **tagged** `general kernel concepts`: 16. **Search** bookmarks **tagged** `general kernel concepts`:
$ buku --st general kernel concepts $ buku --st general kernel concepts

42
buku
View File

@ -424,6 +424,11 @@ class BukuDb:
:param desc: string description :param desc: string description
''' '''
# Return error for empty URL
if not url or url == '':
logger.error('Invalid URL')
return
# Ensure that the URL does not exist in DB already # Ensure that the URL does not exist in DB already
id = self.get_bookmark_index(url) id = self.get_bookmark_index(url)
if id != -1: if id != -1:
@ -1062,11 +1067,15 @@ class BukuDb:
else: else:
f.write("List of buku bookmarks:\n\n") f.write("List of buku bookmarks:\n\n")
for row in resultset: for row in resultset:
out = '- [%s](%s)\n' % (row[2], row[1]) if row[2] == '':
out = '- [Untitled](%s)\n' % (row[1])
else:
out = '- [%s](%s)\n' % (row[2], row[1])
f.write(out) f.write(out)
count += 1 count += 1
f.close() f.close()
print('%s bookmarks exported' % count) print('%s exported' % count)
def import_bookmark(self, fp, markdown=False): def import_bookmark(self, fp, markdown=False):
'''Import bookmarks from a html file. '''Import bookmarks from a html file.
@ -1103,12 +1112,24 @@ class BukuDb:
f.close() f.close()
else: else:
with open(fp, mode='r', encoding='utf-8') as f: with open(fp, mode='r', encoding='utf-8') as f:
reg = re.compile("\[([^\]]+)\]\(([^\)]+)\)")
for line in f: for line in f:
m = reg.search(line) # Supported markdown format: [title](url)
if m: # Find position of title end, url start delimiter combo
desc = m.group(1) index = line.find('](')
self.add_bookmark(m.group(2), None, None, desc, True) if index != -1:
# Reverse find title start delimiter
title_start_delim = line[:index].rfind('[')
# Find the url end delimiter
url_end_delim = line[index + 2:].find(')')
if title_start_delim != -1 and url_end_delim > 0:
# Parse title
title = line[title_start_delim + 1:index]
# Parse url
url = line[index + 2:index + 2 + url_end_delim]
self.add_bookmark(url, title, None, None, True)
self.conn.commit() self.conn.commit()
f.close() f.close()
@ -1781,7 +1802,8 @@ if __name__ == '__main__':
use --tag to export only specific tags use --tag to export only specific tags
-i, --import file import bookmarks from html file; Firefox, -i, --import file import bookmarks from html file; Firefox,
Google Chrome and IE formats supported Google Chrome and IE formats supported
--markdown use markdown format, works with -e and -i --markdown use markdown with -e and -i
supported format: [title](url), 1 per line
-m, --merge file merge bookmarks from another buku database -m, --merge file merge bookmarks from another buku database
-p, --print [...] show details of bookmark by DB index -p, --print [...] show details of bookmark by DB index
accepts indices and ranges accepts indices and ranges
@ -1801,6 +1823,8 @@ if __name__ == '__main__':
help=argparse.SUPPRESS) help=argparse.SUPPRESS)
addarg('-i', '--import', nargs=1, dest='imports', metavar='file', addarg('-i', '--import', nargs=1, dest='imports', metavar='file',
help=argparse.SUPPRESS) help=argparse.SUPPRESS)
addarg('--markdown', dest='markdown', action='store_true',
help=argparse.SUPPRESS)
addarg('-m', '--merge', nargs=1, dest='merge', metavar='file', addarg('-m', '--merge', nargs=1, dest='merge', metavar='file',
help=argparse.SUPPRESS) help=argparse.SUPPRESS)
addarg('-p', '--print', nargs='*', dest='print', metavar='N', addarg('-p', '--print', nargs='*', dest='print', metavar='N',
@ -1809,8 +1833,6 @@ if __name__ == '__main__':
choices=[1, 2, 3], metavar='N', help=argparse.SUPPRESS) choices=[1, 2, 3], metavar='N', help=argparse.SUPPRESS)
addarg('-r', '--replace', nargs='+', dest='replace', addarg('-r', '--replace', nargs='+', dest='replace',
metavar=('oldtag', 'newtag'), help=argparse.SUPPRESS) metavar=('oldtag', 'newtag'), help=argparse.SUPPRESS)
addarg('--markdown', dest='markdown', action='store_true',
help=argparse.SUPPRESS)
addarg('-j', '--json', dest='json_output', action='store_true', addarg('-j', '--json', dest='json_output', action='store_true',
help=argparse.SUPPRESS) help=argparse.SUPPRESS)
addarg('--noprompt', dest='noninteractive', action='store_true', addarg('--noprompt', dest='noninteractive', action='store_true',

2
buku.1
View File

@ -124,7 +124,7 @@ Export bookmarks to Firefox bookmarks formatted HTML. Works with --tag to export
Import bookmarks exported from Firefox, Google Chrome or IE in HTML format. Import bookmarks exported from Firefox, Google Chrome or IE in HTML format.
.TP .TP
.BI \--markdown .BI \--markdown
Use markdown format for --export and --import. Use markdown for --export and --import. Supported format is '[title](url)', one entry per line.
.TP .TP
.BI \-m " " \--merge " file" .BI \-m " " \--merge " file"
Merge bookmarks from another Buku database file. Merge bookmarks from another Buku database file.