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

A "Hello World"? #38

Open
rusimody opened this issue Apr 23, 2022 · 5 comments
Open

A "Hello World"? #38

rusimody opened this issue Apr 23, 2022 · 5 comments

Comments

@rusimody
Copy link

rusimody commented Apr 23, 2022

I find this project more promising and accessible than org-element.

Thanks for the efforts!

But is there somewhere a "hello world"?

Yeah I see a good amt of documentation in docs/api-refeence.md

I am looking for a simple end-to-end reference with setting up of hooks, export etc

ie some toy example that uses org-ml API and say transforms some org file into another?

If possible something that transforms:

*** Lorem Ipsum :LAW:

into

#+begin_law
Lorem Ipsum
#+end_law

would be wonderful.

But if even a simpler eg is given/pointed out I can hack from there on.

@ndwarshuis
Copy link
Owner

I've been meaning to build a 'cookbook' which would probably fill the need you are describing. I've been busy with other things but am hoping to return to that soon.

In the meantime, the code that does what you want is this:

(org-ml-update-this-headline*
  (org-ml-build-special-block
    (downcase (car (org-ml-get-property :tags it)))
    (org-ml-build-paragraph (org-ml-get-property :raw-value it))))

This is actually an example I hadn't considered yet. I only envisioned org-ml-update-this-headline and the like to be used to modify headlines, but apparently it can transform headlines into something other than a headline, so thank you for the question ;)

@rusimody
Copy link
Author

rusimody commented Apr 23, 2022

Thanks much for the response.
But I still dont know how to use it😄
Has to be put on some hook I guess??

I see that if I put point on a suitable headline it transforms it.
But on a headline without the suitable tag I get an error (wrong-type-argument char-or-string-p nil)

More fundamentally: I dont want to change the file literally; I want to change it "as if" during export (currently latex)

I was just perusing the org hooks and there are apparently hundreds!!!!!
[Ok ½-hundred is I think not an exaggeration]

@ndwarshuis
Copy link
Owner

ndwarshuis commented Apr 24, 2022

Not sure I understand your use case. Are you trying to recreate what would happen if you exported using the latex exporter?

If you don't want to actually change the file you can just emit a string like so:

(let* ((hl (org-ml-parse-this-headline))
       (sb (org-ml-build-special-block
            (downcase (car (org-ml-get-property :tags hl)))
            (org-ml-build-paragraph (org-ml-get-property :raw-value hl)))))
  (org-ml-to-string sb))

This (and the above example) are only going to work on headlines with at least one tag; if you want to make it more robust there needs to be logic to check the validity of the tag itself.

@rusimody
Copy link
Author

Not sure I understand your use case. Are you trying to recreate what would happen if you exported using the latex exporter?

I want to export to latex
The file that org sees as input for latex-export should be identical to my file:
Except that

  • tag :L should become #+begin_law
  • tag:A should become #+begin_action
  • etc

@ndwarshuis
Copy link
Owner

I don't use the latex exporter very much so I can't say how exactly this would fit into what you want. It seems the exporter should have some built in functionality to map tags to different block names.

The only (easy) way I can see org-ml being useful here is to transform your file into a temporary buffer that you then run through the latex exporter. You would basically copy the file and then run something like org-ml-update-subtrees or org-ml-update-headlines which will apply a transformer function to all subtrees/headlines (whichever is more convenient). There's probably some clever way you could even advise the current export function to transparently do this if that's the desired outcome.

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