3

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

JoelKuiper
  • 4,362
  • 2
  • 22
  • 33

3 Answers3

3

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
ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
  • This is not quite what I meant since I would like the example source in the same document. The concrete use case is that I would like to write a document explaining some HTML constructs and embedding those same constructs in the document, without copy+paste – JoelKuiper Nov 25 '13 at 14:40
  • @JoelKuiper How about changing the first `:exports none` to `:exports code` – Tyler Nov 25 '13 at 19:14
  • @Tyler I haven't tried this yet but doing :tangle output doesn't make any guarantees about the placement of the HTML right? So if I do `org-html-export-as-html` it would be preferable to know the location of the HTML as is the case with a `#+BEGIN_HTML` block. – JoelKuiper Nov 25 '13 at 19:45
  • @JoelKuiper, so you want to essentially mirror one block of text in an org file to another arbitrary location in the same org file? – ChrisGPT was on strike Nov 25 '13 at 20:51
  • @Chris if you put it like that, then yes. Where the one place would be inside a `SRC` or `EXAMPLE` block and the other in a `HTML` (or possibly `LATEX` block). – JoelKuiper Nov 25 '13 at 22:34
3

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:

Rendered

It doesn't do exactly what you're asking, but may scratch the same itch...

Kris Jenkins
  • 4,083
  • 30
  • 40
3

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.

monkeymind
  • 101
  • 5