Last active
March 19, 2021 08:39
-
-
Save aoba17/8ad43934b2b635f3b5d4f03aa77cb09a to your computer and use it in GitHub Desktop.
Mac版のKindleクライアントからコピーした情報を整形しorgファイルに貼り付けるbabashkaスクリプト
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bb | |
(require '[clojure.java.shell :refer [sh]] | |
'[clojure.string :as string]) | |
(def default-file "highlights.org") | |
;; (def test-text | |
;; "プリズム──本来の名称である「プラガ世界間通信機器」の頭字語をもじった呼び名──には、赤と青のLEDが一個ずつついている。 | |
;; テッド チャン. 息吹 (Japanese Edition) (Kindle の位置No.4816-4818). Kindle 版. ") | |
(defn get-clipboard [] | |
(-> (sh "pbpaste") :out)) | |
(defn clipboard->org [] | |
(let [highlight (string/split (get-clipboard) #"\n") | |
body (or (first highlight) "") | |
meta-info (or (last highlight) "") | |
book-info (re-find #"^.+(?=\(Kindle の位置)" meta-info) | |
[author title] (try | |
(string/split book-info #"\. ") | |
(catch Exception _ nil)) | |
location (re-find #"(?<=Kindle の位置)No.[0-9-]+" meta-info)] | |
(if (and body author title location) | |
(let [header (str "* " (string/trim author) | |
"『" (string/trim title) "』" | |
" - " location) | |
body (str " " body "\n")] | |
(spit default-file (string/join "\n" [header body]) :append true) | |
(System/exit 0)) | |
(do | |
(println "Invalid Input") | |
(System/exit 1))))) | |
(clipboard->org) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment