Search in sources :

Example 1 with HtmlDocumentBuilder

use of org.eclipse.mylyn.wikitext.parser.builder.HtmlDocumentBuilder 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 HtmlDocumentBuilder

use of org.eclipse.mylyn.wikitext.parser.builder.HtmlDocumentBuilder in project mylyn.docs by eclipse.

the class MarkupToHtmlTask method processFile.

/**
 * process the file
 *
 * @param baseDir
 * @param source
 * @return the lightweight markup, or null if the file was not written
 * @throws BuildException
 */
protected String processFile(MarkupLanguage markupLanguage, final File baseDir, final File source) throws BuildException {
    // $NON-NLS-1$
    log(MessageFormat.format(Messages.getString("MarkupToHtmlTask.14"), source), Project.MSG_VERBOSE);
    String markupContent = null;
    String name = source.getName();
    if (name.lastIndexOf('.') != -1) {
        name = name.substring(0, name.lastIndexOf('.'));
    }
    File htmlOutputFile = computeHtmlFile(source, name);
    if (!htmlOutputFile.exists() || overwrite || htmlOutputFile.lastModified() < source.lastModified()) {
        if (markupContent == null) {
            markupContent = readFully(source);
        }
        performValidation(source, markupContent);
        Writer writer;
        try {
            // $NON-NLS-1$
            writer = new OutputStreamWriter(new BufferedOutputStream(new FileOutputStream(htmlOutputFile)), "utf-8");
        } catch (Exception e) {
            throw new BuildException(MessageFormat.format(Messages.getString("MarkupToHtmlTask.16"), htmlOutputFile, e.getMessage()), // $NON-NLS-1$
            e);
        }
        try {
            HtmlDocumentBuilder builder = new HtmlDocumentBuilder(writer, formatOutput);
            for (Stylesheet stylesheet : stylesheets) {
                HtmlDocumentBuilder.Stylesheet builderStylesheet;
                if (stylesheet.url != null) {
                    builderStylesheet = new HtmlDocumentBuilder.Stylesheet(stylesheet.url);
                } else {
                    builderStylesheet = new HtmlDocumentBuilder.Stylesheet(stylesheet.file);
                }
                builder.addCssStylesheet(builderStylesheet);
                if (!stylesheet.attributes.isEmpty()) {
                    for (Map.Entry<String, String> attr : stylesheet.attributes.entrySet()) {
                        builderStylesheet.getAttributes().put(attr.getKey(), attr.getValue());
                    }
                }
            }
            builder.setTitle(title == null ? name : title);
            builder.setEmitDtd(emitDoctype);
            if (emitDoctype && htmlDoctype != null) {
                builder.setHtmlDtd(htmlDoctype);
            }
            builder.setUseInlineStyles(useInlineCssStyles);
            builder.setSuppressBuiltInStyles(suppressBuiltInCssStyles);
            builder.setLinkRel(linkRel);
            builder.setDefaultAbsoluteLinkTarget(defaultAbsoluteLinkTarget);
            builder.setPrependImagePrefix(prependImagePrefix);
            builder.setXhtmlStrict(xhtmlStrict);
            builder.setCopyrightNotice(copyrightNotice);
            builder.setHtmlFilenameFormat(htmlFilenameFormat);
            SplittingStrategy splittingStrategy = multipleOutputFiles ? new DefaultSplittingStrategy() : new NoSplittingStrategy();
            SplittingOutlineParser outlineParser = new SplittingOutlineParser();
            outlineParser.setMarkupLanguage(markupLanguage.clone());
            outlineParser.setSplittingStrategy(splittingStrategy);
            SplitOutlineItem item = outlineParser.parse(markupContent);
            item.setSplitTarget(htmlOutputFile.getName());
            SplittingHtmlDocumentBuilder splittingBuilder = new SplittingHtmlDocumentBuilder();
            splittingBuilder.setRootBuilder(builder);
            splittingBuilder.setOutline(item);
            splittingBuilder.setRootFile(htmlOutputFile);
            splittingBuilder.setNavigationImages(navigationImages);
            splittingBuilder.setFormatting(formatOutput);
            MarkupParser parser = new MarkupParser();
            parser.setMarkupLanguage(markupLanguage);
            parser.setBuilder(splittingBuilder);
            parser.parse(markupContent);
            processed(markupContent, item, baseDir, source);
        } finally {
            try {
                writer.close();
            } catch (Exception e) {
                throw new BuildException(MessageFormat.format(// $NON-NLS-1$
                Messages.getString("MarkupToHtmlTask.17"), // $NON-NLS-1$
                htmlOutputFile, e.getMessage()), e);
            }
        }
    }
    return markupContent;
}
Also used : SplittingStrategy(org.eclipse.mylyn.wikitext.splitter.SplittingStrategy) NoSplittingStrategy(org.eclipse.mylyn.wikitext.splitter.NoSplittingStrategy) DefaultSplittingStrategy(org.eclipse.mylyn.wikitext.splitter.DefaultSplittingStrategy) SplitOutlineItem(org.eclipse.mylyn.wikitext.splitter.SplitOutlineItem) HtmlDocumentBuilder(org.eclipse.mylyn.wikitext.parser.builder.HtmlDocumentBuilder) SplittingHtmlDocumentBuilder(org.eclipse.mylyn.wikitext.splitter.SplittingHtmlDocumentBuilder) BuildException(org.apache.tools.ant.BuildException) FileOutputStream(java.io.FileOutputStream) SplittingOutlineParser(org.eclipse.mylyn.wikitext.splitter.SplittingOutlineParser) NoSplittingStrategy(org.eclipse.mylyn.wikitext.splitter.NoSplittingStrategy) OutputStreamWriter(java.io.OutputStreamWriter) BuildException(org.apache.tools.ant.BuildException) SplittingHtmlDocumentBuilder(org.eclipse.mylyn.wikitext.splitter.SplittingHtmlDocumentBuilder) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) HashMap(java.util.HashMap) Map(java.util.Map) OutputStreamWriter(java.io.OutputStreamWriter) Writer(java.io.Writer) DefaultSplittingStrategy(org.eclipse.mylyn.wikitext.splitter.DefaultSplittingStrategy) MarkupParser(org.eclipse.mylyn.wikitext.parser.MarkupParser)

Example 3 with HtmlDocumentBuilder

use of org.eclipse.mylyn.wikitext.parser.builder.HtmlDocumentBuilder in project mylyn.docs by eclipse.

the class HtmlParserTest method parseHtmlToHtml.

private String parseHtmlToHtml(String input, boolean asDocument) throws IOException, SAXException {
    HtmlParser parser = new HtmlParser();
    StringWriter out = new StringWriter();
    HtmlDocumentBuilder builder = new HtmlDocumentBuilder(out);
    parser.parse(new InputSource(new StringReader(input)), builder, asDocument);
    return out.toString();
}
Also used : HtmlParser(org.eclipse.mylyn.wikitext.parser.HtmlParser) InputSource(org.xml.sax.InputSource) StringWriter(java.io.StringWriter) StringReader(java.io.StringReader) HtmlDocumentBuilder(org.eclipse.mylyn.wikitext.parser.builder.HtmlDocumentBuilder)

Example 4 with HtmlDocumentBuilder

use of org.eclipse.mylyn.wikitext.parser.builder.HtmlDocumentBuilder in project mylyn.docs by eclipse.

the class HtmlParserTest method parsePreservesAsDocumentSetting.

@Test
public void parsePreservesAsDocumentSetting() throws IOException, SAXException {
    HtmlParser parser = new HtmlParser();
    StringWriter out = new StringWriter();
    HtmlDocumentBuilder builder = new HtmlDocumentBuilder(out);
    builder.setEmitAsDocument(false);
    parser.parse(new InputSource(new StringReader("before <em>emphasis</em> after")), builder);
    assertEquals("before <em>emphasis</em> after", out.toString());
}
Also used : HtmlParser(org.eclipse.mylyn.wikitext.parser.HtmlParser) InputSource(org.xml.sax.InputSource) StringWriter(java.io.StringWriter) StringReader(java.io.StringReader) HtmlDocumentBuilder(org.eclipse.mylyn.wikitext.parser.builder.HtmlDocumentBuilder) Test(org.junit.Test)

Example 5 with HtmlDocumentBuilder

use of org.eclipse.mylyn.wikitext.parser.builder.HtmlDocumentBuilder in project mylyn.docs by eclipse.

the class AbstractSaxParserTest method assertParseHtml.

private void assertParseHtml(String expectedResult, String html) {
    StringWriter out = new StringWriter();
    HtmlDocumentBuilder htmlBuilder = new HtmlDocumentBuilder(out);
    htmlBuilder.setEmitAsDocument(false);
    try {
        parser.parse(sourceForHtml(html), htmlBuilder, true);
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }
    assertEquals(expectedResult, out.toString());
}
Also used : StringWriter(java.io.StringWriter) HtmlDocumentBuilder(org.eclipse.mylyn.wikitext.parser.builder.HtmlDocumentBuilder) SAXException(org.xml.sax.SAXException) IOException(java.io.IOException)

Aggregations

HtmlDocumentBuilder (org.eclipse.mylyn.wikitext.parser.builder.HtmlDocumentBuilder)58 StringWriter (java.io.StringWriter)46 Test (org.junit.Test)17 MarkupParser (org.eclipse.mylyn.wikitext.parser.MarkupParser)16 SplittingHtmlDocumentBuilder (org.eclipse.mylyn.wikitext.splitter.SplittingHtmlDocumentBuilder)10 AbstractMarkupGenerationTest (org.eclipse.mylyn.wikitext.toolkit.AbstractMarkupGenerationTest)10 File (java.io.File)8 Writer (java.io.Writer)7 StringReader (java.io.StringReader)6 Attributes (org.eclipse.mylyn.wikitext.parser.Attributes)6 SplitOutlineItem (org.eclipse.mylyn.wikitext.splitter.SplitOutlineItem)6 OutputStreamWriter (java.io.OutputStreamWriter)5 FileOutputStream (java.io.FileOutputStream)4 IOException (java.io.IOException)4 TextileLanguage (org.eclipse.mylyn.wikitext.textile.TextileLanguage)4 PrintWriter (java.io.PrintWriter)3 LinkAttributes (org.eclipse.mylyn.wikitext.parser.LinkAttributes)3 DefaultSplittingStrategy (org.eclipse.mylyn.wikitext.splitter.DefaultSplittingStrategy)3 SplittingOutlineParser (org.eclipse.mylyn.wikitext.splitter.SplittingOutlineParser)3 Before (org.junit.Before)3