Customizing how html is generated for source code blogs when using export in org-mode

5

1

I have been using org-mode to author blog posts about programming. For this, I have included inline code segments in my .org file like this:

#+BEGIN_SRC csharp
public class UserBuilder
{
    private string _firstName;
    private string _lastName;
...
#+END_SRC

When I use the export html function, It gets formatted like this:

<pre class="src src-csharp"><span style="color: #7f007f;">public</span> <span style="color: #7f007f;">class</span> <span style="color: #228b22;">UserBuilder</span>
{
    <span style="color: #7f007f;">private</span> <span style="color: #228b22;">string</span> <span style="color: #a0522d;">_firstName</span>;
    <span style="color: #7f007f;">private</span> <span style="color: #228b22;">string</span> <span style="color: #a0522d;">_lastName</span>;
...
</pre>

As I use a JavaScript on my blog to format code fragments, I would just have liked the output to be like this:

<pre name="code" class="csharp">
public class UserBuilder
{
    private string _firstName;
    private string _lastName;
...
</pre>

Can that be done, and how?

Pete

Posted 2012-06-09T12:54:51.733

Reputation: 370

I don't use emacs but just an idea, can't you just copy/paste the code into a <pre class="csharp" name="code"> yourself? I mean it would probably take less time than trying to export it the "right" way. – OneOfOne – 2012-11-06T01:56:39.137

Some sort of feedback would be nice... – Trudbert – 2012-11-09T13:10:57.020

And poof goes the bounty... – Trudbert – 2012-11-10T18:53:17.707

Answers

2

The documentation where to change the exporter for source code can be found in the documentation for org-export-format-source-code-or-example, C-h f org-export-format-source-code-or-example:

...
Check if a function by name "org--format-source-code-or-example" is bound. If yes, use it as the custom formatter. Otherwise, use the default formatter. Default formatters are provided for docbook, html, latex and ascii backends. For example, use `org-html-format-source-code-or-example' to provide a custom formatter for export to "html".
...

An example export function can be found here (l. 2635). Modified to match your requierments:

(defun org-html-format-source-code-or-example
  (lines lang caption textareap cols rows num cont rpllbl fmt)
  (setq lines
    (concat
     "<pre name=\"code\" class=\"" lang "\">\n"
     (cond
      (textareap
       (concat
        (format "<p>\n<textarea cols=\"%d\" rows=\"%d\">"
            cols rows)
        lines "</textarea>\n</p>\n"))
      (t
       (with-temp-buffer
         (insert lines)
         (goto-char (point-min))
         (while (re-search-forward "[<>&]" nil t)
           (replace-match (cdr (assq (char-before)
                     '((?&."&amp;")(?<."&lt;")(?>."&gt;"))))
                  t t))
         (buffer-string))))
     "</pre>\n"))
  (unless textareap
    (setq lines (org-export-number-lines lines 1 1 num cont rpllbl fmt)))  
  lines)

After evaluation (copy to *scratch* and run M-x eval-buffer, or place in your .emacs and restart) html export exports

#+BEGIN_SRC java
public class UserBuilder
{
    private string _firstName;
    private string _lastName;
}
#+END_SRC

as

<pre name="code" class="java">
public class UserBuilder
{
    private string _firstName;
    private string _lastName;
}
</pre>

(Had to switch languages in the example because my emacs doesn't know c# but it works for all languages).

Trudbert

Posted 2012-06-09T12:54:51.733

Reputation: 336