From mboxrd@z Thu Jan 1 00:00:00 1970 From: Nicolas Goaziou Subject: Re: Modifying the exporter Date: Wed, 13 Feb 2013 15:03:00 +0100 Message-ID: <87liascnvv.fsf@gmail.com> References: <87txpkrvzi.fsf@sophokles.streitblatt.de> <87bobsf68g.fsf@gmail.com> <87zjzcqcq8.fsf@sophokles.streitblatt.de> <877gmgf2ts.fsf@gmail.com> <87halkq9kq.fsf@sophokles.streitblatt.de> <87liavet10.fsf@gmail.com> <8738x16rli.fsf_-_@sophokles.streitblatt.de> Mime-Version: 1.0 Content-Type: text/plain Return-path: Received: from eggs.gnu.org ([208.118.235.92]:35678) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1U5d2w-0001Zt-S9 for emacs-orgmode@gnu.org; Wed, 13 Feb 2013 09:10:52 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1U5d2u-0006Jc-2M for emacs-orgmode@gnu.org; Wed, 13 Feb 2013 09:10:50 -0500 Received: from we-in-x0231.1e100.net ([2a00:1450:400c:c03::231]:56496 helo=mail-we0-x231.google.com) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1U5d2t-0006Ib-KJ for emacs-orgmode@gnu.org; Wed, 13 Feb 2013 09:10:47 -0500 Received: by mail-we0-f177.google.com with SMTP id d7so1017907wer.36 for ; Wed, 13 Feb 2013 06:10:46 -0800 (PST) In-Reply-To: <8738x16rli.fsf_-_@sophokles.streitblatt.de> (Florian Beck's message of "Wed, 13 Feb 2013 00:27:05 +0100") 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-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: Florian Beck Cc: emacs-orgmode@gnu.org Florian Beck writes: > Ok, I took some time to extract a minimal example. It works fine, but > on a very low level (see below). > > Again, the goal is to add an optional argument to sectioning command. > The best way I could come up with is this (I omit the > `fb/org-export-pdf' function): > > #+BEGIN_SRC emacs-lisp > (defun fb/org-export-modify-headline (headline string) > (if (string-match > (rx > string-start "\\" > (group-n 1 (0+ "sub")) > (group-n 2 (or "part" "chapter" "section" "paragraph")) > (group-n 3 (zero-or-one "\*")) > "{" (group-n 4 (minimal-match (0+ (not (any "}"))))) "}") > string) > (let* ((level (match-string 1 string)) > (type (match-string 2 string)) > (stars (match-string 3 string)) > (title (match-string 4 string)) > (toc-title (org-element-property :toc-title headline)) > (new-hl > (format "\\%s%s%s%s{%s}" > (or level "") > type > (or stars "") > (if toc-title (format "[%s]" toc-title) "") > title))) > (replace-match new-hl t t string 0)) > string)) Why don't you simply replace (or add, if needed) optional argument from sectioning command with `replace-regexp-in-string' instead of building the whole string again? Like the following (untested): #+begin_src emacs-lisp (defun fb/org-latex-headline (headline contents info) (let ((original-hl (org-export-with-backend 'latex headline contents info)) (toc-title (org-element-property :toc-title headline))) (cond ((not toc-title) original-hl) ((string-match "\\`\\.*?\\*?\\[\\([^]]\\)\\]" original-hl) (replace-match toc-title nil nil original-hl 1)) (t (replace-regexp-in-string "\\`\\.*?\\*?" (concat "\\&" toc-title) original-hl))))) #+end_src > As you can see, I pull apart the string and then put it back together. > (Relatively straightforward in this case, much more involved for, say, > links.) Writing translators for headlines links and tables is usually a major undertaking when creating a new back-end. That's why there are so many tools to operate on these elements in ox.el. Anyway, this is not bad, as you forked a 3k locs back-end with about 30 locs. > In a perfect world, I would have access to these elements and the format > string, so I could either modify them before calling > `org-export-with-backend' or assemble the string myself. In your perfect world, there would be billions of variables, depending on the element, the back-end and the task. Customization is still important, so I offer generic tools when variables are not enough. Note that I might implement this feature at one point, but I still have to think about its specifications. Regards, -- Nicolas Goaziou