org.commonmark.parser.delimiter.DelimiterProcessor Java Examples

The following examples show how to use org.commonmark.parser.delimiter.DelimiterProcessor. 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: InlineParserImpl.java    From commonmark-java with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Attempt to parse delimiters like emphasis, strong emphasis or custom delimiters.
 */
private Node parseDelimiters(DelimiterProcessor delimiterProcessor, char delimiterChar) {
    DelimiterData res = scanDelimiters(delimiterProcessor, delimiterChar);
    if (res == null) {
        return null;
    }
    int length = res.count;
    int startIndex = index;

    index += length;
    Text node = text(input, startIndex, index);

    // Add entry to stack for this opener
    lastDelimiter = new Delimiter(node, delimiterChar, res.canOpen, res.canClose, lastDelimiter);
    lastDelimiter.length = length;
    lastDelimiter.originalLength = length;
    if (lastDelimiter.previous != null) {
        lastDelimiter.previous.next = lastDelimiter;
    }

    return node;
}
 
Example #2
Source File: InlineParserImpl.java    From commonmark-java with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private static void addDelimiterProcessors(Iterable<DelimiterProcessor> delimiterProcessors, Map<Character, DelimiterProcessor> map) {
    for (DelimiterProcessor delimiterProcessor : delimiterProcessors) {
        char opening = delimiterProcessor.getOpeningCharacter();
        char closing = delimiterProcessor.getClosingCharacter();
        if (opening == closing) {
            DelimiterProcessor old = map.get(opening);
            if (old != null && old.getOpeningCharacter() == old.getClosingCharacter()) {
                StaggeredDelimiterProcessor s;
                if (old instanceof StaggeredDelimiterProcessor) {
                    s = (StaggeredDelimiterProcessor) old;
                } else {
                    s = new StaggeredDelimiterProcessor(opening);
                    s.add(old);
                }
                s.add(delimiterProcessor);
                map.put(opening, s);
            } else {
                addDelimiterProcessorForChar(opening, delimiterProcessor, map);
            }
        } else {
            addDelimiterProcessorForChar(opening, delimiterProcessor, map);
            addDelimiterProcessorForChar(closing, delimiterProcessor, map);
        }
    }
}
 
Example #3
Source File: StaggeredDelimiterProcessor.java    From 1Rramp-Android with MIT License 6 votes vote down vote up
void add(DelimiterProcessor dp) {
    final int len = dp.getMinLength();
    ListIterator<DelimiterProcessor> it = processors.listIterator();
    boolean added = false;
    while (it.hasNext()) {
        DelimiterProcessor p = it.next();
        int pLen = p.getMinLength();
        if (len > pLen) {
            it.previous();
            it.add(dp);
            added = true;
            break;
        } else if (len == pLen) {
            throw new IllegalArgumentException("Cannot add two delimiter processors for char '" + delim + "' and minimum length " + len);
        }
    }
    if (!added) {
        processors.add(dp);
        this.minLength = len;
    }
}
 
Example #4
Source File: StaggeredDelimiterProcessor.java    From commonmark-java with BSD 2-Clause "Simplified" License 6 votes vote down vote up
void add(DelimiterProcessor dp) {
    final int len = dp.getMinLength();
    ListIterator<DelimiterProcessor> it = processors.listIterator();
    boolean added = false;
    while (it.hasNext()) {
        DelimiterProcessor p = it.next();
        int pLen = p.getMinLength();
        if (len > pLen) {
            it.previous();
            it.add(dp);
            added = true;
            break;
        } else if (len == pLen) {
            throw new IllegalArgumentException("Cannot add two delimiter processors for char '" + delim + "' and minimum length " + len);
        }
    }
    if (!added) {
        processors.add(dp);
        this.minLength = len;
    }
}
 
Example #5
Source File: MarkwonInlineParser.java    From Markwon with Apache License 2.0 6 votes vote down vote up
@Override
public InlineParser create(InlineParserContext inlineParserContext) {
    final List<DelimiterProcessor> delimiterProcessors;
    final List<DelimiterProcessor> customDelimiterProcessors = inlineParserContext.getCustomDelimiterProcessors();
    final int size = customDelimiterProcessors != null
            ? customDelimiterProcessors.size()
            : 0;
    if (size > 0) {
        delimiterProcessors = new ArrayList<>(size + this.delimiterProcessors.size());
        delimiterProcessors.addAll(this.delimiterProcessors);
        delimiterProcessors.addAll(customDelimiterProcessors);
    } else {
        delimiterProcessors = this.delimiterProcessors;
    }
    return new MarkwonInlineParser(
            inlineParserContext,
            referencesEnabled,
            inlineProcessors,
            delimiterProcessors);
}
 
Example #6
Source File: InlineParserImpl.java    From 1Rramp-Android with MIT License 6 votes vote down vote up
private static void addDelimiterProcessors(Iterable<DelimiterProcessor> delimiterProcessors, Map<Character, DelimiterProcessor> map) {
    for (DelimiterProcessor delimiterProcessor : delimiterProcessors) {
        char opening = delimiterProcessor.getOpeningCharacter();
        char closing = delimiterProcessor.getClosingCharacter();
        if (opening == closing) {
            DelimiterProcessor old = map.get(opening);
            if (old != null && old.getOpeningCharacter() == old.getClosingCharacter()) {
                StaggeredDelimiterProcessor s;
                if (old instanceof StaggeredDelimiterProcessor) {
                    s = (StaggeredDelimiterProcessor) old;
                } else {
                    s = new StaggeredDelimiterProcessor(opening);
                    s.add(old);
                }
                s.add(delimiterProcessor);
                map.put(opening, s);
            } else {
                addDelimiterProcessorForChar(opening, delimiterProcessor, map);
            }
        } else {
            addDelimiterProcessorForChar(opening, delimiterProcessor, map);
            addDelimiterProcessorForChar(closing, delimiterProcessor, map);
        }
    }
}
 
Example #7
Source File: MarkwonInlineParser.java    From Markwon with Apache License 2.0 6 votes vote down vote up
/**
 * Attempt to parse delimiters like emphasis, strong emphasis or custom delimiters.
 */
@Nullable
private Node parseDelimiters(DelimiterProcessor delimiterProcessor, char delimiterChar) {
    DelimiterData res = scanDelimiters(delimiterProcessor, delimiterChar);
    if (res == null) {
        return null;
    }
    int length = res.count;
    int startIndex = index;

    index += length;
    Text node = text(input, startIndex, index);

    // Add entry to stack for this opener
    lastDelimiter = new Delimiter(node, delimiterChar, res.canOpen, res.canClose, lastDelimiter);
    lastDelimiter.length = length;
    lastDelimiter.originalLength = length;
    if (lastDelimiter.previous != null) {
        lastDelimiter.previous.next = lastDelimiter;
    }

    return node;
}
 
Example #8
Source File: InlineParserImpl.java    From 1Rramp-Android with MIT License 6 votes vote down vote up
/**
 * Attempt to parse delimiters like emphasis, strong emphasis or custom delimiters.
 */
private boolean parseDelimiters(DelimiterProcessor delimiterProcessor, char delimiterChar) {
    DelimiterData res = scanDelimiters(delimiterProcessor, delimiterChar);
    if (res == null) {
        return false;
    }
    int length = res.count;
    int startIndex = index;

    index += length;
    Text node = appendText(input, startIndex, index);

    // Add entry to stack for this opener
    lastDelimiter = new Delimiter(node, delimiterChar, res.canOpen, res.canClose, lastDelimiter);
    lastDelimiter.length = length;
    lastDelimiter.originalLength = length;
    if (lastDelimiter.previous != null) {
        lastDelimiter.previous.next = lastDelimiter;
    }

    return true;
}
 
Example #9
Source File: MarkwonInlineParser.java    From Markwon with Apache License 2.0 6 votes vote down vote up
private static void addDelimiterProcessors(Iterable<DelimiterProcessor> delimiterProcessors, Map<Character, DelimiterProcessor> map) {
    for (DelimiterProcessor delimiterProcessor : delimiterProcessors) {
        char opening = delimiterProcessor.getOpeningCharacter();
        char closing = delimiterProcessor.getClosingCharacter();
        if (opening == closing) {
            DelimiterProcessor old = map.get(opening);
            if (old != null && old.getOpeningCharacter() == old.getClosingCharacter()) {
                StaggeredDelimiterProcessor s;
                if (old instanceof StaggeredDelimiterProcessor) {
                    s = (StaggeredDelimiterProcessor) old;
                } else {
                    s = new StaggeredDelimiterProcessor(opening);
                    s.add(old);
                }
                s.add(delimiterProcessor);
                map.put(opening, s);
            } else {
                addDelimiterProcessorForChar(opening, delimiterProcessor, map);
            }
        } else {
            addDelimiterProcessorForChar(opening, delimiterProcessor, map);
            addDelimiterProcessorForChar(closing, delimiterProcessor, map);
        }
    }
}
 
Example #10
Source File: StaggeredDelimiterProcessor.java    From Markwon with Apache License 2.0 6 votes vote down vote up
void add(DelimiterProcessor dp) {
    final int len = dp.getMinLength();
    ListIterator<DelimiterProcessor> it = processors.listIterator();
    boolean added = false;
    while (it.hasNext()) {
        DelimiterProcessor p = it.next();
        int pLen = p.getMinLength();
        if (len > pLen) {
            it.previous();
            it.add(dp);
            added = true;
            break;
        } else if (len == pLen) {
            throw new IllegalArgumentException("Cannot add two delimiter processors for char '" + delim + "' and minimum length " + len);
        }
    }
    if (!added) {
        processors.add(dp);
        this.minLength = len;
    }
}
 
Example #11
Source File: DocumentParser.java    From commonmark-java with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public DocumentParser(List<BlockParserFactory> blockParserFactories, InlineParserFactory inlineParserFactory,
                      List<DelimiterProcessor> delimiterProcessors) {
    this.blockParserFactories = blockParserFactories;
    this.inlineParserFactory = inlineParserFactory;
    this.delimiterProcessors = delimiterProcessors;

    this.documentBlockParser = new DocumentBlockParser();
    activateBlockParser(this.documentBlockParser);
}
 
Example #12
Source File: StaggeredDelimiterProcessor.java    From commonmark-java with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private DelimiterProcessor findProcessor(int len) {
    for (DelimiterProcessor p : processors) {
        if (p.getMinLength() <= len) {
            return p;
        }
    }
    return processors.getFirst();
}
 
Example #13
Source File: MarkwonInlineParser.java    From Markwon with Apache License 2.0 5 votes vote down vote up
InlineParserFactoryImpl(
        boolean referencesEnabled,
        @NonNull List<InlineProcessor> inlineProcessors,
        @NonNull List<DelimiterProcessor> delimiterProcessors) {
    this.referencesEnabled = referencesEnabled;
    this.inlineProcessors = inlineProcessors;
    this.delimiterProcessors = delimiterProcessors;
}
 
Example #14
Source File: MarkwonInlineParser.java    From Markwon with Apache License 2.0 5 votes vote down vote up
@NonNull
@Override
public FactoryBuilder excludeDelimiterProcessor(@NonNull Class<? extends DelimiterProcessor> type) {
    for (int i = 0, size = delimiterProcessors.size(); i < size; i++) {
        if (type.equals(delimiterProcessors.get(i).getClass())) {
            delimiterProcessors.remove(i);
            break;
        }
    }
    return this;
}
 
Example #15
Source File: MarkwonInlineParser.java    From Markwon with Apache License 2.0 5 votes vote down vote up
/**
 * Parse the next inline element in subject, advancing input index.
 * On success, add the result to block's children and return true.
 * On failure, return false.
 */
@Nullable
private Node parseInline() {

    final char c = peek();

    if (c == '\0') {
        return null;
    }

    Node node = null;

    final List<InlineProcessor> inlines = this.inlineProcessors.get(c);

    if (inlines != null) {
        for (InlineProcessor inline : inlines) {
            node = inline.parse(this);
            if (node != null) {
                break;
            }
        }
    } else {
        final DelimiterProcessor delimiterProcessor = delimiterProcessors.get(c);
        if (delimiterProcessor != null) {
            node = parseDelimiters(delimiterProcessor, c);
        } else {
            node = parseString();
        }
    }

    if (node != null) {
        return node;
    } else {
        index++;
        // When we get here, it's only for a single special character that turned out to not have a special meaning.
        // So we shouldn't have a single surrogate here, hence it should be ok to turn it into a String.
        String literal = String.valueOf(c);
        return text(literal);
    }
}
 
Example #16
Source File: MarkwonInlineParser.java    From Markwon with Apache License 2.0 5 votes vote down vote up
public MarkwonInlineParser(
        @NonNull InlineParserContext inlineParserContext,
        boolean referencesEnabled,
        @NonNull List<InlineProcessor> inlineProcessors,
        @NonNull List<DelimiterProcessor> delimiterProcessors) {
    this.inlineParserContext = inlineParserContext;
    this.referencesEnabled = referencesEnabled;
    this.inlineProcessors = calculateInlines(inlineProcessors);
    this.delimiterProcessors = calculateDelimiterProcessors(delimiterProcessors);
    this.specialCharacters = calculateSpecialCharacters(
            this.inlineProcessors.keySet(),
            this.delimiterProcessors.keySet());
}
 
Example #17
Source File: StaggeredDelimiterProcessor.java    From Markwon with Apache License 2.0 5 votes vote down vote up
private DelimiterProcessor findProcessor(int len) {
    for (DelimiterProcessor p : processors) {
        if (p.getMinLength() <= len) {
            return p;
        }
    }
    return processors.getFirst();
}
 
Example #18
Source File: StaggeredDelimiterProcessor.java    From 1Rramp-Android with MIT License 5 votes vote down vote up
private DelimiterProcessor findProcessor(int len) {
    for (DelimiterProcessor p : processors) {
        if (p.getMinLength() <= len) {
            return p;
        }
    }
    return processors.getFirst();
}
 
Example #19
Source File: InlineParserImpl.java    From commonmark-java with BSD 2-Clause "Simplified" License 4 votes vote down vote up
/**
 * Parse the next inline element in subject, advancing input index.
 * On success, return the new inline node.
 * On failure, return null.
 */
private Node parseInline(Node previous) {
    char c = peek();
    if (c == '\0') {
        return null;
    }

    Node node;
    switch (c) {
        case '\n':
            node = parseNewline(previous);
            break;
        case '\\':
            node = parseBackslash();
            break;
        case '`':
            node = parseBackticks();
            break;
        case '[':
            node = parseOpenBracket();
            break;
        case '!':
            node = parseBang();
            break;
        case ']':
            node = parseCloseBracket();
            break;
        case '<':
            node = parseAutolink();
            if (node == null) {
                node = parseHtmlInline();
            }
            break;
        case '&':
            node = parseEntity();
            break;
        default:
            boolean isDelimiter = delimiterCharacters.get(c);
            if (isDelimiter) {
                DelimiterProcessor delimiterProcessor = delimiterProcessors.get(c);
                node = parseDelimiters(delimiterProcessor, c);
            } else {
                node = parseString();
            }
            break;
    }
    if (node != null) {
        return node;
    } else {
        index++;
        // When we get here, it's only for a single special character that turned out to not have a special meaning.
        // So we shouldn't have a single surrogate here, hence it should be ok to turn it into a String.
        String literal = String.valueOf(c);
        return text(literal);
    }
}
 
Example #20
Source File: InlineParserImpl.java    From commonmark-java with BSD 2-Clause "Simplified" License 4 votes vote down vote up
private static void addDelimiterProcessorForChar(char delimiterChar, DelimiterProcessor toAdd, Map<Character, DelimiterProcessor> delimiterProcessors) {
    DelimiterProcessor existing = delimiterProcessors.put(delimiterChar, toAdd);
    if (existing != null) {
        throw new IllegalArgumentException("Delimiter processor conflict with delimiter char '" + delimiterChar + "'");
    }
}
 
Example #21
Source File: Parser.java    From 1Rramp-Android with MIT License 4 votes vote down vote up
@Override
public List<DelimiterProcessor> getCustomDelimiterProcessors() {
    return delimiterProcessors;
}
 
Example #22
Source File: InlineParserImpl.java    From commonmark-java with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public static Map<Character, DelimiterProcessor> calculateDelimiterProcessors(List<DelimiterProcessor> delimiterProcessors) {
    Map<Character, DelimiterProcessor> map = new HashMap<>();
    addDelimiterProcessors(Arrays.<DelimiterProcessor>asList(new AsteriskDelimiterProcessor(), new UnderscoreDelimiterProcessor()), map);
    addDelimiterProcessors(delimiterProcessors, map);
    return map;
}
 
Example #23
Source File: InlineParserContextImpl.java    From commonmark-java with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public List<DelimiterProcessor> getCustomDelimiterProcessors() {
    return delimiterProcessors;
}
 
Example #24
Source File: InlineParserContextImpl.java    From commonmark-java with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public InlineParserContextImpl(List<DelimiterProcessor> delimiterProcessors,
                               Map<String, LinkReferenceDefinition> linkReferenceDefinitions) {
    this.delimiterProcessors = delimiterProcessors;
    this.linkReferenceDefinitions = linkReferenceDefinitions;
}
 
Example #25
Source File: Parser.java    From 1Rramp-Android with MIT License 4 votes vote down vote up
CustomInlineParserContext(List<DelimiterProcessor> delimiterProcessors) {
    this.delimiterProcessors = delimiterProcessors;
}
 
Example #26
Source File: InlineParserImpl.java    From commonmark-java with BSD 2-Clause "Simplified" License 4 votes vote down vote up
/**
 * Scan a sequence of characters with code delimiterChar, and return information about the number of delimiters
 * and whether they are positioned such that they can open and/or close emphasis or strong emphasis.
 *
 * @return information about delimiter run, or {@code null}
 */
private DelimiterData scanDelimiters(DelimiterProcessor delimiterProcessor, char delimiterChar) {
    int startIndex = index;

    int delimiterCount = 0;
    while (peek() == delimiterChar) {
        delimiterCount++;
        index++;
    }

    if (delimiterCount < delimiterProcessor.getMinLength()) {
        index = startIndex;
        return null;
    }

    String before = startIndex == 0 ? "\n" :
            input.substring(startIndex - 1, startIndex);

    char charAfter = peek();
    String after = charAfter == '\0' ? "\n" :
            String.valueOf(charAfter);

    // We could be more lazy here, in most cases we don't need to do every match case.
    boolean beforeIsPunctuation = PUNCTUATION.matcher(before).matches();
    boolean beforeIsWhitespace = UNICODE_WHITESPACE_CHAR.matcher(before).matches();
    boolean afterIsPunctuation = PUNCTUATION.matcher(after).matches();
    boolean afterIsWhitespace = UNICODE_WHITESPACE_CHAR.matcher(after).matches();

    boolean leftFlanking = !afterIsWhitespace &&
            (!afterIsPunctuation || beforeIsWhitespace || beforeIsPunctuation);
    boolean rightFlanking = !beforeIsWhitespace &&
            (!beforeIsPunctuation || afterIsWhitespace || afterIsPunctuation);
    boolean canOpen;
    boolean canClose;
    if (delimiterChar == '_') {
        canOpen = leftFlanking && (!rightFlanking || beforeIsPunctuation);
        canClose = rightFlanking && (!leftFlanking || afterIsPunctuation);
    } else {
        canOpen = leftFlanking && delimiterChar == delimiterProcessor.getOpeningCharacter();
        canClose = rightFlanking && delimiterChar == delimiterProcessor.getClosingCharacter();
    }

    index = startIndex;
    return new DelimiterData(delimiterCount, canOpen, canClose);
}
 
Example #27
Source File: InlineParserImpl.java    From 1Rramp-Android with MIT License 4 votes vote down vote up
public InlineParserImpl(List<DelimiterProcessor> delimiterProcessors) {
    this.delimiterProcessors = calculateDelimiterProcessors(delimiterProcessors);
    this.delimiterCharacters = calculateDelimiterCharacters(this.delimiterProcessors.keySet());
    this.specialCharacters = calculateSpecialCharacters(delimiterCharacters);
}
 
Example #28
Source File: InlineParserImpl.java    From 1Rramp-Android with MIT License 4 votes vote down vote up
public static Map<Character, DelimiterProcessor> calculateDelimiterProcessors(List<DelimiterProcessor> delimiterProcessors) {
    Map<Character, DelimiterProcessor> map = new HashMap<>();
    addDelimiterProcessors(Arrays.<DelimiterProcessor>asList(new AsteriskDelimiterProcessor(), new UnderscoreDelimiterProcessor()), map);
    addDelimiterProcessors(delimiterProcessors, map);
    return map;
}
 
Example #29
Source File: InlineParserImpl.java    From 1Rramp-Android with MIT License 4 votes vote down vote up
private static void addDelimiterProcessorForChar(char delimiterChar, DelimiterProcessor toAdd, Map<Character, DelimiterProcessor> delimiterProcessors) {
    DelimiterProcessor existing = delimiterProcessors.put(delimiterChar, toAdd);
    if (existing != null) {
        throw new IllegalArgumentException("Delimiter processor conflict with delimiter char '" + delimiterChar + "'");
    }
}
 
Example #30
Source File: MarkwonInlineParser.java    From Markwon with Apache License 2.0 4 votes vote down vote up
@NonNull
@Override
public FactoryBuilder addDelimiterProcessor(@NonNull DelimiterProcessor processor) {
    this.delimiterProcessors.add(processor);
    return this;
}