DokuWiki

It's better when it's simple

User Tools

Site Tools


tips:csv2dokuwiki

CSV2Dokuwiki

I wrote a small Java-Programm, to convert Spreadsheet-Data into DokuWiki's table syntax. Feel free to leave a comment on my discussion-page.

Feel free to use and distribute - but of course I no warranty - if your PC breaks don't sue me.

Code

TableConvert.java
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);
	}
 
}

Tip

To paste the copied table from your spreadsheet program into the Windows Prompt-Window (cmd.exe) and to mark and copy it from there use the system menu (ALT-SPACE → EDIT).

Download Binaries

All binaries were compiled with Java 1.6. You should have Java 1.6 JRE (runtime environment) or higher installed on your system.

tips/csv2dokuwiki.txt · Last modified: 2010-02-24 20:03 by 88.128.88.13

Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Share Alike 4.0 International
CC Attribution-Share Alike 4.0 International Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki