Export syntax highlighted source code to HTML

2023-10-10

To export syntax highlighted source code to HTML use GNU Source-highlight.

Example output

Export to HTML via:

source-highlight main.c

Code (from Getting Started with GTK):

#include <gtk/gtk.h>

static void
activate (GtkApplication* app,
          gpointer        user_data)
{
  GtkWidget *window;

  window = gtk_application_window_new (app);
  gtk_window_set_title (GTK_WINDOW (window), "Window");
  gtk_window_set_default_size (GTK_WINDOW (window), 200, 200);
  gtk_widget_show (window);
}

int
main (int    argc,
      char **argv)
{
  GtkApplication *app;
  int status;

  app = gtk_application_new ("org.gtk.example", G_APPLICATION_DEFAULT_FLAGS);
  g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
  status = g_application_run (G_APPLICATION (app), argc, argv);
  g_object_unref (app);

  return status;
}

Apply custom styles

To apply own styles create a style file and customize colors:

Taken from the source-highlight info page "5.1 Output format style":

info Source-highlight

Code (syntax highlighted via source-highlight):

bgcolor white;          // the background color for documents
context gray;           // the color for context lines (when specified with line ranges)
keyword blue b ;        // for language keywords
type darkgreen ;        // for basic types
usertype teal ;         // for user defined types
string red f ;          // for strings and chars
regexp orange f ;       // for strings and chars
specialchar pink f ;    // for special chars, e.g., \n, \t, \\
comment brown i, noref; // for comments
number purple ;         // for literal numbers
preproc darkblue b ;    // for preproc directives (e.g. #include, import)
symbol darkred ;        // for simbols (e.g. <, >, +)
function black b;       // for function calls and declarations
cbracket red;           // for block brackets (e.g. {, })
todo bg:cyan b;         // for TODO and FIXME
code bg:brightgreen b;  // for code snippets
//
//Predefined variables and functions (for instance glsl)
//
predef_var darkblue ;
predef_func darkblue b ;
//
// for OOP
//
classname teal ; // for class names, e.g., in Java and C++
//
// line numbers
//
linenum black f;
//
// Internet related
//
url blue u, f;
//
// other elements for ChangeLog and Log files
//
date blue b ;
time, file darkblue b ;
ip, name darkgreen ;
//
// for Prolog, Perl...
//
variable darkgreen ;
//
// explicit for Latex
//
italics darkgreen i;
bold darkgreen b;
underline darkgreen u;
fixed green f;
argument darkgreen;
optionalargument purple;
math orange;
bibtex blue;
//
// for diffs
//
oldfile orange;
newfile darkgreen;
difflines blue;
//
// for css
//
selector purple;
property blue;
value darkgreen i;
//
// for oz
//
atom orange;
meta i;
//
// for file system
//
path orange;
//
// for C (or other language) labels
//
label teal b;
//
// for errors
//
error purple;
warning darkgreen;
//
// for feature (Cucumber) files
//
cuketag green ;
gherken blue ;
given red ;
when cyan ;
then yellow ;
and_but pink ;
table gray ;

Apply custom style with:

source-highlight --style-file=/home/username/.source-highlight/default.style main.c