0

I have an elisp script that creates some html pages. I would like to use a custom key-value pair that sets the class for the body part of my document. How can I write an elisp function that gets the name of the file and the name of the key, and returns the corresponding value? (By the way, I am very very much a beginner, so some detailed explanation would be appreciated.)

In simple terms: Suppose I have an org-document "A.org" with a custom property called ":grid" with value "main", and I have a script called "B.el" that should contain a function whose arguments are the name of the org file ("A.org", in this case) and the key of the property (":grid", in this case), and returns the corresponding value of the property ("main", in this case). How can I do that?

1
  • It's not clear what you mean: in one parts of the question, you want to get the value (out of an HTML document?), in another part you want to set the value (based on what?). Please provide a detailed description of what you are trying to do.
    – NickD
    Commented May 5 at 11:33

1 Answer 1

1

You have to find the file and then return the value(s) of the property:

 (defun my/prop-value-in-file (file prop)
   (save-excursion
     (find-file file)
     (org-property-values prop)))

Some problems:

  • As the name of the function implies, you may get multiple values, since the property can be defined at top level as well as under different headlines and be given a different value in each case. It's not clear whether you consider that a problem and if so, how to resolve it.

  • find-file creates a new buffer for the file, if necessary, or it returns an already existing buffer associated with the file (if the file has been previously opened). It is not clear what you want to do with the buffer, so the above function just lets it be. If you repeatedly call the function with different files, you'll end up with a bunch of buffers which eventually you will need to clean up.

As you can see, there are details that need to be considered and depending on how you want those details handled, the function may need changes, sometimes radical ones. The more you can say about its expected behavior, the better off you are.

So try the above function, but understand that you might need to change it to meet your (currently unstated) requirements.


N.B. I have ignored the second sentence in your question ("I would like to use a custom key-value pair that sets the class for the body part of my document") which AFAICT either contradicts or is completely unrelated to the new second paragraph of your amended question.

6
  • Thank you very much for your answer. I realize my question is not that specific or clear. I am running Emacs as emacs -Q --script publish.el, where publish.el is my script that takes care of generating the website I am building. This script reads some org files and converts them to html. My intention is to write something like :grid: full in the org file, so that the corresponding html file has a <main class="full">. I hope this clarifies my intention. Commented May 7 at 1:12
  • Yes, it clarifies your intention - a bit too late though: you should have said all that in the first place, or at least in your edit. I would then have told you that (AFAIK) you need to change the HTML exporter (or more likely define your own exporter, derived from the HTML exporter) to do what you want. All I can suggest now is for you to close this question and open another question in which you ask what you really wanted to do. Instead you misled me with your edit and now you say: "Surprise! That was not my question.". Next time, please include all relevant details.
    – NickD
    Commented May 7 at 1:30
  • Okay, okay, there's no need for sarcasm. Yes, I believe an apology is in order. I was not trying to confuse anybody, nor being vague. I actually thought the question was clear, perhaps because I am quite inexperienced with Emacs and StackExchange in general. I am closing this question and opening a new one. I appreciate your help. Commented May 7 at 1:55
  • I am not being sarcastic: I admit I was upset because I spent time on an answer to what turned out to be the wrong question. But I was misled by your edit: I should have stuck to my guns and kept to my original vote to close your question as unclear, instead of thinking that you had clarified it. So it is my fault as much as it is yours. But I hope that you include all relevant details in your questions in the future: you'll get better answers and I will be less of a grouch.
    – NickD
    Commented May 7 at 2:05
  • BTW, do you mean <body class="full"> on the last line of your comment?
    – NickD
    Commented May 7 at 2:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.