Org mode syntax highlight

One problem I came across when I try to use Org mode as the markup language for my Pelian generated static blog is the syntax highlight for source code blocks. Org mode has the ability to highlight syntax of languages supported in its Babel functionality, be it in the Org mode buffer or the language major mode buffer, or exported PDF document or HTML file. However, things became a bit different for Org mode when it was used in Pelican through the org_reader plugin. Markdown, one of the two major supported markup languages of Pelcian, uses CodeHilite extension which in turn uses Pygments to fulfill the syntax highlight for code blocks. The Pygments .css file can be easily found in the static/css/ directory of any Pelican theme. Org mode can not take this advantage easily. When an Org file is exported to a HTML file, the export engine can choose either the current Emacs color theme or a specified .css file for the color option of code segments in the exported HTML file. The choices is depending on the value of org-html-htmlize-output-type and can be seen from this SE thread.

Below is some examples for source code syntax highlight.

  • Syntax highlighting for python code
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-np.pi, np.pi, 50, endpoint=False)
plt.plot(x, np.sin(x))
print("The triple-colon syntax will *not* show line numbers.")
  • Elisp source code export with line numbers and reference
(save-excursion                  (ref:sc)
   (goto-char (point-min)))      (ref:jump)

In line (sc) we remember the current position. Line (jump) jumps to point-min.

It is a shame that Org mode does not highlight syntax of hardware description languages (HDL) such as VHDL and Verilog. I can not blame Org mode for this one though. After all, what kind of execution should I expect from Babel for those HDLs? Yet I found a dirty solution to use syntax highlight from VHDL mode simply by defining a ob-vhdl.el file based on the ob-template.el and requiring it in my Emacs’s configuration file. The result can be seen in the following:

library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;

entity counter is

  port (
    clk : in  std_logic;
    rst : in  std_logic;
    cnt : out unsigned(3 downto 0));

end entity counter;

architecture RTL of counter is

begin  -- architecture RTL

  process (clk, rst) is
  begin  -- process
    if rst = '1' then                   -- asynchronous reset (active high)
      cnt <= (others => '0');
    elsif rising_edge(clk) then         -- rising clock edge
      cnt <= cnt + 1;
    end if;
  end process;

end architecture RTL;

The original problems and potential solutions are listed below. Although I followed the above post to solve the puzzle rather than the two links, they might be useful in the future.

Syntax highlight is not working properly in the html files export from org files, due to this error message “Cannot fontify src block (htmlize.el >= 1.34 required)“. However even the ‘htmlize.el’ works, the syntax color will be awful as it does not take the advantage defined in the ‘.css’ file of this theme. Better check out this two solutions: http://ergoemacs.org/emacs/elisp_htmlize.html

http://cheukyin.github.io/python/2014-08/pygments-highlight-src-export-html.html

Related