use of org.eclipse.mylyn.wikitext.parser.MarkupParser 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;
}
use of org.eclipse.mylyn.wikitext.parser.MarkupParser 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);
}
}
use of org.eclipse.mylyn.wikitext.parser.MarkupParser in project mylyn.docs by eclipse.
the class MarkupTextPresenter method updatePresentation.
@Override
public String updatePresentation(Drawable drawable, String hoverInfo, TextPresentation presentation, int maxWidth, int maxHeight) {
if (markupLanguage == null) {
throw new IllegalStateException();
}
if (hoverInfo == null || hoverInfo.length() == 0) {
return hoverInfo;
}
MarkupParser parser = new MarkupParser();
parser.setMarkupLanguage(markupLanguage);
String html = parser.parseToHtml(hoverInfo);
return super.updatePresentation(drawable, html, presentation, maxWidth, maxHeight);
}
use of org.eclipse.mylyn.wikitext.parser.MarkupParser 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);
}
}
use of org.eclipse.mylyn.wikitext.parser.MarkupParser 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;
}
Aggregations