Skip to content

Commit

Permalink
Fixing parser to discard whitespace after formatting tags, to remove …
Browse files Browse the repository at this point in the history
…extra required newlines. (#250)
  • Loading branch information
craig-openlaw authored Mar 26, 2020
1 parent c40fea0 commit 0004d06
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -167,29 +167,39 @@ trait BlockRules extends Parser with ExpressionRules with GlobalRules {
}

def centeredLine: Rule1[TemplateText] = rule {
capture(centered) ~> ((_: String) => TemplateText(List(Centered)))
capture(centered) ~ optional(ws) ~> (
(_: String) => TemplateText(List(Centered))
)
}

def rightLine: Rule1[TemplateText] = rule {
capture(right) ~> ((_: String) => TemplateText(List(RightAlign)))
capture(right) ~ optional(ws) ~> (
(_: String) => TemplateText(List(RightAlign))
)
}

def rightThreeQuartersLine: Rule1[TemplateText] = rule {
capture(rightThreeQuarters) ~> (
capture(rightThreeQuarters) ~ optional(ws) ~> (
(_: String) => TemplateText(List(RightThreeQuarters))
)
}

def pageBreak: Rule1[TemplateText] = rule {
capture(pagebreak) ~ "\n" ~> ((_: String) => TemplateText(List(PageBreak)))
capture(pagebreak) ~ optional(ws) ~> (
(_: String) => TemplateText(List(PageBreak))
)
}

def sectionBreak: Rule1[TemplateText] = rule {
capture(sectionbreak) ~> ((_: String) => TemplateText(List(SectionBreak)))
capture(sectionbreak) ~ optional(ws) ~> (
(_: String) => TemplateText(List(SectionBreak))
)
}

def indentLine: Rule1[TemplateText] = rule {
capture(indent) ~> ((_: String) => TemplateText(List(Indent)))
capture(indent) ~ optional(ws) ~> (
(_: String) => TemplateText(List(Indent))
)
}

def textPart: Rule1[TemplateText] = rule {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,29 @@ trait MarkdownRules extends GlobalRules {
}

def centeredLine: Rule1[Seq[TextElement]] = rule {
capture(centered) ~> ((_: String) => Seq(Centered))
capture(centered) ~ optional(ws) ~> ((_: String) => Seq(Centered))
}

def rightLine: Rule1[Seq[TextElement]] = rule {
capture(right) ~> ((_: String) => Seq(RightAlign))
capture(right) ~ optional(ws) ~> ((_: String) => Seq(RightAlign))
}

def rightThreeQuartersLine: Rule1[Seq[TextElement]] = rule {
capture(rightThreeQuarters) ~> ((_: String) => Seq(RightThreeQuarters))
capture(rightThreeQuarters) ~ optional(ws) ~> (
(_: String) => Seq(RightThreeQuarters)
)
}

def pageBreak: Rule1[Seq[TextElement]] = rule {
capture(pagebreak) ~> ((_: String) => Seq(PageBreak))
capture(pagebreak) ~ optional(ws) ~> ((_: String) => Seq(PageBreak))
}

def sectionBreak: Rule1[Seq[TextElement]] = rule {
capture(sectionbreak) ~> ((_: String) => Seq(SectionBreak))
capture(sectionbreak) ~ optional(ws) ~> ((_: String) => Seq(SectionBreak))
}

def indentLine: Rule1[Seq[TextElement]] = rule {
capture(indent) ~> ((_: String) => Seq(Indent))
capture(indent) ~ optional(ws) ~> ((_: String) => Seq(Indent))
}

def twoStar: Rule0 = rule(strong ~ !twoStar)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,10 +334,6 @@ final case class XHtmlAgreementPrinter(
})

case FreeText(t: Text) =>
// This might be necessary for some cases, but has not been enabled yet
// Consume any following text elements which are only newlines
//val remaining = xs.dropWhile(_ === FreeText(Text("\n")))

// Generate text output
val innerFrag = text(t.str)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ class OpenlawTemplateLanguageParserSpec extends FlatSpec with Matchers {
""".stripMargin

val text2 =
"""<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>"""
"""<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>"""

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

val text2 =
"""<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>"""
"""<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>"""

val result = forPreview(text)
resultShouldBe(result, text2)
Expand Down Expand Up @@ -1346,14 +1346,37 @@ class OpenlawTemplateLanguageParserSpec extends FlatSpec with Matchers {
it should "be able to break pages" in {
val text =
"""first paragraph of text
|\pagebreak
|second paragraph of text""".stripMargin
|\pagebreak
|
|second paragraph of text""".stripMargin
resultShouldBe(
forReview(text),
"""<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>"""
)
}

it should "drop extraneous newlines in pagebreak tag" in {
val text =
"""first paragraph of text
|\pagebreak
|second paragraph of text""".stripMargin
resultShouldBe(
forReview(text),
"""<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>"""
)
}

it should "drop extraneous newlines in sectionbreak tag" in {
val text =
"""first paragraph of text
|\sectionbreak
|second paragraph of text""".stripMargin
resultShouldBe(
forReview(text),
"""<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>"""
)
}

it should "be able to indent lines" in {
val text =
"""first paragraph of text
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class XHtmlAgreementPrinterSpec
val html = XHtmlAgreementPrinter(false)
.printParagraphs(agreement.right.value.paragraphs)
.print
html shouldBe """<p class="no-section align-right"> <strong>[[Company Name]]</strong></p>"""
html shouldBe """<p class="no-section align-right"><strong>[[Company Name]]</strong></p>"""
}

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

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

it should "handle agreements with multiple centered sections" in {
Expand All @@ -133,7 +133,7 @@ class XHtmlAgreementPrinterSpec
val html = XHtmlAgreementPrinter(false)
.printParagraphs(agreement.right.value.paragraphs)
.print
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>"""
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>"""

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2298,8 +2298,7 @@ class OpenlawExecutionEngineSpec extends FlatSpec with Matchers {

it should "handle missing values in a structure" in {
val template =
compile("""
|\centered **Test Agreement - structure**
compile("""\centered **Test Agreement - structure**
|
|# Structure definition
|[[Contestant Emergency Contact: Structure(
Expand Down Expand Up @@ -2330,7 +2329,7 @@ class OpenlawExecutionEngineSpec extends FlatSpec with Matchers {
case Success(result) =>
result.state shouldBe ExecutionFinished
val text = parser.forReview(result.agreements.head)
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>"
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>"
case Failure(_, message) =>
fail(message)
}
Expand Down

0 comments on commit 0004d06

Please sign in to comment.