Skip to content

Commit 0004d06

Browse files
Fixing parser to discard whitespace after formatting tags, to remove extra required newlines. (#250)
1 parent c40fea0 commit 0004d06

File tree

6 files changed

+57
-27
lines changed

6 files changed

+57
-27
lines changed

shared/src/main/scala/org/adridadou/openlaw/parser/template/BlockRules.scala

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -167,29 +167,39 @@ trait BlockRules extends Parser with ExpressionRules with GlobalRules {
167167
}
168168

169169
def centeredLine: Rule1[TemplateText] = rule {
170-
capture(centered) ~> ((_: String) => TemplateText(List(Centered)))
170+
capture(centered) ~ optional(ws) ~> (
171+
(_: String) => TemplateText(List(Centered))
172+
)
171173
}
172174

173175
def rightLine: Rule1[TemplateText] = rule {
174-
capture(right) ~> ((_: String) => TemplateText(List(RightAlign)))
176+
capture(right) ~ optional(ws) ~> (
177+
(_: String) => TemplateText(List(RightAlign))
178+
)
175179
}
176180

177181
def rightThreeQuartersLine: Rule1[TemplateText] = rule {
178-
capture(rightThreeQuarters) ~> (
182+
capture(rightThreeQuarters) ~ optional(ws) ~> (
179183
(_: String) => TemplateText(List(RightThreeQuarters))
180184
)
181185
}
182186

183187
def pageBreak: Rule1[TemplateText] = rule {
184-
capture(pagebreak) ~ "\n" ~> ((_: String) => TemplateText(List(PageBreak)))
188+
capture(pagebreak) ~ optional(ws) ~> (
189+
(_: String) => TemplateText(List(PageBreak))
190+
)
185191
}
186192

187193
def sectionBreak: Rule1[TemplateText] = rule {
188-
capture(sectionbreak) ~> ((_: String) => TemplateText(List(SectionBreak)))
194+
capture(sectionbreak) ~ optional(ws) ~> (
195+
(_: String) => TemplateText(List(SectionBreak))
196+
)
189197
}
190198

191199
def indentLine: Rule1[TemplateText] = rule {
192-
capture(indent) ~> ((_: String) => TemplateText(List(Indent)))
200+
capture(indent) ~ optional(ws) ~> (
201+
(_: String) => TemplateText(List(Indent))
202+
)
193203
}
194204

195205
def textPart: Rule1[TemplateText] = rule {

shared/src/main/scala/org/adridadou/openlaw/parser/template/MarkdownRules.scala

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,29 @@ trait MarkdownRules extends GlobalRules {
99
}
1010

1111
def centeredLine: Rule1[Seq[TextElement]] = rule {
12-
capture(centered) ~> ((_: String) => Seq(Centered))
12+
capture(centered) ~ optional(ws) ~> ((_: String) => Seq(Centered))
1313
}
1414

1515
def rightLine: Rule1[Seq[TextElement]] = rule {
16-
capture(right) ~> ((_: String) => Seq(RightAlign))
16+
capture(right) ~ optional(ws) ~> ((_: String) => Seq(RightAlign))
1717
}
1818

1919
def rightThreeQuartersLine: Rule1[Seq[TextElement]] = rule {
20-
capture(rightThreeQuarters) ~> ((_: String) => Seq(RightThreeQuarters))
20+
capture(rightThreeQuarters) ~ optional(ws) ~> (
21+
(_: String) => Seq(RightThreeQuarters)
22+
)
2123
}
2224

2325
def pageBreak: Rule1[Seq[TextElement]] = rule {
24-
capture(pagebreak) ~> ((_: String) => Seq(PageBreak))
26+
capture(pagebreak) ~ optional(ws) ~> ((_: String) => Seq(PageBreak))
2527
}
2628

2729
def sectionBreak: Rule1[Seq[TextElement]] = rule {
28-
capture(sectionbreak) ~> ((_: String) => Seq(SectionBreak))
30+
capture(sectionbreak) ~ optional(ws) ~> ((_: String) => Seq(SectionBreak))
2931
}
3032

3133
def indentLine: Rule1[Seq[TextElement]] = rule {
32-
capture(indent) ~> ((_: String) => Seq(Indent))
34+
capture(indent) ~ optional(ws) ~> ((_: String) => Seq(Indent))
3335
}
3436

3537
def twoStar: Rule0 = rule(strong ~ !twoStar)

shared/src/main/scala/org/adridadou/openlaw/parser/template/printers/XHtmlAgreementPrinter.scala

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -334,10 +334,6 @@ final case class XHtmlAgreementPrinter(
334334
})
335335

336336
case FreeText(t: Text) =>
337-
// This might be necessary for some cases, but has not been enabled yet
338-
// Consume any following text elements which are only newlines
339-
//val remaining = xs.dropWhile(_ === FreeText(Text("\n")))
340-
341337
// Generate text output
342338
val innerFrag = text(t.str)
343339

shared/src/test/scala/org/adridadou/openlaw/parser/OpenlawTemplateLanguageParserSpec.scala

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ class OpenlawTemplateLanguageParserSpec extends FlatSpec with Matchers {
393393
""".stripMargin
394394

395395
val text2 =
396-
"""<div class="openlaw-paragraph paragraph-1"><p class="no-section">a small title</p></div><ul class="list-lvl-1"><li><div class="openlaw-paragraph paragraph-2"><p>1. I should have a ul tag.<br /></p></div></li><li><div class="openlaw-paragraph paragraph-3"><p>2. So should I.<br /></p></div></li><li><div class="openlaw-paragraph paragraph-4"><p>3. And I also.<br /></p></div></li></ul><div class="openlaw-paragraph paragraph-5"><p class="no-section"><hr class="section-break" /></p></div><div class="openlaw-paragraph paragraph-6"><p class="no-section"><br />But I should not!<br /><br /> </p></div>"""
396+
"""<div class="openlaw-paragraph paragraph-1"><p class="no-section">a small title</p></div><ul class="list-lvl-1"><li><div class="openlaw-paragraph paragraph-2"><p>1. I should have a ul tag.<br /></p></div></li><li><div class="openlaw-paragraph paragraph-3"><p>2. So should I.<br /></p></div></li><li><div class="openlaw-paragraph paragraph-4"><p>3. And I also.<br /></p></div></li></ul><div class="openlaw-paragraph paragraph-5"><p class="no-section"><hr class="section-break" /></p></div><div class="openlaw-paragraph paragraph-6"><p class="no-section">But I should not!<br /><br /> </p></div>"""
397397

398398
val result = forPreview(text)
399399
resultShouldBe(result, text2)
@@ -408,7 +408,7 @@ class OpenlawTemplateLanguageParserSpec extends FlatSpec with Matchers {
408408
|more text""".stripMargin
409409

410410
val text2 =
411-
"""<div class="openlaw-paragraph paragraph-1"><p class="no-section">some text</p></div><div class="openlaw-paragraph paragraph-2"><p class="no-section"><hr class="pagebreak" /></p></div><div class="openlaw-paragraph paragraph-3"><p class="no-section"><br />more text</p></div>"""
411+
"""<div class="openlaw-paragraph paragraph-1"><p class="no-section">some text</p></div><div class="openlaw-paragraph paragraph-2"><p class="no-section"><hr class="pagebreak" /></p></div><div class="openlaw-paragraph paragraph-3"><p class="no-section">more text</p></div>"""
412412

413413
val result = forPreview(text)
414414
resultShouldBe(result, text2)
@@ -1346,14 +1346,37 @@ class OpenlawTemplateLanguageParserSpec extends FlatSpec with Matchers {
13461346
it should "be able to break pages" in {
13471347
val text =
13481348
"""first paragraph of text
1349-
|\pagebreak
1350-
|second paragraph of text""".stripMargin
1349+
|\pagebreak
1350+
|
1351+
|second paragraph of text""".stripMargin
1352+
resultShouldBe(
1353+
forReview(text),
1354+
"""<p class="no-section">first paragraph of text<br /></p><p class="no-section"><hr class="pagebreak" /></p><p class="no-section">second paragraph of text</p>"""
1355+
)
1356+
}
1357+
1358+
it should "drop extraneous newlines in pagebreak tag" in {
1359+
val text =
1360+
"""first paragraph of text
1361+
|\pagebreak
1362+
|second paragraph of text""".stripMargin
13511363
resultShouldBe(
13521364
forReview(text),
13531365
"""<p class="no-section">first paragraph of text<br /></p><p class="no-section"><hr class="pagebreak" /></p><p class="no-section">second paragraph of text</p>"""
13541366
)
13551367
}
13561368

1369+
it should "drop extraneous newlines in sectionbreak tag" in {
1370+
val text =
1371+
"""first paragraph of text
1372+
|\sectionbreak
1373+
|second paragraph of text""".stripMargin
1374+
resultShouldBe(
1375+
forReview(text),
1376+
"""<p class="no-section">first paragraph of text<br /></p><p class="no-section"><hr class="section-break" /></p><p class="no-section">second paragraph of text</p>"""
1377+
)
1378+
}
1379+
13571380
it should "be able to indent lines" in {
13581381
val text =
13591382
"""first paragraph of text

shared/src/test/scala/org/adridadou/openlaw/parser/template/printers/XHtmlAgreementPrinterSpec.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ class XHtmlAgreementPrinterSpec
9595
val html = XHtmlAgreementPrinter(false)
9696
.printParagraphs(agreement.right.value.paragraphs)
9797
.print
98-
html shouldBe """<p class="no-section align-right"> <strong>[[Company Name]]</strong></p>"""
98+
html shouldBe """<p class="no-section align-right"><strong>[[Company Name]]</strong></p>"""
9999
}
100100

101101
it should "handle right aligned underlined section" in {
@@ -104,7 +104,7 @@ class XHtmlAgreementPrinterSpec
104104
val html = XHtmlAgreementPrinter(false)
105105
.printParagraphs(agreement.right.value.paragraphs)
106106
.print
107-
html shouldBe """<p class="no-section align-right"> <u>[[Company Name]]</u></p>"""
107+
html shouldBe """<p class="no-section align-right"><u>[[Company Name]]</u></p>"""
108108
}
109109

110110
it should "handle right 3/4 aligned section" in {
@@ -113,7 +113,7 @@ class XHtmlAgreementPrinterSpec
113113
val html = XHtmlAgreementPrinter(false)
114114
.printParagraphs(agreement.right.value.paragraphs)
115115
.print
116-
html shouldBe """<p class="no-section align-right-three-quarters"> <strong>[[Company Name]]</strong></p>"""
116+
html shouldBe """<p class="no-section align-right-three-quarters"><strong>[[Company Name]]</strong></p>"""
117117
}
118118

119119
it should "handle agreements with multiple centered sections" in {
@@ -133,7 +133,7 @@ class XHtmlAgreementPrinterSpec
133133
val html = XHtmlAgreementPrinter(false)
134134
.printParagraphs(agreement.right.value.paragraphs)
135135
.print
136-
html shouldBe """<p class="no-section align-center"> <strong>BYLAWS</strong><br /> <strong>OF</strong><br /> <strong>[[Company Name]]</strong><br /> (A DELAWARE CORPORATION)<br /></p><ul class="list-lvl-1"><li><p>1. <strong>Offices</strong></p><ul class="list-lvl-2"><li><p>(a) <strong>Registered Office</strong>. The registered office of the corporation in the State of Delaware shall be [[Registered Agent Address]], and the name of the registered agent of the corporation in the State of Delaware at such address is [[Registered Agent Name]].<br /></p></li></ul></li></ul>"""
136+
html shouldBe """<p class="no-section align-center"><strong>BYLAWS</strong><br /><strong>OF</strong><br /><strong>[[Company Name]]</strong><br />(A DELAWARE CORPORATION)<br /></p><ul class="list-lvl-1"><li><p>1. <strong>Offices</strong></p><ul class="list-lvl-2"><li><p>(a) <strong>Registered Office</strong>. The registered office of the corporation in the State of Delaware shall be [[Registered Agent Address]], and the name of the registered agent of the corporation in the State of Delaware at such address is [[Registered Agent Name]].<br /></p></li></ul></li></ul>"""
137137

138138
}
139139

shared/src/test/scala/org/adridadou/openlaw/vm/OpenlawExecutionEngineSpec.scala

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2298,8 +2298,7 @@ class OpenlawExecutionEngineSpec extends FlatSpec with Matchers {
22982298

22992299
it should "handle missing values in a structure" in {
23002300
val template =
2301-
compile("""
2302-
|\centered **Test Agreement - structure**
2301+
compile("""\centered **Test Agreement - structure**
23032302
|
23042303
|# Structure definition
23052304
|[[Contestant Emergency Contact: Structure(
@@ -2330,7 +2329,7 @@ class OpenlawExecutionEngineSpec extends FlatSpec with Matchers {
23302329
case Success(result) =>
23312330
result.state shouldBe ExecutionFinished
23322331
val text = parser.forReview(result.agreements.head)
2333-
text shouldBe "<p class=\"no-section\"><br /> <strong>Test Agreement - structure</strong></p><p class=\"no-section\"># Structure definition<br /></p><p class=\"no-section\"># Structure type var<br /></p><p class=\"no-section\"><strong>Emergency Contact</strong><br />Name: test<br />Age: [[Medical Contact]]<br />DOB: [[Medical Contact]]<br />Address: [[Medical Contact]]</p><p class=\"no-section\"><br /> </p>"
2332+
text shouldBe "<p class=\"no-section align-center\"><strong>Test Agreement - structure</strong></p><p class=\"no-section\"># Structure definition<br /></p><p class=\"no-section\"># Structure type var<br /></p><p class=\"no-section\"><strong>Emergency Contact</strong><br />Name: test<br />Age: [[Medical Contact]]<br />DOB: [[Medical Contact]]<br />Address: [[Medical Contact]]</p><p class=\"no-section\"><br /> </p>"
23342333
case Failure(_, message) =>
23352334
fail(message)
23362335
}

0 commit comments

Comments
 (0)