emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* setting a custom flag for src blocks
@ 2016-12-10 19:24 Matt Price
  2016-12-11  5:19 ` Charles C. Berry
  0 siblings, 1 reply; 3+ messages in thread
From: Matt Price @ 2016-12-10 19:24 UTC (permalink / raw)
  To: Org Mode

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

On Sat, Dec 10, 2016 at 12:19 AM, Matt Price <moptop99@gmail.com> wrote:

>
>
> On Fri, Dec 9, 2016 at 12:19 PM, Eric S Fraga <e.fraga@ucl.ac.uk> wrote:
>
>> On Friday,  9 Dec 2016 at 16:42, Matt Price wrote:
>> > I think I am getting closer, actually (details soon, when I have a
>> > fully working  solution)).
>>
>> I look forward to seeing it!
>>
>
> OK, I've run into the limits of my knowledge.
>
> There are at least 2 plugins that will give a live code execution
> environemnt within an HTML presentation:
> - RevealEditor, which adds one global Ace editor instance to the
> presentation, and which shows the live rendering of HTML, CSS, and JS code
> when the html encoding follows the format:
>
> <pre><code class="hljs js" data-trim contenteditable>[1,2,3].map ((x) => x + 1)
> </code></pre>
>
>
> - klipse, which  instantiates a new instance of CodeMirror for every
> appropriately formatted set of tags in the form:
>
> <klipse-snippet data-language="javascript">[1,2,3].map ((x) => x + 1)</klipse-snippet>
>
> Meanwhile, html export (and also reveal export) will give something more like:
>
> <pre  class="src src-javascript"><span style="color: #8c8c8c;">[</span>1,2,3<span style="color: #8c8c8c;">]</span>.map <span style="color: #8c8c8c;">((</span>x<span style="color: #8c8c8c;">)</span> =&gt; x + 1<span style="color: #8c8c8c;">)</span>
>
> I would like to conditionally export
>
> - revealeditor-compatible code if (a) a flag "org-reveal-use-editor" is set AND (b) the code block meets certain criteria, e.g. language and maybe something in the header like "make-live t"
>
> - klipse-compatible code if (a) a flag "org-reveal-klipsify is set AND similar conditions to (b) above
>
> - standard html output if neither of the above conditions is met.
>
> What are the best ways do achieve this, do you think? Thanks guys,
>
>
I *think* that I'm looking for an export filter. From what I can see, it
has access to all the information that  the  initial export function does.
So now I'm wondering what the easiest way is to set a simple flag for a src
block, and make that flag available to the export filter.  For instance, if
I want a particular block to be renderd in klipse on export, could I
specify somehow:

#+HEADER: klipsify t
#+BEGIN_SRC: javascript
console.log("success");
#+END_SRC

or alternatively in a subtree:

* Lots of Examples
:PROPERTIES:
#+PROPERTY: header-args:javascript  :klipsify t
:END:

#+BEGIN_SRC: javascript
console.log("success");
#+END_SRC

 Thanks again, matt

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

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

* Re: setting a custom flag for src blocks
  2016-12-10 19:24 setting a custom flag for src blocks Matt Price
@ 2016-12-11  5:19 ` Charles C. Berry
  2016-12-11 11:46   ` Matt Price
  0 siblings, 1 reply; 3+ messages in thread
From: Charles C. Berry @ 2016-12-11  5:19 UTC (permalink / raw)
  To: Matt Price; +Cc: Org Mode

On Sat, 10 Dec 2016, Matt Price wrote:

> On Sat, Dec 10, 2016 at 12:19 AM, Matt Price <moptop99@gmail.com> wrote:
>
>>
>>
>> On Fri, Dec 9, 2016 at 12:19 PM, Eric S Fraga <e.fraga@ucl.ac.uk> wrote:
>>
>>> On Friday,  9 Dec 2016 at 16:42, Matt Price wrote:
>>>> I think I am getting closer, actually (details soon, when I have a
>>>> fully working  solution)).

[deleted]

> I *think* that I'm looking for an export filter. From what I can see, it
> has access to all the information that  the  initial export function does.

It does not. :-(

When `org-babel-exp-process-buffer' runs (under `org-export-as'), all the 
header info is dropped.

There might be a way to backtrack from the copy buffer to the original and 
remap the src blocks, but it seems like the wrong way to go. It would be a 
lot of work, I think.

More below ...

> So now I'm wondering what the easiest way is to set a simple flag for a src
> block, and make that flag available to the export filter.  For instance, if
> I want a particular block to be renderd in klipse on export, could I
> specify somehow:
>
> #+HEADER: klipsify t
> #+BEGIN_SRC: javascript
> console.log("success");
> #+END_SRC
>
> or alternatively in a subtree:
>
> * Lots of Examples
> :PROPERTIES:
> #+PROPERTY: header-args:javascript  :klipsify t
> :END:
>
> #+BEGIN_SRC: javascript
> console.log("success");
> #+END_SRC
>

I think the better way to go is to do all the formatting in babel. That 
is, make a babel src block handle the formatting for you and subvert the 
normal mechanisms.

To do this, write a src block that is given the name of another src
block, that grabs the body (and if you really need it the header
info), formats it as you need, and inserts it in final form wrapped
in an `export html' block.

So if you had a src block named `codeA' and one named `klipsify' which
has the code needed to render the output you desire depending on the
value of

: :var src-blk-name="my-codes"

then a

#+CALL:klipsify("codeA")

line will put the output in the exported document.

Something like this:

#+NAME: codeA
#+BEGIN_SRC: javascript :eval never-export :exports none
console.log("success");
#+END_SRC

#+NAME: klipsify
#+header: :var src-blk-name="my-code" :exports none
#+header: :results raw :wrap export html
#+BEGIN_SRC emacs-lisp
   (save-excursion
     (org-babel-goto-named-src-block
      src-block-name)
     (let ((body-code
 	   (org-babel-expand-src-block)))
       (klipsify-my-code body-code)))
#+END_SRC

Obviously, you need to defun `klipsify-my-code' or whatever.


All the code blocks would need to be named and have these headers:

:  :eval never-export :exports none

or maybe

:  :exports results

You would use the latter, if you want the results of evaluating the code 
to appear in the exported document. Put the #+CALL line just before the 
code block and then the code will appear first and the results next.

HTH,

Chuck

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

* Re: setting a custom flag for src blocks
  2016-12-11  5:19 ` Charles C. Berry
@ 2016-12-11 11:46   ` Matt Price
  0 siblings, 0 replies; 3+ messages in thread
From: Matt Price @ 2016-12-11 11:46 UTC (permalink / raw)
  To: Org Mode

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

Huh.  This is pretty cool, Charles. But I think it will be a little hard
for me to implement for lectures, since I really need to quickly write up
slides in the half-hour before class...  So, instead, I rewrote
org-reveal-src-block to do the work for me. I've just written a blog post
-- for some reason my posts are no longer showing up on Planet Emacsen
(maybe b/c I have soem non-Emas stuff o nthat blog now), but the blog is
here:

http://matt.hackinghistory.ca/2016/12/11/org-mode-run-code-live-in-a-reveal-slideshow-with-klipse/

I would really love to get feedback on this solution, or any other ones.
Thanks as always to all readers and commentators!

On Sun, Dec 11, 2016 at 12:19 AM, Charles C. Berry <ccberry@ucsd.edu> wrote:

> On Sat, 10 Dec 2016, Matt Price wrote:
>
> On Sat, Dec 10, 2016 at 12:19 AM, Matt Price <moptop99@gmail.com> wrote:
>>
>>
>>>
>>> On Fri, Dec 9, 2016 at 12:19 PM, Eric S Fraga <e.fraga@ucl.ac.uk> wrote:
>>>
>>> On Friday,  9 Dec 2016 at 16:42, Matt Price wrote:
>>>>
>>>>> I think I am getting closer, actually (details soon, when I have a
>>>>> fully working  solution)).
>>>>>
>>>>
> [deleted]
>
> I *think* that I'm looking for an export filter. From what I can see, it
>> has access to all the information that  the  initial export function does.
>>
>
> It does not. :-(
>
> When `org-babel-exp-process-buffer' runs (under `org-export-as'), all the
> header info is dropped.
>
> There might be a way to backtrack from the copy buffer to the original and
> remap the src blocks, but it seems like the wrong way to go. It would be a
> lot of work, I think.
>
> More below ...
>
> So now I'm wondering what the easiest way is to set a simple flag for a src
>> block, and make that flag available to the export filter.  For instance,
>> if
>> I want a particular block to be renderd in klipse on export, could I
>> specify somehow:
>>
>> #+HEADER: klipsify t
>> #+BEGIN_SRC: javascript
>> console.log("success");
>> #+END_SRC
>>
>> or alternatively in a subtree:
>>
>> * Lots of Examples
>> :PROPERTIES:
>> #+PROPERTY: header-args:javascript  :klipsify t
>> :END:
>>
>> #+BEGIN_SRC: javascript
>> console.log("success");
>> #+END_SRC
>>
>>
> I think the better way to go is to do all the formatting in babel. That
> is, make a babel src block handle the formatting for you and subvert the
> normal mechanisms.
>
> To do this, write a src block that is given the name of another src
> block, that grabs the body (and if you really need it the header
> info), formats it as you need, and inserts it in final form wrapped
> in an `export html' block.
>
> So if you had a src block named `codeA' and one named `klipsify' which
> has the code needed to render the output you desire depending on the
> value of
>
> : :var src-blk-name="my-codes"
>
> then a
>
> #+CALL:klipsify("codeA")
>
> line will put the output in the exported document.
>
> Something like this:
>
> #+NAME: codeA
> #+BEGIN_SRC: javascript :eval never-export :exports none
> console.log("success");
> #+END_SRC
>
> #+NAME: klipsify
> #+header: :var src-blk-name="my-code" :exports none
> #+header: :results raw :wrap export html
> #+BEGIN_SRC emacs-lisp
>   (save-excursion
>     (org-babel-goto-named-src-block
>      src-block-name)
>     (let ((body-code
>            (org-babel-expand-src-block)))
>       (klipsify-my-code body-code)))
> #+END_SRC
>
> Obviously, you need to defun `klipsify-my-code' or whatever.
>
>
> All the code blocks would need to be named and have these headers:
>
> :  :eval never-export :exports none
>
> or maybe
>
> :  :exports results
>
> You would use the latter, if you want the results of evaluating the code
> to appear in the exported document. Put the #+CALL line just before the
> code block and then the code will appear first and the results next.
>
> HTH,
>
> Chuck
>

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

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

end of thread, other threads:[~2016-12-11 11:47 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-12-10 19:24 setting a custom flag for src blocks Matt Price
2016-12-11  5:19 ` Charles C. Berry
2016-12-11 11:46   ` Matt Price

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