Search in sources :

Example 1 with Stylesheet

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

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

the class HtmlDocumentBuilder2Test method testCssStylesheetEmbedded.

public void testCssStylesheetEmbedded() throws Exception {
    URL cssResource = HtmlDocumentBuilder2Test.class.getResource("resources/test.css");
    File cssFile = new File(cssResource.toURI().getPath());
    fileToUrl.put(cssFile, cssResource);
    builder.addCssStylesheet(new Stylesheet(cssFile));
    builder.beginDocument();
    builder.beginBlock(BlockType.PARAGRAPH, new Attributes());
    builder.characters("some para text");
    builder.endBlock();
    builder.endDocument();
    String html = out.toString();
    html = html.replace("&#xd;", "");
    assertTrue(Pattern.compile("<head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/><style type=\"text/css\">\\s*body\\s+\\{\\s+background-image: test-content.png;\\s+\\}\\s*</style></head>", Pattern.MULTILINE).matcher(html).find());
}
Also used : LinkAttributes(org.eclipse.mylyn.wikitext.parser.LinkAttributes) Attributes(org.eclipse.mylyn.wikitext.parser.Attributes) File(java.io.File) URL(java.net.URL) Stylesheet(org.eclipse.mylyn.wikitext.parser.builder.HtmlDocumentBuilder.Stylesheet)

Example 3 with Stylesheet

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

the class HtmlDocumentBuilder2Test method testStylesheetWithAttributes.

public void testStylesheetWithAttributes() {
    Stylesheet stylesheet = new Stylesheet("a/test.css");
    stylesheet.getAttributes().put("foo", "bar");
    builder.addCssStylesheet(stylesheet);
    builder.beginDocument();
    builder.endDocument();
    String html = out.toString();
    assertTrue(html.contains("<link type=\"text/css\" rel=\"stylesheet\" href=\"a/test.css\" foo=\"bar\"/>"));
}
Also used : Stylesheet(org.eclipse.mylyn.wikitext.parser.builder.HtmlDocumentBuilder.Stylesheet)

Example 4 with Stylesheet

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

the class HtmlDocumentBuilder2Test method testCssStylesheetAsLink.

public void testCssStylesheetAsLink() {
    builder.addCssStylesheet(new Stylesheet("styles/test.css"));
    builder.beginDocument();
    builder.beginBlock(BlockType.PARAGRAPH, new Attributes());
    builder.characters("some para text");
    builder.endBlock();
    builder.endDocument();
    String html = out.toString();
    assertTrue(html.contains("<head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/><link type=\"text/css\" rel=\"stylesheet\" href=\"styles/test.css\"/></head>"));
}
Also used : LinkAttributes(org.eclipse.mylyn.wikitext.parser.LinkAttributes) Attributes(org.eclipse.mylyn.wikitext.parser.Attributes) Stylesheet(org.eclipse.mylyn.wikitext.parser.builder.HtmlDocumentBuilder.Stylesheet)

Example 5 with Stylesheet

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

the class HtmlDocumentBuilder2Test method testStylesheetWithNoAttributes.

public void testStylesheetWithNoAttributes() {
    builder.addCssStylesheet(new Stylesheet("a/test.css"));
    builder.beginDocument();
    builder.endDocument();
    String html = out.toString();
    assertTrue(html.contains("<link type=\"text/css\" rel=\"stylesheet\" href=\"a/test.css\"/>"));
}
Also used : Stylesheet(org.eclipse.mylyn.wikitext.parser.builder.HtmlDocumentBuilder.Stylesheet)

Aggregations

Stylesheet (org.eclipse.mylyn.wikitext.parser.builder.HtmlDocumentBuilder.Stylesheet)6 File (java.io.File)2 Attributes (org.eclipse.mylyn.wikitext.parser.Attributes)2 LinkAttributes (org.eclipse.mylyn.wikitext.parser.LinkAttributes)2 HtmlDocumentBuilder (org.eclipse.mylyn.wikitext.parser.builder.HtmlDocumentBuilder)2 FileReader (java.io.FileReader)1 FileWriter (java.io.FileWriter)1 Writer (java.io.Writer)1 URL (java.net.URL)1 Item (org.eclipse.mylyn.docs.epub.opf.Item)1 MarkupParser (org.eclipse.mylyn.wikitext.parser.MarkupParser)1 SplittingHtmlDocumentBuilder (org.eclipse.mylyn.wikitext.splitter.SplittingHtmlDocumentBuilder)1 XmlStreamWriter (org.eclipse.mylyn.wikitext.util.XmlStreamWriter)1 Test (org.junit.Test)1