org-mode with code example as html - emacs

I'm trying to embed a literate code example as HTML in Emacs org-mode.
The idea is that I can use something like
#+BEGIN_SRC html :noweb-ref content :exports source
<span>some content </span>
#+END_SRC
#+BEGIN_HTML :noweb tangle
<<content>>
#+END_HTML
Would something like this be possible? Because currently I have to copy&paste the part I want to be included (quoted) in the HTML source and the SRC bit that I want to show in the document.
EDIT: The concrete use case is that I would like to write a document explaining some HTML constructs (as a code block) and embedding (quoted) those same constructs in the document, without copy+paste

The example below is adapted from something similar I've used for writing about Org-mode. It seems to work for your use case too. The #+OPTIONS: d:RESULTS ensures that the :RESULTS: drawer is exported. Put this in an Org-mode buffer and export to HTML.
#+OPTIONS: d:RESULTS
* Examples
The HTML source
#+name: eg-1
#+begin_src org :results replace drawer :exports both :post wrap-html(text=*this*)
A <b>bold</b> statement.
#+end_src
Results in the output
#+results: eg-1
* Utils :noexport:
#+name: wrap-html
#+begin_src emacs-lisp :var text="" :results raw
(concat "#+BEGIN_HTML\n<div class=\"html-output\">\n" text "\n</div>\n#+END_HTML")
#+end_src
You can avoid repeating the headers by adding them as properties to the subtree heading, e.g.
* Example 2
:PROPERTIES:
:results: replace drawer
:exports: both
:post: wrap-html(text=*this*)
:END:
#+name: eg-2
#+begin_src org
Some <i>italic</i>.
#+end_src
#+results: eg-2
#+name: eg-3
#+begin_src org
You can <b>nest <i>inline</i> tags</b>.
#+end_src
#+results: eg-3
but note that these headers will apply to every source block in the subtree unless explicitly overridden.

I believe you have to make the following changes:
Give your first block a name
Change your HTML block to a SRC block
Add a :tangle <file-name> to your second block
Try this:
#+NAME: content
#+BEGIN_SRC html :exports none
<span>some content </span>
#+END_SRC
#+BEGIN_SRC html :tangle output-file :exports none :noweb yes
<<content>>
#+END_SRC

I had a similar requirement recently, and wrote ob-browser. It takes HTML source blocks and uses org-babel and phantomjs to display images of how the browser would render them.
So you can say:
#+BEGIN_SRC browser :out demo.png
<!DOCTYPE html>
<html>
<head>
<link href="http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/2.3.2/css/bootstrap.min.css" rel="stylesheet" media="screen">
</head>
<body>
<div class="row">
<div class="span6 offset1">
<h1>Rendered PNG</h1>
<button class="btn btn-primary">You Can't Press This</button>
</div>
</div>
</body>
</html>
#+END_SRC
And get the image:
It doesn't do exactly what you're asking, but may scratch the same itch...

Related

Literate programming, org mode, function declaration

I want to write a Literate Program with org-mode. Let's say I have the following function:
fn do_stuff()
{
// 200 lines of code go here.
}
I would write something like that in org-mode:
#+BEGIN_SRC rust :tangle /some/path
fn do_stuff()
{
#+END_SRC
// Many more blocks of `BEGIN_SRC` go here to exlpain 200 lines of code mentioned above.
// They will have formatting, and prose between them, and headings with different nesting levels.
// The whole shebang in short.
#+BEGIN_SRC rust :tangle /some/path
}
#+END_SRC
Now, here is the question. I hope I will explain it well, it's kinda hard to put in words.
What do I do with the first and last #+BEGIN_SRC blocks shown above?
How do I style the function declaration with org-mode and/or Literate Programming?
It seems kind of out of place with all the “formatting, prose, headings” of the 200 lines of code mentioned above.
I need ideas please :-)
Thanks in advance.
I would use noweb to tangle the full code without necessarily presenting it all in order. That is, I would do something like this:
The core code is
#+name: code1
#+begin_src rust :noweb yes :tangle no
...
#+end_src
More code etc. and then, at the end:
#+BEGIN_SRC rust :tangle /some/path
fn do_stuff()
{
<<code1>>
}
#+END_SRC
You may need :noweb yes on the full code block as well.

What can cause org-auto-tangle to result nil for all noweb code block evaluations

I'm using literate programming for some configuration files and would like to have some parts from elisp code block evaluations. I tried evaluating named code blocks with :noweb tangle but they always results nil and I do not see any errors in the *Messages*. Here's a simplified hello world example and the results I got.
Org file
#+title: Hello
#+PROPERTY: header-args :tangle hello.txt :cache no :exports none
#+auto_tangle: t
#+name: hello-world-output
#+begin_src emacs-lisp :tangle no :eval no-export :results output
(print "Hello world")
#+end_src
#+name: hello-world-value
#+begin_src emacs-lisp :tangle no :eval no-export :results value
"Hello world"
#+end_src
#+begin_src text :noweb tangle
<<hello-world-output>> -> <<hello-world-output()>>
<<hello-world-value>> -> <<hello-world-value()>>
#+end_src
Tangled results
(print "Hello world") -> nil
"Hello world" -> nil
I also checked that org-link-elisp-confirm-function and org-confirm-babel-evaluate both have nil value, so they should not be preventing evaluation.
EDIT: I forgot to mention that I used org-auto-tangle. Issue doesn't occur when calling org-bable-tangle directly.
I had the same issue and below solved it for me.
According to the code of org-auto-tangle the code will not be evaluated by default. In order to have the code auto evaluated you need to add your org file to the org-auto-tangle-babel-safelist. I've posted the definition of the variable and a link to the README with an example of how to set the variable.
(defvar org-auto-tangle-babel-safelist '()
"List of full path of files for which code blocks need to be evaluated.
By default, code blocks are not evaluated during the auto-tangle to avoid
possible code execution from unstrusted source. To enable code blocks evaluation
for a specific file, add its full path to this list.")
https://github.com/yilkalargaw/org-auto-tangle#babel-auto-tangle-safelist

Can org-mode tangle during export

I can use the org-babel-tangle to tangle the current file. I was wondering if you could have org export tangle all the code blocks in the org file.
thanks
EA
This runs org-babel-tangle when exporting:
#+NAME: tangle-it
#+BEGIN_SRC emacs-lisp :exports none
(org-babel-tangle)
#+END_SRC
#+BEGIN_SRC text :results silent :noweb yes :exports results
<<tangle-it()>>
#+END_SRC
#+BEGIN_SRC css :tangle test.css
body {
font-size: 12px;
}
#+END_SRC
It doesn't work when using it with #+CALL: tangle-it().
It is also possible to do this with a macro:
#+MACRO: tangle-it (eval (progn (org-babel-tangle) ""))
{{{tangle-it()}}}
#+BEGIN_SRC css :tangle test.css
body {
font-size: 12px;
}
#+END_SRC
I do this to export my elisp source blocks to specific files
#+BEGIN_SRC emacs-lisp :tangle lisp-file.el
(message "Hello lisp-file")
#+END_SRC
I think you can also set this as a property, so you could set properties at the node/tree level.
I would do it like this:
* build :noexport:
#+BEGIN_SRC emacs-lisp
(org-babel-tangle)
(org-latex-export-as-latex)
#+END_SRC
Then just type C-c C-c in the code block to tangle, then export. You can change the export command to whatever you want for other export types.

How to export ditaa or dot blocks as captioned figures (LaTeX)?

I have an org-mode document with captioned ditaa and dot figures. When I export to LaTeX, the resulting images are not placed inside a figure environment, not given a \caption, and not given a \label. Other source code blocks export fine.
How can I fix this?
Here is an example org-mode document:
* Plain source code works
#+CAPTION: This works
#+LABEL: fig:works
#+BEGIN_SRC
this is a test
#+END_SRC
The above (Figure [[fig:works]]) is a figure with some source code. When
exported to LaTeX, it is placed inside a ~figure~ environment and
given a caption and label as expected.
* Ditaa doesn't work
#+CAPTION: Foo
#+LABEL: fig:foo
#+BEGIN_SRC ditaa :file foo.png :cmdline -E
/-----\
----------------------------->| foo |<-----------------------------
\-----/
#+END_SRC
The above (Figure [[fig:foo]]) is a ditaa figure. When exported to LaTeX,
the image is not inside a ~figure~ environment, it is missing the
caption, and there is no ~\label~.
* Dot doesn't work
#+CAPTION: Bar
#+LABEL: fig:bar
#+BEGIN_SRC dot :file bar.png
digraph foo {
asdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdf -> b;
}
#+END_SRC
The above (Figure [[fig:bar]]) is a dot figure. When exported to LaTeX,
the image is not inside a ~figure~ environment, it is missing the
caption, and there is no ~\label~.
#+OPTIONS: toc:nil author:nil
#+TITLE:
#+DATE:
I'm using org-mode 8.2.10 on Emacs 24.3.1.
To get it to work you need to have a #+RESULTS: section under the #+BEGIN_SRC block and caption that instead:
* Plain source code
#+CAPTION: This works
#+LABEL: fig:works
#+BEGIN_SRC
this is a test
#+END_SRC
See Figure [[fig:works]].
* Ditaa
#+BEGIN_SRC ditaa :file foo.png :cmdline -E
/-----\
----------------------------->| foo |<-----------------------------
\-----/
#+END_SRC
#+CAPTION: Foo
#+LABEL: fig:foo
#+RESULTS:
[[file:foo.png]]
See Figure [[fig:foo]].
* Dot
#+BEGIN_SRC dot :file bar.png
digraph foo {
asdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdf -> b;
}
#+END_SRC
#+CAPTION: Bar
#+LABEL: fig:bar
#+RESULTS:
[[file:bar.png]]
See Figure [[fig:bar]].
#+OPTIONS: toc:nil author:nil
#+TITLE:
#+DATE:

Literate Programming using org-babel

I am on a literate program using org-babel. My source is structured like so,
-imports
-utility fns
-game structure
- detailed explanations
This is the normal structure of the code, what I would like to do is move explanations of the utility fns to the end so it does not show up first in the generated pdf file. Now this can be done with noweb extension, but the problem is when you have lots of little functions for each one I have to add a src_block with a unique name scroll down the file and add a reference to that in the file which is really annoying. Is there a way to name all the src_blocks in a section? say all code in this section goes into block A.
You can give multiple chunks the same name. For example, I generate my .emacs file with org-tangle, and at the top of the org file, I have a master template that looks something like this:
#+begin_src emacs-lisp :tangle "/path/to/.emacs" :comments both :noweb tangle
<<generated-file-warning>
<<includes>>
<<definitions>>
<<settings>>
<<generated-file-warning>
#+end_src
Underneath that, I have my outline with source blocks like so:
* extensions
** yasnippet
#+name: early-includes
#+begin_src emacs-lisp
(require 'yasnippet)
(yas/initialize)
#+end_src
#+name: settings
#+begin_src emacs-lisp
(yas/load/directory "/path/to/my/snippets")
#+end_src
Note: for older versions of org-mode, you may need to use #+srcname: instead of #+name:
You can also create a property called noweb-ref, which applies the same name to all source blocks in a sub-tree.