-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathprocess_url.el
More file actions
84 lines (70 loc) · 3 KB
/
Copy pathprocess_url.el
File metadata and controls
84 lines (70 loc) · 3 KB
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
;;; -*- lexical-binding: t -*-
(defun simpleweb-preprocess-url (input-url)
"Takes a URL as input, and returns a file location as output.
The file contains the result of applying the simpleweb LLM on
the contents of the URL. It's useful because the clojure webserver
will load the URL in Selenium, which ~might~ be able to do more processing
than the emacs web browser."
(let ((url-request-method "POST")
(url-request-extra-headers `(("Content-Type" . "application/x-www-form-urlencoded")))
(url-request-data (concat "url=" input-url)))
(with-current-buffer
(url-retrieve-synchronously "http://localhost:8131/simplifyURL")
(goto-char (point-min))
(re-search-forward "^$")
(delete-region (point) (point-min))
(string-trim (buffer-string)))))
(defun simpleweb--create-test-page-outputs ()
"Just a simple little util to grab test data
(the HTML content of a webpage without anything applied)
interactive development.
Writes the output to simpleweb--test-html-data"
(request "https://gptmafia.io"
:params '()
:parser 'buffer-string
:success (cl-function (lambda (&key data &allow-other-keys)
(when data
(with-current-buffer (get-buffer-create "*request demo*")
(erase-buffer)
(setq simpleweb--test-html-data data)
(insert data)
(pop-to-buffer (current-buffer))))))))
(defun simpleweb--simplify-html-page (html-contents callback)
"Simplify the contents of a webpage. This BLOCKS until the simplification returns.
Takes a string representing an html response as input, and
simplifies the HTML.
It uses a callback
(simplified-html: String) --> Void
to access the simplified HTML"
(request "http://localhost:8131/simplifyHTML"
:data (list (cons "contents" html-contents))
:sync t
:type "POST"
:parser 'buffer-string
:success (cl-function
(lambda (&key data &allow-other-keys)
(when data
(funcall callback data))))))
(defun simpleweb-simplify-html-advice-hook (start end)
"Simplify the contents of a webpage in the current buffer."
(let* ((html-contents (buffer-substring (point-min) (point-max)))
(active-buffer (current-buffer)))
(simpleweb--simplify-html-page
html-contents
(lambda (simplified-html)
(with-current-buffer active-buffer
(save-excursion
(goto-char start)
(delete-region start end)
(insert simplified-html)))))))
(defun simpleweb-initialize ()
(interactive)
(let* ((simpleweb-curr-filepath (find-lisp-object-file-name #'simpleweb-simplify-html-advice-hook 'defun))
(simpleweb-jar-file (concat (file-name-directory simpleweb-curr-filepath)
"simpleweb/target/simpleweb.jar")))
(start-process "simplify-web process" "*simplify-web-server*"
"java" "-cp" simpleweb-jar-file "clojure.main" "-m" "simpleweb.core")
(advice-add 'eww--preprocess-html :after #'simpleweb-simplify-html-advice-hook)))
;; (advice-add 'eww--preprocess-html :after #'simplify-html-advice-hook)
;; (advice-unadvice 'eww--preprocess-html)
;; eww--preprocess-html