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
41
kagi.el
41
kagi.el
|
@ -136,40 +136,35 @@ https://help.kagi.com/kagi/api/summarizer.html."
|
||||||
"--header" "Content-Type: application/json"
|
"--header" "Content-Type: application/json"
|
||||||
"--data" "@-")))
|
"--data" "@-")))
|
||||||
|
|
||||||
(defun kagi--html-bold-to-face (string)
|
(defconst kagi--markup-to-face
|
||||||
"Convert HTML tags inside STRING to faces.
|
'(("<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:
|
FastGPT with the following prompt:
|
||||||
|
|
||||||
write an Emacs Lisp function that accepts a string with html
|
write an Emacs Lisp function that accepts a string with html
|
||||||
bold tags, and returns a string with bold face text properties
|
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
|
(with-temp-buffer
|
||||||
(insert string)
|
(insert string)
|
||||||
(goto-char (point-min))
|
(dolist (entry kagi--markup-to-face)
|
||||||
(let ((bold-match (rx (seq "<b>" (group (* (not "<"))) "</b>"))))
|
(goto-char (point-min))
|
||||||
(while (re-search-forward bold-match nil t)
|
(let ((start (nth 0 entry))
|
||||||
(replace-match (propertize (match-string 1) 'font-lock-face 'bold) t nil)))
|
(end (nth 1 entry))
|
||||||
(buffer-string)))
|
(face (nth 2 entry))
|
||||||
|
(regexp (rx (seq (literal start) (group (* any)) (literal end)))))
|
||||||
(defun kagi--code-to-face (string)
|
(while (re-search-forward regexp nil t)
|
||||||
"Convert code inside STRING to faces.
|
(replace-match (propertize (match-string 1) 'font-lock-face face) t nil))))
|
||||||
|
|
||||||
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)))
|
|
||||||
(buffer-string)))
|
(buffer-string)))
|
||||||
|
|
||||||
(defun kagi--format-output (output)
|
(defun kagi--format-output (output)
|
||||||
"Format the OUTPUT by replacing markup elements to proper faces."
|
"Format the OUTPUT by replacing markup elements to proper faces."
|
||||||
(thread-first output
|
(kagi--convert-markup-to-faces output))
|
||||||
kagi--html-bold-to-face
|
|
||||||
kagi--code-to-face))
|
|
||||||
|
|
||||||
(defun kagi--format-reference-index (i)
|
(defun kagi--format-reference-index (i)
|
||||||
"Format the index of reference number I."
|
"Format the index of reference number I."
|
||||||
|
|
Loading…
Reference in a new issue