From fdcde7d425ed371b7c886846526fc19b04abaf77 Mon Sep 17 00:00:00 2001 From: Bram Schoenmakers Date: Tue, 6 Aug 2024 06:12:12 +0200 Subject: [PATCH] Add CSV export --- README.org | 3 ++- elfeed-export.el | 34 ++++++++++++++++++++++++++++++++++ elfeed-export.info | 15 ++++++++------- 3 files changed, 44 insertions(+), 8 deletions(-) diff --git a/README.org b/README.org index 18001a2..af9b3ec 100644 --- a/README.org +++ b/README.org @@ -11,8 +11,9 @@ The following export formats are currently supported: | Format | Command | |-------------------------+----------------------------| -| JSON | ~elfeed-export-to-json-file~ | +| CSV | ~elfeed-export-to-csv-file~ | | Emacs Lisp Data (*.eld) | ~elfeed-export-to-lisp-file~ | +| JSON | ~elfeed-export-to-json-file~ | Please note there is *no import functionality*. The goal is to have a (more or less) readable copy of your database. For a real backup, make a backup of ~/.elfeed instead. diff --git a/elfeed-export.el b/elfeed-export.el index b4d8bf2..52832c1 100644 --- a/elfeed-export.el +++ b/elfeed-export.el @@ -133,6 +133,34 @@ the entry should be exported." "Return the Lisp data string for all entries that should be exported." (prin1-to-string (elfeed-export--entries))) +(defun elfeed-export--format-csv-value (value) + (let* ((string-value (if (listp value) + (format "[%s]" + (mapconcat (lambda (elm) + (format "%s" elm)) + value ";")) + (format "%s" value))) + (escaped-string (string-replace "\"" "\"\"" string-value))) + (format "\"%s\"" escaped-string))) + +;;;###autoload +(defun elfeed-export-to-csv () + "Return the CSV string for all entries that should be exported." + (let* ((entries (elfeed-export--entries)) + (columns (map-keys (seq-first entries)))) + (concat + ;; header + (string-join columns ",") + "\n" + ;; data + (mapconcat (lambda (entry) + (mapconcat (lambda (column) + (elfeed-export--format-csv-value (alist-get column entry))) + columns + ",")) + entries + "\n")))) + ;;;###autoload (defun elfeed-export-to-json-file (path) "Write the JSON string to a file at PATH." @@ -145,6 +173,12 @@ the entry should be exported." (interactive "F") (write-region (elfeed-export-to-lisp) nil path nil nil nil t)) +;;;###autoload +(defun elfeed-export-to-csv-file (path) + "Write the CSV string to a file at PATH." + (interactive "F") + (write-region (elfeed-export-to-csv) nil path nil nil nil t)) + (provide 'elfeed-export) ;;; elfeed-export.el ends here diff --git a/elfeed-export.info b/elfeed-export.info index c079d2c..5b28c68 100644 --- a/elfeed-export.info +++ b/elfeed-export.info @@ -39,8 +39,9 @@ Emacs (https://github.com/skeeto/elfeed). Format Command --------------------------------------------------------- -JSON ‘elfeed-export-to-json-file’ +CSV ‘elfeed-export-to-csv-file’ Emacs Lisp Data (*.eld) ‘elfeed-export-to-lisp-file’ +JSON ‘elfeed-export-to-json-file’ Please note there is *no import functionality*. The goal is to have a (more or less) readable copy of your database. For a real backup, @@ -162,12 +163,12 @@ configuration snippet: Tag Table: Node: Top87 Node: Introduction406 -Node: Installation1092 -Node: Configuration1497 -Node: Customize export fields2231 -Node: Customize entry filters3328 -Node: By tag3740 -Node: By feed4214 +Node: Installation1150 +Node: Configuration1555 +Node: Customize export fields2289 +Node: Customize entry filters3386 +Node: By tag3798 +Node: By feed4272  End Tag Table