diff options
| author | 2012-12-28 20:24:20 +0100 | |
|---|---|---|
| committer | 2012-12-28 20:24:20 +0100 | |
| commit | 5535308fffdd58ac17937d676c096fe889acb77e (patch) | |
| tree | 9cbebf6655a102cf3fa02acbf92fdbc30d8da74d /src/linkwave.scm | |
| parent | 57f06ec8c977d9011b2692f20c7d5967c7d2a0cc (diff) | |
| download | markam-5535308fffdd58ac17937d676c096fe889acb77e.tar.gz markam-5535308fffdd58ac17937d676c096fe889acb77e.zip | |
Restructure
Put all source files in `src/' and add the `conkeror/' directory from
the old project.
Diffstat (limited to 'src/linkwave.scm')
| -rw-r--r-- | src/linkwave.scm | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/src/linkwave.scm b/src/linkwave.scm new file mode 100644 index 0000000..85e2d20 --- /dev/null +++ b/src/linkwave.scm @@ -0,0 +1,56 @@ +(declare (uses paths)) + +(require-extension sqlite3) +(require-library posix srfi-4) + +(: string-no-null (string -> string)) +(define (string-no-null str) + (if (and (> (string-length str) 0) + (char=? (string-ref str (- (string-length str) 1)) #\nul)) + (substring str 0 (- (string-length str) 1)) + str)) + +(: print-row (string fixnum string string -> void)) +(define (print-row url seconds name description) + (format #t "~a~% ~a~% ~a~% ~a~%~%" (string-no-null name) (string-no-null description) + (string-no-null url) (seconds->string seconds))) + +(define (add-tag db name) + (execute db "INSERT INTO tag VALUES (?)" name) + (last-insert-rowid db)) + +(define (add-bookmark db url name description tags) + (execute db "INSERT INTO bookmark VALUES (?, STRFTIME('%s'), ?, ?)" + url name description) + (let ((bookmark-id (last-insert-rowid db))) + (for-each (lambda (tag) + (let ((tag-id '())) + (condition-case + (set! tag-id (first-result db "SELECT rowid FROM tag WHERE name = ?" tag)) + (exn (exn sqlite3) + (if (eq? (get-condition-property exn 'sqlite3 'status) 'done) + (set! tag-id (add-tag db tag)) + (signal exn)))) + (execute db "INSERT INTO bookmark_tag VALUES (?, ?)" + bookmark-id tag-id))) + tags))) + +(define (url-string? str) + (and (> (string-length str) 7) + (or (string= (substring str 0 7) "http://") + (string= (substring str 0 8) "https://")))) + +(define (main args) + (let ((db (open-database (data-file "linkwave.db")))) + (if (null? args) + (for-each-row print-row db "select * from bookmark") + (cond + ((url-string? (car args)) + (with-transaction + db (lambda () (add-bookmark db (car args) (cadr args) (caddr args) (cdddr args)) #t))) + (else + (format #t "Unrecognized option: ~a~%" (car args))))) + + (finalize! db #t))) + +(main (command-line-arguments)) |
