From mboxrd@z Thu Jan 1 00:00:00 1970 From: Kaushal Modi Subject: Re: Restrict include to some backend Date: Wed, 14 Sep 2016 12:33:25 +0000 Message-ID: References: <87mvjluiin.fsf@saiph.selenimh> <1c034d43-221c-2f48-8693-9fbf7b5cb727@gmail.com> <87oa40rcpt.fsf@saiph.selenimh> <475d2764-542a-6bb0-bc3a-ff7fd7294097@gmail.com> Mime-Version: 1.0 Content-Type: multipart/alternative; boundary=94eb2c13cde0228504053c76f04e Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:37089) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bk9Ol-0001RN-My for emacs-orgmode@gnu.org; Wed, 14 Sep 2016 08:34:44 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bk9Oe-0002mK-PM for emacs-orgmode@gnu.org; Wed, 14 Sep 2016 08:34:42 -0400 Received: from mail-yw0-f180.google.com ([209.85.161.180]:34664) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bk9Oe-0002lZ-JW for emacs-orgmode@gnu.org; Wed, 14 Sep 2016 08:34:36 -0400 Received: by mail-yw0-f180.google.com with SMTP id g192so17024182ywh.1 for ; Wed, 14 Sep 2016 05:34:36 -0700 (PDT) In-Reply-To: List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: "Emacs-orgmode" To: =?UTF-8?B?Q2zDqW1lbnQgUGl0LS1DbGF1ZGVs?= Cc: emacs-orgmode@gnu.org --94eb2c13cde0228504053c76f04e Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable On Wed, Sep 14, 2016 at 8:21 AM Cl=C3=A9ment 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 chang= e > "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=3D "ONLY" only-or-export) (string=3D type block-type)) (and (string=3D "EXCEPT" only-or-export) (not (string=3D 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) --=20 Kaushal Modi --94eb2c13cde0228504053c76f04e Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable


On Wed= , Sep 14, 2016 at 8:21 AM Cl=C3=A9ment 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 ca= lled org-latex-export-to-latex :)
=C2=A0
:)<= /div>
=C2=A0
Looks good. Maybe using the same syntax for both would be good though; some= thing like "BEGIN_ONLY tex" and "BEGIN_ONLY :not tex" m= aybe?

Thanks. I was exactly my earlier = syntax idea but I scrapped it!=C2=A0
=C2=A0
We could probably get rid of the "tex" part after the END_ONLY, a= nd change "tex" to "latex" for consistency with BEGIN_E= XPORT :)

I thought of removing that too. Bu= t then thought of keeping it in case we want to support nesting of these bl= ocks.. 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, nesti= ng anyways doesn't work as of now I believe.

B= elow is a modification of your code.

(defun cpit/f= ilter-begin-only (type)
=C2=A0 "Remove BEGIN_ONLY %s blocks = whose %s doesn't equal TYPE.
For those that match, only remov= e the delimiters.

On the flip side, for BEGIN_EXCE= PT %s blocks, remove those if %s equals TYPE. "
=C2=A0 (goto= -char (point-min))
=C2=A0 (while (re-search-forward " *#\\+B= EGIN_\\(ONLY\\|EXCEPT\\) +\\([a-z]+\\)\n" nil t)
=C2=A0 =C2= =A0 (let ((only-or-export (match-string-no-properties 1))
=C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 (block-type (match-string-no-properties 2))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (begin-from (match-beginning 0))<= /div>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (begin-to (match-end 0)))
=C2=A0 =C2=A0 =C2=A0 (re-search-forward (format " *#\\+END_%s +%= s\n" only-or-export block-type))
=C2=A0 =C2=A0 =C2=A0 (let (= (end-from (match-beginning 0))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0= =C2=A0 (end-to (match-end 0)))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 (if (= or (and (string=3D "ONLY" only-or-export)
=C2=A0 =C2=A0= =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(string=3D t= ype block-type))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0= =C2=A0 (and (string=3D "EXCEPT" only-or-export)
=C2=A0= =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(not = (string=3D type block-type))))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0= =C2=A0 (progn =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0; Keep the block,
=C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 ; delete just the comment markers
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (delete-region end= -from end-to)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (d= elete-region begin-from begin-to))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 ;; Delete the block
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (me= ssage "Removing %s block" block-type)
=C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 (delete-region begin-from end-to))))))
(add-hoo= k 'org-export-before-process #'cpit/filter-begin-only)=C2=A0
<= /div>
--

Kaushal Modi

--94eb2c13cde0228504053c76f04e--