-
Notifications
You must be signed in to change notification settings - Fork 49
/
elnode-bm.el
110 lines (98 loc) · 3.05 KB
/
elnode-bm.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
;;; elnode-bm.el -- bookmarking helpers with elnode -*- lexical-binding: t -*-
(require 'elnode)
(require 's)
(defgroup elnode-bookmark nil
"The Elnode bookmarker application."
:group 'elnode)
(defcustom elnode-bookmark-file-name "~/bookmarks.org"
"The filename used to log bookmarks.
This file is expanded to find the file that bookmarks will be
stored in."
:group 'elnode-bookmark
:type 'filename)
(defun elnode-stud (port forward-port pem-file)
"Start stud on PORT, sending to FORWARD-PORT with PEM-FILE."
(start-process
"elnode-stud" "elnode-stud"
"stud" "-ssl" "-b" "127.0.0.1,8004" "-f" "*,8443" (expand-file-name pem-file))
(switch-to-buffer "elnode-stud"))
(defun elnode-bm-time->org (time)
"Format the specified time in org-mode format."
(format-time-string "[%Y-%m-%d %a %H:%M]" time))
(defun elnode-bm-save (httpcon)
"Take a bookmarklet and save it."
(let* ((method (elnode-http-method httpcon))
(page (decode-coding-string
(elnode-http-param httpcon "u")
'utf-8))
(title (or
(decode-coding-string
(elnode-http-param httpcon "i")
'utf-8)
page))
(time (seconds-to-time
(/ (string-to-int
(elnode-http-param httpcon "t"))
1000))))
(with-current-buffer
(find-file-noselect
(expand-file-name elnode-bookmark-file-name))
(case major-mode
('org-mode
(save-excursion
(goto-char (point-min))
(let ((org-time-str
(elnode-bm-time->org time)))
(insert
(s-lex-format
"* [[${page}][${title}]] ${org-time-str}\n")))))))
(elnode-send-json httpcon (list :ok))))
(defun elnode-bm-chrome-ext (httpcon)
"Send the chrome extension."
(elnode-send-file
httpcon
(concat
(file-name-directory
(or (buffer-file-name)
load-file-name
default-directory))
"bookmark4chrome.crx")
:mime-types
'(("application/x-chrome-extension" . "crx"))))
(defun elnode-bm-index (httpcon)
(elnode-send-html httpcon "<html>
<style>
body { font-family: sans-serif;}
</style>
<body>
<h1>Elnode Bookmarking IT</h1>
chrome extension: <a href=\"/elnode-bookmarks.crx\">elnode</a><br></br>
bookmarklet: <a href=\"javascript:function elnodebm001(){
var d=document,
i=''+d.title,
z=d.createElement('scr'+'ipt'),
b=d.body,
l=d.location;
try{
if(!b)throw(0);
z.setAttribute('src','http://localhost:8004/bm/save?u='
+encodeURIComponent(l.href)
+'&t='+(new Date().getTime())
+'&i='+encodeURIComponent(i));
b.appendChild(z);
}catch(e){
alert('Please wait until the page has loaded.');
}
}
elnodebm001();void(0)\">elnode</a>
</body></html>"))
;;;###autoload
(defun elnode-bm-handler (httpcon)
(elnode-hostpath-dispatcher
httpcon
'(("^[^/]+//$" . elnode-bm-index)
("^[^/]+//elnode-bookmarks.crx" . elnode-bm-chrome-ext)
("^[^/]+//bm/save" . elnode-bm-save)
("^[^/]+//bm/report" . elnode-bm-report))))
(provide 'elnode-bm)
;;; elnode-bm.el ends here