-
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
271 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
ltexls/src/main/java/org/bsplines/ltexls/parsing/html/HtmlAnnotatedTextBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
/* Copyright (C) 2020 Julian Valentin, LTeX Development Community | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
package org.bsplines.ltexls.parsing.html; | ||
|
||
import com.ctc.wstx.api.WstxInputProperties; | ||
import java.io.StringReader; | ||
import java.util.Stack; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
import javax.xml.stream.XMLInputFactory; | ||
import javax.xml.stream.XMLStreamException; | ||
import javax.xml.stream.XMLStreamReader; | ||
import org.bsplines.ltexls.parsing.CodeAnnotatedTextBuilder; | ||
import org.bsplines.ltexls.tools.Tools; | ||
|
||
public class HtmlAnnotatedTextBuilder extends CodeAnnotatedTextBuilder { | ||
private static final Pattern whitespacePattern = Pattern.compile(" *\r?\n *"); | ||
|
||
private XMLInputFactory xmlInputFactory; | ||
|
||
public HtmlAnnotatedTextBuilder(String codeLanguageId) { | ||
super(codeLanguageId); | ||
|
||
this.xmlInputFactory = XMLInputFactory.newInstance(); | ||
this.xmlInputFactory.setProperty(WstxInputProperties.P_MIN_TEXT_SEGMENT, 1); | ||
this.xmlInputFactory.setProperty(WstxInputProperties.P_TREAT_CHAR_REFS_AS_ENTS, true); | ||
this.xmlInputFactory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, true); | ||
this.xmlInputFactory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false); | ||
this.xmlInputFactory.setProperty(XMLInputFactory.IS_VALIDATING, false); | ||
this.xmlInputFactory.setProperty(XMLInputFactory.SUPPORT_DTD, false); | ||
} | ||
|
||
@Override | ||
public CodeAnnotatedTextBuilder addCode(String code) { | ||
int pos = 0; | ||
Stack<String> elementNameStack = new Stack<>(); | ||
elementNameStack.push("html"); | ||
String nextText = ""; | ||
|
||
try { | ||
XMLStreamReader xmlStreamReader = | ||
this.xmlInputFactory.createXMLStreamReader(new StringReader(code)); | ||
|
||
while (xmlStreamReader.hasNext()) { | ||
int eventType = xmlStreamReader.next(); | ||
int oldPos = pos; | ||
pos = xmlStreamReader.getLocation().getCharacterOffset(); | ||
String skippedCode = code.substring(oldPos, pos); | ||
String interpretAs = ""; | ||
|
||
Tools.logger.finest("Position " + pos + " (" + xmlStreamReader.getLocation().getLineNumber() | ||
+ "," + xmlStreamReader.getLocation().getColumnNumber() + "): Event type = " | ||
+ eventType + ", skippedCode = '" + skippedCode + "'"); | ||
|
||
if (!nextText.isEmpty()) { | ||
if (nextText.equals(skippedCode)) { | ||
addTextWithWhitespace(nextText); | ||
} else { | ||
addMarkup(skippedCode, nextText); | ||
} | ||
|
||
skippedCode = ""; | ||
nextText = ""; | ||
} | ||
|
||
if (eventType == XMLStreamReader.START_ELEMENT) { | ||
String elementName = xmlStreamReader.getLocalName(); | ||
elementNameStack.push(elementName); | ||
Tools.logger.finest("START_ELEMENT: elementName = '" + xmlStreamReader.getLocalName() | ||
+ "'"); | ||
|
||
if ((elementName == "body") || (elementName == "div") | ||
|| (elementName == "h1") || (elementName == "h2") || (elementName == "h3") | ||
|| (elementName == "h4") || (elementName == "h5") || (elementName == "h6") | ||
|| (elementName == "p") || (elementName == "table") || (elementName == "tr")) { | ||
interpretAs += "\n\n"; | ||
} else if ((elementName == "br") || (elementName == "li")) { | ||
interpretAs += "\n"; | ||
} | ||
} else if (eventType == XMLStreamReader.END_ELEMENT) { | ||
Tools.logger.finest("END_ELEMENT"); | ||
if (!elementNameStack.isEmpty()) elementNameStack.pop(); | ||
} else if (eventType == XMLStreamReader.CHARACTERS) { | ||
String elementName = (elementNameStack.isEmpty() ? "" : elementNameStack.peek()); | ||
String text = xmlStreamReader.getText(); | ||
Tools.logger.finest("CHARACTERS: text = '" + text + "'"); | ||
if ((elementName != "script") && (elementName != "style")) nextText = text; | ||
} else if (eventType == XMLStreamReader.ENTITY_REFERENCE) { | ||
nextText = xmlStreamReader.getText(); | ||
Tools.logger.finest("ENTITY_REFERENCE: text = '" + nextText + "'"); | ||
} | ||
|
||
addMarkup(skippedCode, interpretAs); | ||
} | ||
} catch (XMLStreamException e) { | ||
// ignore parser errors | ||
} | ||
|
||
if (pos < code.length()) addTextWithWhitespace(code.substring(pos)); | ||
|
||
return this; | ||
} | ||
|
||
protected CodeAnnotatedTextBuilder addTextWithWhitespace(String text) { | ||
Matcher matcher = whitespacePattern.matcher(text); | ||
int pos = 0; | ||
|
||
while (matcher.find()) { | ||
if (matcher.start() > 0) addText(text.substring(pos, matcher.start())); | ||
addMarkup(matcher.group()); | ||
pos = matcher.end(); | ||
} | ||
|
||
if (pos < text.length()) addText(text.substring(pos)); | ||
|
||
return this; | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
ltexls/src/main/java/org/bsplines/ltexls/parsing/html/HtmlFragmentizer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* Copyright (C) 2020 Julian Valentin, LTeX Development Community | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
package org.bsplines.ltexls.parsing.html; | ||
|
||
import java.util.regex.Pattern; | ||
import org.bsplines.ltexls.parsing.RegexCodeFragmentizer; | ||
|
||
public class HtmlFragmentizer extends RegexCodeFragmentizer { | ||
private static final Pattern pattern = Pattern.compile( | ||
"^[ \t]*<!--[ \t]*(?i)ltex(?-i):(.*?)[ \t]*-->[ \t]*$", | ||
Pattern.MULTILINE); | ||
|
||
public HtmlFragmentizer(String codeLanguageId) { | ||
super(codeLanguageId, pattern); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
ltexls/src/test/java/org/bsplines/ltexls/parsing/html/HtmlAnnotatedTextBuilderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* Copyright (C) 2020 Julian Valentin, LTeX Development Community | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
package org.bsplines.ltexls.parsing.html; | ||
|
||
import org.bsplines.ltexls.parsing.CodeAnnotatedTextBuilder; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
import org.languagetool.markup.AnnotatedText; | ||
|
||
public class HtmlAnnotatedTextBuilderTest { | ||
private static void assertPlainText(String code, String expectedPlainText) { | ||
AnnotatedText annotatedText = buildAnnotatedText(code); | ||
Assertions.assertEquals(expectedPlainText, annotatedText.getPlainText()); | ||
} | ||
|
||
private static AnnotatedText buildAnnotatedText(String code) { | ||
CodeAnnotatedTextBuilder builder = CodeAnnotatedTextBuilder.create("html"); | ||
return builder.addCode(code).build(); | ||
} | ||
|
||
@Test | ||
public void test() { | ||
assertPlainText( | ||
"<html>\n" | ||
+ " <head>\n" | ||
+ " <title>Title</title>\n" | ||
+ " </head>\n" | ||
+ " <body style=\"color:red;\">\n" | ||
+ " This is a <b>test</b>.\n" | ||
+ " <!-- This is a comment. -->\n" | ||
+ " </body>\n" | ||
+ "</html>\n", | ||
"Title\n\nThis is a test."); | ||
assertPlainText( | ||
"<html><body>This is a te<script>abc</script>st.</body></html>\n", | ||
"\n\nThis is a test."); | ||
assertPlainText( | ||
"<html><body>This is a te<br/>st.</body></html>\n", | ||
"\n\nThis is a te\nst."); | ||
assertPlainText( | ||
"<html><body>This is a test & another test.</body></html>\n", | ||
"\n\nThis is a test & another test."); | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
ltexls/src/test/java/org/bsplines/ltexls/parsing/html/HtmlFragmentizerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* Copyright (C) 2020 Julian Valentin, LTeX Development Community | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
package org.bsplines.ltexls.parsing.html; | ||
|
||
import java.util.List; | ||
import org.bsplines.ltexls.parsing.CodeFragment; | ||
import org.bsplines.ltexls.parsing.CodeFragmentizer; | ||
import org.bsplines.ltexls.settings.Settings; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class HtmlFragmentizerTest { | ||
private static void testFragmentizer(CodeFragmentizer fragmentizer, String code) { | ||
List<CodeFragment> codeFragments = fragmentizer.fragmentize(code, new Settings()); | ||
Assertions.assertEquals(3, codeFragments.size()); | ||
|
||
Assertions.assertEquals("html", codeFragments.get(0).getCodeLanguageId()); | ||
Assertions.assertEquals(0, codeFragments.get(0).getFromPos()); | ||
Assertions.assertEquals(12, codeFragments.get(0).getCode().length()); | ||
Assertions.assertEquals("en-US", codeFragments.get(0).getSettings().getLanguageShortCode()); | ||
|
||
Assertions.assertEquals("html", codeFragments.get(1).getCodeLanguageId()); | ||
Assertions.assertEquals(12, codeFragments.get(1).getFromPos()); | ||
Assertions.assertEquals(50, codeFragments.get(1).getCode().length()); | ||
Assertions.assertEquals("de-DE", codeFragments.get(1).getSettings().getLanguageShortCode()); | ||
|
||
Assertions.assertEquals("html", codeFragments.get(2).getCodeLanguageId()); | ||
Assertions.assertEquals(62, codeFragments.get(2).getFromPos()); | ||
Assertions.assertEquals(48, codeFragments.get(2).getCode().length()); | ||
Assertions.assertEquals("en-US", codeFragments.get(2).getSettings().getLanguageShortCode()); | ||
} | ||
|
||
@Test | ||
public void test() { | ||
CodeFragmentizer fragmentizer = CodeFragmentizer.create("html"); | ||
|
||
testFragmentizer(fragmentizer, | ||
"Sentence 1\n" | ||
+ "\n <!-- ltex: language=de-DE--> \n\nSentence 2\n" | ||
+ "\n<!--\t\t\tltex:\t\t\t\tlanguage=en-US\t\t-->\n\nSentence 3\n"); | ||
} | ||
|
||
@Test | ||
public void testWrongSettings() { | ||
CodeFragmentizer fragmentizer = CodeFragmentizer.create("html"); | ||
Assertions.assertDoesNotThrow(() -> fragmentizer.fragmentize( | ||
"Sentence 1\n<!-- ltex: languagede-DE -->\n\nSentence 2\n", new Settings())); | ||
Assertions.assertDoesNotThrow(() -> fragmentizer.fragmentize( | ||
"Sentence 1\n<!-- ltex: unknownKey=abc -->\n\nSentence 2\n", new Settings())); | ||
} | ||
} |