Skip to content

Commit

Permalink
Introduce markdown support
Browse files Browse the repository at this point in the history
  • Loading branch information
adamvoss committed Jun 3, 2017
1 parent c5fbbfd commit cfb414d
Show file tree
Hide file tree
Showing 6 changed files with 173 additions and 1 deletion.
32 changes: 32 additions & 0 deletions ACKNOWLEDGMENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -491,3 +491,35 @@ Redistribution and use in source and binary forms, with or without modification,
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
```

## License notice for flexmark-java

[flexmark-java](https://github.com/vsch/flexmark-java) is licensed under a 2-clause BSD license.

```
Copyright (c) 2015-2016, Atlassian Pty Ltd
All rights reserved.
Copyright (c) 2016, Vladimir Schneider,
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
```
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ dependencies {

// This dependency is found on compile classpath of this component and consumers.
compile group: 'org.eclipse.lsp4j', name: 'org.eclipse.lsp4j', version: '0.2.0.M7'

compile 'com.vladsch.flexmark:flexmark-all:0.19.1'
}

// Define the main class for the application
Expand Down
23 changes: 22 additions & 1 deletion src/main/java/LanguageToolLanguageServer.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import com.vladsch.flexmark.ast.Document;
import com.vladsch.flexmark.parser.Parser;
import markdown.AnnotatedTextBuildingVisitor;
import org.eclipse.lsp4j.*;
import org.eclipse.lsp4j.services.*;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -133,8 +136,26 @@ private List<RuleMatch> validateDocument(TextDocumentItem document) {
return Collections.emptyList();
} else {
JLanguageTool languageTool = new JLanguageTool(language);

String languageId = document.getLanguageId();
try {
return languageTool.check(document.getText());
switch (languageId) {
case "plaintext": {
return languageTool.check(document.getText());
}
case "markdown": {
Parser p = Parser.builder().build();
Document mdDocument = (Document) p.parse(document.getText());

AnnotatedTextBuildingVisitor builder = new AnnotatedTextBuildingVisitor();
builder.visit(mdDocument);

return languageTool.check(builder.getText());
}
default: {
throw new UnsupportedOperationException(String.format("Language, %s, is not supported.", languageId));
}
}
} catch (IOException e) {
e.printStackTrace();
return Collections.emptyList();
Expand Down
73 changes: 73 additions & 0 deletions src/main/java/markdown/AnnotatedTextBuildingVisitor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package markdown;

import com.vladsch.flexmark.ast.Document;
import com.vladsch.flexmark.ast.Node;
import com.vladsch.flexmark.ast.Paragraph;
import com.vladsch.flexmark.ast.Text;
import com.vladsch.flexmark.util.sequence.BasedSequence;
import org.languagetool.markup.AnnotatedText;
import org.languagetool.markup.AnnotatedTextBuilder;

public class AnnotatedTextBuildingVisitor {
private AnnotatedTextBuilder builder = new AnnotatedTextBuilder();

private int idx = 0;

private boolean inParagraph = false;

public void visit(Document node) {
visitChildren(node);
}

private void visitChildren(final Node parent) {
parent.getChildren().forEach(this::visit);
}

private void visit(Node node) {
if (node.getClass() == Paragraph.class) {
BasedSequence originalText = node.getChars().getBaseSequence();
BasedSequence passedOver = originalText.subSequence(idx, node.getStartOffset());

if (inParagraph) {
throw new UnsupportedOperationException("Nested paragraphs are not supported");
}

processProceedingCharacters(passedOver);

idx = node.getStartOffset();

inParagraph = true;
visitChildren(node);
inParagraph = false;
} else if (node.getClass() == Text.class) {
BasedSequence originalText = node.getChars().getBaseSequence();
BasedSequence passedOver = originalText.subSequence(idx, node.getStartOffset());

processProceedingCharacters(passedOver);

idx = node.getEndOffset();

builder.addText(node.getChars().toString());
} else {
visitChildren(node);
}
}

private void processProceedingCharacters(BasedSequence passedOver) {
for (char c : passedOver.toString().toCharArray()) {
if (c == '\r' || c == '\n') {
if (inParagraph) {
builder.addText(" ");
} else {
builder.addText(new String(new char[]{c}));
}
} else {
builder.addMarkup(new String(new char[]{c}));
}
}
}

public AnnotatedText getText() {
return builder.build();
}
}
27 changes: 27 additions & 0 deletions src/test/java/markdown/AnnotatedTextBuildingVisitorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package markdown;

import com.vladsch.flexmark.ast.Document;
import com.vladsch.flexmark.ast.Node;
import com.vladsch.flexmark.parser.Parser;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.languagetool.markup.AnnotatedText;

import java.io.IOException;

public class AnnotatedTextBuildingVisitorTest {
@Test
void test() throws IOException {
Parser p = Parser.builder().build();

Node document = p.parse("# Heading\n" +
"Paragraph with\n" +
"multiple lines and [link](example.com)");

AnnotatedTextBuildingVisitor visitor = new AnnotatedTextBuildingVisitor();
visitor.visit((Document) document);
AnnotatedText text = visitor.getText();

Assertions.assertEquals("Heading\nParagraph with multiple lines and link", text.getPlainText());
}
}
17 changes: 17 additions & 0 deletions src/test/java/markdown/PrintAstVisitor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package markdown;

import com.vladsch.flexmark.ast.Node;

class PrintAstVisitor {
public void visit(Node node, int indent) {
String i = "";

for (int j = 0; j < indent; j++) {
i += " ";
}

System.out.println(i + node.toAstString(true));

node.getChildIterator().forEachRemaining(n -> this.visit(n, indent + 1));
}
}

0 comments on commit cfb414d

Please sign in to comment.