Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ebib-import-file file rename #294

Open
rLry opened this issue Apr 1, 2024 · 6 comments
Open

ebib-import-file file rename #294

rLry opened this issue Apr 1, 2024 · 6 comments

Comments

@rLry
Copy link

rLry commented Apr 1, 2024

Hello, I just started switching from zotero to ebib
When I use ebib-import-file, the pdf is imported into the database and renamed to entry key.pdf, which is great. But I'm confused, is there a function how to customize the pdf name to date-titile.pdf? Thanks.

@rLry rLry changed the title ` ebib-import-file file rename Apr 1, 2024
@joostkremers
Copy link
Owner

Hi, the user option ebib-name-transform-function holds a function that takes a key and converts it to a file name (without the .pdf extension, which is added automatically). To convert the key to the format date-title.pdf, you'd need to write a function that takes the key, retrieves the title and returns a string of the desired format (without the extension.)

You can retrieve the title from the database with the function ebib-get-field-value, something like this:

(ebib-get-field-value "Title" "<key>" ebib--cur-db t t)

Check the doc string for ebib-get-field-value for details, you may want to pass slightly different arguments.

@rLry
Copy link
Author

rLry commented May 7, 2024

I know very little about Elisp. According to your opinion, the following code is written by ChatGPT.
It is right for me. Thanks.

(defun my/ebib-name-transform-function (key)
"Generate a filename based on the year, title, and publisher of the entry."
(let* ((year (ebib-get-field-value "year" key ebib--cur-db t t))
  (title (ebib-get-field-value "title" key ebib--cur-db t t))
  (publisher (ebib-get-field-value "publisher" key ebib--cur-db t t)))
 (if (and year title publisher)
 (format "%s-%s-%s" year title publisher)
 (progn
 (message "Year, title or publisher missing in entry %s" key)
 nil))))

(setq ebib-name-transform-function 'my/ebib-name-transform-function)

@rLry rLry closed this as completed May 7, 2024
@rLry rLry reopened this May 7, 2024
@joostkremers
Copy link
Owner

Well, let me just say that ChatGPT isn't much of an Elisp programmer... 😆

I'll take a look later today or tomorrow and come up with something better. 🙂

@joostkremers
Copy link
Owner

If you want to create a file name based on year, title and publisher, you can use something like this:

(defun my/ebib-name-transform-function (key)
  "Generate a filename based on the year, title, and publisher of the entry."
  (let ((year (ebib-get-field-value "year" key ebib--cur-db "XXXX" t))
        (title (ebib-get-field-value "title" key ebib--cur-db "No_Title" t))
        (publisher (ebib-get-field-value "publisher" key ebib--cur-db "No_Publisher" t)))
    (format "%s-%s-%s" year title publisher)))

This way you'll get default strings if year, title or publisher are not available.

If, on the other hand, you want to get an error if one of these is missing, you can use the following:

(defun my/ebib-name-transform-function (key)
  "Generate a filename based on the year, title, and publisher of the entry."
  (let ((year (ebib-get-field-value "year" key ebib--cur-db nil t))
        (title (ebib-get-field-value "title" key ebib--cur-db nil t))
        (publisher (ebib-get-field-value "publisher" key ebib--cur-db nil t)))
    (format "%s-%s-%s" year title publisher)))

@joostkremers
Copy link
Owner

Other variations are possible, of course, depending on your exact preferences.

@rLry
Copy link
Author

rLry commented May 8, 2024

thanks~It's good

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants