Combine two markup conversion functions into one
This commit is contained in:
parent
8a5a59f39f
commit
ef9b4c0578
1 changed files with 18 additions and 23 deletions
39
kagi.el
39
kagi.el
|
@ -136,40 +136,35 @@ https://help.kagi.com/kagi/api/summarizer.html."
|
|||
"--header" "Content-Type: application/json"
|
||||
"--data" "@-")))
|
||||
|
||||
(defun kagi--html-bold-to-face (string)
|
||||
"Convert HTML tags inside STRING to faces.
|
||||
(defconst kagi--markup-to-face
|
||||
'(("<b>" "</b>" 'bold)
|
||||
("```" "```" 'fixed-pitch))
|
||||
"Contains a mapping from markup elements to faces.")
|
||||
|
||||
Fun fact: initial version of this function was generated by
|
||||
(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 atd the tags removed"
|
||||
applied for the tags content and the tags removed"
|
||||
(with-temp-buffer
|
||||
(insert string)
|
||||
(dolist (entry kagi--markup-to-face)
|
||||
(goto-char (point-min))
|
||||
(let ((bold-match (rx (seq "<b>" (group (* (not "<"))) "</b>"))))
|
||||
(while (re-search-forward bold-match nil t)
|
||||
(replace-match (propertize (match-string 1) 'font-lock-face 'bold) t nil)))
|
||||
(buffer-string)))
|
||||
|
||||
(defun kagi--code-to-face (string)
|
||||
"Convert code inside STRING to faces.
|
||||
|
||||
In the FastGPT output, code is surrounded by three backticks (```)."
|
||||
(with-temp-buffer
|
||||
(insert string)
|
||||
(goto-char (point-min))
|
||||
(let ((code-match (rx (seq "```" (group (* (not "`"))) "```"))))
|
||||
(while (re-search-forward code-match nil t)
|
||||
(replace-match (propertize (match-string 1) 'font-lock-face 'fixed-pitch) t nil)))
|
||||
(let ((start (nth 0 entry))
|
||||
(end (nth 1 entry))
|
||||
(face (nth 2 entry))
|
||||
(regexp (rx (seq (literal start) (group (* any)) (literal end)))))
|
||||
(while (re-search-forward regexp nil t)
|
||||
(replace-match (propertize (match-string 1) 'font-lock-face face) t nil))))
|
||||
(buffer-string)))
|
||||
|
||||
(defun kagi--format-output (output)
|
||||
"Format the OUTPUT by replacing markup elements to proper faces."
|
||||
(thread-first output
|
||||
kagi--html-bold-to-face
|
||||
kagi--code-to-face))
|
||||
(kagi--convert-markup-to-faces output))
|
||||
|
||||
(defun kagi--format-reference-index (i)
|
||||
"Format the index of reference number I."
|
||||
|
|
Loading…
Reference in a new issue