src/org/sonews/storage/DrupalMessage.java
changeset 116 4ddc1020a154
parent 109 03cc47e9daee
child 118 ba7ea56fd672
     1.1 --- a/src/org/sonews/storage/DrupalMessage.java	Sat Nov 05 00:06:09 2011 +0100
     1.2 +++ b/src/org/sonews/storage/DrupalMessage.java	Sun Nov 06 00:08:05 2011 +0100
     1.3 @@ -71,6 +71,11 @@
     1.4   */
     1.5  public class DrupalMessage extends MimeMessage {
     1.6  
     1.7 +	/**
     1.8 +	 * If body of message posted by user through NNTP starts with this text,
     1.9 +	 * it will be treated as formated text in Markdown syntax.
    1.10 +	 */
    1.11 +	private static final String MARKDOWN_HEADER = "#!markdown\r\n";
    1.12  	private static final Logger log = Logger.getLogger(DrupalMessage.class.getName());
    1.13  	private static final String MESSAGE_ID_HEADER = "Message-ID";
    1.14  	private static final String CRLF = "\r\n";
    1.15 @@ -266,6 +271,33 @@
    1.16  	}
    1.17  
    1.18  	/**
    1.19 +	 * Converts markdown to XHTML.
    1.20 +	 * @param markdown text in Markdown syntax
    1.21 +	 * @return XHTML document (with html/body elements)
    1.22 +	 * @throws StorageBackendException when markdown proces returned any errors 
    1.23 +	 * (other exceptions are thrown when afterwards XHTML validation fails).
    1.24 +	 */
    1.25 +	private String readXhtmlTextMarkdown(String markdown) throws TransformerException, IOException, ParserConfigurationException, SAXException, StorageBackendException {
    1.26 +		Runtime r = Runtime.getRuntime();
    1.27 +		Process p = r.exec(new String[]{"sudo", "-u", "markdown", "/usr/bin/markdown"});
    1.28 +
    1.29 +		PrintStream processInput = new PrintStream(p.getOutputStream());
    1.30 +		processInput.print(markdown);
    1.31 +		processInput.close();
    1.32 +
    1.33 +		String errors = streamToString(p.getErrorStream());
    1.34 +		String htmlFragment = streamToString(p.getInputStream());
    1.35 +
    1.36 +		if (errors.length() == 0) {
    1.37 +			String htmlDocument = makeSimpleXHTML(htmlFragment);
    1.38 +			String xhtmlDocument = readXhtmlText(htmlDocument, null, -1, null, null, null);
    1.39 +			return xhtmlDocument;
    1.40 +		} else {
    1.41 +			throw new StorageBackendException("Error while transforming Markdown to XHTML: " + errors);
    1.42 +		}
    1.43 +	}
    1.44 +
    1.45 +	/**
    1.46  	 * Does not parse XML works just with text.
    1.47  	 * @param body XHTML fragment that should be put between <body> and </body>
    1.48  	 * @return simple XHTML document (body wrapped in html and body tags)
    1.49 @@ -503,13 +535,21 @@
    1.50  		try {
    1.51  			Object c = getContent();
    1.52  			if (isMimeType("text/plain") && c instanceof String) {
    1.53 -				String xhtml = readXhtmlText(
    1.54 -						(String) c,
    1.55 -						getSubject(),
    1.56 -						getParentID(),
    1.57 -						null,
    1.58 -						null,
    1.59 -						null);
    1.60 +				String inputText = (String) c;
    1.61 +				String xhtml;
    1.62 +
    1.63 +				if (inputText.startsWith(MARKDOWN_HEADER)) {
    1.64 +					xhtml = readXhtmlTextMarkdown(inputText.substring(MARKDOWN_HEADER.length()));
    1.65 +				} else {
    1.66 +
    1.67 +					xhtml = readXhtmlText(
    1.68 +							inputText,
    1.69 +							getSubject(),
    1.70 +							getParentID(),
    1.71 +							null,
    1.72 +							null,
    1.73 +							null);
    1.74 +				}
    1.75  				return makeFragmentXHTML(xhtml);
    1.76  			} else {
    1.77  				throw new StorageBackendException("Only text/plain messages are supported for now – post it as plain text please.");