import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.regex.Pattern; /** * Converts CSV input (tab separated) into DokuWiki's table syntax * * Tip: mark your data in Spreadsheet-Program and copy-paste it into the console * * @author slartidan * */ public class TableConvert { private static final String PATTERN_START_QUOTATION = "(^|.*\t)\"((?:\"\"|[^\"])*)"; private static final String PATTERN_END_QUOTATION = "(^|.*\t)((?:\"\"|[^\"])*)\"(\t.*|$)"; private static final int MODE_STANDARD = 0; private static final int MODE_IN_QUOTATION = 1; public static void main(String[] args) throws IOException { System.out.println("CSV2DokuWiki - type your tab separated CSV here:"); BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); Pattern startQuotation = Pattern.compile(PATTERN_START_QUOTATION); Pattern endOuotation = Pattern.compile(PATTERN_END_QUOTATION); int mode = MODE_STANDARD; String line; StringBuffer output = new StringBuffer(); while ((line = reader.readLine()) != null) { if ("".equals(line)) break; // empty line signalizes end of input if (mode == MODE_IN_QUOTATION) { // closing Quotation? if (endOuotation.matcher(line).matches()) { mode = MODE_STANDARD; line = line.replaceAll(PATTERN_END_QUOTATION, "1ドル2ドル3ドル"); } } else { // lines (not in quotation) start with " | " line = "\t"+line; } // starting Quotation? if (startQuotation.matcher(line).matches()) { mode = MODE_IN_QUOTATION; line = line.replaceAll(PATTERN_START_QUOTATION, "1ドル2ドル"); } // remove unnessesary quotation marks String old = new String(); while (!old.equals(line)) { old = line; line = line.replaceAll("(^|\t)\"((?:\"\"|[^\"])*)\"(\t|$)", "1ドル2ドル3ドル"); } // normalize escaped quotation marks line = line.replaceAll("\"\"", "\""); // pipes instead of tabs line = line.replaceAll("\\t", " | "); // breaks in Quotations become \\ if (mode == MODE_IN_QUOTATION) { line += "\\\\ "; } else { line += " |\n"; } // save line line = line.replaceAll("^ ", ""); output.append(line); } // print output System.out.println(output); } }

AltStyle によって変換されたページ (->オリジナル) /