List of usage examples for com.google.common.html.types SafeHtmlBuilder SafeHtmlBuilder
public SafeHtmlBuilder(@CompileTimeConstant final String elementName)
From source file:com.google.devtools.kythe.doc.DocUnbracketer.java
/** * Unbracket {@link printable}, following the rules for text in {@link Printable} messages. * * @param makeLink if provided, this function will be used to generate link URIs from semantic * node tickets. It may return null if there is no available URI. * @param printable the document text to unbracket. *//*from www. j a va2s .c om*/ public static SafeHtml unbracket(Function<String, SafeUrl> makeLink, Printable printable) { int size = printable.getRawText().length(); String raw = printable.getRawText(); SafeHtmlBuilder builder = new SafeHtmlBuilder("pre"); SafeHtmlBuilder linkBuilder = null; // is non-null if we are inside a link boolean linkTextIsEmpty = true; int linkNumber = 0; // is equal to the number of link-opening [s we've seen so far Stack<SafeUrl> links = new Stack<SafeUrl>(); // is always as high as the nesting level, for well-formed documents for (int c = 0; c < size; ++c) { char ch = raw.charAt(c); if (ch == '\\' && c + 1 < size && ALLOWED_ESCAPES.indexOf(raw.charAt(c + 1)) != -1) { if (linkBuilder != null) { linkBuilder = linkBuilder.escapeAndAppendContent(raw.substring(c + 1, c + 2)); linkTextIsEmpty = false; } else { builder = builder.escapeAndAppendContent(raw.substring(c + 1, c + 2)); } ++c; } else if (ch == '[') { if (linkBuilder != null && !linkTextIsEmpty) { builder = builder.appendContent(linkBuilder.build()); } linkBuilder = null; linkTextIsEmpty = true; SafeUrl link = null; if (linkNumber < printable.getLinkCount() && makeLink != null) { // Pick the first definition ticket that will provide a location for a link. // (We expect to see only one definition ticket per link in general.) for (String definition : printable.getLink(linkNumber).getDefinitionList()) { link = makeLink.apply(definition); if (link != null) { break; } } ++linkNumber; } links.push(link); if (link != null) { linkBuilder = new SafeHtmlBuilder("a").setHref(link); linkTextIsEmpty = true; } } else if (ch == ']') { if (linkBuilder != null) { if (!linkTextIsEmpty) { builder = builder.appendContent(linkBuilder.build()); } linkBuilder = null; } if (!links.empty()) { links.pop(); if (!links.empty() && links.peek() != null) { linkBuilder = new SafeHtmlBuilder("a").setHref(links.peek()); linkTextIsEmpty = true; } } } else if (linkBuilder != null) { linkBuilder = linkBuilder.escapeAndAppendContent(raw.substring(c, c + 1)); linkTextIsEmpty = false; } else { builder = builder.escapeAndAppendContent(raw.substring(c, c + 1)); } } if (linkBuilder != null) { builder = builder.appendContent(linkBuilder.build()); } return builder.build(); }