use of org.pegdown.LinkRenderer in project webprotege by protegeproject.
the class CommentRenderer method renderComment.
public String renderComment(String commentBody) {
List<ParsedMention> parsedMentions = mentionParser.parseMentions(commentBody);
StringBuilder rendering = new StringBuilder();
int currentPos = 0;
for (ParsedMention pm : parsedMentions) {
int startIndex = pm.getStartIndex();
int endIndex = pm.getEndIndex();
rendering.append(commentBody.substring(currentPos, startIndex));
Mention mention = pm.getParsedMention();
if (mention.getMentionedUserId().isPresent()) {
rendering.append("<span class=\"wp-comment__user-mention\">");
rendering.append(mention.getMentionedUserId().get().getUserName());
rendering.append("</span>");
} else {
rendering.append(commentBody.substring(startIndex, endIndex));
}
currentPos = endIndex;
}
if (currentPos < commentBody.length()) {
rendering.append(commentBody.substring(currentPos));
}
PegDownProcessor processor = new PegDownProcessor(Extensions.ABBREVIATIONS | Extensions.QUOTES | Extensions.STRIKETHROUGH | Extensions.AUTOLINKS | Extensions.FENCED_CODE_BLOCKS, new PegDownPlugins.Builder().build());
String html = processor.markdownToHtml(rendering.toString(), new LinkRenderer() {
});
return html;
}
use of org.pegdown.LinkRenderer in project gitblit by gitblit.
the class MarkupProcessor method parse.
/**
* Parses the document as Markdown using Pegdown.
*
* @param doc
* @param repositoryName
* @param commitId
*/
private void parse(final MarkupDocument doc, final String repositoryName, final String commitId) {
LinkRenderer renderer = new LinkRenderer() {
@Override
public Rendering render(ExpImageNode node, String text) {
if (!isFullUrl(node.url)) {
// repository-relative image link
String path = doc.getRelativePath(node.url);
String contextUrl = RequestCycle.get().getRequest().getRelativePathPrefixToContextRoot();
String url = RawServlet.asLink(contextUrl, repositoryName, commitId, path);
return new Rendering(url, text);
}
// absolute image link
return new Rendering(node.url, text);
}
@Override
public Rendering render(RefImageNode node, String url, String title, String alt) {
Rendering rendering;
if (!isFullUrl(url)) {
// repository-relative image link
String path = doc.getRelativePath(url);
String contextUrl = RequestCycle.get().getRequest().getRelativePathPrefixToContextRoot();
String wurl = RawServlet.asLink(contextUrl, repositoryName, commitId, path);
rendering = new Rendering(wurl, alt);
} else {
// absolute image link
rendering = new Rendering(url, alt);
}
return StringUtils.isEmpty(title) ? rendering : rendering.withAttribute("title", encode(title));
}
@Override
public Rendering render(WikiLinkNode node) {
String path = doc.getRelativePath(node.getText());
String name = getDocumentName(path);
String url = getWicketUrl(DocPage.class, repositoryName, commitId, path);
return new Rendering(url, name);
}
@Override
public Rendering render(ExpLinkNode node, String text) {
// like "/xx/tt" or "../somefolder".
if (isFullUrl(node.url)) {
// This is URL, fallback to superclass.
return super.render(node, text);
}
// repository-relative link
String path = doc.getRelativePath(node.url);
String url = getWicketUrl(DocPage.class, repositoryName, commitId, path);
return new Rendering(url, text);
}
@Override
public Rendering render(RefLinkNode node, String url, String title, String text) {
if (isFullUrl(url)) {
// This is URL, fallback to superclass.
return super.render(node, url, title, text);
}
// repository-relative link
String path = doc.getRelativePath(url);
String local_url = getWicketUrl(DocPage.class, repositoryName, commitId, path);
return super.render(node, local_url, title, text);
}
};
final String content = MarkdownUtils.transformMarkdown(doc.markup, renderer);
final String safeContent = xssFilter.relaxed(content);
doc.html = safeContent;
}
use of org.pegdown.LinkRenderer in project gitblit by gitblit.
the class MarkdownUtils method transformMarkdown.
/**
* Returns the html version of the markdown source text.
*
* @param markdown
* @return html version of markdown text
* @throws java.text.ParseException
*/
public static String transformMarkdown(String markdown, LinkRenderer linkRenderer) {
try {
PegDownProcessor pd = new PegDownProcessor(ALL & ~SMARTYPANTS & ~ANCHORLINKS);
RootNode astRoot = pd.parseMarkdown(markdown.toCharArray());
return new WorkaroundHtmlSerializer(linkRenderer == null ? new LinkRenderer() : linkRenderer).toHtml(astRoot);
} catch (ParsingTimeoutException e) {
return null;
}
}
Aggregations