TLDR: use SRC
blocks for syntax highlighting exports. When I have to export code that I want highlighted, I use SRC
blocks.
The documentation says the following :
You can include literal examples that should not be subjected to markup. Such examples are typeset in monospace, so this is well suited for source code and similar examples.
#+BEGIN_EXAMPLE
Some example from a text file.
#+END_EXAMPLE
If the example is source code from a programming language, or any other text that can be marked up by Font Lock in Emacs, you can ask for the example to look like the fontified Emacs buffer
#+BEGIN_SRC emacs-lisp
(defun org-xor (a b)
"Exclusive or."
(if a (not b) b))
#+END_SRC
It's true that this does not talk explicitly about exporting, but this is supported by the CSS support documentation which tells you that you can customize the following CSS classes:
pre.src formatted source code
pre.example normal example
So, what is happening for you is the expected behavior, and what you consider a workaround is what is recommended. You only need :eval never
if you are routinely evaluating all the code in the file, in literate programming style. If you don't do that, you should not need to add that flag.
The main reason for this is that EXAMPLE
blocks don't use attributes, so whereas BEGIN_SRC python
is a hint that we should be using RubyPython syntax highlighting (which is to say, here is how you recognize the various bits of syntax), BEGIN_EXAMPLE python
is just meaningless since it's .. monospaced, without markup.