Use thread_last to make results rendering funcs more readable in khoj.el

This commit is contained in:
Debanjum Singh Solanky 2023-01-26 18:59:44 -03:00
parent a8ab9448da
commit 85ae46f429

View file

@ -169,74 +169,80 @@ Use `which-key` if available, else display simple message in echo area"
(defun khoj--extract-entries-as-markdown (json-response query) (defun khoj--extract-entries-as-markdown (json-response query)
"Convert JSON-RESPONSE, QUERY from API to markdown entries." "Convert JSON-RESPONSE, QUERY from API to markdown entries."
;; remove leading (, ) or SPC from extracted entries string (thread-last
(replace-regexp-in-string json-response
"^[\(\) ]" "" ;; Extract and render each markdown entry from response
;; extract entries from response as single string and convert to entries (mapcar (lambda (json-response-item)
(format "# %s\n%s" (thread-last
query ;; Extract markdown entry from each item in json response
(mapcar (cdr (assoc 'entry json-response-item))
(lambda (args) ;; Format markdown entry as a string
(replace-regexp-in-string (format "%s")
"^\#+" "##" ;; Standardize results to 2nd level heading for consistent rendering
(format "%s" (cdr (assoc 'entry args))))) (replace-regexp-in-string "^\#+" "##"))))
json-response)))) ;; Render entries into markdown formatted string with query set as as top level heading
(format "# %s\n%s" query)
;; remove leading (, ) or SPC from extracted entries string
(replace-regexp-in-string "^[\(\) ]" "")))
(defun khoj--extract-entries-as-org (json-response query) (defun khoj--extract-entries-as-org (json-response query)
"Convert JSON-RESPONSE, QUERY from API to `org-mode' entries." "Convert JSON-RESPONSE, QUERY from API to `org-mode' entries."
;; remove leading (, ) or SPC from extracted entries string (let ((org-results-buffer-format-str "* %s\n%s\n#+STARTUP: showall hidestars inlineimages"))
(replace-regexp-in-string (thread-last
"^[\(\) ]" "" json-response
;; extract entries from response as single string and convert to entries ;; Extract and render each org-mode entry from response
(format "* %s\n%s\n#+STARTUP: showall hidestars inlineimages" (mapcar (lambda (json-response-item)
query (thread-last
(mapcar ;; Extract org entry from each item in json response
(lambda (args) (cdr (assoc 'entry json-response-item))
(replace-regexp-in-string ;; Format org entry as a string
"^\*+" "**" (format "%s")
(format "%s" (cdr (assoc 'entry args))))) ;; Standardize results to 2nd level heading for consistent rendering
json-response)))) (replace-regexp-in-string "^\*+" "**"))))
;; Render entries into org formatted string with query set as as top level heading
(format org-results-buffer-format-str query)
;; remove leading (, ) or SPC from extracted entries string
(replace-regexp-in-string "^[\(\) ]" ""))))
(defun khoj--extract-entries-as-images (json-response query) (defun khoj--extract-entries-as-images (json-response query)
"Convert JSON-RESPONSE, QUERY from API to html with images." "Convert JSON-RESPONSE, QUERY from API to html with images."
;; remove leading (, ) or SPC from extracted entries string (let ((image-results-buffer-html-format-str "<html>\n<body>\n<h1>%s</h1>%s\n\n</body>\n</html>")
(replace-regexp-in-string ;; Format string to wrap images into html img, href tags with metadata in headings
"[\(\) ]$" "" (image-result-html-format-str "\n\n<h2>Score: %s Meta: %s Image: %s</h2>\n\n<a href=\"%s\">\n<img src=\"%s?%s\" width=%s height=%s>\n</a>"))
;; remove leading (, ) or SPC from extracted entries string (thread-last
(replace-regexp-in-string json-response
"^[\(\) ]" "" ;; Extract each image entry from response and render as html
;; extract entries from response as single string and convert to entries (mapcar (lambda (json-response-item)
(format "<html>\n<body>\n<h1>%s</h1>%s\n\n</body>\n</html>" (let ((score (cdr (assoc 'score json-response-item)))
query (metadata_score (cdr (assoc 'metadata_score (assoc 'additional json-response-item))))
(mapcar (image_score (cdr (assoc 'image_score (assoc 'additional json-response-item))))
(lambda (args) (format (image_url (concat khoj-server-url (cdr (assoc 'entry json-response-item)))))
"\n\n<h2>Score: %s Meta: %s Image: %s</h2>\n\n<a href=\"%s%s\">\n<img src=\"%s%s?%s\" width=%s height=%s>\n</a>" ;; Wrap images into html img, href tags with metadata in headings
(cdr (assoc 'score args)) (format image-result-html-format-str
(cdr (assoc 'metadata_score (assoc 'additional args))) ;; image scores metadata
(cdr (assoc 'image_score (assoc 'additional args))) score metadata_score image_score
khoj-server-url ;; image url
(cdr (assoc 'entry args)) image_url image_url (random 10000)
khoj-server-url ;; image dimensions
(cdr (assoc 'entry args)) khoj-image-width khoj-image-height))))
(random 10000) ;; Collate entries into single html page string
khoj-image-width (format image-results-buffer-html-format-str query)
khoj-image-height)) ;; remove leading (, ) or SPC from extracted entries string
json-response))))) (replace-regexp-in-string "^[\(\) ]" "")
;; remove trailing (, ) or SPC from extracted entries string
(replace-regexp-in-string "[\(\) ]$" ""))))
(defun khoj--extract-entries-as-ledger (json-response query) (defun khoj--extract-entries-as-ledger (json-response query)
"Convert JSON-RESPONSE, QUERY from API to ledger entries." "Convert JSON-RESPONSE, QUERY from API to ledger entries."
;; remove leading (, ) or SPC from extracted entries string (thread-last json-response
(replace-regexp-in-string ;; extract and render entries from API response
"[\(\) ]$" "" (mapcar (lambda (args) (format "%s\n\n" (cdr (assoc 'entry args)))))
(replace-regexp-in-string ;; Set query as heading in rendered results buffer
"^[\(\) ]" "" (format ";; %s\n\n%s\n" query)
;; extract entries from response as single string and convert to entries ;; remove leading (, ) or SPC from extracted entries string
(format ";; %s\n\n%s\n" (replace-regexp-in-string "^[\(\) ]" "")
query ;; remove trailing (, ) or SPC from extracted entries string
(mapcar (replace-regexp-in-string "[\(\) ]$" "")))
(lambda (args)
(format "%s\n\n" (cdr (assoc 'entry args))))
json-response)))))
(defun khoj--buffer-name-to-content-type (buffer-name) (defun khoj--buffer-name-to-content-type (buffer-name)
"Infer content type based on BUFFER-NAME." "Infer content type based on BUFFER-NAME."