Skip to content

Commit

Permalink
Changed parsing library to flexmark
Browse files Browse the repository at this point in the history
Signed-off-by: Kristina S. Simova <kssimovaa@gmail.com>
  • Loading branch information
kssimova committed Jan 22, 2018
1 parent da1a8b5 commit 755071c
Show file tree
Hide file tree
Showing 17 changed files with 378 additions and 348 deletions.
6 changes: 3 additions & 3 deletions custom-checks/checkstyle/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@
</dependency>

<dependency>
<groupId>com.atlassian.commonmark</groupId>
<artifactId>commonmark</artifactId>
<version>${commonmark.version}</version>
<groupId>com.vladsch.flexmark</groupId>
<artifactId>flexmark-all</artifactId>
<version>${flexmark.version}</version>
</dependency>
<dependency>
<groupId>org.apache.ivy</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import java.text.MessageFormat;
import java.text.ParseException;
import java.util.Properties;
import java.util.Set;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
Expand All @@ -31,9 +30,6 @@
import org.apache.commons.logging.LogFactory;
import org.apache.ivy.osgi.core.BundleInfo;
import org.apache.ivy.osgi.core.ManifestParser;
import org.commonmark.node.Block;
import org.commonmark.node.Node;
import org.commonmark.parser.Parser;
import org.eclipse.core.internal.filebuffers.SynchronizableDocument;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.text.IDocument;
Expand All @@ -47,6 +43,9 @@
import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
import com.puppycrawl.tools.checkstyle.api.FileText;
import com.puppycrawl.tools.checkstyle.api.MessageDispatcher;
import com.vladsch.flexmark.ast.Node;
import com.vladsch.flexmark.parser.Parser;
import com.vladsch.flexmark.util.options.MutableDataSet;

/**
* Provides common functionality for different static code analysis checks
Expand Down Expand Up @@ -256,11 +255,11 @@ protected void logMessage(String filePath, int line, String fileName, String mes
* Parsed the content of a markdown file.
*
* @param fileText - Represents the text contents of a file
* @param blockTypes - the enabled block types
* @param parsingOptions - parsing options
* @return The markdown node
*/
protected Node parseMarkdown(FileText fileText, Set<Class<? extends Block>> blockTypes) {
Parser parser = Parser.builder().enabledBlockTypes(blockTypes).build();
protected Node parseMarkdown(FileText fileText, MutableDataSet parsingOptions) {
Parser parser = Parser.builder(parsingOptions).build();
return parser.parse(fileText.getFullText().toString());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,16 @@
import static org.openhab.tools.analysis.checkstyle.api.CheckConstants.README_MD_FILE_NAME;

import java.io.File;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

import org.commonmark.node.Block;
import org.commonmark.node.FencedCodeBlock;
import org.commonmark.node.Heading;
import org.commonmark.node.IndentedCodeBlock;
import org.commonmark.node.ListBlock;
import org.commonmark.node.Node;
import org.eclipse.pde.core.build.IBuild;
import org.eclipse.pde.core.build.IBuildEntry;
import org.openhab.tools.analysis.checkstyle.api.AbstractStaticCheck;
import org.openhab.tools.analysis.checkstyle.api.NoResultException;

import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
import com.puppycrawl.tools.checkstyle.api.FileText;
import com.vladsch.flexmark.ast.Node;
import com.vladsch.flexmark.parser.Parser;
import com.vladsch.flexmark.util.options.MutableDataSet;

/**
* Checks the README.md files for:
Expand All @@ -51,6 +44,7 @@
* info</a>
*
* @author Erdoan Hadzhiyusein - Initial contribution
* @author Lyubomir Papazov - Change the Markdown parser to flexmark
*/
public class MarkdownCheck extends AbstractStaticCheck {
private static final String ADDED_README_FILE_IN_BUILD_PROPERTIES_MSG = "README.MD file must not be added to the bin.includes property";
Expand Down Expand Up @@ -90,25 +84,21 @@ private void checkBuildProperties(FileText fileText) throws CheckstyleException
}

private void checkReadMe(FileText fileText) {
// Don't need all block types visited that's why only these are enabled
Set<Class<? extends Block>> enabledBlockTypes = new HashSet<>(
Arrays.asList(Heading.class, ListBlock.class, FencedCodeBlock.class, IndentedCodeBlock.class));
Node readmeMarkdownNode = parseMarkdown(fileText, enabledBlockTypes);
// CallBack is used in order to use the protected methods of the AbstractStaticCheck in the Visitor
MarkdownVisitorCallback callBack = new MarkdownVisitorCallback() {
@Override
public int findLineNumber(FileText fileContent, String searchedText, int startLineNumber)
throws NoResultException {
return MarkdownCheck.this.findLineNumber(fileText, searchedText, startLineNumber);
}

MutableDataSet options = new MutableDataSet();
// By setting this option to true, the parser provides line numbers in the original markdown text for each node
options.set(Parser.TRACK_DOCUMENT_LINES, true);

Node readmeMarkdownNode = parseMarkdown(fileText, options);
// CallBack is used in order to use the protected log method of the AbstractStaticCheck in the Visitor
MarkdownVisitorCallback callBack = new MarkdownVisitorCallback() {
@Override
public void log(int line, String message) {
MarkdownCheck.this.log(line, message);
MarkdownCheck.this.log(line + 1, message);
}
};
MarkdownVisitor visitor = new MarkdownVisitor(callBack, fileText);
readmeMarkdownNode.accept(visitor);
visitor.visit(readmeMarkdownNode);
}

/**
Expand Down

This file was deleted.

Loading

0 comments on commit 755071c

Please sign in to comment.