Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question
    
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 Nov 6 '12 at 1:56
    
Some sort of feedback would be nice... –  Trudbert Nov 9 '12 at 13:10
    
And poof goes the bounty... –  Trudbert Nov 10 '12 at 18:53

1 Answer 1

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).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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