org.tinyjee.maven.dim.sh
Class CodeHighlighter

java.lang.Object
  extended by org.tinyjee.maven.dim.sh.CodeHighlighter

public class CodeHighlighter
extends Object

Performs code highlighting on the provided source before writing it to the Sink.

Version:
1.0
Author:
Juergen_Kellerer, 2010-09-04

Constructor Summary
CodeHighlighter()
           
 
Method Summary
 boolean attachStylesheet(Sink sink, Map requestParameters)
          Attaches the cascading stylesheet required to do code highlighting to the sink.
 Map<String,String> buildHighlighterParams(URL source, Map<Integer,List<String>> sourceLines, Map requestParameters)
          Builds a map of params that can be passed to the SyntaxHighlighter library.
 void execute(URL source, Map requestParameters, Map<Integer,List<String>> sourceLines, Sink sink)
          Processes the given source lines and writes the results into the sink.
protected  String findBrushAlias(URL source, Map<Integer,List<String>> sourceLines, Map requestParameters)
          Finds the name of the code highlighter to use.
 String findBrushName(URL source, Map<Integer,List<String>> sourceLines, Map requestParameters)
          Finds the name of the code highlighter to use.
 String highlight(Map<Integer,List<String>> sourceLines, int firstLine, String brushName, Map<String,String> params)
          Hightlights the given source lines using HTML markup.
 String highlight(String brushName, String sourceContent, Map<String,String> params)
           
protected  boolean isCodeSurroundedByHTML(URL source, Map<Integer,List<String>> sourceLines, Map requestParameters)
          Returns true if the source content is HTML with embedded source code of another language.
protected  String removeLines(String content, Collection<Integer> linesToRemove)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CodeHighlighter

public CodeHighlighter()
Method Detail

highlight

public String highlight(String brushName,
                        String sourceContent,
                        Map<String,String> params)
Parameters:
brushName -
sourceContent -
params -
Returns:

highlight

public String highlight(Map<Integer,List<String>> sourceLines,
                        int firstLine,
                        String brushName,
                        Map<String,String> params)
Hightlights the given source lines using HTML markup.

Parameters:
sourceLines - a map of first-line number to lines.
firstLine - set to >= 0 to adjust the first line index.
brushName - the name of the brush used for code highlighting, e.g. "java".
params - any params to pass to the highlighting library.
Returns:
the highlighted html code.

attachStylesheet

public boolean attachStylesheet(Sink sink,
                                Map requestParameters)
                         throws IOException
Attaches the cascading stylesheet required to do code highlighting to the sink.

Parameters:
sink - the sink to operate on, may be 'null' to use the global default.
requestParameters - the request parameters that may modify the style selection.
Returns:
true if the sink supported attaching the styles.
Throws:
IOException - In case of writing the attachment failed.

execute

public void execute(URL source,
                    Map requestParameters,
                    Map<Integer,List<String>> sourceLines,
                    Sink sink)
             throws IOException
Processes the given source lines and writes the results into the sink.

Parameters:
source - The source URL used to obtain the content (can be 'null').
sourceLines - a map of first line number to lines mapping.
requestParameters - the original request params.
sink - the target sink to write to.
Throws:
IOException - in case of the process fails.

findBrushName

public String findBrushName(URL source,
                            Map<Integer,List<String>> sourceLines,
                            Map requestParameters)
Finds the name of the code highlighter to use.

Parameters:
source - the source URL.
sourceLines - the source content.
requestParameters - the request parameters of the macro request.
Returns:
the name of the code highlighter to use or 'plain' if not found.

findBrushAlias

protected String findBrushAlias(URL source,
                                Map<Integer,List<String>> sourceLines,
                                Map requestParameters)
Finds the name of the code highlighter to use.

Parameters:
source - the source URL.
sourceLines - the source content.
requestParameters - the request parameters of the macro request.
Returns:
the name of the code highlighter to use or 'plain' if not found.

isCodeSurroundedByHTML

protected boolean isCodeSurroundedByHTML(URL source,
                                         Map<Integer,List<String>> sourceLines,
                                         Map requestParameters)
Returns true if the source content is HTML with embedded source code of another language.

Parameters:
source - the source URL.
sourceLines - the source content.
requestParameters - the request parameters of the macro request.
Returns:
true if the source content is HTML with embedded source code of another language.

buildHighlighterParams

public Map<String,String> buildHighlighterParams(URL source,
                                                 Map<Integer,List<String>> sourceLines,
                                                 Map requestParameters)
Builds a map of params that can be passed to the SyntaxHighlighter library.

Parameters:
source - the source URL.
sourceLines - the source content.
requestParameters - the request parameters of the macro request.
Returns:
a map of params that can be passed to the SyntaxHighlighter library.

removeLines

protected String removeLines(String content,
                             Collection<Integer> linesToRemove)