1
0
Fork 0

Remove all own formatting code in favor of markdown-mode formatting

Do not attempt to format the Markdown returned by FastGPT ourselves,
but rely on markdown-mode to do this for us. This means an additional
dependency on the kagi.el package.
This commit is contained in:
Bram Schoenmakers 2024-07-22 19:49:04 +02:00
parent 8aafb8cf7f
commit 54bd8466a5
Signed by: bram
GPG key ID: 0CCD19DFDC63258F
2 changed files with 14 additions and 46 deletions

1
Cask
View file

@ -8,6 +8,7 @@
"kagi.el"
"ob-kagi-fastgpt.el")
(depends-on "markdown-mode")
(depends-on "shell-maker")
(development

59
kagi.el
View file

@ -6,7 +6,7 @@
;; Maintainer: Bram Schoenmakers <me@bramschoenmakers.nl>
;; Created: 16 Dec 2023
;; Package-Version: 0.5
;; Package-Requires: ((emacs "29.1") (shell-maker "0.46.1"))
;; Package-Requires: ((emacs "29.1") (markdown-mode "2.6") (shell-maker "0.46.1"))
;; Keywords: terminals wp
;; URL: https://codeberg.org/bram85/kagi.el
@ -53,6 +53,7 @@
;;; Code:
(require 'markdown-mode)
(require 'shell-maker)
(defcustom kagi-api-token nil
@ -203,14 +204,20 @@ returns a bullet list."
"Face for bold parts in the Kagi output."
:group 'kagi)
(define-obsolete-face-alias 'kagi-bold nil "0.6")
(defface kagi-italic '((t :inherit italic))
"Face for italic parts in the Kagi output."
:group 'kagi)
(define-obsolete-face-alias 'kagi-italic nil "0.6")
(defface kagi-code '((t :inherit fixed-pitch))
"Face for code parts in the Kagi output."
:group 'kagi)
(define-obsolete-face-alias 'kagi-code nil "0.6")
(defun kagi--gethash (hash &rest keys)
"Get the value inside a (nested) HASH following the sequence of KEYS."
(let ((value hash))
@ -221,43 +228,6 @@ returns a bullet list."
nil
value)))
(defconst kagi--markup-to-face
'(("<b>" "</b>" kagi-bold)
("**" "**" kagi-bold)
("$" "$" kagi-italic)
("```" "```" kagi-code))
"Contains a mapping from markup elements to faces.")
(defun kagi--convert-markup-to-faces (string)
"Convert markup elements inside STRING to faces.
Fun fact: the 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 and the tags removed"
(with-temp-buffer
(insert string)
(dolist (entry kagi--markup-to-face)
(cl-destructuring-bind (start end face) entry
(goto-char (point-min))
(let ((regexp (rx (seq (literal start)
(group (*? any))
(literal end)))))
(while (re-search-forward regexp nil t)
(let ((escaped-replacement (string-replace "\\" "\\\\" (match-string 1))))
(replace-match (propertize escaped-replacement 'font-lock-face face) t nil))))))
(buffer-string)))
(defun kagi--format-output (output)
"Format the OUTPUT by replacing markup elements to proper faces."
(kagi--convert-markup-to-faces output))
(defun kagi--format-reference-index (i)
"Format the index of reference number I."
(propertize (format "[%d]" i) 'font-lock-face 'kagi-bold))
(defun kagi--format-references (references)
"Format the REFRENCES as a string.
@ -269,9 +239,8 @@ https://help.kagi.com/kagi/api/fastgpt.html for more information."
(snippet (gethash "snippet" ref))
(url (gethash "url" ref)))
(format "%s %s\n%s\n%s"
(kagi--format-reference-index (1+ i))
title
(kagi--convert-markup-to-faces snippet) url)))
(format "[%d]" (1+ i))
title snippet url)))
references)
"\n\n"))
@ -375,7 +344,7 @@ Lisp code."
(references (kagi--gethash parsed-response "data" "references")))
(if output
(string-trim (format "%s\n\n%s"
(kagi--format-output output)
output
(kagi--format-references references)))
(if-let ((firsterror (aref (kagi--gethash parsed-response "error") 0)))
(error (format "%s (%s)"
@ -391,9 +360,7 @@ Lisp code."
(with-current-buffer (get-buffer-create buffer-name)
(save-excursion
(insert result))
(if (featurep 'markdown-mode)
(markdown-mode)
(text-mode))
(markdown-mode)
(read-only-mode)
(display-buffer buffer-name))))
@ -682,7 +649,7 @@ content."
(kagi--call-text-summarizer text-or-url)))
(parsed-response (json-parse-string response))
(output (kagi--gethash parsed-response "data" "output")))
(kagi--format-output output)
output
(if-let ((firsterror (aref (kagi--gethash parsed-response "error") 0)))
(error (format "%s (%s)"
(gethash "msg" firsterror)