Re: [O] Restrict include to some backend

2016-09-14 Thread Kaushal Modi
On Wed, Sep 14, 2016 at 8:21 AM Clément Pit--Claudel 
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


Re: [O] Restrict include to some backend

2016-09-14 Thread Clément Pit--Claudel
On 2016-09-14 07:55, Kaushal Modi wrote:
> On Mon, Sep 12, 2016 at 11:14 PM Clément Pit--Claudel  > 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.



signature.asc
Description: OpenPGP digital signature


Re: [O] Restrict include to some backend

2016-09-14 Thread Kaushal Modi
On Mon, Sep 12, 2016 at 11:14 PM Clément Pit--Claudel 
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


Re: [O] Restrict include to some backend

2016-09-12 Thread Clément Pit--Claudel
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  > wrote:
> 
> Hello,
> 
> Clément Pit--Claudel  > 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
> 



signature.asc
Description: OpenPGP digital signature


Re: [O] Restrict include to some backend

2016-09-12 Thread Kaushal Modi
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 
wrote:

> Hello,
>
> Clément Pit--Claudel  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


Re: [O] Restrict include to some backend

2016-09-06 Thread Nicolas Goaziou
Hello,

Clément Pit--Claudel  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



Re: [O] Restrict include to some backend

2016-09-06 Thread Fabrice Popineau
2016-09-06 15:59 GMT+02:00 Clément Pit--Claudel :

> 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


Re: [O] Restrict include to some backend

2016-09-06 Thread Clément Pit--Claudel
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.



signature.asc
Description: OpenPGP digital signature


Re: [O] Restrict include to some backend

2016-09-06 Thread Nicolas Goaziou
Hello,

Clément Pit--Claudel  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



Re: [O] Restrict include to some backend

2016-09-06 Thread Clément Pit--Claudel
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
> 



signature.asc
Description: OpenPGP digital signature


Re: [O] Restrict include to some backend

2016-09-06 Thread Clément Pit--Claudel
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



signature.asc
Description: OpenPGP digital signature


[O] Restrict include to some backend

2016-09-06 Thread Fabrice Popineau
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