emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [babel] would like to to do simple text block expansion with parameters or multiline macro replacement text
@ 2012-04-07 17:22 Olaf.Hamann
  2012-04-12 11:04 ` Olaf.Hamann
  0 siblings, 1 reply; 4+ messages in thread
From: Olaf.Hamann @ 2012-04-07 17:22 UTC (permalink / raw)
  To: Emacs-orgmode mailing list

Hello all,

I'm very happy with all the new features I discovered in org-babel 
(since 6.8 or so) and played around a lot trying to convert my old 
funnelweb-techniques (kind of LPstuff) into org-babel and now I'm a 
little confused with all that elaborated stuff like header arguments, 
result options :var types and function calls and so on :-)

Is there an easy way to do a simple text block expansion with 
parameters, I mean sth like this:

#+begin_src xsl :tangle outputfile :noweb tangle
{{{gCV(argument1,param2)}}}
<<other_srcblockvalueWhenTangeling()>>
#+end_src

#+MACRO: gCV replacement text...
...spread over different...
...multiple lines  with arguments like $1 ...
...and $2 and so on...
...
#+end_macro

Multiline macros did not work in my tries (Org version 7.8.03 with Emacs 
version 24)

For expanding macros inside srcblocks I got this hint from the list some 
time ago what works really fine:

#+begin_src emacs-lisp :results silent :exports none
   (add-hook 'org-babel-tangle-body-hook
             (lambda () (org-export-preprocess-apply-macros)))
#+end_src


Below I give an executable example in org-code. It's stripped down from 
my work.
It works fine, but I would like to get rid of the lisp-concat and 
string-quoting and wonder if org-mode does not offer another way.


Thank you very much in advance,
Olaf

====

* simple text block expansion with parameters

#+NAME: gCV(PDEF="param_pdef",XPATH="param_value")
#+begin_src emacs-lisp :results value   :exports results :noweb tangle
           (concat
		"    <xsl:when test=\"$pdef-identifier = '" PDEF  "'\">
                               <xsl:value-of select=\"" XPATH "\" />
                       </xsl:when> " )
#+end_src


#+begin_src xsl :tangle mappings.xslt :noweb tangle
       <xsl:variable name="oneOfMany">
          <xsl:choose>
            <<gCV("Condition001","$tree/some/node/@value")>>
            <<gCV("Condition002","$tree/some/other/node/text()")>>
            <xsl:otherwise/>
          </xsl:choose>
       </xsl:variable>
#+end_src

====

Output looks like expected:

<xsl:variable name="oneOfMany">
<xsl:choose>
      <xsl:when test="$pdef-identifier = 'Condition001'">
            <xsl:value-of select="$tree/some/node/@value" />
            </xsl:when>
      <xsl:when test="$pdef-identifier = 'Condition002'">
            <xsl:value-of select="$tree/some/other/node/text()" />
                     </xsl:when>
<xsl:otherwise/>
</xsl:choose>
</variable>

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

* Re: [babel] would like to to do simple text block expansion with parameters or multiline macro replacement text
  2012-04-07 17:22 [babel] would like to to do simple text block expansion with parameters or multiline macro replacement text Olaf.Hamann
@ 2012-04-12 11:04 ` Olaf.Hamann
  2012-04-21 11:09   ` Olaf.Hamann
  0 siblings, 1 reply; 4+ messages in thread
From: Olaf.Hamann @ 2012-04-12 11:04 UTC (permalink / raw)
  To: emacs-orgmode

Am 07.04.2012 19:22, schrieb Olaf.Hamann:
> Hello all,
>
> Is there an easy way to do a simple text block expansion with
> parameters, I mean sth like this:
>
> #+begin_src xsl :tangle outputfile :noweb tangle
> {{{gCV(argument1,param2)}}}
> #+end_src
>
> #+MACRO: gCV replacement text...
> ...spread over different...
> ...multiple lines with arguments like $1 ...
> ...and $2 and so on...
> ...
> #+end_macro
>

I think I will try to adapt m4 as org-language so that it can be used 
for executing src-blocks.
That may fit my needs and #+MACRO stays good for oneline replacement

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

* Re: [babel] would like to to do simple text block expansion with parameters or multiline macro replacement text
  2012-04-12 11:04 ` Olaf.Hamann
@ 2012-04-21 11:09   ` Olaf.Hamann
  2012-04-21 15:33     ` Eric Schulte
  0 siblings, 1 reply; 4+ messages in thread
From: Olaf.Hamann @ 2012-04-21 11:09 UTC (permalink / raw)
  To: emacs-orgmode

> I think I will try to adapt m4 as org-language so that it can be used
> for executing src-blocks.

m4 is no solution as it does not support multibyte character

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

* Re: [babel] would like to to do simple text block expansion with parameters or multiline macro replacement text
  2012-04-21 11:09   ` Olaf.Hamann
@ 2012-04-21 15:33     ` Eric Schulte
  0 siblings, 0 replies; 4+ messages in thread
From: Eric Schulte @ 2012-04-21 15:33 UTC (permalink / raw)
  To: Olaf.Hamann; +Cc: emacs-orgmode

"Olaf.Hamann" <O.Hamann@gmx.net> writes:

>> I think I will try to adapt m4 as org-language so that it can be used
>> for executing src-blocks.
>
> m4 is no solution as it does not support multibyte character
>
>

I missed the beginning of this thread, but would the following very
simple support for text block evaluation be sufficient?

#+begin_src emacs-lisp
(defun org-babel-expand-body:text (body params)
  (mapc
   (lambda (pair)
     (let ((name (symbol-name (car pair)))
           (value (cdr pair)))
       (setq body
             (replace-regexp-in-string
              (concat "\$" (regexp-quote name))
              (if (stringp value) value (format "%S" value))
              body))))
   (mapcar #'cdr (org-babel-get-header params :var)))
  body)

(defun org-babel-execute:text (body params)
  (org-babel-expand-body:text body params))
#+end_src

#+begin_src text :var name="Eric"
  Hi, my name is $name.
#+end_src

#+RESULTS:
: Hi, my name is Eric.

-- 
Eric Schulte
http://cs.unm.edu/~eschulte/

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

end of thread, other threads:[~2012-04-21 17:34 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-04-07 17:22 [babel] would like to to do simple text block expansion with parameters or multiline macro replacement text Olaf.Hamann
2012-04-12 11:04 ` Olaf.Hamann
2012-04-21 11:09   ` Olaf.Hamann
2012-04-21 15:33     ` Eric Schulte

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).