Search in sources :

Example 11 with BlockType

use of org.eclipse.mylyn.wikitext.parser.DocumentBuilder.BlockType in project mylyn.docs by eclipse.

the class ListBlock method processLineContent.

@Override
public int processLineContent(String line, int offset) {
    if (blockLineCount == 0) {
        ListAttributes attributes = new ListAttributes();
        String spaces = matcher.group(1);
        String listSpec = matcher.group(2);
        String numericListSpec = matcher.group(3);
        if (numericListSpec != null && !"1".equals(numericListSpec)) {
            // $NON-NLS-1$
            attributes.setStart(numericListSpec);
        }
        int level = calculateLevel(spaces);
        BlockType type = listSpec == null ? BlockType.NUMERIC_LIST : BlockType.BULLETED_LIST;
        offset = matcher.start(LINE_REMAINDER_GROUP_OFFSET);
        listState.push(new ListState(level, spaces.length(), offset, type));
        builder.beginBlock(type, attributes);
    } else {
        ListAttributes attributes = new ListAttributes();
        Matcher matcher = startPattern.matcher(line);
        if (!matcher.matches()) {
            Matcher nextLineMatcher = nextLinePattern.matcher(line);
            ListState listState = this.listState.peek();
            if (listState.openItem && nextLineMatcher.matches()) {
                String spaces = nextLineMatcher.group(1);
                if (spaces.length() > 0 && spaces.length() >= listState.numSpaces && spaces.length() <= listState.lineRemainderStart) {
                    ++blockLineCount;
                    offset = nextLineMatcher.start(2) - 1;
                    markupLanguage.emitMarkupLine(getParser(), state, line, offset);
                    return -1;
                }
            }
            setClosed(true);
            return 0;
        }
        String spaces = matcher.group(1);
        String listSpec = matcher.group(2);
        String numericListSpec = matcher.group(3);
        if (numericListSpec != null && !"1".equals(numericListSpec)) {
            // $NON-NLS-1$
            attributes.setStart(numericListSpec);
        }
        int level = calculateLevel(spaces);
        BlockType type = listSpec == null ? BlockType.NUMERIC_LIST : BlockType.BULLETED_LIST;
        offset = matcher.start(LINE_REMAINDER_GROUP_OFFSET);
        for (ListState listState = this.listState.peek(); listState.level != level || listState.type != type; listState = this.listState.peek()) {
            if (listState.level > level || (listState.level == level && listState.type != type)) {
                closeOne();
                if (this.listState.isEmpty()) {
                    this.listState.push(new ListState(1, spaces.length(), offset, type));
                    builder.beginBlock(type, attributes);
                }
            } else {
                this.listState.push(new ListState(level, spaces.length(), offset, type));
                builder.beginBlock(type, attributes);
            }
        }
    }
    ++blockLineCount;
    ListState listState = this.listState.peek();
    if (listState.openItem) {
        builder.endBlock();
    }
    listState.openItem = true;
    builder.beginBlock(BlockType.LIST_ITEM, new Attributes());
    markupLanguage.emitMarkupLine(getParser(), state, line, offset);
    return -1;
}
Also used : BlockType(org.eclipse.mylyn.wikitext.parser.DocumentBuilder.BlockType) Matcher(java.util.regex.Matcher) ListAttributes(org.eclipse.mylyn.wikitext.parser.ListAttributes) Attributes(org.eclipse.mylyn.wikitext.parser.Attributes) ListAttributes(org.eclipse.mylyn.wikitext.parser.ListAttributes)

Example 12 with BlockType

use of org.eclipse.mylyn.wikitext.parser.DocumentBuilder.BlockType in project mylyn.docs by eclipse.

the class BlockStrategiesTest method fallBackToNoOp.

@Test
public void fallBackToNoOp() {
    BlockStrategies strategies = new BlockStrategies(Sets.newHashSet(BlockType.PARAGRAPH));
    List<BlockType> unsupportedBlockTypes = ImmutableList.of(BlockType.TABLE, BlockType.BULLETED_LIST, BlockType.NUMERIC_LIST, BlockType.DEFINITION_LIST);
    for (BlockType blockType : unsupportedBlockTypes) {
        BlockStrategy strategy = strategies.getStrategy(blockType, new Attributes());
        assertNotNull(strategy);
        assertTrue(NoOpBlockStrategy.class.equals(strategy.getClass()));
    }
}
Also used : NoOpBlockStrategy(org.eclipse.mylyn.wikitext.html.internal.NoOpBlockStrategy) BlockType(org.eclipse.mylyn.wikitext.parser.DocumentBuilder.BlockType) Attributes(org.eclipse.mylyn.wikitext.parser.Attributes) BlockStrategies(org.eclipse.mylyn.wikitext.html.internal.BlockStrategies) UnsupportedBlockStrategy(org.eclipse.mylyn.wikitext.html.internal.UnsupportedBlockStrategy) NoOpBlockStrategy(org.eclipse.mylyn.wikitext.html.internal.NoOpBlockStrategy) BlockStrategy(org.eclipse.mylyn.wikitext.html.internal.BlockStrategy) SupportedBlockStrategy(org.eclipse.mylyn.wikitext.html.internal.SupportedBlockStrategy) SubstitutionBlockStrategy(org.eclipse.mylyn.wikitext.html.internal.SubstitutionBlockStrategy) Test(org.junit.Test)

Example 13 with BlockType

use of org.eclipse.mylyn.wikitext.parser.DocumentBuilder.BlockType in project mylyn.docs by eclipse.

the class ListBlock method processLineContent.

@Override
public int processLineContent(String line, int offset) {
    boolean continuation = false;
    if (blockLineCount == 0) {
        listState = new Stack<ListState>();
        String listSpec = matcher.group(1);
        int level = calculateLevel(listSpec);
        BlockType type = calculateType(listSpec.charAt(0));
        offset = matcher.start(2);
        listState.push(new ListState(1, type));
        builder.beginBlock(type, new Attributes());
        adjustLevel(level, type);
    } else {
        Matcher matcher = pattern.matcher(line);
        if (!matcher.matches()) {
            setClosed(true);
            return 0;
        }
        String listSpec = matcher.group(1);
        int lineLevel = calculateLevel(listSpec);
        BlockType type = calculateType(listSpec.charAt(0));
        offset = matcher.start(2);
        continuation = adjustLevel(lineLevel, type);
    }
    ++blockLineCount;
    ListState listState = this.listState.peek();
    if (!continuation && listState.openItem) {
        listState.openItem = false;
        builder.endBlock();
    }
    if (!listState.openItem) {
        listState.openItem = true;
        builder.beginBlock(BlockType.LIST_ITEM, new Attributes());
    }
    markupLanguage.emitMarkupLine(getParser(), state, line, offset);
    return -1;
}
Also used : BlockType(org.eclipse.mylyn.wikitext.parser.DocumentBuilder.BlockType) Matcher(java.util.regex.Matcher) Attributes(org.eclipse.mylyn.wikitext.parser.Attributes)

Example 14 with BlockType

use of org.eclipse.mylyn.wikitext.parser.DocumentBuilder.BlockType 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)

Aggregations

BlockType (org.eclipse.mylyn.wikitext.parser.DocumentBuilder.BlockType)14 Attributes (org.eclipse.mylyn.wikitext.parser.Attributes)12 Matcher (java.util.regex.Matcher)8 BlockStrategies (org.eclipse.mylyn.wikitext.html.internal.BlockStrategies)3 Test (org.junit.Test)3 ArrayList (java.util.ArrayList)2 ListAttributes (org.eclipse.mylyn.wikitext.parser.ListAttributes)2 HtmlDocumentBuilder (org.eclipse.mylyn.wikitext.parser.builder.HtmlDocumentBuilder)2 PrintWriter (java.io.PrintWriter)1 StringReader (java.io.StringReader)1 StringWriter (java.io.StringWriter)1 IFile (org.eclipse.core.resources.IFile)1 CoreException (org.eclipse.core.runtime.CoreException)1 IPath (org.eclipse.core.runtime.IPath)1 BlockStrategy (org.eclipse.mylyn.wikitext.html.internal.BlockStrategy)1 NoOpBlockStrategy (org.eclipse.mylyn.wikitext.html.internal.NoOpBlockStrategy)1 SubstitutionBlockStrategy (org.eclipse.mylyn.wikitext.html.internal.SubstitutionBlockStrategy)1 SupportedBlockStrategy (org.eclipse.mylyn.wikitext.html.internal.SupportedBlockStrategy)1 UnsupportedBlockStrategy (org.eclipse.mylyn.wikitext.html.internal.UnsupportedBlockStrategy)1 DocumentBuilder (org.eclipse.mylyn.wikitext.parser.DocumentBuilder)1