protected void toTeX(JavaSourceRun run, BufferedWriter writer) throws IOException { writer.write(texFormats[run.getType().getID()]); // Replace white space by non-breaking space and line breaks by \\ // Also enclose special characters in \\verb# # String text = run.getCode(); for (int i = 0; i < text.length(); ++i) { char ch = text.charAt(i); if (ch == ' ') writer.write('~'); else if (ch == '_' || ch == '\\' || ch == '^' || ch == '~' || ch == '\"' || ch == '|' || ch == '<' || ch == '>' || ch == '*') writer.write("\\verb#" + ch + "#"); else if (ch == '{' || ch == '}' || ch == '_' || ch == '&' || ch == '%' || ch == '$' || ch == '#') { writer.write("\\" + ch); } else { writer.write(ch); } } }
/** * Converts the parsed source code to HTML by adding color information, adding line breaks and * replacing characters as needed for HTML. Also adds a table with line numbers etc. */ public void convert(JavaSource source, JavaSourceConversionOptions options, BufferedWriter writer) throws IOException { if (source == null) { throw new IllegalStateException( "Trying to write out converted code without having source set."); } writer.write(BLOCK_HEADER); // 1) Header with filename if available if (options.isShowFileName() && source.getFileName() != null) { // TODO: Pretty print file name } writer.write("{"); writer.newLine(); writer.write("\\noindent \\ttfamily"); writer.newLine(); int lineCount = source.getLineCount(); int lineNumber = 1; JavaSourceIterator iterator = source.getIterator(); while (iterator.hasNext()) { JavaSourceRun run = iterator.getNext(); if (run.isAtStartOfLine() && options.isShowLineNumbers()) { writeLineNumber(writer, lineNumber++, lineCount); } toTeX(run, writer); if (run.isAtEndOfLine()) { writer.write("\\\\"); writer.newLine(); } } writer.newLine(); writer.write("}"); writer.newLine(); }