emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [Question] Custom parse tree filter
@ 2021-05-16 15:31 Juan Manuel Macías
  2021-05-16 15:59 ` Nicolas Goaziou
  0 siblings, 1 reply; 3+ messages in thread
From: Juan Manuel Macías @ 2021-05-16 15:31 UTC (permalink / raw)
  To: orgmode

Hi all,

I am writing a custom parse tree filter that does the following (LaTeX
backend): if a heading has the ':font:' property, the content of that
heading is enclosed in a LaTeX group. If the property is ':fontfeature:',
then the content is enclosed in a different group. The filter works fine
when all the headings are at the same level. But with different levels,
it does not returns the expected result. It's evident that I'm doing
something catastrophically wrong :-). I wonder if anyone could put me on
the track of the origin of my error...

Below, the offender function and a sample. Thanks in advance!

Best regards,

Juan Manuel 

#+BIND: org-export-filter-parse-tree-functions (my-custom-filters/fontspec-headline)
#+begin_src emacs-lisp :exports results :results none
  (defun my-custom-filters/fontspec-headline (tree backend info)
    (when (org-export-derived-backend-p backend 'latex)
      (org-element-map tree 'headline
	(lambda (hl)
	  (cond ((org-element-property :FONT hl)
		 (let* ((font (org-element-property :FONT hl))
			(contents (org-element-interpret-data (org-element-contents hl)))
			(contents-new (concat
				       "@@latex:{\\fontspec{@@"
				       (replace-regexp-in-string "\s*\\(\\[.+\\]\\)\s*" "" font)
				       "@@latex:}%@@\n"
				       (if (string-match "\\(\\[.+\\]\\)" font)
					   (concat "@@latex:" (match-string 1 font) "%@@\n\n")
					 "\n")
				       contents
				       "\n@@latex:}@@")))
		   (org-element-set-contents hl (with-temp-buffer
						  (insert contents-new)
						  (org-element-parse-buffer)))))
		((org-element-property :FONTFEATURE hl)
		 (let* ((fontfeature (org-element-property :FONTFEATURE hl))
			(contents (org-element-interpret-data (org-element-contents hl)))
			(contents-new (concat
				       "@@latex:{\\addfontfeature{@@"
				       fontfeature
				       "@@latex:}%@@\n"
				       contents
				       "\n@@latex:}@@")))
		   (org-element-set-contents hl (with-temp-buffer
						  (insert contents-new)
						  (org-element-parse-buffer)))))))
	info)
      tree))
#+end_src

* Minion Pro
  :PROPERTIES:
  :font: Minion Pro [Style=Historic,Color=teal]
  :END:

Lorem ipsum dolor.

** Lowercase Numbers
   :PROPERTIES:
   :fontfeature: Numbers=Lowercase
   :END:

Lorem ipsum dolor 1234567890.

*** Letter Space
    :PROPERTIES:
    :fontfeature: LetterSpace=14.6
    :END:

Lorem ipsum dolor 1234567890.


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Question] Custom parse tree filter
  2021-05-16 15:31 [Question] Custom parse tree filter Juan Manuel Macías
@ 2021-05-16 15:59 ` Nicolas Goaziou
  2021-05-17 12:20   ` Juan Manuel Macías
  0 siblings, 1 reply; 3+ messages in thread
From: Nicolas Goaziou @ 2021-05-16 15:59 UTC (permalink / raw)
  To: Juan Manuel Macías; +Cc: orgmode

Hello,

Juan Manuel Macías <maciaschain@posteo.net> writes:

> I am writing a custom parse tree filter that does the following (LaTeX
> backend): if a heading has the ':font:' property, the content of that
> heading is enclosed in a LaTeX group. If the property is ':fontfeature:',
> then the content is enclosed in a different group. The filter works fine
> when all the headings are at the same level. But with different levels,
> it does not returns the expected result. It's evident that I'm doing
> something catastrophically wrong :-). I wonder if anyone could put me on
> the track of the origin of my error...
>
> Below, the offender function and a sample. Thanks in advance!

I think you are operating at the wrong level. Higher level headlines
contain lower level ones. I suggest to operate on sections instead.

Also, using `org-element-interpret-data' is meh because you're operating
at the parse tree level. You can insert export-snippet objects directly.

Here's a proposal. This could be refactored, but you get the idea.

--8<---------------cut here---------------start------------->8---
(defun my-custom-filters/fontspec-headline (tree backend info)
  (when (org-export-derived-backend-p backend 'latex)
    (org-element-map tree 'section
      (lambda (section)
	(let ((font (org-export-get-node-property :FONT section t))
              (fontfeature (org-export-get-node-property :FONTFEATURE section t))
              (create-export-snippet
               ;; Create "latex" export-snippet with value V.
               (lambda (v)
                 (org-element-create 'export-snippet (list :back-end "latex" :value v)))))
          (cond
           (font
            (apply #'org-element-set-contents
                   section
                   (append (list (funcall create-export-snippet "%font start\n"))
                           (org-element-contents section)
                           (list (funcall create-export-snippet "%font end\n")))))
	   (fontfeature
            (apply #'org-element-set-contents
                   section
                   (append (list (funcall create-export-snippet "%fontfeature start\n"))
                           (org-element-contents section)
                           (list (funcall create-export-snippet "%fontfeature end\n"))))))))
      info)
    tree))
--8<---------------cut here---------------end--------------->8---

Also, when "org-cite-wip" is merged, you will be able to replace, e.g.,

  (funcall create-export-snippet "%fontfeature start\n")

with 

  (org-export-raw-string "%fontfeature start\n")

Regards,
-- 
Nicolas Goaziou


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Question] Custom parse tree filter
  2021-05-16 15:59 ` Nicolas Goaziou
@ 2021-05-17 12:20   ` Juan Manuel Macías
  0 siblings, 0 replies; 3+ messages in thread
From: Juan Manuel Macías @ 2021-05-17 12:20 UTC (permalink / raw)
  To: Nicolas Goaziou; +Cc: orgmode

Hi Nicolas,

Thank you very much for your excelent explanation, which has helped me
to learn new and valuable things.

I chose `headline' instead of `section' because in the scenario where I
want to apply those LaTeX groups I was looking for something like this:

{level 1 - group 1
    {level 2 group 1 + group 2
      {level 3 group 1 + group 2 + group 3
	{etc. }}}}

I have tested your function and it works fine when all headers are at
the same level, but with different levels the filter understands both
properties as `font' (?). Anyway, and only for practical purposes, and
because I think that my initial idea of 2 properties was uneconomical, I
have slightly modified your function with a single property
`:fontspec:'. Now, with a couple of marks and some regexp both the
\fontspec group and the one with \addfontfeature are extracted:

| :fontspec: font ! optional: features   | \fontspec{font}[features] |
|----------------------------------------+---------------------------|
| :fontspec: > features                  | \addfontfeature{features} |

then the variable `fontspec':

(let* ((font <font-string-obtained-via-regexp>)
      (fontfeature <fontfeature-string-obtained-via-regexp>)
(fontspec (cond (font font) (fontfeature fontfeature))))

is passed as an argument of `(funcall create-export-snippet ...)'

With a single property it works fine, although I have to test more...

Thak you very much!

Best regards,

Juan Manuel 

Nicolas Goaziou writes:

> Hello,
>
> Juan Manuel Macías <maciaschain@posteo.net> writes:
>
>> I am writing a custom parse tree filter that does the following (LaTeX
>> backend): if a heading has the ':font:' property, the content of that
>> heading is enclosed in a LaTeX group. If the property is ':fontfeature:',
>> then the content is enclosed in a different group. The filter works fine
>> when all the headings are at the same level. But with different levels,
>> it does not returns the expected result. It's evident that I'm doing
>> something catastrophically wrong :-). I wonder if anyone could put me on
>> the track of the origin of my error...
>>
>> Below, the offender function and a sample. Thanks in advance!
>
> I think you are operating at the wrong level. Higher level headlines
> contain lower level ones. I suggest to operate on sections instead.
>
> Also, using `org-element-interpret-data' is meh because you're operating
> at the parse tree level. You can insert export-snippet objects directly.
>
> Here's a proposal. This could be refactored, but you get the idea.
>
> (defun my-custom-filters/fontspec-headline (tree backend info)
>   (when (org-export-derived-backend-p backend 'latex)
>     (org-element-map tree 'section
>       (lambda (section)
> 	(let ((font (org-export-get-node-property :FONT section t))
>               (fontfeature (org-export-get-node-property :FONTFEATURE section t))
>               (create-export-snippet
>                ;; Create "latex" export-snippet with value V.
>                (lambda (v)
>                  (org-element-create 'export-snippet (list :back-end "latex" :value v)))))
>           (cond
>            (font
>             (apply #'org-element-set-contents
>                    section
>                    (append (list (funcall create-export-snippet "%font start\n"))
>                            (org-element-contents section)
>                            (list (funcall create-export-snippet "%font end\n")))))
> 	   (fontfeature
>             (apply #'org-element-set-contents
>                    section
>                    (append (list (funcall create-export-snippet "%fontfeature start\n"))
>                            (org-element-contents section)
>                            (list (funcall create-export-snippet "%fontfeature end\n"))))))))
>       info)
>     tree))
>
> Also, when "org-cite-wip" is merged, you will be able to replace, e.g.,
>
>   (funcall create-export-snippet "%fontfeature start\n")
>
> with 
>
>   (org-export-raw-string "%fontfeature start\n")
>
> Regards,



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-05-17 12:24 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-05-16 15:31 [Question] Custom parse tree filter Juan Manuel Macías
2021-05-16 15:59 ` Nicolas Goaziou
2021-05-17 12:20   ` Juan Manuel Macías

Code repositories for project(s) associated with this inbox:

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).