3 * Copyright © 2014 František Kučera (frantovo.cz)
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package info.globalcode.sql.dk.formatting;
20 import info.globalcode.sql.dk.ColorfulPrintWriter;
21 import info.globalcode.sql.dk.Constants;
22 import info.globalcode.sql.dk.configuration.PropertyDeclaration;
23 import static info.globalcode.sql.dk.formatting.CommonProperties.COLORFUL;
24 import static info.globalcode.sql.dk.formatting.CommonProperties.COLORFUL_DESCRIPTION;
25 import java.util.Collections;
26 import java.util.HashMap;
27 import java.util.List;
31 * Outputs result sets in (La)TeX format.
33 * @author Ing. František Kučera (frantovo.cz)
35 @PropertyDeclaration(name = COLORFUL, defaultValue = "false", type = Boolean.class, description = COLORFUL_DESCRIPTION)
36 public class TeXFormatter extends AbstractFormatter {
38 public static final String NAME = "tex"; // bash-completion:formatter
39 private static final ColorfulPrintWriter.TerminalColor COMMAND_COLOR = ColorfulPrintWriter.TerminalColor.Magenta;
40 private static final ColorfulPrintWriter.TerminalColor OPTIONS_COLOR = ColorfulPrintWriter.TerminalColor.Yellow;
41 private static final Map<Character, String> TEX_ESCAPE_MAP;
42 private final ColorfulPrintWriter out;
45 Map<Character, String> replacements = new HashMap<>();
47 replacements.put('\\', "\\textbackslash{}");
48 replacements.put('{', "\\{{}");
49 replacements.put('}', "\\}{}");
50 replacements.put('_', "\\_{}");
51 replacements.put('^', "\\textasciicircum{}");
52 replacements.put('#', "\\#{}");
53 replacements.put('&', "\\&{}");
54 replacements.put('$', "\\${}");
55 replacements.put('%', "\\%{}");
56 replacements.put('~', "\\textasciitilde{}");
57 replacements.put('-', "{-}");
59 TEX_ESCAPE_MAP = Collections.unmodifiableMap(replacements);
62 public TeXFormatter(FormatterContext formatterContext) {
63 super(formatterContext);
64 boolean colorful = formatterContext.getProperties().getBoolean(COLORFUL, false);
65 out = new ColorfulPrintWriter(formatterContext.getOutputStream(), false, colorful);
69 public void writeStartBatch() {
70 super.writeStartBatch();
72 printCommand("documentclass", "a4paper,twoside", "article", true);
73 printCommand("usepackage", "T1", "fontenc", true);
74 printCommand("usepackage", "utf8x", "inputenc", true);
75 printCommand("usepackage", "pdfauthor={" + Constants.WEBSITE + "}, bookmarks=true,unicode,colorlinks=true,linkcolor=black,urlcolor=blue,citecolor=blue", "hyperref", true);
76 printBegin("document");
80 public void writeEndBatch() {
81 super.writeEndBatch();
86 public void writeColumnValue(Object value) {
87 super.writeColumnValue(value);
88 // TODO: arrays, numbers, booleans, nulls etc.:
89 out.print(escapeTex(toString(value)));
91 if (!isCurrentColumnLast()) {
92 printColumnSeparator();
97 public void writeEndRow() {
103 public void writeStartResultSet(ColumnsHeader header) {
104 super.writeStartResultSet(header);
105 printCommand("begin", null, "tabular", false);
107 List<ColumnDescriptor> columnDescriptors = header.getColumnDescriptors();
109 StringBuilder columnAlignments = new StringBuilder();
110 for (ColumnDescriptor cd : columnDescriptors) {
111 if (cd.isNumeric() || cd.isBoolean()) {
112 columnAlignments.append('r');
114 columnAlignments.append('l');
118 printCommand(null, null, columnAlignments.toString(), true);
119 printCommand("hline", null, null, true);
121 for (ColumnDescriptor cd : columnDescriptors) {
122 printCommand("textbf", null, cd.getLabel(), false);
123 if (cd.isLastColumn()) {
126 printColumnSeparator();
130 printCommand("hline", null, null, true);
134 public void writeEndResultSet() {
135 super.writeEndResultSet();
136 printCommand("hline", null, null, true);
140 private String escapeTex(String text) {
144 StringBuilder result = new StringBuilder(text.length() * 2);
146 for (char ch : text.toCharArray()) {
147 String replacement = TEX_ESCAPE_MAP.get(ch);
148 result.append(replacement == null ? ch : replacement);
151 return result.toString();
155 protected String toString(Object value) {
156 return String.valueOf(value);
159 private void printColumnSeparator() {
160 out.print(COMMAND_COLOR, " & ");
163 private void printEndRow() {
164 out.println(COMMAND_COLOR, " \\\\");
170 * @param command will not be escaped – should contain just a valid TeX command name
171 * @param options will not be escaped – should be properly formatted to be printed inside [
173 * @param value will be escaped
174 * @param println whether to print line end and flush
176 private void printCommand(String command, String options, String value, boolean println) {
178 if (command != null) {
179 out.print(COMMAND_COLOR, "\\" + command);
182 if (options != null) {
183 out.print(COMMAND_COLOR, "[");
184 out.print(OPTIONS_COLOR, options);
185 out.print(COMMAND_COLOR, "]");
189 out.print(COMMAND_COLOR, "{");
190 out.print(escapeTex(value));
191 out.print(COMMAND_COLOR, "}");
200 private void printBegin(String environment) {
201 printCommand("begin", null, environment, true);
204 private void printEnd(String environment) {
205 printCommand("end", null, environment, true);