Skip to content

Commit

Permalink
Extract DefinitionMap as a public class
Browse files Browse the repository at this point in the history
  • Loading branch information
robinst committed May 19, 2024
1 parent f30c787 commit 04ba63f
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

import org.commonmark.internal.util.Parsing;
import org.commonmark.node.*;
import org.commonmark.parser.*;
import org.commonmark.parser.IncludeSourceSpans;
import org.commonmark.parser.InlineParserFactory;
import org.commonmark.parser.SourceLine;
import org.commonmark.parser.SourceLines;
import org.commonmark.parser.beta.BracketProcessor;
import org.commonmark.parser.beta.InlineContentParserFactory;
import org.commonmark.parser.block.*;
Expand Down Expand Up @@ -73,7 +76,7 @@ public class DocumentParser implements ParserState {
private final List<BracketProcessor> bracketProcessors;
private final IncludeSourceSpans includeSourceSpans;
private final DocumentBlockParser documentBlockParser;
private final LinkReferenceDefinitions definitions = new LinkReferenceDefinitions();
private final DefinitionMap<LinkReferenceDefinition> linkReferenceDefinitions = new DefinitionMap<>();

private final List<OpenBlockParser> openBlockParsers = new ArrayList<>();
private final List<BlockParser> allBlockParsers = new ArrayList<>();
Expand Down Expand Up @@ -472,19 +475,22 @@ private void finalize(BlockParser blockParser) {
}

private void addDefinitionsFrom(ParagraphParser paragraphParser) {
// TODO: Generalize this allow block parsers to add definitions by their types.
// We'll keep a map for each type, e.g. one for LinkReferenceDefinition, one for FootnoteDefinition, etc :)
// The context then allows lookup with the type and label
for (LinkReferenceDefinition definition : paragraphParser.getDefinitions()) {
// Add nodes into document before paragraph.
paragraphParser.getBlock().insertBefore(definition);

definitions.add(definition);
linkReferenceDefinitions.putIfAbsent(definition.getLabel(), definition);
}
}

/**
* Walk through a block & children recursively, parsing string content into inline content where appropriate.
*/
private void processInlines() {
var context = new InlineParserContextImpl(inlineContentParserFactories, delimiterProcessors, bracketProcessors, definitions);
var context = new InlineParserContextImpl(inlineContentParserFactories, delimiterProcessors, bracketProcessors, linkReferenceDefinitions);
var inlineParser = inlineParserFactory.create(context);

for (var blockParser : allBlockParsers) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.commonmark.internal;

import org.commonmark.node.DefinitionMap;
import org.commonmark.node.LinkReferenceDefinition;
import org.commonmark.parser.InlineParserContext;
import org.commonmark.parser.beta.BracketProcessor;
Expand All @@ -13,12 +14,12 @@ public class InlineParserContextImpl implements InlineParserContext {
private final List<InlineContentParserFactory> inlineContentParserFactories;
private final List<DelimiterProcessor> delimiterProcessors;
private final List<BracketProcessor> bracketProcessors;
private final LinkReferenceDefinitions linkReferenceDefinitions;
private final DefinitionMap<LinkReferenceDefinition> linkReferenceDefinitions;

public InlineParserContextImpl(List<InlineContentParserFactory> inlineContentParserFactories,
List<DelimiterProcessor> delimiterProcessors,
List<BracketProcessor> bracketProcessors,
LinkReferenceDefinitions linkReferenceDefinitions) {
DefinitionMap<LinkReferenceDefinition> linkReferenceDefinitions) {
this.inlineContentParserFactories = inlineContentParserFactories;
this.delimiterProcessors = delimiterProcessors;
this.bracketProcessors = bracketProcessors;
Expand Down

This file was deleted.

38 changes: 38 additions & 0 deletions commonmark/src/main/java/org/commonmark/node/DefinitionMap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.commonmark.node;

import org.commonmark.internal.util.Escaping;

import java.util.LinkedHashMap;
import java.util.Map;

/**
* A map that can be used to store and lookup reference definitions by a label. The labels are case-insensitive and
* normalized, the same way as for {@link LinkReferenceDefinition} nodes.
*
* @param <V> the type of value
*/
public class DefinitionMap<V> {

// LinkedHashMap for determinism and to preserve document order
private final Map<String, V> definitions = new LinkedHashMap<>();

/**
* Store a new definition unless one is already in the map.
*/
public void putIfAbsent(String label, V definition) {
String normalizedLabel = Escaping.normalizeLabelContent(label);

// spec: When there are multiple matching link reference definitions, the first is used
definitions.putIfAbsent(normalizedLabel, definition);
}

/**
* Lookup a definition by normalized label.
*
* @return the value or null
*/
public V get(String label) {
String normalizedLabel = Escaping.normalizeLabelContent(label);
return definitions.get(normalizedLabel);
}
}
3 changes: 1 addition & 2 deletions commonmark/src/main/java/org/commonmark/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import org.commonmark.internal.DocumentParser;
import org.commonmark.internal.InlineParserContextImpl;
import org.commonmark.internal.InlineParserImpl;
import org.commonmark.internal.LinkReferenceDefinitions;
import org.commonmark.node.*;
import org.commonmark.parser.beta.BracketProcessor;
import org.commonmark.parser.beta.InlineContentParserFactory;
Expand Down Expand Up @@ -50,7 +49,7 @@ private Parser(Builder builder) {
// Try to construct an inline parser. Invalid configuration might result in an exception, which we want to
// detect as soon as possible.
var context = new InlineParserContextImpl(
inlineContentParserFactories, delimiterProcessors, bracketProcessors, new LinkReferenceDefinitions());
inlineContentParserFactories, delimiterProcessors, bracketProcessors, new DefinitionMap<>());
this.inlineParserFactory.create(context);
}

Expand Down

0 comments on commit 04ba63f

Please sign in to comment.