Permalink
Browse files

calling pygments from inside tubaina

  • Loading branch information...
1 parent 0840353 commit 0750434dcc295457b51cd9892c1ced02ff8acc69 @csokol csokol committed Oct 5, 2012
@@ -15,9 +15,9 @@
public static final String HTML_OUTPUT = "html";
public static final String LATEX_OUTPUT = "latex";
- public SyntaxHighlighter(CommandExecutor commandExecutor, String output) {
+ public SyntaxHighlighter(CommandExecutor commandExecutor, String outputType) {
this.commandExecutor = commandExecutor;
- this.output = output;
+ this.output = outputType;
}
public SyntaxHighlighter(CommandExecutor commandExecutor, String output, boolean allLinesNumbered) {
@@ -27,8 +27,9 @@ public SyntaxHighlighter(CommandExecutor commandExecutor, String output, boolean
public String highlight(String code, String language, boolean numbered, List<Integer> lines) {
StringBuilder options = new StringBuilder();
- if (numbered || allLinesNumbered)
- options.append(",linenos=inline");
+ if (numbered || allLinesNumbered) { // for kindle output all lines are numbered
+ appendLineNumberingOption(options);
+ }
addLineHighlightOption(lines, options);
@@ -46,6 +47,15 @@ public String highlight(String code, String language, boolean numbered, List<Int
return commandExecutor.execute(commands, code);
}
+ private void appendLineNumberingOption(StringBuilder options) {
+ if (output.equals(HTML_OUTPUT)) {
+ options.append(",linenos=inline");
+ } else {
+ options.append(",linenos=yes");
+ }
+
+ }
+
private void addLineHighlightOption(List<Integer> lines, StringBuilder options) {
if (!lines.isEmpty()) {
options.append(",hl_lines=");
@@ -5,35 +5,41 @@
import br.com.caelum.tubaina.parser.Indentator;
import br.com.caelum.tubaina.parser.Tag;
import br.com.caelum.tubaina.parser.html.CodeTagOptionsParser;
+import br.com.caelum.tubaina.parser.html.desktop.SyntaxHighlighter;
public class CodeTag implements Tag {
private final Indentator indentator;
- public static final String BEGIN = "\n\\begin{small}\n\\begin{minted}";
- public static final String END = "\n\\end{minted}\n\\end{small}";
+ public static final String BEGIN = "\n\\begin{small}\n";
+ public static final String END = "\n\\end{small}";
public static final String CODE_LABEL = "\\tubainaCodeLabel{";
private static final String FILE_NAME = "\\tubainaCodeFileName";
private CodeTagOptionsParser codeTagOptionsParser;
- public CodeTag(Indentator indentator) {
+ private final SyntaxHighlighter syntaxHighlighter;
+
+ public CodeTag(Indentator indentator, SyntaxHighlighter syntaxHighlighter) {
this.indentator = indentator;
+ this.syntaxHighlighter = syntaxHighlighter;
codeTagOptionsParser = new CodeTagOptionsParser();
}
public String parse(String string, String options) {
String mintedOptionalParameters = parseOptinals(options);
-
String chosenLanguage = codeTagOptionsParser.parseLanguage(options);
String latexFilename = latexFilenameFor(codeTagOptionsParser.parseFileName(options));
String latexReference = latexLabelFor(codeTagOptionsParser.parseLabel(options));
-
+ boolean numbered = options.contains(" #");
+
String indentedString = this.indentator.indent(string);
+
+ String highlightedCode = syntaxHighlighter.highlight(indentedString, chosenLanguage, numbered);
return latexReference + latexFilename + CodeTag.BEGIN + mintedOptionalParameters + "{"
- + chosenLanguage + "}\n" + indentedString + CodeTag.END;
+ + chosenLanguage + "}\n" + highlightedCode + CodeTag.END;
}
private String parseOptinals(String options) {
@@ -4,14 +4,16 @@
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.SyntaxHighlighter;
+import br.com.caelum.tubaina.util.CommandExecutor;
public class GistTag implements Tag {
private CodeTag code;
private GistResultRetriever retriever;
public GistTag(Indentator i, GistResultRetriever retriever) {
- this.code = new CodeTag(i);
+ this.code = new CodeTag(i, new SyntaxHighlighter(new CommandExecutor(), SyntaxHighlighter.LATEX_OUTPUT));
this.retriever = retriever;
}
@@ -9,6 +9,8 @@
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.SyntaxHighlighter;
+import br.com.caelum.tubaina.util.CommandExecutor;
public class LatexParser implements Parser {
@@ -63,7 +65,7 @@ public String parseParagraph(String text) {
}
public String parseCode(String text, String options) {
- String string = new CodeTag(new SimpleIndentator(4))
+ String string = new CodeTag(new SimpleIndentator(4), new SyntaxHighlighter(new CommandExecutor(), SyntaxHighlighter.LATEX_OUTPUT))
.parse(text, options);
return string + VSPACE;
}
@@ -1,28 +1,36 @@
package br.com.caelum.tubaina.parser.latex;
-import org.junit.Assert;
+import static org.junit.Assert.assertTrue;
+
+import org.junit.Before;
import org.junit.Test;
import br.com.caelum.tubaina.parser.SimpleIndentator;
+import br.com.caelum.tubaina.parser.html.desktop.SyntaxHighlighter;
+import br.com.caelum.tubaina.util.CommandExecutor;
public class CodeTagTest {
- @Test
+ private CodeTag codeTag;
+
+ @Before
+ public void setUp() {
+ this.codeTag = new CodeTag(new SimpleIndentator(4), new SyntaxHighlighter(new CommandExecutor(),
+ SyntaxHighlighter.LATEX_OUTPUT));
+ }
+
+ @Test
public void testPropertiesCodeTag() throws Exception {
String options = "properties";
String string = "blablah blah\n" +
"#algum comentario\n" +
"texto=valor\n" +
"texto:valor\n" +
"texto valor";
- String output = new CodeTag(new SimpleIndentator(4)).parse(string, options);
- Assert.assertEquals(CodeTag.BEGIN + "{properties}\n" +
- "blablah blah\n" +
- "#algum comentario\n" +
- "texto=valor\n" +
- "texto:valor\n" +
- "texto valor" + CodeTag.END, output);
+ String output = codeTag.parse(string, options);
+
+ assertPygmentsRan(output);
}
@Test
public void testPropertiesCodeTagWithEscapes() throws Exception {
@@ -33,123 +41,106 @@ public void testPropertiesCodeTagWithEscapes() throws Exception {
"texto\\:valor:valor\n" +
"texto\\ valor valor\n" +
"a b\\#fake comentario";
- String output = new CodeTag(new SimpleIndentator(4)).parse(string, options);
- Assert.assertEquals(CodeTag.BEGIN + "{properties}\n" +
- "blablah blah\n" +
- "#algum comentario\n" +
- "texto\\=valor=valor\n" +
- "texto\\:valor:valor\n" +
- "texto\\ valor valor\n" +
- "a b\\#fake comentario" + CodeTag.END, output);
+ String output = codeTag.parse(string, options);
- }
-
- @Test
- public void languageCodeTagIsReturnedInsideMintedEnvironment() throws Exception {
- String string = "public static void main(String[] args) {";
- String options = "java";
- String output = new CodeTag(new SimpleIndentator(4)).parse(string , options );
- Assert.assertEquals(CodeTag.BEGIN + "{java}\n" +
- string +
- CodeTag.END, output);
+ assertPygmentsRan(output);
}
@Test
public void languageCodeTagShouldInsertLineNumbersWhenOptionContainsSharp(){
String string = "public static void main(String[] args) {";
String options = "java #";
- String output = new CodeTag(new SimpleIndentator(4)).parse(string , options );
- Assert.assertEquals(CodeTag.BEGIN + "[linenos, numbersep=5pt]{java}\n" +
- string +
- CodeTag.END, output);
+ String output = codeTag.parse(string , options );
+
+ assertPygmentsRan(output);
}
@Test
public void languageCodeTagShouldUnderstandLineNumbersEvenWhenNoLanguageIsSelected(){
String string = "def some: \"bizarre code\" in: unknownLanguage";
String options = "#";
- String output = new CodeTag(new SimpleIndentator(4)).parse(string , options );
- Assert.assertEquals(CodeTag.BEGIN + "[linenos, numbersep=5pt]{text}\n" +
- string +
- CodeTag.END, output);
+ String output = codeTag.parse(string , options);
+
+ assertPygmentsRan(output);
}
@Test
public void languageCodeTagShouldUnderstandLineNumbersAndHighlightsWhenNoLanguageIsSelected(){
String string = "def some: \"bizarre code\" \nin: unknownLanguage";
String options = "# h=1,2";
- String output = new CodeTag(new SimpleIndentator(4)).parse(string , options );
- Assert.assertEquals(CodeTag.BEGIN + "[linenos, numbersep=5pt, h=1,2]{text}\n" +
- string +
- CodeTag.END, output);
+ String output = codeTag.parse(string , options );
+
+ assertPygmentsRan(output);
}
@Test
public void languageCodeTagShouldUnderstandLineNumbersAndCSharpLanguage(){
String string = "public class SomeClass {";
- String options = "C# #";
- String output = new CodeTag(new SimpleIndentator(4)).parse(string , options );
- Assert.assertEquals(CodeTag.BEGIN + "[linenos, numbersep=5pt]{C#}\n" +
- string +
- CodeTag.END, output);
+ String options = "c# #";
+ String output = codeTag.parse(string , options );
+
+ assertPygmentsRan(output);
}
@Test
public void codeTagWithReferenceWithoutLanguage() throws Exception {
String options = "label=javacode1";
String code = "class Main {\n" + "public static void main(String[] args) {\n"
+ "System.out.println(\"Hello world\");\n" + "}\n}";
- String output = new CodeTag(new SimpleIndentator(4)).parse(code, options);
- Assert.assertEquals("\\tubainaCodeLabel{javacode1}\n" + CodeTag.BEGIN
- + "{text}\n" + code + CodeTag.END,
- output);
-
+ String output = codeTag.parse(code, options);
+
+ assertTrue(output.startsWith("\\tubainaCodeLabel{javacode1}"));
+ assertPygmentsRan(output);
}
+
@Test
public void codeTagWithReferenceWithLanguage() throws Exception {
String options = "java label=javacode1";
String code = "class Main {\n" + "public static void main(String[] args) {\n"
+ "System.out.println(\"Hello world\");\n" + "}\n}";
- String output = new CodeTag(new SimpleIndentator(4)).parse(code, options);
- Assert.assertEquals("\\tubainaCodeLabel{javacode1}\n" + CodeTag.BEGIN
- + "{java}\n" + code + CodeTag.END, output);
-
+ String output = codeTag.parse(code, options);
+
+ assertTrue(output.startsWith("\\tubainaCodeLabel{javacode1}"));
+ assertPygmentsRan(output);
}
@Test
public void codeTagWithFileNameWithoutLanguage() throws Exception {
String options = "filename=src/Main.java";
String code = "class Main {\n" + "public static void main(String[] args) {\n"
+ "System.out.println(\"Hello world\");\n" + "}\n}";
- String output = new CodeTag(new SimpleIndentator(4)).parse(code, options);
- Assert.assertEquals("\\tubainaCodeFileName{src/Main.java}\n" + CodeTag.BEGIN + "{text}\n"
- + code
- + CodeTag.END, output);
-
+ String output = codeTag.parse(code, options);
+
+ assertTrue(output.startsWith("\\tubainaCodeFileName{src/Main.java}\n"));
+ assertPygmentsRan(output);
}
@Test
public void codeTagWithFileNameWithLanguage() throws Exception {
String options = "java filename=src/Main.java";
String code = "class Main {\n" + "public static void main(String[] args) {\n"
+ "System.out.println(\"Hello world\");\n" + "}\n}";
- String output = new CodeTag(new SimpleIndentator(4)).parse(code, options);
- Assert.assertEquals("\\tubainaCodeFileName{src/Main.java}\n" + CodeTag.BEGIN + "{java}\n"
- + code + CodeTag.END, output);
+ String output = codeTag.parse(code, options);
+
+ assertTrue(output.startsWith("\\tubainaCodeFileName{src/Main.java}\n"));
+ assertPygmentsRan(output);
}
@Test
public void codeTagWithFileNameWithLanguageAndLabel() throws Exception {
String options = "java filename=src/Main2.java label=javacode1";
String code = "class Main {\n" + "public static void main(String[] args) {\n"
+ "System.out.println(\"Hello world\");\n" + "}\n}";
- String output = new CodeTag(new SimpleIndentator(4)).parse(code, options);
- Assert.assertEquals("\\tubainaCodeLabel{javacode1}\n"
- + "\\tubainaCodeFileName{src/Main2.java}\n" + CodeTag.BEGIN + "{java}\n"
- + code + CodeTag.END, output);
+ String output = codeTag.parse(code, options);
+ assertTrue(output.startsWith("\\tubainaCodeLabel{javacode1}\n"));
+ assertPygmentsRan(output);
}
- //TODO: file name as an option to code
+ private void assertPygmentsRan(String output) {
+ assertTrue(output.contains("\\begin{Verbatim}[commandchars="));
+ assertTrue(output.contains("\\end{Verbatim}"));
+ }
+ //TODO: file name as an option to code
}
@@ -1,11 +1,11 @@
package br.com.caelum.tubaina.parser.latex;
+import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.Scanner;
-import org.junit.Assert;
import org.junit.Test;
import br.com.caelum.tubaina.gists.GistConnector;
@@ -23,13 +23,15 @@ public void gistedCodeIsRetrievedAndUsed() throws Exception {
new GistRequest()));
String options = "417835";
- String gistedCode = "javascript:(function() {window.frames[3][0].document.getElementById('frameplugin').style.display='none'})()";
+ @SuppressWarnings("unused") //code to be retrieved, we can only assert parts of it
+ String gistedCode = "javascript:(function() {window.frames[3][0].document.getElementById('frameplugin').style.display='none'})()";
String output = new GistTag(new SimpleIndentator(4), retriever).parse(
"", options);
-
- Assert.assertEquals(CodeTag.BEGIN + "{text}\n" + gistedCode
- + CodeTag.END, output);
+
+ assertPygmentsRan(output);
+ assertTrue(output.contains("javascript"));
+ assertTrue(output.contains("funcion"));
}
@Test
@@ -52,8 +54,14 @@ public void gistedCodeIsParsedWithLineNumbers() throws Exception {
String output = new GistTag(new SimpleIndentator(4), retriever).parse(
"", options);
- Assert.assertEquals(CodeTag.BEGIN + "[linenos, numbersep=5pt]{text}\n"
- + gistedCode + CodeTag.END, output);
+
+ assertPygmentsRan(output);
+ assertTrue(output.contains("GivenCode"));
}
+
+ private void assertPygmentsRan(String output) {
+ assertTrue(output.contains("\\begin{Verbatim}[commandchars="));
+ assertTrue(output.contains("\\end{Verbatim}"));
+ }
}
Oops, something went wrong.

0 comments on commit 0750434

Please sign in to comment.