Search in sources :

Example 1 with XmlStreamWriter

use of org.eclipse.mylyn.wikitext.util.XmlStreamWriter in project mylyn.docs by eclipse.

the class MarkupToOPS method parse.

/**
 * Parses the markup file and populates the publication with the result.
 *
 * @param ops
 *            the publication the content will be added to
 * @param markupFile
 *            the WikiText markup file
 * @return the temporary folder used for generating the HTML from markup
 * @since 2.0
 */
public File parse(Publication ops, File markupFile) throws IOException, FileNotFoundException {
    if (markupLanguage == null) {
        // $NON-NLS-1$
        throw new IllegalStateException("must set markupLanguage");
    }
    // Create a temporary working folder
    // $NON-NLS-1$
    File workingFolder = File.createTempFile("wikitext_", null);
    if (workingFolder.delete() && workingFolder.mkdirs()) {
        // $NON-NLS-1$
        File htmlFile = new File(workingFolder.getAbsolutePath() + File.separator + "markup.html");
        FileWriter out = new FileWriter(htmlFile);
        HtmlDocumentBuilder builder = new HtmlDocumentBuilder(out) {

            @Override
            protected XmlStreamWriter createXmlStreamWriter(Writer out) {
                return super.createFormattingXmlStreamWriter(out);
            }
        };
        List<Item> stylesheets = ops.getItemsByMIMEType(Publication.MIMETYPE_CSS);
        for (Item item : stylesheets) {
            File file = new File(item.getFile());
            Stylesheet css = new Stylesheet(file);
            builder.addCssStylesheet(css);
        }
        // Make sure we get the correct XHTML header
        builder.setEmitDtd(true);
        builder.setHtmlDtd(// $NON-NLS-1$
        "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">");
        builder.setXhtmlStrict(true);
        MarkupParser markupParser = new MarkupParser();
        markupParser.setBuilder(builder);
        markupParser.setMarkupLanguage(markupLanguage);
        markupParser.parse(new FileReader(markupFile));
        ops.setGenerateToc(true);
        ops.setIncludeReferencedResources(true);
        Item item = ops.addItem(htmlFile);
        item.setSourcePath(markupFile.getAbsolutePath());
    }
    return workingFolder;
}
Also used : Item(org.eclipse.mylyn.docs.epub.opf.Item) FileWriter(java.io.FileWriter) FileReader(java.io.FileReader) File(java.io.File) HtmlDocumentBuilder(org.eclipse.mylyn.wikitext.parser.builder.HtmlDocumentBuilder) XmlStreamWriter(org.eclipse.mylyn.wikitext.util.XmlStreamWriter) FileWriter(java.io.FileWriter) Writer(java.io.Writer) Stylesheet(org.eclipse.mylyn.wikitext.parser.builder.HtmlDocumentBuilder.Stylesheet) MarkupParser(org.eclipse.mylyn.wikitext.parser.MarkupParser)

Example 2 with XmlStreamWriter

use of org.eclipse.mylyn.wikitext.util.XmlStreamWriter in project mylyn.docs by eclipse.

the class MarkupToDocbook method parse.

public String parse(String markupContent) throws Exception {
    if (markupLanguage == null) {
        // $NON-NLS-1$
        throw new IllegalStateException("must set markupLanguage");
    }
    StringWriter out = new StringWriter();
    DocBookDocumentBuilder builder = new DocBookDocumentBuilder(out) {

        @Override
        protected XmlStreamWriter createXmlStreamWriter(Writer out) {
            return super.createFormattingXmlStreamWriter(out);
        }
    };
    builder.setBookTitle(bookTitle);
    MarkupParser markupParser = new MarkupParser();
    markupParser.setBuilder(builder);
    markupParser.setMarkupLanguage(markupLanguage);
    markupParser.parse(markupContent);
    return out.toString();
}
Also used : StringWriter(java.io.StringWriter) StringWriter(java.io.StringWriter) XmlStreamWriter(org.eclipse.mylyn.wikitext.util.XmlStreamWriter) Writer(java.io.Writer) DocBookDocumentBuilder(org.eclipse.mylyn.wikitext.parser.builder.DocBookDocumentBuilder) MarkupParser(org.eclipse.mylyn.wikitext.parser.MarkupParser)

Example 3 with XmlStreamWriter

use of org.eclipse.mylyn.wikitext.util.XmlStreamWriter in project mylyn.docs by eclipse.

the class MarkupToDocbookTask method processFile.

private void processFile(MarkupLanguage markupLanguage, final File baseDir, final File source) throws BuildException {
    // $NON-NLS-1$
    log(MessageFormat.format(Messages.getString("MarkupToDocbookTask.8"), source), Project.MSG_VERBOSE);
    String markupContent = null;
    String name = source.getName();
    if (name.lastIndexOf('.') != -1) {
        name = name.substring(0, name.lastIndexOf('.'));
    }
    // $NON-NLS-1$
    File docbookOutputFile = new File(source.getParentFile(), docbookFilenameFormat.replace("$1", name));
    if (!docbookOutputFile.exists() || overwrite || docbookOutputFile.lastModified() < source.lastModified()) {
        if (markupContent == null) {
            markupContent = readFully(source);
        }
        performValidation(source, markupContent);
        Writer writer;
        try {
            writer = new OutputStreamWriter(new BufferedOutputStream(new FileOutputStream(docbookOutputFile)), // $NON-NLS-1$
            "utf-8");
        } catch (Exception e) {
            throw new BuildException(MessageFormat.format(// $NON-NLS-1$
            Messages.getString("MarkupToDocbookTask.11"), // $NON-NLS-1$
            docbookOutputFile, e.getMessage()), e);
        }
        try {
            DocBookDocumentBuilder builder = new DocBookDocumentBuilder(writer) {

                @Override
                protected XmlStreamWriter createXmlStreamWriter(Writer out) {
                    return super.createFormattingXmlStreamWriter(out);
                }
            };
            MarkupParser parser = new MarkupParser();
            parser.setMarkupLanguage(markupLanguage);
            parser.setBuilder(builder);
            builder.setBookTitle(bookTitle == null ? name : bookTitle);
            if (doctype != null) {
                builder.setDoctype(doctype);
            }
            parser.parse(markupContent);
        } finally {
            try {
                writer.close();
            } catch (Exception e) {
                throw new BuildException(MessageFormat.format(// $NON-NLS-1$
                Messages.getString("MarkupToDocbookTask.12"), // $NON-NLS-1$
                docbookOutputFile, e.getMessage()), e);
            }
        }
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) BuildException(org.apache.tools.ant.BuildException) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) XmlStreamWriter(org.eclipse.mylyn.wikitext.util.XmlStreamWriter) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) BuildException(org.apache.tools.ant.BuildException) DocBookDocumentBuilder(org.eclipse.mylyn.wikitext.parser.builder.DocBookDocumentBuilder) MarkupParser(org.eclipse.mylyn.wikitext.parser.MarkupParser)

Example 4 with XmlStreamWriter

use of org.eclipse.mylyn.wikitext.util.XmlStreamWriter in project mylyn.docs by eclipse.

the class HtmlSubsetLanguageTest method cloneSupported.

@Test
public void cloneSupported() {
    HtmlDocumentHandler documentHandler = new HtmlDocumentHandler() {

        @Override
        public void endDocument(HtmlDocumentBuilder builder, XmlStreamWriter writer) {
        // ignore
        }

        @Override
        public void beginDocument(HtmlDocumentBuilder builder, XmlStreamWriter writer) {
        // ignore
        }
    };
    HtmlSubsetLanguage language = new HtmlSubsetLanguage("Test", documentHandler, 6, Sets.newHashSet(BlockType.PARAGRAPH, BlockType.DIV, BlockType.QUOTE), Sets.newHashSet(SpanType.CITATION, SpanType.EMPHASIS), ImmutableMap.of(SpanType.EMPHASIS, "new-em"), Collections.<SpanHtmlElementStrategy>emptyList(), false, true);
    HtmlSubsetLanguage cloned = language.clone();
    assertEquals(language.getName(), cloned.getName());
    assertEquals(language.getSupportedBlockTypes(), cloned.getSupportedBlockTypes());
    assertEquals(language.getSupportedHeadingLevel(), cloned.getSupportedHeadingLevel());
    assertEquals(language.getSupportedSpanTypes(), cloned.getSupportedSpanTypes());
    assertEquals(language.getTagNameSubstitutions(), cloned.getTagNameSubstitutions());
}
Also used : HtmlDocumentHandler(org.eclipse.mylyn.wikitext.parser.builder.HtmlDocumentHandler) XmlStreamWriter(org.eclipse.mylyn.wikitext.util.XmlStreamWriter) HtmlDocumentBuilder(org.eclipse.mylyn.wikitext.parser.builder.HtmlDocumentBuilder) Test(org.junit.Test)

Example 5 with XmlStreamWriter

use of org.eclipse.mylyn.wikitext.util.XmlStreamWriter in project mylyn.docs by eclipse.

the class CommonMarkAsserts method createDocumentBuilder.

private static DocumentBuilder createDocumentBuilder(StringWriter out) {
    HtmlDocumentBuilder builder = new SimplifiedHtmlDocumentBuilder(out);
    builder.setDocumentHandler(new HtmlDocumentHandler() {

        @Override
        public void endDocument(HtmlDocumentBuilder builder, XmlStreamWriter writer) {
        }

        @Override
        public void beginDocument(HtmlDocumentBuilder builder, XmlStreamWriter writer) {
        }
    });
    return builder;
}
Also used : SimplifiedHtmlDocumentBuilder(org.eclipse.mylyn.wikitext.commonmark.internal.spec.SimplifiedHtmlDocumentBuilder) HtmlDocumentHandler(org.eclipse.mylyn.wikitext.parser.builder.HtmlDocumentHandler) XmlStreamWriter(org.eclipse.mylyn.wikitext.util.XmlStreamWriter) HtmlDocumentBuilder(org.eclipse.mylyn.wikitext.parser.builder.HtmlDocumentBuilder) SimplifiedHtmlDocumentBuilder(org.eclipse.mylyn.wikitext.commonmark.internal.spec.SimplifiedHtmlDocumentBuilder)

Aggregations

XmlStreamWriter (org.eclipse.mylyn.wikitext.util.XmlStreamWriter)7 Writer (java.io.Writer)3 MarkupParser (org.eclipse.mylyn.wikitext.parser.MarkupParser)3 HtmlDocumentBuilder (org.eclipse.mylyn.wikitext.parser.builder.HtmlDocumentBuilder)3 File (java.io.File)2 StringWriter (java.io.StringWriter)2 DocBookDocumentBuilder (org.eclipse.mylyn.wikitext.parser.builder.DocBookDocumentBuilder)2 HtmlDocumentHandler (org.eclipse.mylyn.wikitext.parser.builder.HtmlDocumentHandler)2 BufferedOutputStream (java.io.BufferedOutputStream)1 FileOutputStream (java.io.FileOutputStream)1 FileReader (java.io.FileReader)1 FileWriter (java.io.FileWriter)1 OutputStreamWriter (java.io.OutputStreamWriter)1 BuildException (org.apache.tools.ant.BuildException)1 Item (org.eclipse.mylyn.docs.epub.opf.Item)1 SimplifiedHtmlDocumentBuilder (org.eclipse.mylyn.wikitext.commonmark.internal.spec.SimplifiedHtmlDocumentBuilder)1 Stylesheet (org.eclipse.mylyn.wikitext.parser.builder.HtmlDocumentBuilder.Stylesheet)1 DefaultXmlStreamWriter (org.eclipse.mylyn.wikitext.util.DefaultXmlStreamWriter)1 Test (org.junit.Test)1