porting in progress
diff --git a/nbproject/project.properties b/nbproject/project.properties index ac5d4e0..6c55589 100644 --- a/nbproject/project.properties +++ b/nbproject/project.properties
@@ -64,7 +64,7 @@ jnlp.signing= jnlp.signing.alias= jnlp.signing.keystore= -main.class= +main.class=prettify.example.Example manifest.file=manifest.mf meta.inf.dir=${src.dir}/META-INF mkdist.disabled=false
diff --git a/src/prettify/Prettify.java b/src/prettify/Prettify.java index 58c4eb7..20310cd 100644 --- a/src/prettify/Prettify.java +++ b/src/prettify/Prettify.java
@@ -233,7 +233,7 @@ shortcutStylePatterns = new ArrayList<List<Object>>(); fallthroughStylePatterns = new ArrayList<List<Object>>(); shortcutStylePatterns.add(Arrays.asList(new Object[]{PR_PLAIN, Pattern.compile("^[\\s]+"), null, " \t\r\n"})); - shortcutStylePatterns.add(Arrays.asList(new Object[]{PR_ATTRIB_VALUE, Pattern.compile("^(?:\\\"[^\\\"]*\\\"?|\\'[^\\']*\\'?)"), null, "\'"})); + shortcutStylePatterns.add(Arrays.asList(new Object[]{PR_ATTRIB_VALUE, Pattern.compile("^(?:\\\"[^\\\"]*\\\"?|\\'[^\\']*\\'?)"), null, "\"'"})); fallthroughStylePatterns.add(Arrays.asList(new Object[]{PR_TAG, Pattern.compile("^^<\\/?[a-z](?:[\\w.:-]*\\w)?|\\/?>$", Pattern.CASE_INSENSITIVE)})); fallthroughStylePatterns.add(Arrays.asList(new Object[]{PR_ATTRIB_NAME, Pattern.compile("^(?!style[\\s=]|on)[a-z](?:[\\w:-]*\\w)?", Pattern.CASE_INSENSITIVE)})); fallthroughStylePatterns.add(Arrays.asList(new Object[]{"lang-uq.val", Pattern.compile("^=\\s*([^>\\'\\\"\\s]*(?:[^>\\'\\\"\\s\\/]|\\/(?=\\s)))", Pattern.CASE_INSENSITIVE)})); @@ -510,7 +510,7 @@ // beginning which would cause us to infinitely recurse on the // entire token, so we catch the right context in match[2]. embeddedSourceEnd = token.length() - match[2].length(); - embeddedSourceStart = embeddedSourceEnd = embeddedSource.length(); + embeddedSourceStart = embeddedSourceEnd - embeddedSource.length(); } String lang = style.substring(5); // Decorate the left of the embedded source @@ -746,7 +746,7 @@ if (!(extension != null && langHandlerRegistry.get(extension) != null)) { // Treat it as markup if the first non whitespace character is a < and // the last non-whitespace character is a >. - extension = Util.test(Pattern.compile("^\\s&<"), source) + extension = Util.test(Pattern.compile("^\\s*<"), source) ? "default-markup" : "default-code"; }
diff --git a/src/prettify/SyntaxHighlighter.java b/src/prettify/SyntaxHighlighter.java index a53cf26..e4d95da 100644 --- a/src/prettify/SyntaxHighlighter.java +++ b/src/prettify/SyntaxHighlighter.java
@@ -24,7 +24,7 @@ static { // set debug mode - System.setProperty("PrettifyDebugMode", "false"); + System.setProperty("PrettifyDebugMode", "true"); } private static final long serialVersionUID = 1L; /** @@ -40,6 +40,10 @@ */ protected Theme theme; /** + * The Prettify object. + */ + protected Prettify prettify; + /** * The content of the syntax highlighter, null if there is no content so far. */ protected String content; @@ -68,6 +72,7 @@ } this.theme = theme; + prettify = new Prettify(); setBorder(null); @@ -86,17 +91,18 @@ * This will re-parse the content and set the style. */ protected void render() { -// if (content != null) { -// Parser parser = new Parser(); -// // stop the change listener on the row header to speed up rendering -// highlighterRowHeader.setListenToDocumentUpdate(false); -// highlighter.setStyle(parser.parse(brush, htmlScript, content.toCharArray(), 0, content.length())); -// // resume the change listener on the row header -// highlighterRowHeader.setListenToDocumentUpdate(true); -// // notify the row header to update its information related to the SyntaxHighlighterPane -// // need to call this because we have stopped the change listener of the row header in previous code -// highlighterRowHeader.checkPanelSize(); -// } + if (content != null) { + // stop the change listener on the row header to speed up rendering + highlighterRowHeader.setListenToDocumentUpdate(false); + Job job = new Job(0, content); + prettify.langHandlerForExtension(null, content).decorate(job); + highlighter.setStyle(job.getDecorations()); + // resume the change listener on the row header + highlighterRowHeader.setListenToDocumentUpdate(true); + // notify the row header to update its information related to the SyntaxHighlighterPane + // need to call this because we have stopped the change listener of the row header in previous code + highlighterRowHeader.checkPanelSize(); + } } /**
diff --git a/src/prettify/SyntaxHighlighterPane.java b/src/prettify/SyntaxHighlighterPane.java index 82059ec..9df1190 100644 --- a/src/prettify/SyntaxHighlighterPane.java +++ b/src/prettify/SyntaxHighlighterPane.java
@@ -19,9 +19,7 @@ import java.awt.event.MouseEvent; import java.awt.event.MouseMotionListener; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; -import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.JTextPane; @@ -83,9 +81,9 @@ */ protected Theme theme; /** - * The style list. see {@link #setStyle(java.util.Map)}. + * The style list. see {@link #setStyle(java.util.List)}. */ - protected Map<String, List<Object>> styleList; + protected List<Object> styleList; /** * Record the mouse cursor is currently pointing at which line of the document. -1 means not any line. * It is used internally. @@ -319,13 +317,12 @@ * Apply the list of style to the script text pane. * @param styleList the style list */ - public void setStyle(Map<String, List<Object>> styleList) { + public void setStyle(List<Object> styleList) { if (styleList == null) { - this.styleList = new HashMap<String, List<Object>>(); + this.styleList = new ArrayList<Object>(); return; } - // not a deep copy - this.styleList = new HashMap<String, List<Object>>(styleList); + this.styleList = new ArrayList<Object>(styleList); if (theme == null) { return; @@ -335,15 +332,14 @@ // clear all the existing style document.setCharacterAttributes(0, document.getLength(), theme.getPlain().getAttributeSet(), true); -// // apply style according to the style list -// for (String key : styleList.keySet()) { -// List<Object> posList = styleList.get(key); -// -// AttributeSet attributeSet = theme.getStyle(key).getAttributeSet(); -// for (Object pos : posList) { -// document.setCharacterAttributes(pos.getOffset(), pos.getLength(), attributeSet, true); -// } -// } + Integer startPos = 0, endPos = 0; + // apply style according to the style list + for (int i = 0, iEnd = styleList.size(); i < iEnd; i += 2) { + SimpleAttributeSet attributeSet = theme.getStyle((String) styleList.get(i + 1)).getAttributeSet(); + endPos = i + 2 < iEnd ? (Integer) styleList.get(i + 2) : document.getLength(); + startPos = (Integer) styleList.get(i); + document.setCharacterAttributes(startPos, endPos - startPos, attributeSet, true); + } repaint(); }
diff --git a/src/prettify/Theme.java b/src/prettify/Theme.java index cf60dbc..0c6f505 100644 --- a/src/prettify/Theme.java +++ b/src/prettify/Theme.java
@@ -171,7 +171,7 @@ return type; } else if (key.equals("lit")) { return literal; - } else if (key.equals("pub")) { + } else if (key.equals("pun")) { return punctuation; } else if (key.equals("pln")) { return plain;
diff --git a/src/prettify/Util.java b/src/prettify/Util.java index b8c69e9..874a111 100644 --- a/src/prettify/Util.java +++ b/src/prettify/Util.java
@@ -18,6 +18,9 @@ Matcher matcher = pattern.matcher(string); while (matcher.find()) { matchesList.add(matcher.group(0)); + for (int i = 1, iEnd = matcher.groupCount(); i <= iEnd; i++) { + matchesList.add(matcher.group(i)); + } } return matchesList.toArray(new String[matchesList.size()]);
diff --git a/src/prettify/example/Example.java b/src/prettify/example/Example.java new file mode 100644 index 0000000..5c528a2 --- /dev/null +++ b/src/prettify/example/Example.java
@@ -0,0 +1,111 @@ +/** + * This is part of the Java SyntaxHighlighter. + * + * It is distributed under MIT license. See the file 'readme.txt' for + * information on usage and redistribution of this file, and for a + * DISCLAIMER OF ALL WARRANTIES. + * + * @author Chan Wai Shing <cws1989@gmail.com> + */ +package prettify.example; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.Arrays; +import java.util.logging.Level; +import java.util.logging.Logger; +import javax.swing.JFrame; +import javax.swing.SwingUtilities; +import javax.swing.UIManager; +import prettify.SyntaxHighlighter; +import prettify.theme.ThemeDefault; + +/** + * Usage example. This will just cover some of the functions. To know other available functions, please read the JavaDoc. + * @author Chan Wai Shing <cws1989@gmail.com> + */ +public class Example { + + /** + * Read the resource file from the jar. + * @param path the resource path + * @return the content of the resource file in byte array + * @throws IOException error occurred when reading the content from the file + */ + public static byte[] readResourceFile(String path) throws IOException { + if (path == null) { + throw new NullPointerException("argument 'path' cannot be null"); + } + + ByteArrayOutputStream bout = new ByteArrayOutputStream(); + InputStream in = null; + try { + in = Example.class.getResourceAsStream(path); + if (in == null) { + throw new IOException("Resources not found: " + path); + } + + int byteRead = 0; + byte[] b = new byte[8096]; + + while ((byteRead = in.read(b)) != -1) { + bout.write(b, 0, byteRead); + } + } finally { + if (in != null) { + try { + in.close(); + } catch (IOException ex) { + } + } + } + + return bout.toByteArray(); + } + + public static void main(String[] args) { + // set look & feel + try { + UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); + } catch (Exception ex) { + Logger.getLogger(Example.class.getName()).log(Level.INFO, "Failed to set system look and feel.", ex); + } + + SwingUtilities.invokeLater(new Runnable() { + + @Override + public void run() { + try { + // timer start + long start, end; + start = System.currentTimeMillis(); + + // use XML (for HTML) brush and RDark theme + SyntaxHighlighter highlighter = new SyntaxHighlighter(new ThemeDefault()); + // set the line number count from 10 instead of 1 + highlighter.setFirstLine(10); + // set to highlight line 13, 27, 28, 38, 42, 43 and 53 + highlighter.setHighlightedLineList(Arrays.asList(13, 27, 28, 38, 42, 43, 53)); + // set the content of the script, the example.html is located in the jar: /syntaxhighlighter/example/example.html + highlighter.setContent(new String(readResourceFile("/prettify/example/example.html"))); + + // timer end + end = System.currentTimeMillis(); + System.out.println("time elapsed: " + (end - start) + "ms"); + + // initiate a frame and display + JFrame frame = new JFrame(); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + // SyntaxHighlighter is actually a JScrollPane + frame.setContentPane(highlighter); + frame.setLocationByPlatform(true); + frame.pack(); + frame.setVisible(true); + } catch (Exception ex) { + Logger.getLogger(Example.class.getName()).log(Level.SEVERE, null, ex); + } + } + }); + } +}
diff --git a/src/prettify/example/example.html b/src/prettify/example/example.html new file mode 100644 index 0000000..d378884 --- /dev/null +++ b/src/prettify/example/example.html
@@ -0,0 +1,47 @@ +<html var="value"> +<head> + <title> HTML Script Example </title> + <style type="text/css"> + body { + font-family: Helvetica, Geneva, Arial, sans-serif; + font-size: 1em; + margin: 4em 15em 4em 4em; + } + body.with_sponsor { margin-right: 410px; } + a.external { + background: url(../images/external.png) right 50% no-repeat; + padding-right: 12px; + } + code, pre, .file-list { + font-family: Consolas, Monaco, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; + } + </style> + <script type="text/javascript"> + var _gaq = _gaq || []; + _gaq.push(['_setAccount', 'UA-XXXXX-XX']); + _gaq.push(['_trackPageview']); + + (function() { + var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; + ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; + var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); + })(); + </script> +</head> +<body> + +<div style="font-weight: bold"><?= str_replace("\n", "<br/>", $var) ?></div> +<?php + /*********************************** + ** Multiline block comments + **********************************/ + + $stringWithUrl = "http://alexgorbatchev.com"; + $stringWithUrl = 'http://alexgorbatchev.com'; + + ob_start("parseOutputBuffer"); // Start Code Buffering + session_start(); +?> + +</body> +</html> \ No newline at end of file
diff --git a/src/prettify/example/package-info.java b/src/prettify/example/package-info.java new file mode 100644 index 0000000..736f3f9 --- /dev/null +++ b/src/prettify/example/package-info.java
@@ -0,0 +1,4 @@ +/** + * Usage example. + */ +package prettify.example; \ No newline at end of file
diff --git a/src/prettify/theme/ThemeDefault.java b/src/prettify/theme/ThemeDefault.java index 43cdceb..5f7cc3e 100644 --- a/src/prettify/theme/ThemeDefault.java +++ b/src/prettify/theme/ThemeDefault.java
@@ -15,71 +15,71 @@ setFont(new Font("Consolas", Font.PLAIN, 12)); setBackground(Color.white); - setHighlightedBackground(Color.decode("0xeee")); + setHighlightedBackground(Color.decode("0xcccccc")); - setGutterText(Color.decode("0x000")); - setGutterBorderColor(Color.decode("0xeee")); + setGutterText(Color.decode("0x000000")); + setGutterBorderColor(Color.decode("0xaaaaaa")); setGutterBorderWidth(3); setGutterTextFont(new Font("Verdana", Font.PLAIN, 11)); setGutterTextPaddingLeft(7); setGutterTextPaddingRight(7); Style plainStyle = new Style(); - plainStyle.setColor(Color.decode("0x000")); + plainStyle.setColor(Color.decode("0x000000")); setPlain(plainStyle); Style style; style = new Style(); - style.setColor(Color.decode("0x080")); + style.setColor(Color.decode("0x008800")); setString(style); style = new Style(); - style.setColor(Color.decode("0x008")); + style.setColor(Color.decode("0x000088")); setKeyword(style); style = new Style(); - style.setColor(Color.decode("0x800")); + style.setColor(Color.decode("0x880000")); setComment(style); style = new Style(); - style.setColor(Color.decode("0x606")); + style.setColor(Color.decode("0x660066")); setType(style); style = new Style(); - style.setColor(Color.decode("0x066")); + style.setColor(Color.decode("0x006666")); setLiteral(style); style = new Style(); - style.setColor(Color.decode("0x660")); + style.setColor(Color.decode("0x666600")); setPunctuation(style); style = new Style(); - style.setColor(Color.decode("0x008")); + style.setColor(Color.decode("0x000088")); setTag(style); setDeclaration(plainStyle); style = new Style(); - style.setColor(Color.decode("0x606")); + style.setColor(Color.decode("0x660066")); setAttributeName(style); style = new Style(); - style.setColor(Color.decode("0x080")); + style.setColor(Color.decode("0x008800")); setAttributeValue(style); setNoCode(plainStyle); style = new Style(); - style.setColor(Color.decode("0x660")); + style.setColor(Color.decode("0x666600")); setOpenBracket(style); style = new Style(); - style.setColor(Color.decode("0x660")); + style.setColor(Color.decode("0x666600")); setCloseBracket(style); style = new Style(); - style.setColor(Color.decode("0x606")); + style.setColor(Color.decode("0x660066")); setVariable(style); style = new Style();
diff --git a/src/prettify/theme/ThemeDesert.java b/src/prettify/theme/ThemeDesert.java index 70398d0..a967a8c 100644 --- a/src/prettify/theme/ThemeDesert.java +++ b/src/prettify/theme/ThemeDesert.java
@@ -15,19 +15,19 @@ /* desert scheme ported from vim to google prettify */ setFont(new Font("Consolas", Font.PLAIN, 12)); - setBackground(Color.decode("0x000")); + setBackground(Color.decode("0x111111")); - setHighlightedBackground(Color.decode("0x111")); + setHighlightedBackground(Color.decode("0x444444")); - setGutterText(Color.decode("0xfff")); - setGutterBorderColor(Color.decode("0x111")); + setGutterText(Color.decode("0xffffff")); + setGutterBorderColor(Color.decode("0x888888")); setGutterBorderWidth(3); setGutterTextFont(new Font("Verdana", Font.PLAIN, 11)); setGutterTextPaddingLeft(7); setGutterTextPaddingRight(7); Style plainStyle = new Style(); - plainStyle.setColor(Color.decode("0xfff")); + plainStyle.setColor(Color.decode("0xffffff")); setPlain(plainStyle); Style style; @@ -54,7 +54,7 @@ setLiteral(style); style = new Style(); - style.setColor(Color.decode("0xfff")); + style.setColor(Color.decode("0xffffff")); setPunctuation(style); style = new Style(); @@ -77,7 +77,7 @@ setAttributeValue(style); style = new Style(); - style.setColor(Color.decode("0x333")); + style.setColor(Color.decode("0x333333")); setNoCode(style); setOpenBracket(plainStyle);
diff --git a/src/prettify/theme/ThemeSonsOfObsidian.java b/src/prettify/theme/ThemeSonsOfObsidian.java index 08920bb..c6177c1 100644 --- a/src/prettify/theme/ThemeSonsOfObsidian.java +++ b/src/prettify/theme/ThemeSonsOfObsidian.java
@@ -21,12 +21,12 @@ */ setFont(new Font("Consolas", Font.PLAIN, 12)); - setBackground(Color.white); + setBackground(Color.decode("0x000000")); - setHighlightedBackground(Color.decode("0x111")); + setHighlightedBackground(Color.decode("0x333333")); - setGutterText(Color.decode("0xF1F2F3")); - setGutterBorderColor(Color.decode("0x111")); + setGutterText(Color.decode("0x555555")); + setGutterBorderColor(Color.decode("0x666666")); setGutterBorderWidth(3); setGutterTextFont(new Font("Verdana", Font.PLAIN, 11)); setGutterTextPaddingLeft(7);
diff --git a/src/prettify/theme/ThemeSunburst.java b/src/prettify/theme/ThemeSunburst.java index ef75c80..4865938 100644 --- a/src/prettify/theme/ThemeSunburst.java +++ b/src/prettify/theme/ThemeSunburst.java
@@ -19,17 +19,17 @@ setFont(new Font("Consolas", Font.PLAIN, 12)); setBackground(Color.black); - setHighlightedBackground(Color.decode("0xAEAEAE")); + setHighlightedBackground(Color.decode("0x444444")); - setGutterText(Color.decode("0xfff")); - setGutterBorderColor(Color.decode("0xAEAEAE")); + setGutterText(Color.decode("0xffffff")); + setGutterBorderColor(Color.decode("0x777777")); setGutterBorderWidth(3); setGutterTextFont(new Font("Verdana", Font.PLAIN, 11)); setGutterTextPaddingLeft(7); setGutterTextPaddingRight(7); Style plainStyle = new Style(); - plainStyle.setColor(Color.decode("0xfff")); + plainStyle.setColor(Color.decode("0xffffff")); setPlain(plainStyle); Style style; @@ -56,7 +56,7 @@ setLiteral(style); style = new Style(); - style.setColor(Color.decode("0xfff")); /* punctuation - white */ + style.setColor(Color.decode("0xffffff")); /* punctuation - white */ setPunctuation(style); style = new Style();