2

Is it possible to include Babel header lines when publishing Org-mode files to HTML with org-html-publish-to-html? I would like to have the org-mode keywords exported in addition to the code. Ideally I would like to have a choice of what keywords get published (e.g., #+NAME:, #+BEGIN_SRC, but not #+RESULTS). For a bit of background, I have an org-mode file with multi-language code blocks. I would like to show the results from one code block are input into other code blocks. I see there are many options, but apart from "todo", none reference keyword export.

1 Answer 1

4

Try This

I use this method to create code examples to write How To Use org-mode documentation.

Wrap Code Blocks within a New org Code Block

  1. Use C-c C-v d to create empty org code block then add #+NAME: and :exports code headers to code block.

    #+NAME: org-code-block-wrapper
    #+begin_src org  :exports code 
    #+end_src
    
  2. Edit org block.

    Important: Click inside new org code block, add a blank line then use C-c ' to begin editing.

    • Add new code blocks with headers

      e.g.

      #+NAME: echo-message
      #+HEADER: :var MESSAGE="Hello from Shell Code Block!"
      #+begin_src sh
        echo $MESSAGE
      #+end_src
      
      #+NAME: display-message
      #+HEADER: :var message="Hello from Ruby Code Block!"
      #+begin_src ruby
        message
      #+end_src
      
    • Use C-c ' to return to original buffer.

    • Use C-x C-s to save.

When you're done, any org-mode syntax will be escaped and ready for export.

#+NAME: org-code-block-wrapper
#+begin_src org  :exports code

  ,#+NAME: echo-message
  ,#+HEADER: :var MESSAGE="Hello from Shell Code Block!"
  ,#+begin_src sh
    echo $MESSAGE
  ,#+end_src

  ,#+NAME: display-message
  ,#+HEADER: :var message="Hello from Ruby Code Block!"
  ,#+begin_src ruby
    message
  ,#+end_src

#+end_src

When exported, the code blocks with headers should be displayed.

#+NAME: echo-message
#+HEADER: :var MESSAGE="Hello from Shell Code Block!"
#+begin_src sh
  echo $MESSAGE
#+end_src

#+NAME: display-message
#+HEADER: :var message="Hello from Ruby Code Block!"
#+begin_src ruby
  message
#+end_src

Note: This method should work with most export formats including html. I wrote this answer in an org file then exported the answer as markdown to post it to StackExchange.

How To Extract the Original Blocks from within the org Block Wrapper.

  • Use add #+CALL: syntax with :results drawer replace, then execute statement with C-c C-c

    This will put the original blocks inside a drawer where they executed with C-c C-c.

    #+CALL: org-code-block-wrapper() :results drawer replace   :exports none 
    
    #+RESULTS:
    :RESULTS:
    
    #+NAME: echo-message
    #+HEADER: :var MESSAGE="Hello from Shell Code Block!"
    #+begin_src sh
      echo $MESSAGE
    #+end_src
    
    #+NAME: display-message
    #+HEADER: :var message="Hello from Ruby Code Block!"
    #+begin_src ruby
      message
    #+end_src
    :END:
    

Code Tested with

GNU Emacs 24.4.1 (x86_64-unknown-cygwin, GTK+ Version 3.10.9)
Org-Mode Version: 8.2.10

3
  • Thank you very much for the thorough, in depth response (+1). At this point, I am wondering if this should simply be an publishing option and if there is a good reason why it is not. I am tempted to alter the org source code to provide the option of exporting keywords. (Maybe easier said than done.) Commented Oct 16, 2015 at 21:47
  • @JulienChastang - Thanks! I am always looking for a better way! I'm still learning elisp so I find making changes to the innards of Emacs very challenging.
    – Melioratus
    Commented Oct 16, 2015 at 23:09
  • "I use this method to create code examples to write How To Use org-mode documentation." -- Please provide links to examples. Commented Jul 1, 2021 at 9:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.