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

Using groups for matching regex and other hacks #336

Open
haji-ali opened this issue Apr 30, 2024 · 3 comments
Open

Using groups for matching regex and other hacks #336

haji-ali opened this issue Apr 30, 2024 · 3 comments

Comments

@haji-ali
Copy link
Contributor

Apologies if this has been raised before. My limited search did not turn anything up.

I am trying to introduce a custom filetype for denote (for tex files), and I found the hackability of denote somewhat limiting. In particular, the way that denotes searches for titles and tags is by looking for a line starting with
title-key-regexp and keywords-key-regexp, respectively, and then using the rest of the line as the value.
In a tex file, I want my front matter to have a custom form that does not follow a key/value pairing (this is easy to achieve in TeX). I think a more general way to do this would be to use match groups in the title and keywords regex and use those to extract such values.

On the opposite end, front-matter imposes an order of the fields which is not always desirable. Is there a reason why denote doesn't implement a more flexible text-replacement strategy (For example, replacing %(title) with the title wherever it appears -- AUCTeX has a similar functionality).

@jeanphilippegg
Copy link
Contributor

Denote is an evolving project. With regard to the reordering, you can
check this section in the manual. You can use the "%1$s", "%2$s",
etc. syntax to reorder the elements of the front matter.

I agree that the front-matter specification could benefit from more
generality. In some of my experimental code, I have this:

(defvar denote-file-types
  `((org
     :extension ".org"
     :front-matter-start ""
     :front-matter-end "\n"
     :front-matter-components
     ((title
       :line-format "#+title:      <value>\n"
       :value-set-function identity
       :value-get-function denote-trim-whitespace)
      (date
       :line-format "#+date:       <value>\n"
       :value-set-function denote-date-org-timestamp
       :value-get-function denote-trim-whitespace)
      (keywords
       :line-format "#+filetags:   <value>\n"
       :value-set-function denote-format-keywords-for-org-front-matter
       :value-get-function denote-extract-keywords-from-front-matter))
     :link-format ,denote-org-link-format
     :link-in-context-regexp ,denote-org-link-in-context-regexp)))

This is not perfect and it is just an experiment, but it gives the kind
of generality that is achievable in theory. You can see the use of
"<value>" instead of a regexp or "%s" and the order would be simply
derived from the order of specification.

However, I don't believe that there are plans to work on this part of
the code for the next version of Denote (3.0.0). The next version should
be more about the generalization of the file naming scheme with its
creation and renaming commands with a few other specific features.

Do you have an example of the kind of front-matter that you would like
to have in .tex files? This would give a better idea of what needs to be
changed.

@haji-ali
Copy link
Contributor Author

haji-ali commented May 1, 2024

Sure, the case I have is that in TeX I want to define a command for a new denote entry as such

\newentry[TAGS]{TITLE}

for example

\newentry[emacs, denote]{My first
note with a newline}

Note that adding any \n in the title should be filtered out in title-value-reverse-function and replaced with a single space (this is how TeX would render such text).

I didn't know about the syntax for ordering fields in the front matter, so now I can achieve this with :front-matter as follows

\newentry[%3$s]{%1$s}

However, the parsing would still fail. I was hoping I can specify a regex along the lines (modulo escaping some characters)

\newentry[(?2:[^\\]*)]{(?1:[^}])}

to extract the title and the tags.

Note that I am currently able to hack my way around this limitation by setting both title-key-regexp and keywords-key-regexp to ^\\newentry and do the regex matching in the *-reverse-* functions though this would still not allow newlines in the title.

As an aside, is there any reason to include id, date and tags in the front matter (since this info is already in the filename)? I see from the link you provided that removing id and date is sanctioned with an example (though I am unsure if this is best practice) but it seems that denote expects the tags to be in the front matter in any case.

@protesilaos
Copy link
Owner

protesilaos commented May 1, 2024 via email

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

3 participants