Merge PR 260: Import and export of orgfile

This commit is contained in:
Arun Prakash Jana 2018-05-11 06:46:47 +05:30
parent b6dda452a7
commit 2e61ab72a9
No known key found for this signature in database
GPG Key ID: A75979F35C080412
3 changed files with 79 additions and 11 deletions

View File

@ -76,7 +76,7 @@ Missing a feature? There's a rolling [ToDo List](https://github.com/jarun/Buku/i
- Open bookmarks and search results in browser
- Manual encryption support
- Auto-import from Firefox, Google Chrome and Chromium
- Import/export bookmarks from/to HTML or Markdown
- Import/export bookmarks from/to HTML, Markdown or Orgfile
- Shorten and expand URLs
- Smart tag management using redirection (>>, >, <<)
- Portable, merge-able database to sync between systems
@ -221,10 +221,13 @@ POWER TOYS:
-e, --export file export bookmarks to Firefox format html
export markdown, if file ends with '.md'
format: [title](url), 1 entry per line
export orgfile, if file ends with '.org'
format: *[[url][title]], 1 entry per line
export buku DB, if file ends with '.db'
use --tag to export specific tags
-i, --import file import bookmarks html in Firefox format
import markdown, if file ends with '.md'
import orgfile, if file ends with '.org'
import buku DB, if file ends with '.db'
-p, --print [...] show record details by indices, ranges
print all bookmarks, if no arguments
@ -320,16 +323,18 @@ PROMPT KEYS:
$ buku -u 15012014 -c this is a new comment
Applies to --url, --title and --tag too.
9. **Export** bookmarks tagged `tag 1` or `tag 2` to HTML and markdown:
9. **Export** bookmarks tagged `tag 1` or `tag 2` to HTML, markdown or orgfile:
$ buku -e bookmarks.html --tag tag 1, tag 2
$ buku -e bookmarks.md --tag tag 1, tag 2
$ buku -e bookmarks.org --tag tag 1, tag 2
$ buku -e bookmarks.db --tag tag 1, tag 2
All bookmarks are exported if --tag is not specified.
10. **Import** bookmarks from HTML and markdown:
10. **Import** bookmarks from HTML, markdown or orgfile:
$ buku -i bookmarks.html
$ buku -i bookmarks.md
$ buku -i bookmarks.org
$ buku -i bookmarks.db
11. **Delete only comment** for bookmark at 15012014:

16
buku.1
View File

@ -17,7 +17,7 @@ is a command-line utility to store, tag, search and organize bookmarks.
* Open bookmarks and search results in browser
* Manual encryption support
* Auto-import from Firefox, Google Chrome and Chromium
* Import/export bookmarks from/to HTML or Markdown
* Import/export bookmarks from/to HTML, Markdown or Orgfile
* Shorten and expand URLs
* Smart tag management using redirection (>>, >, <<)
* Portable, merge-able database to sync between systems
@ -79,7 +79,7 @@ Bookmarks with immutable titles are listed with '(L)' after the title.
- Auto-import looks in the default installation path and default user profile.
- URLs starting with `place:`, `file://` and `apt:` are ignored during import.
- Parent folder (and subfolder) names are automatically imported as tags if --tacit is used.
- An auto-generated tag in the format 'YYYYMonDD' is added if --tacit is not used in html or markdown import.
- An auto-generated tag in the format 'YYYYMonDD' is added if --tacit is not used in html, markdown or orgfile import.
.PP
.IP 10. 4
\fBEncryption\fR is optional and manual. AES256 algorithm is used. To use encryption, the database file should be unlocked (-k) before using \fBbuku\fR and locked (-l) afterwards. Between these 2 operations, the database file lies unencrypted on the disk, and NOT in memory. Also, note that the database file is \fBunencrypted on creation\fR.
@ -193,14 +193,18 @@ Export bookmarks to Firefox bookmarks formatted HTML. Works with --tag to export
.I file
has extension '.md'.
.br
Markdown format: [title](url), 1 entry per line. A buku database is generated if
Markdown format: [title](url), 1 entry per line. Orgfile is used if
.I file
has extension '.org'
.br
Orgfile format: * [[url][title]], 1 entry per line. A buku database is generated if
.I file
has extension '.db'.
.TP
.BI \-i " " \--import " file"
Import bookmarks from Firefox bookmarks formatted html.
.I file
is considered Markdown (compliant with --export format) if it has '.md' extension or another buku database if the extension is '.db'.
is considered Markdown (compliant with --export format) if it has '.md' extension, orgfile if the extension is '.org' or another buku database if the extension is '.db'.
.TP
.BI \-p " " \--print " [...]"
Show details (DB index, URL, title, tags and comment) of bookmark record by DB index. If no arguments, all records with actual index from DB are shown. Accepts hyphenated ranges and space-separated indices. A negative value (introduced for convenience) behaves like the tail utility, e.g., -n shows the details of the last n bookmarks.
@ -511,7 +515,7 @@ This will open the existing bookmark's details in the editor for modifications.
Applies to --url, --title and --tag too.
.PP
.IP 9. 4
\fBExport\fR bookmarks tagged 'tag 1' or 'tag 2' to HTML and markdown:
\fBExport\fR bookmarks tagged 'tag 1' or 'tag 2' to HTML, markdown or orgfile:
.PP
.EX
.IP
@ -526,7 +530,7 @@ Applies to --url, --title and --tag too.
All bookmarks are exported if --tag is not specified.
.PP
.IP 10. 4
\fBImport\fR bookmarks from HTML and markdown:
\fBImport\fR bookmarks from HTML, markdown or orgfile:
.PP
.EX
.IP

63
buku.py
View File

@ -1898,7 +1898,10 @@ class BukuDb:
If destination file name ends with '.db', bookmarks are
exported to a Buku database file.
If destination file name ends with '.md', bookmarks are
exported to a markdown file. Otherwise, bookmarks are
exported to a markdown file.
If destination file name ends with '.org' bookmarks are
exported to a org file.
Otherwise, bookmarks are
exported to a Firefox bookmarks.html formatted file.
Parameters
@ -1980,6 +1983,15 @@ class BukuDb:
out = '- [' + row[2] + '](' + row[1] + ')\n'
outfp.write(out)
count += 1
elif filepath.endswith('.org'):
for row in resultset:
if row[2] == '':
out = '* [[{}][Untitled]]\n'.format(row[1])
else:
out = '* [[{}][{}]]\n'.format(row[1], row[2])
outfp.write(out)
count += 1
else:
outfp.write('<!DOCTYPE NETSCAPE-Bookmark-file-1>\n\n'
'<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">\n'
@ -2233,7 +2245,7 @@ class BukuDb:
"""Import bookmarks from a html or a markdown file.
Supports Firefox, Google Chrome, and IE exported html bookmarks.
Supports markdown files with extension '.md'.
Supports markdown files with extension '.md, .org'.
Supports importing bookmarks from another Buku database file.
Parameters
@ -2265,6 +2277,11 @@ class BukuDb:
self.add_rec(*item)
self.conn.commit()
elif filepath.endswith('org'):
for item in import_org(filepath=filepath, newtag=newtag):
self.add_rec(*item)
else:
try:
import bs4
@ -2650,6 +2667,45 @@ def import_md(filepath, newtag):
if newtag else None, None, 0, True
)
def import_org(filepath, newtag):
"""Parse bookmark org file.
Parameters
----------
filepath : str
Path to org file.
newtag : str
New tag for bookmarks in org file.
Returns
-------
tuple
Parsed result.
"""
with open(filepath, mode='r', encoding='utf-8') as infp:
# Supported markdown format: * [[url][title]]
# Find position of url end, title start delimiter combo
for line in infp:
index = line.find('][')
if index != -1:
# Find url start delimiter
url_start_delim = line[:index].find('[[')
# Reverse find title end delimiter
title_end_delim = line[index + 2:].rfind(']]')
if url_start_delim != -1 and title_end_delim > 0:
# Parse title
title = line[index + 2: index + 2 + title_end_delim]
# Parse url
url = line[url_start_delim + 2:index]
if (is_nongeneric_url(url)):
continue
yield (
url, title, delim_wrap(newtag)
if newtag else None, None, 0, True
)
def import_html(html_soup, add_parent_folder_as_tag, newtag):
"""Parse bookmark html.
@ -4173,10 +4229,13 @@ POSITIONAL ARGUMENTS:
-e, --export file export bookmarks to Firefox format html
export markdown, if file ends with '.md'
format: [title](url), 1 entry per line
export orgfile, if file ends with '.org'
format: *[[url][title]], 1 entry per line
export buku DB, if file ends with '.db'
use --tag to export specific tags
-i, --import file import bookmarks html in Firefox format
import markdown, if file ends with '.md'
import orgfile, if file ends with '.org'
import buku DB, if file ends with '.db'
-p, --print [...] show record details by indices, ranges
print all bookmarks, if no arguments