2
votes

Can emacs org-mode be tweaked to fine tune what html is output for code blocks? Inline code is output as expected

<code> ..my code.. </code> 

as expected but a code block (inside #+BEGIN_SRC clojure .. #+END_SRC block markers) is output as

<pre class="src src-clojure> ..my code..</pre>

I would prefer a standard

<pre><code> ..my code..</code></pre>

that I would like as it would mean that no changes to css's would be needed and (hosted) code highlighting (highlight.js) would work out of the box. I have read the org-mode manual and searched long online with no luck. The behaviour seems to be probably set of the org-mode file ox-html.el but tweaking this is a bit beyond my current level of elisp knowledge. thanks!

1

1 Answers

1
votes

You can redefine the translator function that the HTML export engine uses to generate that output. Press C-h f (describe function) and type org-html-src-block to see the documentation and navigate to the function definition. Copy that defun to your dotemacs to customize it.

You might want to change it to:

(defun org-html-src-block (src-block contents info)
  "Transcode a SRC-BLOCK element from Org to HTML.
CONTENTS holds the contents of the item.  INFO is a plist holding
contextual information."
  (if (org-export-read-attribute :attr_html src-block :textarea)
      (org-html--textarea-block src-block)
    (let ((lang (org-element-property :language src-block))
      (caption (org-export-get-caption src-block))
      (code (org-html-format-code src-block info))
      (label (let ((lbl (and (org-element-property :name src-block)
                 (org-export-get-reference src-block info))))
           (if lbl (format " id=\"%s\"" lbl) ""))))
      (if (not lang) (format "<pre><code>\n%s</code></pre>" label code)
    (format
     "<div class=\"org-src-container\">\n%s%s\n</div>"
     (if (not caption) ""
       (format "<label class=\"org-src-name\">%s</label>"
           (org-export-data caption info)))
     (format "\n<pre><code>%s</code></pre>" lang label code))))))