Skip to content

Commit

Permalink
Fixed first space removal in M2DocHTMLParser.
Browse files Browse the repository at this point in the history
  • Loading branch information
ylussaud committed Dec 10, 2021
1 parent db5840c commit a7f0529
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ private void insertText(MList parent, final Context context, TextNode node, bool
final String textToInsert;
if (needNewParagraph) {
createMParagraph(context, parent, null, null, null);
textToInsert = text.replaceFirst("\\s", "");
textToInsert = trimFirst(text);
} else {
textToInsert = text;
}
Expand All @@ -604,6 +604,30 @@ private void insertText(MList parent, final Context context, TextNode node, bool
}
}

/**
* Trims the begining of the given {@link String}.
*
* @param text
* the {@link String}
* @return the trimed {@link String}
*/
private String trimFirst(String text) {
final String res;

if (text != null && !text.isEmpty()) {
int subStringStart = 0;
final int textLength = text.length();
while (subStringStart < textLength && Character.isWhitespace(text.charAt(subStringStart))) {
subStringStart++;
}
res = text.substring(subStringStart);
} else {
res = text;
}

return res;
}

/**
* Starts the given {@link Element}.
*
Expand All @@ -625,7 +649,7 @@ private MList startElement(MList parent, Context context, Element element) {
} else if (BLOCKQUOTE_TAG.equals(nodeName)) {
if (element.childNodeSize() > 0 && element.childNode(0) instanceof TextNode) {
TextNode textNode = (TextNode) element.childNode(0);
String newText = textNode.text().replaceFirst("\\s", "");
String newText = trimFirst(textNode.text());
textNode.text(newText);
if (!newText.isEmpty()) {
res = createMParagraph(context, parent, element, null, null);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

=== HEADER ===

=== BODY ===

A simple demonstration of a query :
[query: .fromHTMLURI('doc.html')]
End of demonstration.
=== FOOTER ===

=== TEMPLATES ===
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<genconf:Generation xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:genconf="http://www.obeonetwork.org/m2doc/genconf/1.0" name="a" templateFileName="a-template.docx" resultFileName="a-actual-generation.docx" validationFileName="a-actual-validation.docx"/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<html>
<header>
</header>
<body>
<p>Some text at the begin:<br/><blockquote>
<div><blockquote>
<div><ul>
<li>&lt;WOW&gt; and the text continue here,</li>
<li>&lt;WAW&gt; and a bit more here,</li>
</ul></div>
</blockquote></div>
</blockquote>Then it's time for &lt;THE&gt; last paragraph.</p>
</body>
</html>


0 comments on commit a7f0529

Please sign in to comment.