Skip to content

Commit

Permalink
Remove SourcePosition from API for now (#1)
Browse files Browse the repository at this point in the history
This is probably not the right data structure, and it's untested. The
option for the renderer was already removed earlier.

We can always resurrect the code when it's more clear what we want.
  • Loading branch information
robinst committed Jul 28, 2015
1 parent 9cf5e94 commit 80e22b3
Show file tree
Hide file tree
Showing 16 changed files with 23 additions and 147 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import org.commonmark.node.Block;
import org.commonmark.node.Node;
import org.commonmark.node.SourcePosition;
import org.commonmark.parser.InlineParser;
import org.commonmark.parser.block.*;

Expand All @@ -20,9 +19,8 @@ public class TableBlockParser extends AbstractBlockParser {
private boolean nextIsSeparatorLine = true;
private String separatorLine = "";

private TableBlockParser(CharSequence headerLine, SourcePosition sourcePosition) {
private TableBlockParser(CharSequence headerLine) {
rowLines.add(headerLine);
block.setSourcePosition(sourcePosition);
}

@Override
Expand Down Expand Up @@ -160,8 +158,7 @@ public BlockStart tryStart(ParserState state, MatchedBlockParser matchedBlockPar
List<String> headParts = split(paragraphStartLine);
List<String> separatorParts = split(separatorLine);
if (separatorParts.size() >= headParts.size()) {
SourcePosition sourcePosition = state.getActiveBlockParser().getBlock().getSourcePosition();
return BlockStart.of(new TableBlockParser(paragraphStartLine, sourcePosition))
return BlockStart.of(new TableBlockParser(paragraphStartLine))
.atIndex(state.getIndex())
.replaceActiveBlockParser();
}
Expand Down
13 changes: 0 additions & 13 deletions commonmark/src/main/java/org/commonmark/html/HtmlRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,13 @@ public class HtmlRenderer {

private final String softbreak;
private final boolean escapeHtml;
private final boolean sourcepos;
private final boolean percentEncodeUrls;
private final List<CustomHtmlRenderer> customHtmlRenderers;
private final List<AttributeProvider> attributeProviders;

private HtmlRenderer(Builder builder) {
this.softbreak = builder.softbreak;
this.escapeHtml = builder.escapeHtml;
this.sourcepos = builder.sourcepos;
this.percentEncodeUrls = builder.percentEncodeUrls;
this.customHtmlRenderers = builder.customHtmlRenderers;
this.attributeProviders = builder.attributeProviders;
Expand Down Expand Up @@ -61,7 +59,6 @@ private String optionallyPercentEncodeUrl(String url) {
public static class Builder {

private String softbreak = "\n";
private boolean sourcepos = false;
private boolean escapeHtml = false;
private boolean percentEncodeUrls = false;
private List<CustomHtmlRenderer> customHtmlRenderers = new ArrayList<>();
Expand Down Expand Up @@ -388,16 +385,6 @@ private Map<String, String> getAttrs(Node node) {

private Map<String, String> getAttrs(Node node, Map<String, String> defaultAttributes) {
Map<String, String> attrs = new LinkedHashMap<>(defaultAttributes);
if (sourcepos && node instanceof Block) {
Block block = (Block) node;
SourcePosition pos = block.getSourcePosition();
if (pos != null) {
attrs.put("data-sourcepos",
"" + pos.getStartLine() + ':' +
pos.getStartColumn() + '-' + pos.getEndLine() + ':' +
pos.getEndColumn());
}
}
setCustomAttributes(node, attrs);
return attrs;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,12 @@

import org.commonmark.node.Block;
import org.commonmark.node.BlockQuote;
import org.commonmark.node.SourcePosition;
import org.commonmark.parser.block.*;

public class BlockQuoteParser extends AbstractBlockParser {

private final BlockQuote block = new BlockQuote();

public BlockQuoteParser(SourcePosition pos) {
block.setSourcePosition(pos);
}

@Override
public boolean isContainer() {
return true;
Expand Down Expand Up @@ -53,7 +48,7 @@ public BlockStart tryStart(ParserState state, MatchedBlockParser matchedBlockPar
if (newOffset < line.length() && line.charAt(newOffset) == ' ') {
newOffset++;
}
return BlockStart.of(new BlockQuoteParser(pos(state, nextNonSpace))).atIndex(newOffset);
return BlockStart.of(new BlockQuoteParser()).atIndex(newOffset);
} else {
return BlockStart.none();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ public class DocumentParser implements ParserState {
new IndentedCodeBlockParser.Factory());

private CharSequence line;
/**
* 1-based line number
*/
private int lineNumber = 0;

/**
* current index (offset) in input line
Expand All @@ -40,8 +36,6 @@ public class DocumentParser implements ParserState {

private int indent = 0;

private int lastLineLength = 0;

private final List<BlockParserFactory> blockParserFactories;
private final InlineParserImpl inlineParser;

Expand All @@ -66,7 +60,6 @@ public static List<BlockParserFactory> calculateBlockParserFactories(List<BlockP
*/
public Document parse(String input) {
DocumentBlockParser documentBlockParser = new DocumentBlockParser();
documentBlockParser.getBlock().setSourcePosition(new SourcePosition(1, 1));
activateBlockParser(documentBlockParser);

int lineStart = 0;
Expand All @@ -84,7 +77,7 @@ public Document parse(String input) {
incorporateLine(Substring.of(input, lineStart, input.length()));
}

finalizeBlocks(activeBlockParsers, lineNumber);
finalizeBlocks(activeBlockParsers);
this.processInlines();
return documentBlockParser.getBlock();
}
Expand Down Expand Up @@ -119,11 +112,6 @@ public boolean isBlank() {
return blank;
}

@Override
public int getLineNumber() {
return lineNumber;
}

@Override
public BlockParser getActiveBlockParser() {
return activeBlockParsers.get(activeBlockParsers.size() - 1);
Expand All @@ -139,7 +127,6 @@ private void incorporateLine(CharSequence ln) {
column = 0;
nextNonSpace = 0;
nextNonSpaceColumn = 0;
lineNumber += 1;

// For each containing block, try to parse the associated line start.
// Bail out on failure: container will point to the last matching block.
Expand All @@ -153,8 +140,7 @@ private void incorporateLine(CharSequence ln) {
if (result instanceof BlockContinueImpl) {
BlockContinueImpl blockContinue = (BlockContinueImpl) result;
if (blockContinue.isFinalize()) {
finalize(blockParser, this.lineNumber);
lastLineLength = line.length() - 1; // -1 for newline
finalize(blockParser);
return;
} else {
if (blockContinue.getNewIndex() != -1) {
Expand Down Expand Up @@ -240,11 +226,10 @@ private void incorporateLine(CharSequence ln) {
addLine();
} else if (!isBlank()) {
// create paragraph container for line
addChild(new ParagraphParser(new SourcePosition(this.lineNumber, nextNonSpace + 1)));
addChild(new ParagraphParser());
addLine();
}
}
this.lastLineLength = ln.length() - 1; // -1 for newline
}

private void findNextNonSpace() {
Expand Down Expand Up @@ -322,17 +307,11 @@ private BlockStartImpl findBlockStart(BlockParser blockParser) {
* setting the 'tight' or 'loose' status of a list, and parsing the beginnings of paragraphs for reference
* definitions.
*/
private void finalize(BlockParser blockParser, int lineNumber) {
private void finalize(BlockParser blockParser) {
if (getActiveBlockParser() == blockParser) {
deactivateBlockParser();
}

// TODO: Maybe this should be done in the block parser instead?
Block block = blockParser.getBlock();
SourcePosition pos = block.getSourcePosition();
block.setSourcePosition(new SourcePosition(pos.getStartLine(), pos.getStartColumn(),
lineNumber, this.lastLineLength + 1));

blockParser.closeBlock();

if (blockParser instanceof ParagraphParser) {
Expand Down Expand Up @@ -421,7 +400,7 @@ private void addLine() {
*/
private <T extends BlockParser> T addChild(T blockParser) {
while (!getActiveBlockParser().canContain(blockParser.getBlock())) {
this.finalize(getActiveBlockParser(), this.lineNumber - 1);
finalize(getActiveBlockParser());
}

getActiveBlockParser().getBlock().appendChild(blockParser.getBlock());
Expand Down Expand Up @@ -486,15 +465,11 @@ private boolean isLastLineBlank(Node node) {
* Finalize blocks of previous line. Returns true.
*/
private boolean finalizeBlocks(List<BlockParser> blockParsers) {
finalizeBlocks(blockParsers, lineNumber - 1);
return true;
}

private void finalizeBlocks(List<BlockParser> blockParsers, int lineNumber) {
for (int i = blockParsers.size() - 1; i >= 0; i--) {
BlockParser blockParser = blockParsers.get(i);
finalize(blockParser, lineNumber);
finalize(blockParser);
}
return true;
}

private static class MatchedBlockParserImpl implements MatchedBlockParser {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import org.commonmark.node.Block;
import org.commonmark.node.FencedCodeBlock;
import org.commonmark.node.SourcePosition;
import org.commonmark.parser.block.*;

import java.util.regex.Matcher;
Expand All @@ -18,11 +17,10 @@ public class FencedCodeBlockParser extends AbstractBlockParser {
private final FencedCodeBlock block = new FencedCodeBlock();
private BlockContent content = new BlockContent();

public FencedCodeBlockParser(char fenceChar, int fenceLength, int fenceIndent, SourcePosition pos) {
public FencedCodeBlockParser(char fenceChar, int fenceLength, int fenceIndent) {
block.setFenceChar(fenceChar);
block.setFenceLength(fenceLength);
block.setFenceIndent(fenceIndent);
block.setSourcePosition(pos);
}

@Override
Expand Down Expand Up @@ -90,7 +88,7 @@ public BlockStart tryStart(ParserState state, MatchedBlockParser matchedBlockPar
if (state.getIndent() < 4 && (matcher = OPENING_FENCE.matcher(line.subSequence(nextNonSpace, line.length()))).find()) {
int fenceLength = matcher.group(0).length();
char fenceChar = matcher.group(0).charAt(0);
FencedCodeBlockParser blockParser = new FencedCodeBlockParser(fenceChar, fenceLength, state.getIndent(), pos(state, nextNonSpace));
FencedCodeBlockParser blockParser = new FencedCodeBlockParser(fenceChar, fenceLength, state.getIndent());
return BlockStart.of(blockParser).atIndex(nextNonSpace + fenceLength);
} else {
return BlockStart.none();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import org.commonmark.node.Block;
import org.commonmark.node.Header;
import org.commonmark.node.SourcePosition;
import org.commonmark.parser.InlineParser;
import org.commonmark.parser.block.*;

Expand All @@ -18,9 +17,8 @@ public class HeaderParser extends AbstractBlockParser {
private final Header block = new Header();
private final String content;

public HeaderParser(int level, String content, SourcePosition pos) {
public HeaderParser(int level, String content) {
block.setLevel(level);
block.setSourcePosition(pos);
this.content = content;
}

Expand Down Expand Up @@ -57,7 +55,7 @@ public BlockStart tryStart(ParserState state, MatchedBlockParser matchedBlockPar
int level = matcher.group(0).trim().length(); // number of #s
// remove trailing ###s:
String content = ATX_TRAILING.matcher(line.subSequence(newOffset, line.length())).replaceAll("");
return BlockStart.of(new HeaderParser(level, content, pos(state, nextNonSpace)))
return BlockStart.of(new HeaderParser(level, content))
.atIndex(line.length());

} else if (paragraphStartLine != null &&
Expand All @@ -66,7 +64,7 @@ public BlockStart tryStart(ParserState state, MatchedBlockParser matchedBlockPar

int level = matcher.group(0).charAt(0) == '=' ? 1 : 2;
String content = paragraphStartLine.toString();
return BlockStart.of(new HeaderParser(level, content, state.getActiveBlockParser().getBlock().getSourcePosition()))
return BlockStart.of(new HeaderParser(level, content))
.atIndex(line.length())
.replaceActiveBlockParser();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import org.commonmark.node.Block;
import org.commonmark.node.HorizontalRule;
import org.commonmark.node.SourcePosition;
import org.commonmark.parser.block.*;

import java.util.regex.Pattern;
Expand All @@ -13,10 +12,6 @@ public class HorizontalRuleParser extends AbstractBlockParser {

private final HorizontalRule block = new HorizontalRule();

public HorizontalRuleParser(SourcePosition pos) {
block.setSourcePosition(pos);
}

@Override
public Block getBlock() {
return block;
Expand All @@ -38,7 +33,7 @@ public BlockStart tryStart(ParserState state, MatchedBlockParser matchedBlockPar
int nextNonSpace = state.getNextNonSpaceIndex();
CharSequence line = state.getLine();
if (H_RULE.matcher(line.subSequence(nextNonSpace, line.length())).matches()) {
return BlockStart.of(new HorizontalRuleParser(pos(state, nextNonSpace))).atIndex(line.length());
return BlockStart.of(new HorizontalRuleParser()).atIndex(line.length());
} else {
return BlockStart.none();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import org.commonmark.node.Block;
import org.commonmark.node.HtmlBlock;
import org.commonmark.node.Paragraph;
import org.commonmark.node.SourcePosition;
import org.commonmark.parser.block.*;

import java.util.regex.Pattern;
Expand Down Expand Up @@ -64,9 +63,8 @@ public class HtmlBlockParser extends AbstractBlockParser {
private boolean finished = false;
private BlockContent content = new BlockContent();

private HtmlBlockParser(SourcePosition pos, Pattern closingPattern) {
private HtmlBlockParser(Pattern closingPattern) {
this.closingPattern = closingPattern;
block.setSourcePosition(pos);
}

@Override
Expand Down Expand Up @@ -120,7 +118,7 @@ public BlockStart tryStart(ParserState state, MatchedBlockParser matchedBlockPar
Pattern closer = BLOCK_PATTERNS[blockType][1];
boolean matches = opener.matcher(line.subSequence(nextNonSpace, line.length())).find();
if (matches) {
return BlockStart.of(new HtmlBlockParser(pos(state, nextNonSpace), closer)).atIndex(state.getIndex());
return BlockStart.of(new HtmlBlockParser(closer)).atIndex(state.getIndex());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ public class IndentedCodeBlockParser extends AbstractBlockParser {
private final IndentedCodeBlock block = new IndentedCodeBlock();
private BlockContent content = new BlockContent();

public IndentedCodeBlockParser(SourcePosition pos) {
block.setSourcePosition(pos);
}

@Override
public Block getBlock() {
return block;
Expand Down Expand Up @@ -57,7 +53,7 @@ public BlockStart tryStart(ParserState state, MatchedBlockParser matchedBlockPar
// An indented code block cannot interrupt a paragraph.
if (state.getIndent() >= INDENT && !state.isBlank() && !(state.getActiveBlockParser().getBlock() instanceof Paragraph)) {
int nextNonSpace = state.getNextNonSpaceIndex();
return BlockStart.of(new IndentedCodeBlockParser(pos(state, nextNonSpace))).atColumn(state.getColumn() + INDENT);
return BlockStart.of(new IndentedCodeBlockParser()).atColumn(state.getColumn() + INDENT);
} else {
return BlockStart.none();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ public class ListBlockParser extends AbstractBlockParser {

private final ListBlock block;

public ListBlockParser(ListBlock block, SourcePosition pos) {
public ListBlockParser(ListBlock block) {
this.block = block;
block.setSourcePosition(pos);
}

@Override
Expand Down Expand Up @@ -111,13 +110,13 @@ public BlockStart tryStart(ParserState state, MatchedBlockParser matchedBlockPar
int newIndex = nextNonSpace + listData.padding;

int itemIndent = state.getIndent() + listData.padding;
ListItemParser listItemParser = new ListItemParser(itemIndent, pos(state, nextNonSpace));
ListItemParser listItemParser = new ListItemParser(itemIndent);

// prepend the list block if needed
if (!(matched instanceof ListBlockParser) ||
!(listsMatch((ListBlock) matched.getBlock(), listData.listBlock))) {

ListBlockParser listBlockParser = new ListBlockParser(listData.listBlock, pos(state, nextNonSpace));
ListBlockParser listBlockParser = new ListBlockParser(listData.listBlock);
listBlockParser.setTight(true);

return BlockStart.of(listBlockParser, listItemParser).atIndex(newIndex);
Expand Down
Loading

0 comments on commit 80e22b3

Please sign in to comment.