Search in sources :

Example 6 with AbstractMarkupLanguage

use of org.eclipse.mylyn.wikitext.parser.markup.AbstractMarkupLanguage in project mylyn.docs by eclipse.

the class OutlineParser method parse.

public OutlineItem parse(OutlineItem root, String markup) {
    if (markup == null || markup.length() == 0 || markupLanguage == null) {
        root.setLength(markup == null ? 0 : markup.length());
        return root;
    }
    root.setLength(markup.length());
    MarkupLanguage markupLanguage = this.markupLanguage.clone();
    if (markupLanguage instanceof AbstractMarkupLanguage) {
        AbstractMarkupLanguage language = (AbstractMarkupLanguage) markupLanguage;
        language.setFilterGenerativeContents(true);
        language.setBlocksOnly(isBlocksOnly());
    }
    OutlineBuilder outlineBuilder = (OutlineBuilder) createOutlineUpdater(root);
    outlineBuilder.idGenerator.setGenerationStrategy(markupLanguage.getIdGenerationStrategy());
    MarkupParser markupParser = new MarkupParser();
    markupParser.setBuilder(outlineBuilder);
    markupParser.setMarkupLanguage(markupLanguage);
    markupParser.parse(markup);
    return root;
}
Also used : AbstractMarkupLanguage(org.eclipse.mylyn.wikitext.parser.markup.AbstractMarkupLanguage) MarkupLanguage(org.eclipse.mylyn.wikitext.parser.markup.MarkupLanguage) AbstractMarkupLanguage(org.eclipse.mylyn.wikitext.parser.markup.AbstractMarkupLanguage) MarkupParser(org.eclipse.mylyn.wikitext.parser.MarkupParser)

Example 7 with AbstractMarkupLanguage

use of org.eclipse.mylyn.wikitext.parser.markup.AbstractMarkupLanguage 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 8 with AbstractMarkupLanguage

use of org.eclipse.mylyn.wikitext.parser.markup.AbstractMarkupLanguage in project mylyn.docs by eclipse.

the class FastMarkupPartitioner method reparse.

public void reparse(IDocument document, Block block) {
    MarkupParser markupParser = new MarkupParser(markupLanguage);
    if (markupLanguage instanceof AbstractMarkupLanguage) {
        AbstractMarkupLanguage language = (AbstractMarkupLanguage) markupLanguage;
        language.setFilterGenerativeContents(true);
        language.setBlocksOnly(false);
    }
    PartitionBuilder partitionBuilder = new PartitionBuilder(block.getOffset(), false);
    markupParser.setBuilder(partitionBuilder);
    try {
        markupParser.parse(document.get(block.getOffset(), block.getLength()));
        for (Segment<?> s : partitionBuilder.outerBlock.getChildren().asList()) {
            if (s.getOffset() == block.getOffset()) {
                if (s instanceof Block) {
                    block.replaceChildren(s);
                    block.setSpansComputed(true);
                    break;
                }
            }
        }
    } catch (BadLocationException e) {
        throw new IllegalStateException(e);
    }
}
Also used : AbstractMarkupLanguage(org.eclipse.mylyn.wikitext.parser.markup.AbstractMarkupLanguage) BadLocationException(org.eclipse.jface.text.BadLocationException) MarkupParser(org.eclipse.mylyn.wikitext.parser.MarkupParser)

Example 9 with AbstractMarkupLanguage

use of org.eclipse.mylyn.wikitext.parser.markup.AbstractMarkupLanguage in project mylyn.docs by eclipse.

the class MarkdownLanguageTest method testCreateDocumentBuilder.

public void testCreateDocumentBuilder() {
    AbstractMarkupLanguage lang = new MarkdownLanguage();
    DocumentBuilder builder = lang.createDocumentBuilder(new StringWriter());
    assertNotNull(builder);
    assertTrue(builder instanceof MarkdownDocumentBuilder);
}
Also used : MarkdownDocumentBuilder(org.eclipse.mylyn.wikitext.markdown.internal.MarkdownDocumentBuilder) StringWriter(java.io.StringWriter) DocumentBuilder(org.eclipse.mylyn.wikitext.parser.DocumentBuilder) MarkdownDocumentBuilder(org.eclipse.mylyn.wikitext.markdown.internal.MarkdownDocumentBuilder) MarkdownLanguage(org.eclipse.mylyn.wikitext.markdown.MarkdownLanguage) AbstractMarkupLanguage(org.eclipse.mylyn.wikitext.parser.markup.AbstractMarkupLanguage)

Aggregations

AbstractMarkupLanguage (org.eclipse.mylyn.wikitext.parser.markup.AbstractMarkupLanguage)9 MarkupLanguage (org.eclipse.mylyn.wikitext.parser.markup.MarkupLanguage)5 MarkupParser (org.eclipse.mylyn.wikitext.parser.MarkupParser)3 StringWriter (java.io.StringWriter)2 IDocument (org.eclipse.jface.text.IDocument)2 IDocumentPartitioner (org.eclipse.jface.text.IDocumentPartitioner)2 ISourceViewer (org.eclipse.jface.text.source.ISourceViewer)2 FastMarkupPartitioner (org.eclipse.mylyn.internal.wikitext.ui.editor.syntax.FastMarkupPartitioner)2 PrintWriter (java.io.PrintWriter)1 StringReader (java.io.StringReader)1 BuildException (org.apache.tools.ant.BuildException)1 IFile (org.eclipse.core.resources.IFile)1 CoreException (org.eclipse.core.runtime.CoreException)1 IPath (org.eclipse.core.runtime.IPath)1 BadLocationException (org.eclipse.jface.text.BadLocationException)1 IReconciler (org.eclipse.jface.text.reconciler.IReconciler)1 MarkupMonoReconciler (org.eclipse.mylyn.internal.wikitext.ui.editor.reconciler.MarkupMonoReconciler)1 MarkupDocumentProvider (org.eclipse.mylyn.internal.wikitext.ui.editor.syntax.MarkupDocumentProvider)1 MarkdownLanguage (org.eclipse.mylyn.wikitext.markdown.MarkdownLanguage)1 MarkdownDocumentBuilder (org.eclipse.mylyn.wikitext.markdown.internal.MarkdownDocumentBuilder)1