Search in sources :

Example 41 with LinkAttributes

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

the class AbstractSaxHtmlParser method computeAttributes.

private org.eclipse.mylyn.wikitext.parser.Attributes computeAttributes(SpanType spanType, Attributes atts) {
    org.eclipse.mylyn.wikitext.parser.Attributes attributes = spanType == SpanType.LINK ? new LinkAttributes() : new org.eclipse.mylyn.wikitext.parser.Attributes();
    populateCommonAttributes(attributes, atts);
    if (spanType == SpanType.LINK) {
        // $NON-NLS-1$
        String href = getValue("href", atts);
        if (href != null) {
            ((LinkAttributes) attributes).setHref(href);
        }
    }
    return attributes;
}
Also used : LinkAttributes(org.eclipse.mylyn.wikitext.parser.LinkAttributes)

Example 42 with LinkAttributes

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

the class SplittingHtmlDocumentBuilder method beginSpan.

@Override
public void beginSpan(SpanType type, Attributes attributes) {
    if (type == SpanType.LINK && attributes instanceof LinkAttributes) {
        LinkAttributes linkAttributes = (LinkAttributes) attributes;
        linkAttributes.setHref(adjustHref(linkAttributes.getHref()));
    }
    out.beginSpan(type, attributes);
}
Also used : LinkAttributes(org.eclipse.mylyn.wikitext.parser.LinkAttributes)

Example 43 with LinkAttributes

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

the class MarkupHyperlinkDetector method detectHyperlinks.

public IHyperlink[] detectHyperlinks(ITextViewer textViewer, IRegion region, boolean canShowMultipleHyperlinks) {
    if (markupLanguage == null || file == null) {
        return null;
    }
    IDocument document = textViewer.getDocument();
    if (document == null || document.getLength() == 0) {
        return null;
    }
    String content;
    int contentOffset;
    try {
        if (region.getLength() == 0) {
            // expand the region to include the whole line
            IRegion lineInfo = document.getLineInformationOfOffset(region.getOffset());
            int lineLength = lineInfo.getLength();
            int lineOffset = lineInfo.getOffset();
            int lineEnd = lineOffset + lineLength;
            int regionEnd = region.getOffset() + region.getLength();
            if (lineOffset < region.getOffset()) {
                int regionLength = Math.max(regionEnd, lineEnd) - lineOffset;
                contentOffset = lineOffset;
                content = document.get(lineOffset, regionLength);
            } else {
                // the line starts after region, may never happen
                int regionLength = Math.max(regionEnd, lineEnd) - region.getOffset();
                contentOffset = region.getOffset();
                content = document.get(contentOffset, regionLength);
            }
        } else {
            content = document.get(region.getOffset(), region.getLength());
            contentOffset = region.getOffset();
        }
    } catch (BadLocationException ex) {
        return null;
    }
    MarkupParser markupParser = new MarkupParser(markupLanguage);
    final List<HyperlinkDescriptor> links = new ArrayList<>();
    markupParser.setBuilder(new NoOpDocumentBuilder() {

        @Override
        public void link(Attributes attributes, String hrefOrHashName, String text) {
            if (hrefOrHashName != null && !hrefOrHashName.startsWith("#")) {
                // $NON-NLS-1$
                IRegion region = createRegion();
                links.add(new HyperlinkDescriptor(hrefOrHashName, region));
            }
        }

        private IRegion createRegion() {
            int offset = getLocator().getLineCharacterOffset();
            int length = getLocator().getLineSegmentEndOffset() - offset;
            return new Region(offset, length);
        }

        @Override
        public void beginSpan(SpanType type, Attributes attributes) {
            if (type == SpanType.LINK) {
                if (attributes instanceof LinkAttributes) {
                    LinkAttributes linkAttributes = (LinkAttributes) attributes;
                    if (linkAttributes.getHref() != null && !linkAttributes.getHref().startsWith("#")) {
                        // $NON-NLS-1$
                        IRegion region = createRegion();
                        links.add(new HyperlinkDescriptor(linkAttributes.getHref(), region));
                    }
                }
            }
        }
    });
    markupParser.parse(content);
    if (!links.isEmpty()) {
        List<IHyperlink> hyperlinks = new ArrayList<>(links.size());
        for (HyperlinkDescriptor descriptor : links) {
            if (descriptor.href.indexOf(':') == -1 && descriptor.href.length() > 1 && descriptor.href.charAt(0) != '/') {
                IRegion hyperlinkRegion = new Region(descriptor.region.getOffset() + contentOffset, descriptor.region.getLength());
                if (region.getLength() > 0) {
                    if (!isInRegion(region, hyperlinkRegion)) {
                        continue;
                    }
                } else {
                    if (!(hyperlinkRegion.getOffset() <= region.getOffset() && (hyperlinkRegion.getOffset() + hyperlinkRegion.getLength()) >= region.getOffset())) {
                        continue;
                    }
                }
                try {
                    IPath containerPath = file.getParent().getFullPath();
                    IPath absolutePath = containerPath.append(descriptor.href);
                    IFile targetFile = ResourcesPlugin.getWorkspace().getRoot().getFile(absolutePath);
                    if (targetFile != null) {
                        if (targetFile.exists()) {
                            hyperlinks.add(new EditFileHyperlink(targetFile, hyperlinkRegion));
                        }
                        IContainer parent = targetFile.getParent();
                        if (parent.exists()) {
                            String nameNoExtension = targetFile.getName();
                            if (nameNoExtension.indexOf('.') != -1) {
                                nameNoExtension = nameNoExtension.substring(0, nameNoExtension.lastIndexOf('.') + 1);
                            }
                            IResource[] members = parent.members();
                            for (IResource resource : members) {
                                if (resource.getType() == IResource.FILE && resource.getName().startsWith(nameNoExtension) && !resource.equals(targetFile)) {
                                    hyperlinks.add(new EditFileHyperlink((IFile) resource, hyperlinkRegion));
                                }
                            }
                        }
                    }
                } catch (Throwable t) {
                // ignore
                }
            }
        }
        if (!hyperlinks.isEmpty()) {
            return hyperlinks.toArray(new IHyperlink[hyperlinks.size()]);
        }
    }
    return null;
}
Also used : NoOpDocumentBuilder(org.eclipse.mylyn.wikitext.parser.builder.NoOpDocumentBuilder) IFile(org.eclipse.core.resources.IFile) IPath(org.eclipse.core.runtime.IPath) ArrayList(java.util.ArrayList) LinkAttributes(org.eclipse.mylyn.wikitext.parser.LinkAttributes) Attributes(org.eclipse.mylyn.wikitext.parser.Attributes) IRegion(org.eclipse.jface.text.IRegion) LinkAttributes(org.eclipse.mylyn.wikitext.parser.LinkAttributes) IHyperlink(org.eclipse.jface.text.hyperlink.IHyperlink) Region(org.eclipse.jface.text.Region) IRegion(org.eclipse.jface.text.IRegion) IContainer(org.eclipse.core.resources.IContainer) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException) IResource(org.eclipse.core.resources.IResource) MarkupParser(org.eclipse.mylyn.wikitext.parser.MarkupParser)

Example 44 with LinkAttributes

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

the class DitaTopicDocumentBuilderTest method testSpanLink.

public void testSpanLink() {
    builder.beginDocument();
    builder.beginBlock(BlockType.DIV, new Attributes());
    builder.beginBlock(BlockType.PARAGRAPH, new Attributes());
    LinkAttributes attributes = new LinkAttributes();
    attributes.setHref("#test1234");
    builder.beginSpan(SpanType.LINK, attributes);
    builder.beginSpan(SpanType.EMPHASIS, new Attributes());
    builder.characters("link text");
    builder.endSpan();
    builder.endSpan();
    // PARAGRAPH
    builder.endBlock();
    // DIV
    builder.endBlock();
    builder.endDocument();
    String dita = out.toString();
    assertTrue(Pattern.compile("<xref href=\"#test1234\">\\s*<i>link text</i>\\s*</xref>").matcher(dita).find());
}
Also used : LinkAttributes(org.eclipse.mylyn.wikitext.parser.LinkAttributes) LinkAttributes(org.eclipse.mylyn.wikitext.parser.LinkAttributes) Attributes(org.eclipse.mylyn.wikitext.parser.Attributes)

Example 45 with LinkAttributes

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

the class HtmlDocumentBuilder2Test method testLinkRel.

public void testLinkRel() throws Exception {
    // link-specific rel
    StringWriter out = new StringWriter();
    HtmlDocumentBuilder builder = new HtmlDocumentBuilder(out);
    builder.beginDocument();
    builder.beginBlock(BlockType.PARAGRAPH, new Attributes());
    LinkAttributes attributes = new LinkAttributes();
    attributes.setRel("nofollow");
    builder.link(attributes, "http://www.foo.bar", "Foo Bar");
    builder.endBlock();
    builder.endDocument();
    String html = out.toString();
    assertTrue(html.contains("<a href=\"http://www.foo.bar\" rel=\"nofollow\">Foo Bar</a>"));
    // default link rel
    out = new StringWriter();
    builder = new HtmlDocumentBuilder(out);
    builder.setLinkRel("nofollow");
    builder.beginDocument();
    builder.beginBlock(BlockType.PARAGRAPH, new Attributes());
    attributes = new LinkAttributes();
    builder.link(attributes, "http://www.foo.bar", "Foo Bar");
    builder.endBlock();
    builder.endDocument();
    html = out.toString();
    assertTrue(html.contains("<a href=\"http://www.foo.bar\" rel=\"nofollow\">Foo Bar</a>"));
    // both link-specific and default link ref
    out = new StringWriter();
    builder = new HtmlDocumentBuilder(out);
    builder.setLinkRel("nofollow");
    builder.beginDocument();
    builder.beginBlock(BlockType.PARAGRAPH, new Attributes());
    attributes = new LinkAttributes();
    attributes.setRel("foobar");
    builder.link(attributes, "http://www.foo.bar", "Foo Bar");
    builder.endBlock();
    builder.endDocument();
    html = out.toString();
    assertTrue(html.contains("<a href=\"http://www.foo.bar\" rel=\"foobar nofollow\">Foo Bar</a>"));
    // no rel at all
    out = new StringWriter();
    builder = new HtmlDocumentBuilder(out);
    builder.beginDocument();
    builder.beginBlock(BlockType.PARAGRAPH, new Attributes());
    attributes = new LinkAttributes();
    builder.link(attributes, "http://www.foo.bar", "Foo Bar");
    builder.endBlock();
    builder.endDocument();
    html = out.toString();
    assertTrue(html.contains("<a href=\"http://www.foo.bar\">Foo Bar</a>"));
}
Also used : LinkAttributes(org.eclipse.mylyn.wikitext.parser.LinkAttributes) StringWriter(java.io.StringWriter) LinkAttributes(org.eclipse.mylyn.wikitext.parser.LinkAttributes) Attributes(org.eclipse.mylyn.wikitext.parser.Attributes) HtmlDocumentBuilder(org.eclipse.mylyn.wikitext.parser.builder.HtmlDocumentBuilder)

Aggregations

LinkAttributes (org.eclipse.mylyn.wikitext.parser.LinkAttributes)45 Test (org.junit.Test)16 ImageAttributes (org.eclipse.mylyn.wikitext.parser.ImageAttributes)12 Attributes (org.eclipse.mylyn.wikitext.parser.Attributes)10 ImageLinkEvent (org.eclipse.mylyn.wikitext.parser.builder.event.ImageLinkEvent)5 TableAttributes (org.eclipse.mylyn.wikitext.parser.TableAttributes)2 LinkEvent (org.eclipse.mylyn.wikitext.parser.builder.event.LinkEvent)2 StringWriter (java.io.StringWriter)1 ArrayList (java.util.ArrayList)1 IContainer (org.eclipse.core.resources.IContainer)1 IFile (org.eclipse.core.resources.IFile)1 IResource (org.eclipse.core.resources.IResource)1 IPath (org.eclipse.core.runtime.IPath)1 BadLocationException (org.eclipse.jface.text.BadLocationException)1 IDocument (org.eclipse.jface.text.IDocument)1 IRegion (org.eclipse.jface.text.IRegion)1 Region (org.eclipse.jface.text.Region)1 IHyperlink (org.eclipse.jface.text.hyperlink.IHyperlink)1 ListAttributes (org.eclipse.mylyn.wikitext.parser.ListAttributes)1 MarkupParser (org.eclipse.mylyn.wikitext.parser.MarkupParser)1