Skip to content

April 13, 2013

Markdown, Syntax-Colored Source Code

by Andreas Schaefer

In this post I want to see if I can use Markdown and Blog Text in order to have syntax colored source code.

Source Code

This is an inner Java Class:

private static class LogLevelConfig {
    private LogLevel mLogLevel;
    private Pattern mModelIdGlobPattern;
    private Pattern mModelTitleGlobPattern;

    public LogLevelConfig( String pModelIdGlob, String pModelTitleGlob, String pLogLevel ) {
        if( pLogLevel == null || pLogLevel.trim().length() == 0 ) {
            mLogLevel = LogLevel.debug;
        } else {
            try {
                mLogLevel = LogLevel.valueOf( pLogLevel );
                LOG.info( "activate(), given log level found: {}", mLogLevel );
            } catch( Exception e ) {
                throw new IllegalArgumentException( "no log level found for: '" + pLogLevel + "'" );
            }
        }
        if( pModelIdGlob != null && pModelIdGlob.trim().length() > 0 ) {
            mModelIdGlobPattern = Pattern.compile( pModelIdGlob );
        }
        if( pModelTitleGlob != null && pModelTitleGlob.trim().length() > 0 ) {
            mModelTitleGlobPattern = Pattern.compile( pModelTitleGlob );
        }
    }

    public LogLevel getLogLevel() {
        return mLogLevel;
    }

    public boolean matchWorkflowModel( WorkflowModel pWorkflowModel ) {
        boolean lReturn = true;
        if( mModelIdGlobPattern != null ) {
            lReturn = mModelIdGlobPattern.matcher( pWorkflowModel.getId() ).matches();
        }
        if( mModelTitleGlobPattern != null ) {
            lReturn &= mModelTitleGlobPattern.matcher( pWorkflowModel.getTitle() ).matches();
        }
        return lReturn;
    }
}

This is now the same code in Markdown:

private static class LogLevelConfig {
    private LogLevel mLogLevel;
    private Pattern mModelIdGlobPattern;
    private Pattern mModelTitleGlobPattern;

    public LogLevelConfig( String pModelIdGlob, String pModelTitleGlob, String pLogLevel ) {
        if( pLogLevel == null || pLogLevel.trim().length() == 0 ) {
            mLogLevel = LogLevel.debug;
        } else {
            try {
                mLogLevel = LogLevel.valueOf( pLogLevel );
                LOG.info( "activate(), given log level found: {}", mLogLevel );
            } catch( Exception e ) {
                throw new IllegalArgumentException( "no log level found for: '" + pLogLevel + "'" );
            }
        }
        if( pModelIdGlob != null && pModelIdGlob.trim().length() > 0 ) {
            mModelIdGlobPattern = Pattern.compile( pModelIdGlob );
        }
        if( pModelTitleGlob != null && pModelTitleGlob.trim().length() > 0 ) {
            mModelTitleGlobPattern = Pattern.compile( pModelTitleGlob );
        }
    }

    public LogLevel getLogLevel() {
        return mLogLevel;
    }

    public boolean matchWorkflowModel( WorkflowModel pWorkflowModel ) {
        boolean lReturn = true;
        if( mModelIdGlobPattern != null ) {
            lReturn = mModelIdGlobPattern.matcher( pWorkflowModel.getId() ).matches();
        }
        if( mModelTitleGlobPattern != null ) {
            lReturn &= mModelTitleGlobPattern.matcher( pWorkflowModel.getTitle() ).matches();
        }
        return lReturn;
    }
}

How I did it

First I must make sure that Markdown is ignoring my Blog Text Code snippet by enclosing the entire Code into a <div> block and then use the Blog Text syntax to define the code block which is are 3 cutely brackets together with the attribute lang defining the source code language for the Syntax Coloring. When I wanted to display the Markdown version of the Source Code I had to use the Blog Text No-Parse tags (two curly brackets followed by two exclamation marks). But if I use Blog Text then I will use only Blog Text for source code and not both.

The only downside of that all is that Markdown preview displays just a blob of text. Still the code is not infused with HTML making it impossible to read and without the additional work to convert the code every time to HTML.

— Andy

Leave a comment