Permalink
Browse files

caching code highlighted by pygments

  • Loading branch information...
csokol committed Oct 9, 2012
1 parent 4aae9f0 commit 938b05c4218a1722e7622e1abe64e2de19acf922
@@ -6,6 +6,7 @@
import br.com.caelum.tubaina.parser.SimpleIndentator;
import br.com.caelum.tubaina.parser.Tag;
+import br.com.caelum.tubaina.parser.html.desktop.CodeCache;
import br.com.caelum.tubaina.parser.html.desktop.SyntaxHighlighter;
import br.com.caelum.tubaina.util.CommandExecutor;
@@ -20,7 +21,8 @@
public HtmlAndKindleCodeTag() {
codeTagOptionsParser = new CodeTagOptionsParser();
- this.htmlCodeHighlighter = new SyntaxHighlighter(new CommandExecutor(), SyntaxHighlighter.HTML_OUTPUT);
+ this.htmlCodeHighlighter = new SyntaxHighlighter(new CommandExecutor(),
+ SyntaxHighlighter.HTML_OUTPUT, false, new CodeCache(SyntaxHighlighter.HTML_OUTPUT));
}
public HtmlAndKindleCodeTag(SyntaxHighlighter htmlCodeHighlighter) {
@@ -0,0 +1,64 @@
+package br.com.caelum.tubaina.parser.html.desktop;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.PrintStream;
+import java.util.Scanner;
+
+import org.apache.commons.codec.digest.DigestUtils;
+import org.apache.log4j.Logger;
+
+public class CodeCache {
+
+ private String output;
+ private String tempDir;
+ public static final Logger LOG = Logger.getLogger(CodeCache.class);
+
+ public CodeCache(String output) {
+ this.output = output;
+ this.tempDir = System.getProperty("java.io.tmpdir");
+ }
+
+ public boolean exists(String code) {
+ File cacheFile = cacheFile(code);
+
+ return cacheFile.exists();
+ }
+
+ public String find(String code) {
+ File cacheFile = cacheFile(code);
+
+ Scanner s;
+ try {
+ s = new Scanner(new FileInputStream(cacheFile));
+ s.useDelimiter("\\A");
+ LOG.debug("found code on cache: " + cacheFile);
+ return s.next();
+ } catch (FileNotFoundException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ public void write(String plainCode, String codeHighlighted) {
+ if (!exists(plainCode)) {
+ File cacheFile = cacheFile(plainCode);
+ try {
+ LOG.debug("writing code to cache: " + cacheFile);
+ cacheFile.createNewFile();
+ PrintStream saida = new PrintStream(cacheFile);
+ saida.print(codeHighlighted);
+ saida.close();
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
+ }
+
+ private File cacheFile(String plainCode) {
+ String md5 = DigestUtils.md5Hex(plainCode);
+ return new File(tempDir, md5 + output + ".code");
+ }
+
+}
@@ -14,18 +14,28 @@
public static final String HTML_OUTPUT = "html";
public static final String LATEX_OUTPUT = "latex";
+
+ private CodeCache codeCache;
- public SyntaxHighlighter(CommandExecutor commandExecutor, String outputType) {
+ public SyntaxHighlighter(CommandExecutor commandExecutor, String outputType, boolean allLinesNumbered, CodeCache codeCache) {
this.commandExecutor = commandExecutor;
this.output = outputType;
- }
-
- public SyntaxHighlighter(CommandExecutor commandExecutor, String output, boolean allLinesNumbered) {
- this(commandExecutor, output);
this.allLinesNumbered = allLinesNumbered;
+ this.codeCache = codeCache;
}
public String highlight(String code, String language, boolean numbered, List<Integer> lines) {
+ if (codeCache.exists(code)) {
+ return codeCache.find(code);
+ }
+ ArrayList<String> commandWithArgs = buildCommand(language, numbered, lines);
+ String codeHighlighted = commandExecutor.execute(commandWithArgs, code);
+
+ codeCache.write(code, codeHighlighted);
+ return codeHighlighted;
+ }
+
+ private ArrayList<String> buildCommand(String language, boolean numbered, List<Integer> lines) {
StringBuilder options = new StringBuilder();
if (numbered || allLinesNumbered) { // for kindle output all lines are numbered
appendLineNumberingOption(options);
@@ -47,8 +57,7 @@ public String highlight(String code, String language, boolean numbered, List<Int
commands.add(output);
commands.add("-l");
commands.add(language);
-
- return commandExecutor.execute(commands, code);
+ return commands;
}
private void appendLineNumberingOption(StringBuilder options) {
@@ -2,6 +2,7 @@
import br.com.caelum.tubaina.parser.Tag;
import br.com.caelum.tubaina.parser.html.HtmlAndKindleCodeTag;
+import br.com.caelum.tubaina.parser.html.desktop.CodeCache;
import br.com.caelum.tubaina.parser.html.desktop.SyntaxHighlighter;
import br.com.caelum.tubaina.util.CommandExecutor;
@@ -11,7 +12,7 @@
public CodeTag() {
this.htmlCodeTag = new HtmlAndKindleCodeTag(new SyntaxHighlighter(new CommandExecutor(),
- SyntaxHighlighter.HTML_OUTPUT, true));
+ SyntaxHighlighter.HTML_OUTPUT, true, new CodeCache(SyntaxHighlighter.HTML_OUTPUT)));
}
public CodeTag(HtmlAndKindleCodeTag htmlCodeTag) {
@@ -21,7 +21,7 @@
public CodeTag(Indentator indentator, SyntaxHighlighter syntaxHighlighter) {
this.indentator = indentator;
this.syntaxHighlighter = syntaxHighlighter;
- codeTagOptionsParser = new CodeTagOptionsParser();
+ this.codeTagOptionsParser = new CodeTagOptionsParser();
}
public String parse(String code, String options) {
@@ -4,6 +4,7 @@
import br.com.caelum.tubaina.gists.GistResultRetriever;
import br.com.caelum.tubaina.parser.Indentator;
import br.com.caelum.tubaina.parser.Tag;
+import br.com.caelum.tubaina.parser.html.desktop.CodeCache;
import br.com.caelum.tubaina.parser.html.desktop.SyntaxHighlighter;
import br.com.caelum.tubaina.util.CommandExecutor;
@@ -13,7 +14,7 @@
private GistResultRetriever retriever;
public GistTag(Indentator i, GistResultRetriever retriever) {
- this.code = new CodeTag(i, new SyntaxHighlighter(new CommandExecutor(), SyntaxHighlighter.LATEX_OUTPUT));
+ this.code = new CodeTag(i, new SyntaxHighlighter(new CommandExecutor(), SyntaxHighlighter.LATEX_OUTPUT, false, new CodeCache(SyntaxHighlighter.LATEX_OUTPUT)));
this.retriever = retriever;
}
@@ -9,6 +9,7 @@
import br.com.caelum.tubaina.parser.Parser;
import br.com.caelum.tubaina.parser.SimpleIndentator;
import br.com.caelum.tubaina.parser.Tag;
+import br.com.caelum.tubaina.parser.html.desktop.CodeCache;
import br.com.caelum.tubaina.parser.html.desktop.SyntaxHighlighter;
import br.com.caelum.tubaina.util.CommandExecutor;
@@ -65,7 +66,7 @@ public String parseParagraph(String text) {
}
public String parseCode(String text, String options) {
- String string = new CodeTag(new SimpleIndentator(4), new SyntaxHighlighter(new CommandExecutor(), SyntaxHighlighter.LATEX_OUTPUT))
+ String string = new CodeTag(new SimpleIndentator(4), new SyntaxHighlighter(new CommandExecutor(), SyntaxHighlighter.LATEX_OUTPUT, false, new CodeCache(SyntaxHighlighter.LATEX_OUTPUT)))
.parse(text, options);
return string + VSPACE;
}
@@ -15,17 +15,21 @@
public class SyntaxHighlighterTest {
private String sampleCode;
+ private CommandExecutor executor;
+ private SyntaxHighlighter highlighter;
+ private CodeCache codeCache;
@Before
public void setUp() {
- sampleCode = "public class Foo {\n" + "public int Bar(){\n" + "return 0;\n" + "}\n" + "}";
+ this.sampleCode = "public class Foo {\n" + "public int Bar(){\n" + "return 0;\n" + "}\n" + "}";
+ this.executor = mock(CommandExecutor.class);
+ this.codeCache = mock(CodeCache.class);
+ this.highlighter = new SyntaxHighlighter(executor, SyntaxHighlighter.HTML_OUTPUT, false, codeCache);
}
@Test
public void shouldCallPygmentsWithJavaLexer() throws Exception {
- CommandExecutor executor = mock(CommandExecutor.class);
String code = "public class Foo {\n" + "public int Bar(){\n" + "return 0;\n" + "}\n" + "}";
- SyntaxHighlighter highlighter = new SyntaxHighlighter(executor, SyntaxHighlighter.HTML_OUTPUT);
highlighter.highlight(code, "java", false);
String encoding = System.getProperty("file.encoding");
List<String> arguments = Arrays.asList("pygmentize", "-O", "encoding=" + encoding
@@ -35,8 +39,6 @@ public void shouldCallPygmentsWithJavaLexer() throws Exception {
@Test
public void shouldCallPygmentsWithNumberedLinesOption() throws Exception {
- CommandExecutor executor = mock(CommandExecutor.class);
- SyntaxHighlighter highlighter = new SyntaxHighlighter(executor, SyntaxHighlighter.HTML_OUTPUT);
highlighter.highlight(sampleCode, "java", true);
String encoding = System.getProperty("file.encoding");
List<String> arguments = Arrays.asList("pygmentize", "-O", "encoding=" + encoding
@@ -46,8 +48,6 @@ public void shouldCallPygmentsWithNumberedLinesOption() throws Exception {
@Test
public void shouldCallPygmentsWithHlLines() throws Exception {
- CommandExecutor executor = mock(CommandExecutor.class);
- SyntaxHighlighter highlighter = new SyntaxHighlighter(executor, SyntaxHighlighter.HTML_OUTPUT);
List<Integer> lines = Arrays.asList(1, 2, 5);
@@ -63,8 +63,7 @@ public void shouldCallPygmentsWithHlLines() throws Exception {
@Test
public void shouldCallPygmentsWithLatexOutput() throws Exception {
- CommandExecutor executor = mock(CommandExecutor.class);
- SyntaxHighlighter highlighter = new SyntaxHighlighter(executor, SyntaxHighlighter.LATEX_OUTPUT);
+ this.highlighter = new SyntaxHighlighter(executor, SyntaxHighlighter.LATEX_OUTPUT, false, codeCache);
highlighter.highlight(sampleCode, "java", false);
String encoding = System.getProperty("file.encoding");
List<String> arguments = Arrays.asList("pygmentize", "-O", "encoding=" + encoding
@@ -7,6 +7,7 @@
import org.junit.Test;
import br.com.caelum.tubaina.parser.SimpleIndentator;
+import br.com.caelum.tubaina.parser.html.desktop.CodeCache;
import br.com.caelum.tubaina.parser.html.desktop.SyntaxHighlighter;
import br.com.caelum.tubaina.util.CommandExecutor;
@@ -17,7 +18,7 @@
@Before
public void setUp() {
this.codeTag = new CodeTag(new SimpleIndentator(4), new SyntaxHighlighter(
- new CommandExecutor(), SyntaxHighlighter.LATEX_OUTPUT));
+ new CommandExecutor(), SyntaxHighlighter.LATEX_OUTPUT, false, new CodeCache(SyntaxHighlighter.LATEX_OUTPUT)));
}
@Test

0 comments on commit 938b05c

Please sign in to comment.