Search in sources :

Example 46 with HtmlDocumentBuilder

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

the class MarkupParser method parseToHtml.

/**
 * parse the given markup content and produce the result as an HTML document.
 *
 * @param markupContent
 *            the content to parse
 * @return the HTML document text.
 */
public String parseToHtml(String markupContent) {
    if (builder != null) {
        // $NON-NLS-1$
        throw new IllegalStateException("Builder must not be set");
    }
    StringWriter out = new StringWriter();
    setBuilder(new HtmlDocumentBuilder(out));
    parse(markupContent);
    setBuilder(null);
    return out.toString();
}
Also used : StringWriter(java.io.StringWriter) HtmlDocumentBuilder(org.eclipse.mylyn.wikitext.parser.builder.HtmlDocumentBuilder)

Example 47 with HtmlDocumentBuilder

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

the class MarkupEditor method updatePreview.

/**
 * updates the preview and optionally reveal the section that corresponds to the given outline item.
 *
 * @param outlineItem
 *            the outline item, or null
 */
private void updatePreview(final OutlineItem outlineItem) {
    if (previewDirty && browser != null) {
        Object result = null;
        try {
            result = browser.evaluate(JAVASCRIPT_GETSCROLLTOP);
        } catch (SWTException e) {
            // bug 517281 javascript fails for some Linux configurations
            logPreviewScrollingFailure(e);
        }
        final int verticalScrollbarPos = result != null ? ((Number) result).intValue() : 0;
        String xhtml = null;
        if (document == null) {
            // $NON-NLS-1$
            xhtml = "<?xml version=\"1.0\" ?><html xmlns=\"http://www.w3.org/1999/xhtml\"><body></body></html>";
        } else {
            try {
                IFile file = getFile();
                // $NON-NLS-1$
                String title = file == null ? "" : file.getName();
                if (title.lastIndexOf('.') != -1) {
                    title = title.substring(0, title.lastIndexOf('.'));
                }
                StringWriter writer = new StringWriter();
                HtmlDocumentBuilder builder = new HtmlDocumentBuilder(writer) {

                    @Override
                    protected void emitAnchorHref(String href) {
                        if (href != null && href.startsWith("#")) {
                            // $NON-NLS-1$
                            // $NON-NLS-1$
                            writer.writeAttribute(// $NON-NLS-1$
                            "onclick", // $NON-NLS-1$
                            String.format("javascript: window.location.hash = '%s'; return false;", href));
                            // $NON-NLS-1$//$NON-NLS-2$
                            writer.writeAttribute("href", "#");
                        } else {
                            super.emitAnchorHref(href);
                        }
                    }

                    @Override
                    public void beginHeading(int level, Attributes attributes) {
                        attributes.appendCssClass(CSS_CLASS_EDITOR_PREVIEW);
                        super.beginHeading(level, attributes);
                    }

                    @Override
                    public void beginBlock(BlockType type, Attributes attributes) {
                        attributes.appendCssClass(CSS_CLASS_EDITOR_PREVIEW);
                        super.beginBlock(type, attributes);
                    }
                };
                builder.setTitle(title);
                IPath location = file == null ? null : file.getLocation();
                if (location != null) {
                    builder.setBaseInHead(true);
                    builder.setBase(location.removeLastSegments(1).toFile().toURI());
                }
                String css = WikiTextUiPlugin.getDefault().getPreferences().getMarkupViewerCss();
                if (css != null && css.length() > 0) {
                    builder.addCssStylesheet(new HtmlDocumentBuilder.Stylesheet(new StringReader(css)));
                }
                MarkupLanguage markupLanguage = getMarkupLanguage();
                if (markupLanguage != null) {
                    markupLanguage = markupLanguage.clone();
                    if (markupLanguage instanceof AbstractMarkupLanguage) {
                        ((AbstractMarkupLanguage) markupLanguage).setEnableMacros(true);
                    }
                    if (markupLanguage instanceof AbstractMarkupLanguage) {
                        AbstractMarkupLanguage language = (AbstractMarkupLanguage) markupLanguage;
                        language.setFilterGenerativeContents(false);
                        language.setBlocksOnly(false);
                    }
                    MarkupParser markupParser = new MarkupParser();
                    markupParser.setBuilder(builder);
                    markupParser.setMarkupLanguage(markupLanguage);
                    markupParser.parse(document.get());
                } else {
                    builder.beginDocument();
                    builder.beginBlock(BlockType.PREFORMATTED, new Attributes());
                    builder.characters(document.get());
                    builder.endBlock();
                    builder.endDocument();
                }
                xhtml = writer.toString();
            } catch (Exception e) {
                StringWriter stackTrace = new StringWriter();
                PrintWriter writer = new PrintWriter(stackTrace);
                e.printStackTrace(writer);
                writer.close();
                StringWriter documentWriter = new StringWriter();
                HtmlDocumentBuilder builder = new HtmlDocumentBuilder(documentWriter);
                builder.beginDocument();
                builder.beginBlock(BlockType.PREFORMATTED, new Attributes());
                builder.characters(stackTrace.toString());
                builder.endBlock();
                builder.endDocument();
                xhtml = documentWriter.toString();
            }
        }
        browser.addProgressListener(new ProgressAdapter() {

            @Override
            public void completed(ProgressEvent event) {
                browser.removeProgressListener(this);
                if (outlineItem != null) {
                    revealInBrowser(outlineItem);
                } else {
                    // $NON-NLS-1$
                    browser.execute(String.format("window.scrollTo(0,%d);", verticalScrollbarPos));
                }
            }
        });
        browser.setText(xhtml);
        previewDirty = false;
    } else if (outlineItem != null && browser != null) {
        revealInBrowser(outlineItem);
    }
}
Also used : IFile(org.eclipse.core.resources.IFile) IPath(org.eclipse.core.runtime.IPath) Attributes(org.eclipse.mylyn.wikitext.parser.Attributes) AbstractMarkupLanguage(org.eclipse.mylyn.wikitext.parser.markup.AbstractMarkupLanguage) ProgressEvent(org.eclipse.swt.browser.ProgressEvent) HtmlDocumentBuilder(org.eclipse.mylyn.wikitext.parser.builder.HtmlDocumentBuilder) Point(org.eclipse.swt.graphics.Point) CoreException(org.eclipse.core.runtime.CoreException) PartInitException(org.eclipse.ui.PartInitException) SWTException(org.eclipse.swt.SWTException) SWTException(org.eclipse.swt.SWTException) StringWriter(java.io.StringWriter) BlockType(org.eclipse.mylyn.wikitext.parser.DocumentBuilder.BlockType) StringReader(java.io.StringReader) AbstractMarkupLanguage(org.eclipse.mylyn.wikitext.parser.markup.AbstractMarkupLanguage) MarkupLanguage(org.eclipse.mylyn.wikitext.parser.markup.MarkupLanguage) ProgressAdapter(org.eclipse.swt.browser.ProgressAdapter) MarkupParser(org.eclipse.mylyn.wikitext.parser.MarkupParser) PrintWriter(java.io.PrintWriter)

Example 48 with HtmlDocumentBuilder

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

the class TextileLanguageTasksTest method testSubversiveBugReport.

public void testSubversiveBugReport() throws IOException {
    StringWriter out = new StringWriter();
    parser.setBuilder(new HtmlDocumentBuilder(out));
    String content = Resources.toString(TextileLanguageTasksTest.class.getResource("resources/subversive-bug-report.txt"), StandardCharsets.UTF_8);
    parser.parse(content);
}
Also used : StringWriter(java.io.StringWriter) HtmlDocumentBuilder(org.eclipse.mylyn.wikitext.parser.builder.HtmlDocumentBuilder)

Example 49 with HtmlDocumentBuilder

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

the class MarkupViewer method computeHtml.

private String computeHtml(String markupContent) {
    StringWriter out = new StringWriter();
    HtmlDocumentBuilder builder = new HtmlDocumentBuilder(out);
    builder.setFilterEntityReferences(true);
    parser.setBuilder(builder);
    parser.parse(markupContent);
    parser.setBuilder(null);
    String htmlText = out.toString();
    return htmlText;
}
Also used : StringWriter(java.io.StringWriter) HtmlDocumentBuilder(org.eclipse.mylyn.wikitext.parser.builder.HtmlDocumentBuilder)

Example 50 with HtmlDocumentBuilder

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

the class HtmlDocumentBuilderIntegrationTest method testCopyrightNoticeFormatted.

public void testCopyrightNoticeFormatted() {
    out = new StringWriter();
    builder = new HtmlDocumentBuilder(out, true);
    parser.setBuilder(builder);
    builder.setCopyrightNotice("Copyright notice here");
    parser.parse("content");
    String html = out.toString();
    String expected = "<!-- Copyright notice here -->";
    assertTrue("Expected " + expected + " but received " + html, html.startsWith("<?xml version='1.0' encoding='utf-8' ?>\n" + "<!-- Copyright notice here -->\n" + "<html"));
}
Also used : StringWriter(java.io.StringWriter) HtmlDocumentBuilder(org.eclipse.mylyn.wikitext.parser.builder.HtmlDocumentBuilder)

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