Automatic Syntax Highlighting in GetPublished

By: Yorai Aminov

Abstract: GetPublished now supports automatic syntax highlighting of source code in your articles

    Automatic Syntax Highlighting

Articles on the Developer Network are usually technical by nature, and often include source code. GetPublished, CDN’s content management system, now supports automatic syntax highlighting using YAPP.

YAPP (Yet Another Pretty Printer) is a source code syntax highlighter, written by John Kaster. The original version of YAPP is available as a Win32 executable from CodeCentral. YAPP is also available as a web service.

    Including Source Code in Articles

Source code should be marked using a <pre class="sourcecode"> tag (for HTML articles) or one of the code styles included in the Developer Network Word template. To get syntax highlighting for your embedded source code, you must also specify the code language.

icon_new

New feature: YAPP now support PHP and Ruby syntax highlighting. PHP and Ruby support is still experimental.

    Specifying Code Language in Word

The Word template includes several source code styles, corresponding to the languages supported by YAPP:

  • C# Code
  • C++ Code
  • Delphi Code
  • Java Code
  • JavaScript Code
  • PHP Code (beta)
  • Ruby Code (beta)
  • SQL Code
  • DFM Code
  • OMG IDL Code
  • MS IDL Code
  • Xbase Code

If your language is not yet supported, you can use the generic “SourceCode” style.

For example, the following code is marked with the “Delphi Code” style:

class procedure WebUtils.RequireSSL;
var
  redir: string;
begin
  if (HttpContext.Current.Request.ServerVariables['HTTPS'] = 'off') then
  begin
    redir := 'https://' + HttpContext.Current.Request.ServerVariables['SERVER_NAME']
      + HttpContext.Current.Request.RawUrl;
    HttpContext.Current.Response.Redirect(redir, True);
  end;
end;

    Specifying Code Language in HTML

Source code in HTML documents is marked by setting the class attribute in a pre tag to “sourcecode”. You can specify the code language using the language attribute:

<pre class="sourcecode" language="csharp">
using System;
using System.IO;

class Test 
{
    public static void Main() 
    {
        // Create an instance of StreamWriter to write text to a file.
        // The using statement also closes the StreamWriter.
        using (StreamWriter sw = new StreamWriter("TestFile.txt")) 
        {
            // Add some text to the file.
            sw.Write("This is the ");
            sw.WriteLine("header for the file.");
            sw.WriteLine("-------------------");
            // Arbitrary objects can also be written to the file.
            sw.Write("The date is: ");
            sw.WriteLine(DateTime.Now);
        }
    }
}
</pre>

The HTML snippet above will be automatically highlighted using C# syntax:

using System;
using System.IO;

class Test 
{
    public static void Main() 
    {
        // Create an instance of StreamWriter to write text to a file.
        // The using statement also closes the StreamWriter.
        using (StreamWriter sw = new StreamWriter("TestFile.txt")) 
        {
            // Add some text to the file.
            sw.Write("This is the ");
            sw.WriteLine("header for the file.");
            sw.WriteLine("-------------------");
            // Arbitrary objects can also be written to the file.
            sw.Write("The date is: ");
            sw.WriteLine(DateTime.Now);
        }
    }
}

The following values are recognized for the language attribute:

Language

Attribute Value

Delphi

delphi

C#

csharp

C/C++

cpp

Java

java

JavaScript

javascript

PHP

php

Ruby

ruby

SQL

sql

DFM

dfm

xBase

xbase

OMG IDL

omgidl

MS IDL

msidl

    Formatting in Code Blocks

GetPublished’s automatic syntax highlighting overrides any existing formatting in source code blocks. Of course, if GetPublished can’t recognize the code language, existing formatting will be preserved.

    Language Filtering

An additional benefit of specifying source code language is the ability to filter languages. If an article contains code samples in more than one language, you can filter the displayed languages by clicking the language filter button at the top of the page:

Hide image

icon_new

New feature: You can now filter code samples in an article.

Clicking on any language from the list will hide code in any other language (code without a language attribute will always be displayed). To see code in any available language, click “Show All.”

Server Response from: ETNASC04