emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Restrict include to some backend
@ 2016-09-06  5:13 Fabrice Popineau
  2016-09-06 13:59 ` Clément Pit--Claudel
  0 siblings, 1 reply; 12+ messages in thread
From: Fabrice Popineau @ 2016-09-06  5:13 UTC (permalink / raw)
  To: emacs-orgmode@gnu.org

[-- Attachment #1: Type: text/plain, Size: 300 bytes --]

Hi,

Maybe there is an obvious answer but I wonder how to restrict
including a file to some backend. The following doesn't work:

#+begin_export latex
#+include foo.py src python
#+end_export

(Not that I expected it to actually work, but it shows the goal)
Any idea ? Thanks for your help.

Fabrice

[-- Attachment #2: Type: text/html, Size: 462 bytes --]

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

* Re: Restrict include to some backend
  2016-09-06  5:13 Restrict include to some backend Fabrice Popineau
@ 2016-09-06 13:59 ` Clément Pit--Claudel
  2016-09-06 14:59   ` Clément Pit--Claudel
                     ` (2 more replies)
  0 siblings, 3 replies; 12+ messages in thread
From: Clément Pit--Claudel @ 2016-09-06 13:59 UTC (permalink / raw)
  To: emacs-orgmode


[-- Attachment #1.1: Type: text/plain, Size: 2036 bytes --]

Hi Fabrice,

I've run into this issue recently (while writing my first large document in Org).  I couldn't find a good natice solution, so I used a rather unpleasant trick, and I've been meaning to write emacs-orgmode about it since then.

In my documents I have a BEGIN_ONLY environment that I use like this:

#+begin_export html
#+include foo.py src python
#+end_export

#+begin_export latex
This is processed as *regular* ~org-mode~ code, but only when exporting to LaTeX.
#+end_export

I remove the blocks based on the current backend using a crude pre-processing step in Emacs lisp. I can share the code if you're in a hurry, but maybe this idea can also be integrated to Org itself?

For the record, here are places where this was useful:

* Some complex math was improperly rendered by MathJax; I made SVG images of it and declared a macro that inserted the actual math in LaTeX mode, and the SVG in HTML mode. To get backend-dependent macro definitions, I used BEGIN_ONLY blocks.

* I wanted to set the TOC depth only for the HTML version; I used 
    #+BEGIN_ONLY html
    #+TOC: headlines 2
    #+END_ONLY html

* In HTML mode listings are labeled as "listings," but in TeX mode they are listed as "figures"; I used a backend-dependent macro definition to smoothe out the difference.

* I split a figure in two in HTML, while using two subfigures in LaTeX

* I included PDF figures in LaTeX and SVG figures in HTML in some places

* I have my own custom-written citation processor for HTML; I included the "* Bibliography" header only in the HTML, since LaTeX inserted it by itself.

Cheers,
Clément.

On 2016-09-06 01:13, Fabrice Popineau wrote:
> Hi,
> 
> Maybe there is an obvious answer but I wonder how to restrict
> including a file to some backend. The following doesn't work:
> 
> #+begin_export latex
> #+include foo.py src python
> #+end_export
> 
> (Not that I expected it to actually work, but it shows the goal)
> Any idea ? Thanks for your help.
> 
> Fabrice


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 819 bytes --]

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

* Re: Restrict include to some backend
  2016-09-06 13:59 ` Clément Pit--Claudel
@ 2016-09-06 14:59   ` Clément Pit--Claudel
  2016-09-06 15:58   ` Nicolas Goaziou
  2016-09-06 20:10   ` Fabrice Popineau
  2 siblings, 0 replies; 12+ messages in thread
From: Clément Pit--Claudel @ 2016-09-06 14:59 UTC (permalink / raw)
  To: emacs-orgmode


[-- Attachment #1.1: Type: text/plain, Size: 2420 bytes --]

The examples below should have read begin_only:

> #+begin_only html
> #+include foo.py src python
> #+end_ony
> 
> #+begin_only latex
> This is processed as *regular* ~org-mode~ code, but only when exporting to LaTeX.
> #+end_only

Clément

On 2016-09-06 09:59, Clément Pit--Claudel wrote:
> Hi Fabrice,
> 
> I've run into this issue recently (while writing my first large document in Org).  I couldn't find a good native solution, so I used a rather unpleasant trick, and I've been meaning to write emacs-orgmode about it since then.
> 
> In my documents I have a BEGIN_ONLY environment that I use like this:
> 
> #+begin_only html
> #+include foo.py src python
> #+end_ony
> 
> #+begin_only latex
> This is processed as *regular* ~org-mode~ code, but only when exporting to LaTeX.
> #+end_only
> 
> I remove the blocks based on the current backend using a crude pre-processing step in Emacs lisp. I can share the code if you're in a hurry, but maybe this idea can also be integrated to Org itself?
> 
> For the record, here are places where this was useful:
> 
> * Some complex math was improperly rendered by MathJax; I made SVG images of it and declared a macro that inserted the actual math in LaTeX mode, and the SVG in HTML mode. To get backend-dependent macro definitions, I used BEGIN_ONLY blocks.
> 
> * I wanted to set the TOC depth only for the HTML version; I used 
>     #+BEGIN_ONLY html
>     #+TOC: headlines 2
>     #+END_ONLY html
> 
> * In HTML mode listings are labeled as "listings," but in TeX mode they are listed as "figures"; I used a backend-dependent macro definition to smoothe out the difference.
> 
> * I split a figure in two in HTML, while using two subfigures in LaTeX
> 
> * I included PDF figures in LaTeX and SVG figures in HTML in some places
> 
> * I have my own custom-written citation processor for HTML; I included the "* Bibliography" header only in the HTML, since LaTeX inserted it by itself.
> 
> Cheers,
> Clément.
> 
> On 2016-09-06 01:13, Fabrice Popineau wrote:
>> Hi,
>>
>> Maybe there is an obvious answer but I wonder how to restrict
>> including a file to some backend. The following doesn't work:
>>
>> #+begin_export latex
>> #+include foo.py src python
>> #+end_export
>>
>> (Not that I expected it to actually work, but it shows the goal)
>> Any idea ? Thanks for your help.
>>
>> Fabrice
> 


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 819 bytes --]

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

* Re: Restrict include to some backend
  2016-09-06 13:59 ` Clément Pit--Claudel
  2016-09-06 14:59   ` Clément Pit--Claudel
@ 2016-09-06 15:58   ` Nicolas Goaziou
  2016-09-06 18:15     ` Clément Pit--Claudel
  2016-09-06 20:10   ` Fabrice Popineau
  2 siblings, 1 reply; 12+ messages in thread
From: Nicolas Goaziou @ 2016-09-06 15:58 UTC (permalink / raw)
  To: Clément Pit--Claudel; +Cc: emacs-orgmode

Hello,

Clément Pit--Claudel <clement.pit@gmail.com> writes:

> I remove the blocks based on the current backend using a crude
> pre-processing step in Emacs lisp. I can share the code if you're in
> a hurry, but maybe this idea can also be integrated to Org itself?

I think implementing it in a pre processing hook is the way to go. It
doesn't seem too hard either.

Besides, we would need to introduce a new syntax to implement this,
which I find not too appealing.

Regards,

-- 
Nicolas Goaziou

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

* Re: Restrict include to some backend
  2016-09-06 15:58   ` Nicolas Goaziou
@ 2016-09-06 18:15     ` Clément Pit--Claudel
  2016-09-06 20:32       ` Nicolas Goaziou
  0 siblings, 1 reply; 12+ messages in thread
From: Clément Pit--Claudel @ 2016-09-06 18:15 UTC (permalink / raw)
  To: emacs-orgmode


[-- Attachment #1.1: Type: text/plain, Size: 317 bytes --]

On 2016-09-06 11:58, Nicolas Goaziou wrote:
> Besides, we would need to introduce a new syntax to implement this,
> which I find not too appealing.

Do you think so? Wouldn't BEGIN_ONLY … END_ONLY work? It would be similar to BEGIN_EXPORT, right?
Or it could even be an option to begin_export?

Clément.


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 819 bytes --]

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

* Re: Restrict include to some backend
  2016-09-06 13:59 ` Clément Pit--Claudel
  2016-09-06 14:59   ` Clément Pit--Claudel
  2016-09-06 15:58   ` Nicolas Goaziou
@ 2016-09-06 20:10   ` Fabrice Popineau
  2 siblings, 0 replies; 12+ messages in thread
From: Fabrice Popineau @ 2016-09-06 20:10 UTC (permalink / raw)
  To: Clément Pit--Claudel; +Cc: emacs-orgmode@gnu.org

[-- Attachment #1: Type: text/plain, Size: 648 bytes --]

2016-09-06 15:59 GMT+02:00 Clément Pit--Claudel <clement.pit@gmail.com>:

> Hi Fabrice,
>
> I've run into this issue recently (while writing my first large document
> in Org).  I couldn't find a good natice solution, so I used a rather
> unpleasant trick, and I've been meaning to write emacs-orgmode about it
> since then.
>
> In my documents I have a BEGIN_ONLY environment that I use like this:
>
>
Ok, I understand. I think I know how to preprocess the file (but thanks for
your offer)

Overall, I think that it would be nice to be able to select parts of the
org file
for some backends only.

Thanks everybody,

Fabrice

[-- Attachment #2: Type: text/html, Size: 1072 bytes --]

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

* Re: Restrict include to some backend
  2016-09-06 18:15     ` Clément Pit--Claudel
@ 2016-09-06 20:32       ` Nicolas Goaziou
  2016-09-13  3:10         ` Kaushal Modi
  0 siblings, 1 reply; 12+ messages in thread
From: Nicolas Goaziou @ 2016-09-06 20:32 UTC (permalink / raw)
  To: Clément Pit--Claudel; +Cc: emacs-orgmode

Hello,

Clément Pit--Claudel <clement.pit@gmail.com> writes:

> Do you think so? Wouldn't BEGIN_ONLY … END_ONLY work? It would be similar to BEGIN_EXPORT, right?
> Or it could even be an option to begin_export?

No, it wouldn't. 

INCLUDE keywords are expanded way before the document is parsed.
Included file can contain, e.g., headlines, and those blocks would
explode without a special treatment.

Regards,

-- 
Nicolas Goaziou

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

* Re: Restrict include to some backend
  2016-09-06 20:32       ` Nicolas Goaziou
@ 2016-09-13  3:10         ` Kaushal Modi
  2016-09-13  3:14           ` Clément Pit--Claudel
  0 siblings, 1 reply; 12+ messages in thread
From: Kaushal Modi @ 2016-09-13  3:10 UTC (permalink / raw)
  To: Clément Pit--Claudel; +Cc: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 481 bytes --]

I am looking forward to the BEGIN_ONLY/END_ONLY kind of solution too.

@Clément Would you please share your draft solution?

Thanks.

On Tue, Sep 6, 2016 at 5:39 PM Nicolas Goaziou <mail@nicolasgoaziou.fr>
wrote:

> Hello,
>
> Clément Pit--Claudel <clement.pit@gmail.com> writes:
>
> > Do you think so? Wouldn't BEGIN_ONLY … END_ONLY work? It would be
> similar to BEGIN_EXPORT, right?
> > Or it could even be an option to begin_export?
>
-- 

Kaushal Modi

[-- Attachment #2: Type: text/html, Size: 954 bytes --]

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

* Re: Restrict include to some backend
  2016-09-13  3:10         ` Kaushal Modi
@ 2016-09-13  3:14           ` Clément Pit--Claudel
  2016-09-14 11:55             ` Kaushal Modi
  0 siblings, 1 reply; 12+ messages in thread
From: Clément Pit--Claudel @ 2016-09-13  3:14 UTC (permalink / raw)
  To: Kaushal Modi; +Cc: emacs-orgmode


[-- Attachment #1.1: Type: text/plain, Size: 1775 bytes --]

Sure thing :) Here it is:

(defun ~/filter-begin-only (type)
  "Remove BEGIN_ONLY %s blocks whose %s doesn't equal TYPE.
For those that match, only remove the delimiters."
  (goto-char (point-min))
  (while (re-search-forward " *#\\+BEGIN_ONLY \\([a-z]+\\)\n" nil t)
    (let ((begin-block-type (match-string-no-properties 1))
          (begin-from (match-beginning 0))
          (begin-to (match-end 0)))
      (re-search-forward " *#\\+END_ONLY \\([a-z]+\\)\n")
      (let ((end-block-type (match-string-no-properties 1))
            (end-from (match-beginning 0))
            (end-to (match-end 0)))
        (cl-assert (string= begin-block-type end-block-type))
        (cond
         ((string= type begin-block-type)
          (delete-region end-from end-to)
          (delete-region begin-from begin-to))
         (t
          (message "Removing %s block" begin-block-type)
          (delete-region begin-from end-to)))))))

I call this before calling `org-latex-export-to-latex'.  The syntax it accepts is a bit weird:

#+BEGIN_ONLY tex
…
#+END_ONLY tex

Please share improvements :)

Cheers,
Clément.

On 2016-09-12 23:10, Kaushal Modi wrote:
> I am looking forward to the BEGIN_ONLY/END_ONLY kind of solution too.
> 
> @Clément Would you please share your draft solution?
> 
> Thanks.
> 
> On Tue, Sep 6, 2016 at 5:39 PM Nicolas Goaziou <mail@nicolasgoaziou.fr <mailto:mail@nicolasgoaziou.fr>> wrote:
> 
>     Hello,
> 
>     Clément Pit--Claudel <clement.pit@gmail.com <mailto:clement.pit@gmail.com>> writes:
> 
>     > Do you think so? Wouldn't BEGIN_ONLY … END_ONLY work? It would be similar to BEGIN_EXPORT, right?
>     > Or it could even be an option to begin_export?
> 
> -- 
> 
> Kaushal Modi
> 


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 819 bytes --]

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

* Re: Restrict include to some backend
  2016-09-13  3:14           ` Clément Pit--Claudel
@ 2016-09-14 11:55             ` Kaushal Modi
  2016-09-14 12:21               ` Clément Pit--Claudel
  0 siblings, 1 reply; 12+ messages in thread
From: Kaushal Modi @ 2016-09-14 11:55 UTC (permalink / raw)
  To: Clément Pit--Claudel; +Cc: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 601 bytes --]

On Mon, Sep 12, 2016 at 11:14 PM Clément Pit--Claudel <clement.pit@gmail.com>
wrote:

> Sure thing :) Here it is:
>

Thanks! This works great! I added it to `org-export-before-processing-hook'.

And it works fine.

I did not understand why you mentioned: "I call this before calling
`org-latex-export-to-latex'.".

I plan to add a "not" condition to this. That way, with below:

#+BEGIN_ONLY tex
foo
#+END_ONLY tex

#+BEGIN_EXCEPT tex
bar
#+END_EXCEPT tex

foo will be exported only for tex, and for every other backend, bar will be
exported.

WDYT?

-- 

Kaushal Modi

[-- Attachment #2: Type: text/html, Size: 2786 bytes --]

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

* Re: Restrict include to some backend
  2016-09-14 11:55             ` Kaushal Modi
@ 2016-09-14 12:21               ` Clément Pit--Claudel
  2016-09-14 12:33                 ` Kaushal Modi
  0 siblings, 1 reply; 12+ messages in thread
From: Clément Pit--Claudel @ 2016-09-14 12:21 UTC (permalink / raw)
  To: Kaushal Modi; +Cc: emacs-orgmode


[-- Attachment #1.1: Type: text/plain, Size: 1248 bytes --]

On 2016-09-14 07:55, Kaushal Modi wrote:
> On Mon, Sep 12, 2016 at 11:14 PM Clément Pit--Claudel <clement.pit@gmail.com <mailto:clement.pit@gmail.com>> wrote:
> 
>     Sure thing :) Here it is:
> 
> 
> Thanks! This works great! I added it to `org-export-before-processing-hook'.

Glad to hear that :)

> I did not understand why you mentioned: "I call this before calling `org-latex-export-to-latex'.".

Back when I wrote this I was starting out with Org-mode, I was in a hurry, and I didn't know about org-export-before-processing-hook :) So instead I had a larger ELisp script that opened the file, ran this filter, then called org-latex-export-to-latex :)

> I plan to add a "not" condition to this. That way, with below:
> 
> #+BEGIN_ONLY tex
> foo
> #+END_ONLY tex  
> 
> #+BEGIN_EXCEPT tex
> bar
> #+END_EXCEPT tex  
> 
> foo will be exported only for tex, and for every other backend, bar will be exported.
> 
> WDYT?

Looks good. Maybe using the same syntax for both would be good though; something like "BEGIN_ONLY tex" and "BEGIN_ONLY :not tex" maybe?

We could probably get rid of the "tex" part after the END_ONLY, and change "tex" to "latex" for consistency with BEGIN_EXPORT :)

Cheers,
Clément.


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 819 bytes --]

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

* Re: Restrict include to some backend
  2016-09-14 12:21               ` Clément Pit--Claudel
@ 2016-09-14 12:33                 ` Kaushal Modi
  0 siblings, 0 replies; 12+ messages in thread
From: Kaushal Modi @ 2016-09-14 12:33 UTC (permalink / raw)
  To: Clément Pit--Claudel; +Cc: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 2536 bytes --]

On Wed, Sep 14, 2016 at 8:21 AM Clément Pit--Claudel <clement.pit@gmail.com>
wrote:

>
> Back when I wrote this I was starting out with Org-mode, I was in a hurry,
> and I didn't know about org-export-before-processing-hook :) So instead I
> had a larger ELisp script that opened the file, ran this filter, then
> called org-latex-export-to-latex :)
>

:)


> Looks good. Maybe using the same syntax for both would be good though;
> something like "BEGIN_ONLY tex" and "BEGIN_ONLY :not tex" maybe?
>

Thanks. I was exactly my earlier syntax idea but I scrapped it!


> We could probably get rid of the "tex" part after the END_ONLY, and change
> "tex" to "latex" for consistency with BEGIN_EXPORT :)


I thought of removing that too. But then thought of keeping it in case we
want to support nesting of these blocks.. I haven't yet needed to have
anything like this, but below is a rough idea.

#+BEGIN_EXCEPT tex
something
#+BEGIN_EXCEPT html
else
#+END_EXCEPT html
#+END_EXCEPT tex

I haven't tested this, but based on the code, nesting anyways doesn't work
as of now I believe.

Below is a modification of your code.

(defun cpit/filter-begin-only (type)
  "Remove BEGIN_ONLY %s blocks whose %s doesn't equal TYPE.
For those that match, only remove the delimiters.

On the flip side, for BEGIN_EXCEPT %s blocks, remove those if %s equals
TYPE. "
  (goto-char (point-min))
  (while (re-search-forward " *#\\+BEGIN_\\(ONLY\\|EXCEPT\\)
+\\([a-z]+\\)\n" nil t)
    (let ((only-or-export (match-string-no-properties 1))
          (block-type (match-string-no-properties 2))
          (begin-from (match-beginning 0))
          (begin-to (match-end 0)))
      (re-search-forward (format " *#\\+END_%s +%s\n" only-or-export
block-type))
      (let ((end-from (match-beginning 0))
            (end-to (match-end 0)))
        (if (or (and (string= "ONLY" only-or-export)
                     (string= type block-type))
                (and (string= "EXCEPT" only-or-export)
                     (not (string= type block-type))))
            (progn                      ; Keep the block,
                                        ; delete just the comment markers
              (delete-region end-from end-to)
              (delete-region begin-from begin-to))
          ;; Delete the block
          (message "Removing %s block" block-type)
          (delete-region begin-from end-to))))))
(add-hook 'org-export-before-process #'cpit/filter-begin-only)
-- 

Kaushal Modi

[-- Attachment #2: Type: text/html, Size: 3851 bytes --]

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

end of thread, other threads:[~2016-09-14 12:34 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-09-06  5:13 Restrict include to some backend Fabrice Popineau
2016-09-06 13:59 ` Clément Pit--Claudel
2016-09-06 14:59   ` Clément Pit--Claudel
2016-09-06 15:58   ` Nicolas Goaziou
2016-09-06 18:15     ` Clément Pit--Claudel
2016-09-06 20:32       ` Nicolas Goaziou
2016-09-13  3:10         ` Kaushal Modi
2016-09-13  3:14           ` Clément Pit--Claudel
2016-09-14 11:55             ` Kaushal Modi
2016-09-14 12:21               ` Clément Pit--Claudel
2016-09-14 12:33                 ` Kaushal Modi
2016-09-06 20:10   ` Fabrice Popineau

Code repositories for project(s) associated with this public 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).