org.commonmark.internal.util.Escaping Java Examples

The following examples show how to use org.commonmark.internal.util.Escaping. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HtmlWriter.java    From 1Rramp-Android with MIT License 6 votes vote down vote up
public void tag(String name, Map<String, String> attrs, boolean voidElement) {
    append("<");
    append(name);
    if (attrs != null && !attrs.isEmpty()) {
        for (Map.Entry<String, String> attrib : attrs.entrySet()) {
            append(" ");
            append(Escaping.escapeHtml(attrib.getKey(), true));
            append("=\"");
            append(Escaping.escapeHtml(attrib.getValue(), true));
            append("\"");
        }
    }
    if (voidElement) {
        append(" /");
    }

    append(">");
}
 
Example #2
Source File: MarkwonInlineParser.java    From Markwon with Apache License 2.0 6 votes vote down vote up
/**
 * Attempt to parse link destination, returning the string or null if no match.
 */
@Override
@Nullable
public String parseLinkDestination() {
    int afterDest = LinkScanner.scanLinkDestination(input, index);
    if (afterDest == -1) {
        return null;
    }

    String dest;
    if (peek() == '<') {
        // chop off surrounding <..>:
        dest = input.substring(index + 1, afterDest - 1);
    } else {
        dest = input.substring(index, afterDest);
    }

    index = afterDest;
    return Escaping.unescapeString(dest);
}
 
Example #3
Source File: HtmlWriter.java    From commonmark-java with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public void tag(String name, Map<String, String> attrs, boolean voidElement) {
    append("<");
    append(name);
    if (attrs != null && !attrs.isEmpty()) {
        for (Map.Entry<String, String> attrib : attrs.entrySet()) {
            append(" ");
            append(Escaping.escapeHtml(attrib.getKey()));
            append("=\"");
            append(Escaping.escapeHtml(attrib.getValue()));
            append("\"");
        }
    }
    if (voidElement) {
        append(" /");
    }

    append(">");
}
 
Example #4
Source File: InlineParserImpl.java    From commonmark-java with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Attempt to parse link destination, returning the string or null if no match.
 */
private String parseLinkDestination() {
    int afterDest = LinkScanner.scanLinkDestination(input, index);
    if (afterDest == -1) {
        return null;
    }

    String dest;
    if (peek() == '<') {
        // chop off surrounding <..>:
        dest = input.substring(index + 1, afterDest - 1);
    } else {
        dest = input.substring(index, afterDest);
    }

    index = afterDest;
    return Escaping.unescapeString(dest);
}
 
Example #5
Source File: HtmlRenderer.java    From 1Rramp-Android with MIT License 5 votes vote down vote up
@Override
public String encodeUrl(String url) {
    if (percentEncodeUrls) {
        return Escaping.percentEncodeUrl(url);
    } else {
        return url;
    }
}
 
Example #6
Source File: InlineParserImpl.java    From 1Rramp-Android with MIT License 5 votes vote down vote up
/**
 * Attempt to parse link destination, returning the string or null if no match.
 */
private String parseLinkDestination() {
    String res = match(LINK_DESTINATION_BRACES);
    if (res != null) { // chop off surrounding <..>:
        if (res.length() == 2) {
            return "";
        } else {
            return Escaping.unescapeString(res.substring(1, res.length() - 1));
        }
    } else {
        int startIndex = index;
        parseLinkDestinationWithBalancedParens();
        return Escaping.unescapeString(input.substring(startIndex, index));
    }
}
 
Example #7
Source File: InlineParserImpl.java    From 1Rramp-Android with MIT License 5 votes vote down vote up
/**
 * Attempt to parse link title (sans quotes), returning the string or null if no match.
 */
private String parseLinkTitle() {
    String title = match(LINK_TITLE);
    if (title != null) {
        // chop off quotes from title and unescape:
        return Escaping.unescapeString(title.substring(1, title.length() - 1));
    } else {
        return null;
    }
}
 
Example #8
Source File: MarkwonInlineParser.java    From Markwon with Apache License 2.0 5 votes vote down vote up
/**
 * Attempt to parse link title (sans quotes), returning the string or null if no match.
 */
@Override
@Nullable
public String parseLinkTitle() {
    int afterTitle = LinkScanner.scanLinkTitle(input, index);
    if (afterTitle == -1) {
        return null;
    }

    // chop off ', " or parens
    String title = input.substring(index + 1, afterTitle - 1);
    index = afterTitle;
    return Escaping.unescapeString(title);
}
 
Example #9
Source File: HtmlRenderer.java    From commonmark-java with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public String encodeUrl(String url) {
    if (percentEncodeUrls) {
        return Escaping.percentEncodeUrl(url);
    } else {
        return url;
    }
}
 
Example #10
Source File: InlineParserImpl.java    From commonmark-java with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Attempt to parse link title (sans quotes), returning the string or null if no match.
 */
private String parseLinkTitle() {
    int afterTitle = LinkScanner.scanLinkTitle(input, index);
    if (afterTitle == -1) {
        return null;
    }

    // chop off ', " or parens
    String title = input.substring(index + 1, afterTitle - 1);
    index = afterTitle;
    return Escaping.unescapeString(title);
}
 
Example #11
Source File: LinkReferenceDefinitionParser.java    From commonmark-java with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private int label(CharSequence line, int i) {
    int afterLabel = LinkScanner.scanLinkLabelContent(line, i);
    if (afterLabel == -1) {
        return -1;
    }

    label.append(line, i, afterLabel);

    if (afterLabel >= line.length()) {
        // label might continue on next line
        label.append('\n');
        return afterLabel;
    } else if (line.charAt(afterLabel) == ']') {
        int colon = afterLabel + 1;
        // end of label
        if (colon >= line.length() || line.charAt(colon) != ':') {
            return -1;
        }

        // spec: A link label can have at most 999 characters inside the square brackets.
        if (label.length() > 999) {
            return -1;
        }

        String normalizedLabel = Escaping.normalizeLabelContent(label.toString());
        if (normalizedLabel.isEmpty()) {
            return -1;
        }

        this.normalizedLabel = normalizedLabel;
        state = State.DESTINATION;

        return Parsing.skipSpaceTab(line, colon + 1, line.length());
    } else {
        return -1;
    }
}
 
Example #12
Source File: LinkReferenceDefinitionParser.java    From commonmark-java with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void finishReference() {
    if (!referenceValid) {
        return;
    }

    String d = Escaping.unescapeString(destination);
    String t = title != null ? Escaping.unescapeString(title.toString()) : null;
    definitions.add(new LinkReferenceDefinition(normalizedLabel, d, t));

    label = null;
    referenceValid = false;
    normalizedLabel = null;
    destination = null;
    title = null;
}
 
Example #13
Source File: InlineParserImpl.java    From 1Rramp-Android with MIT License 4 votes vote down vote up
/**
 * Attempt to parse a link reference, modifying the internal reference map.
 */
@Override
public int parseReference(String s) {
    this.input = s;
    this.index = 0;
    String dest;
    String title;
    int matchChars;
    int startIndex = index;

    // label:
    matchChars = parseLinkLabel();
    if (matchChars == 0) {
        return 0;
    }

    String rawLabel = input.substring(0, matchChars);

    // colon:
    if (peek() != ':') {
        return 0;
    }
    index++;

    // link url
    spnl();

    dest = parseLinkDestination();
    if (dest == null || dest.length() == 0) {
        return 0;
    }

    int beforeTitle = index;
    spnl();
    title = parseLinkTitle();
    if (title == null) {
        // rewind before spaces
        index = beforeTitle;
    }

    boolean atLineEnd = true;
    if (index != input.length() && match(LINE_END) == null) {
        if (title == null) {
            atLineEnd = false;
        } else {
            // the potential title we found is not at the line end,
            // but it could still be a legal link reference if we
            // discard the title
            title = null;
            // rewind before spaces
            index = beforeTitle;
            // and instead check if the link URL is at the line end
            atLineEnd = match(LINE_END) != null;
        }
    }

    if (!atLineEnd) {
        return 0;
    }

    String normalizedLabel = Escaping.normalizeReference(rawLabel);
    if (normalizedLabel.isEmpty()) {
        return 0;
    }

    if (!referenceMap.containsKey(normalizedLabel)) {
        Link link = new Link(dest, title);
        referenceMap.put(normalizedLabel, link);
    }
    return index - startIndex;
}