jgm / djot

A light markup language
https://djot.net
MIT License
1.71k stars 43 forks source link

Syntax for very simple substitutions/snippets #182

Closed tmke8 closed 1 year ago

tmke8 commented 1 year ago

When writing papers in Latex, I often define very simple macros for a single word or expression, for which I want to make sure it's always spelled correctly or which I want to be able to change everywhere all at once.

So, for example

\def \NAS {National Academy of Science}
\def \author {William {\sc Smith}}

I was thinking the same could be done in djot where the substitution is defined externally and then you can reference it from within the djot document. For example, the substitution could be define in the YAML frontmatter, but it doesn't have to be; it just depends on your particular djot implementation/environment.

For the syntax, I was imagining something like @`author` but with @author as a shortcut if the snippet name only contains A-Za-z:

---
NAS: National Academy of Science
author: William Smith
---
Hi, I'm @author and work at the @NAS.

Of course, this could also be done with `author`{=frontmatter} but that's quite verbose.

Depending on the djot implementation/environment, there could also be other values that could be referenced, like @creationdate to get a nicely formatted form of the creation date of the document.

matklad commented 1 year ago

I think we already have it? :NAS: and :author: would be the syntax

Screenshot_20230107_141947

(we need to teach playground to share links...)

tmke8 commented 1 year ago

Ohhh, I thought that was only for emojis. Sorry for the noise!

jgm commented 1 year ago

teach the playground to share links

done

tbdalgaard commented 1 year ago

Wow that sounds cool if we could do that. Please let me know how Djot would handle situations like this. That would just save me for so much trouble while writing :)

Sendt fra min Mac Mini via Apple Mail

Den 7. jan. 2023 kl. 14.56 skrev Thomas MK @.***>:

When writing papers in Latex, I often define very simple macros for a single word or expression, for which I want to make sure it's always spelled correctly or which I want to be able to change everywhere all at once.

So, for example

\def \NAS {National Academy of Science} \def \author {William {\sc Smith}} I was thinking the same could be done in djot where the substitution is defined externally and then you can reference it from within the djot document. For example, the substitution could be define in the YAML frontmatter, but it doesn't have to be; it just depends on your particular djot implementation/environment.

For the syntax, I was imagining something like @author but with @author as a shortcut if the snippet name only contains A-Za-z:


NAS: National Academy of Science author: William Smith

Hi, I'm @author and work at the @NAS. Of course, this could also be done with author{=frontmatter} but that's quite verbose.

Depending on the djot implementation/environment, there could also be other values that could be referenced, like @creationdate to get a nicely formatted form of the creation date of the document.

— Reply to this email directly, view it on GitHub https://github.com/jgm/djot/issues/182, or unsubscribe https://github.com/notifications/unsubscribe-auth/AGGLXRXFM5RIESFNQYU62B3WRFYZPANCNFSM6AAAAAATT6YMLY. You are receiving this because you are subscribed to this thread.

jgm commented 1 year ago

Here's how: https://djot.net/playground/?text=Hi+%3Aauthor%3A%0A&filter=const+expansions+%3D+%7B%0A++author%3A+%22John+Smith%22%2C%0A++editor%3A+%22Sarah+Chen%22%0A%7D%0A%0Areturn+%7B%0A++symb%3A+%28el%29+%3D%3E+%7B%0A++++const+expansion+%3D+expansions%5Bel.alias%5D%3B%0A++++if+%28expansion%29+%7B%0A++++++el.tag+%3D+%22str%22%3B%0A++++++el.text+%3D+expansion%3B%0A++++++el.alias+%3D+undefined%3B%0A++++%7D%0A++%7D%0A%7D

Click on Filter to see the filter that is being used:

const expansions = {
  author: "John Smith",
  editor: "Sarah Chen"
}

return {
  symb: (el) => {
    const expansion = expansions[el.alias];
    if (expansion) {
      el.tag = "str";
      el.text = expansion;
      el.alias = undefined;
    }
  }
}

I will say, though, that I'm not really happy with the current filter API and it will probably change.

Altering elements in place like this is going to lead to stuff that doesn't fit the schema for the AST; it would probably be best to have these functions return a new AST value that replaces the old one.

matklad commented 1 year ago

functions return a new AST value that replaces the old one.

Or array of values, to delete/insert many

jgm commented 1 year ago

Or array of values, to delete/insert many

Yes, that's exactly the interface we have in pandoc.

Firefingertipcoder commented 1 year ago

Hi

Firefingertipcoder commented 1 year ago

Give me some work please