1
0
Fork 0

Set bold text with font-lock-face

This commit is contained in:
Bram Schoenmakers 2023-12-23 10:42:41 +01:00
parent 9a224efcf4
commit cabfd25d2e
Signed by: bram
GPG key ID: 0CCD19DFDC63258F

42
kagi.el
View file

@ -75,19 +75,41 @@ The token can be generated inside your account at https://kagi.com/settings?p=ap
(string-join curl-flags " ")
kagi-api-fastgpt-url)))
(defun kagi--html-bold-to-face (string)
"Convert HTML tags inside STRING to faces.
Fun fact: initial version of this function was generated by
FastGPT with the following prompt:
write an Emacs Lisp function that accepts a string with html
bold tags, and returns a string with bold face text properties
applied for the tags content atd the tags removed"
(with-temp-buffer
(insert string)
(goto-char (point-min))
(while (re-search-forward "<b>\\([^<]*\\)</b>" nil t)
(replace-match (propertize (match-string 1) 'font-lock-face 'bold) t nil))
(buffer-string)))
(defun kagi--format-output (output)
;; Replace Bold tags
(replace-regexp-in-string "</b>" "\e[0m"
(replace-regexp-in-string "<b>" "\e[1m" output)))
(kagi--html-bold-to-face output))
(defun kagi--format-reference-index (i)
(propertize (format "[%d]" i) 'font-lock-face 'bold))
(defun kagi--format-references (references)
(string-join (seq-map-indexed (lambda (ref i)
(let ((title (gethash "title" ref))
(snippet (gethash "snippet" ref))
(url (gethash "url" ref)))
(format "\e[1m[%d]\e[0m %s\n%s\n%s" (1+ i) title (kagi--format-output snippet) url)))
references)
"\n\n"))
(string-join
(seq-map-indexed (lambda (ref i)
(let ((title (gethash "title" ref))
(snippet (gethash "snippet" ref))
(url (gethash "url" ref)))
(format "%s %s\n%s\n%s"
(kagi--format-reference-index (1+ i))
title
(kagi--html-bold-to-face snippet) url)))
references)
"\n\n"))
(defun kagi--call-fastgpt (prompt)
(let ((command (kagi--build-curl-command prompt)))