Skip to content

Commit

Permalink
make it possible to use em, strong and underline in table content (#258)
Browse files Browse the repository at this point in the history
* make it possible to use em, strong and underline in table content

* revert sbt version
  • Loading branch information
adridadou authored Jun 23, 2020
1 parent d03d6c7 commit de18f6d
Show file tree
Hide file tree
Showing 6 changed files with 183 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -197,23 +197,23 @@ trait BlockRules extends Parser with ExpressionRules with GlobalRules {
}

def textPart: Rule1[TemplateText] = rule {
textElement ~> ((s: Seq[TemplatePart]) => TemplateText(s.toList))
textElement ~> ((s: List[TemplatePart]) => TemplateText(s))
}

def textPartNoColons: Rule1[TemplateText] = rule {
textElementNoColons ~> ((s: Seq[TemplatePart]) => TemplateText(s.toList))
textElementNoColons ~> ((s: List[TemplatePart]) => TemplateText(s))
}

def textPartNoStrong: Rule1[TemplateText] = rule {
textElementNoStrong ~> ((s: Seq[TemplatePart]) => TemplateText(s.toList))
textElementNoStrong ~> ((s: List[TemplatePart]) => TemplateText(s))
}

def textPartNoEm: Rule1[TemplateText] = rule {
textElementNoEm ~> ((s: Seq[TemplatePart]) => TemplateText(s.toList))
textElementNoEm ~> ((s: List[TemplatePart]) => TemplateText(s))
}

def textPartNoUnder: Rule1[TemplateText] = rule {
textElementNoUnder ~> ((s: Seq[TemplatePart]) => TemplateText(s.toList))
textElementNoUnder ~> ((s: List[TemplatePart]) => TemplateText(s))
}

def textPartNoStrongNoEmNoUnder: Rule1[TemplateText] = rule {
Expand All @@ -224,19 +224,19 @@ trait BlockRules extends Parser with ExpressionRules with GlobalRules {
tableKey | strongWord | emWord | underWord | text | pipeText | starText | underLineText
}

def textElementNoStrong: Rule1[Seq[TemplatePart]] = rule {
def textElementNoStrong: Rule1[List[TemplatePart]] = rule {
innerEmWord | innerUnderWord | textNoReturn | pipeText | underLineText
}

def textElementNoColons: Rule1[List[TemplatePart]] = rule {
tableKey | strongWord | emWord | underWord | textNoColons | pipeText | starText | underLineText
}

def textElementNoEm: Rule1[Seq[TemplatePart]] = rule {
def textElementNoEm: Rule1[List[TemplatePart]] = rule {
innerStrongWord | innerUnderWord | textNoReturn | pipeText | underLineText
}

def textElementNoUnder: Rule1[Seq[TemplatePart]] = rule {
def textElementNoUnder: Rule1[List[TemplatePart]] = rule {
innerStrongWord | innerEmWord | textNoReturn | pipeText | starText
}

Expand Down Expand Up @@ -323,17 +323,36 @@ trait BlockRules extends Parser with ExpressionRules with GlobalRules {
capture(pipe) ~> ((s: String) => List(Text(TextCleaning.dots(s))))
}

def tableBlockEm: Rule1[TableBlock] = rule {
oneStar ~ tableColumnEntryBlock ~ oneStar ~> (
(block: TableBlock) => TableBlock((Em :: block.elems) ++ List(Em))
)
}

def tableBlockStrong: Rule1[TableBlock] = rule {
twoStar ~ tableColumnEntryBlock ~ twoStar ~> (
(block: TableBlock) =>
TableBlock((Strong :: block.elems) ++ List(Strong))
)
}

def tableBlockUnder: Rule1[TableBlock] = rule {
underLines ~ tableColumnEntryBlock ~ underLines ~> (
(block: TableBlock) => TableBlock((Under :: block.elems) ++ List(Under))
)
}

def tableText: Rule1[TemplatePart] = rule {
capture(normalCharNoReturn) ~> ((s: String) => Text(s))
}

// the table parsing construct below may return empty whitespace at the end of the cell, this trims it and accumulates text nodes
@tailrec final def accumulateTextAndTrim(
seq: Seq[TemplatePart],
accu: Seq[TemplatePart] = Seq()
): Seq[TemplatePart] = seq match {
case Seq() => accu
case seq @ Seq(head, tail @ _*) =>
seq: List[TemplatePart],
accu: List[TemplatePart] = Nil
): List[TemplatePart] = seq match {
case Nil => accu
case head :: tail =>
val texts = seq
.takeWhile({
case _: Text => true
Expand All @@ -354,10 +373,17 @@ trait BlockRules extends Parser with ExpressionRules with GlobalRules {
}

def whitespace: Rule0 = rule { zeroOrMore(anyOf(tabOrSpace)) }
def tableColumnEntryBlock: Rule1[Seq[TemplatePart]] = rule {

def tableColumnEntryBlock: Rule1[TableBlock] = rule {
oneOrMore(
varAliasKey | varKey | varMemberKey | conditionalBlockSetKey | conditionalBlockKey | foreachBlockKey | tableText
) ~> ((seq: Seq[TemplatePart]) => accumulateTextAndTrim(seq))
(varAliasKey | varKey | varMemberKey | conditionalBlockSetKey | conditionalBlockKey | foreachBlockKey | tableText) ~> (
(part: TemplatePart) => TableBlock(List(part))
) |
tableBlockStrong | tableBlockEm | tableBlockUnder
) ~> (
(seq: Seq[TableBlock]) =>
TableBlock(accumulateTextAndTrim(seq.flatMap(_.elems).toList))
)
}

def tableKey: Rule1[List[Table]] = rule {
Expand All @@ -377,9 +403,9 @@ trait BlockRules extends Parser with ExpressionRules with GlobalRules {
tableRow ~ EndOfBlock
) ~> (
(
headers: List[List[TemplatePart]],
headers: List[TableBlock],
alignment: Seq[(Alignment, Border)],
rows: Seq[List[List[TemplatePart]]]
rows: Seq[List[TableBlock]]
) =>
Table(
headers,
Expand All @@ -395,20 +421,20 @@ trait BlockRules extends Parser with ExpressionRules with GlobalRules {
) ~> (
(
styling: Option[Seq[(Alignment, Border)]],
rows: Seq[Seq[Seq[TemplatePart]]]
rows: Seq[List[TableBlock]]
) =>
Table(
Nil,
styling.map(_.toList).getOrElse(Nil),
rows.map(_.map(_.toList).toList).toList
rows.toList
)
)
}

def tableWithoutRow: Rule1[Table] = rule {
tableRow ~ nl ~ whitespace ~ tableHeaderBreak ~ whitespace ~ EndOfBlock ~> (
(
headers: List[List[TemplatePart]],
headers: List[TableBlock],
alignment: Seq[(Alignment, Border)]
) => Table(headers, alignment.toList, Nil)
)
Expand All @@ -419,19 +445,19 @@ trait BlockRules extends Parser with ExpressionRules with GlobalRules {
.separatedBy(pipe) ~ whitespace ~ pipe ~ whitespace
}

def tableColumnEntry: Rule1[Seq[TemplatePart]] = rule {
def tableColumnEntry: Rule1[TableBlock] = rule {
whitespace ~ tableColumnEntryBlock ~ whitespace
}

def tableRow: Rule1[List[List[TemplatePart]]] = rule {
def tableRow: Rule1[List[TableBlock]] = rule {
whitespace ~ pipe ~ (tableColumnEntry ~ pipe ~ oneOrMore(tableColumnEntry)
.separatedBy(pipe) ~ pipe ~ whitespace ~> (
(
row: Seq[TemplatePart],
remaining: Seq[Seq[TemplatePart]]
row: TableBlock,
remaining: Seq[TableBlock]
) => row +: remaining
)) ~ optional(pipe) ~ whitespace ~> (
(elems: Seq[Seq[TemplatePart]]) => elems.map(_.toList).toList
(elems: Seq[TableBlock]) => elems.toList
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ final case class CompiledAgreement(
.map(entry =>
getAgreementElements(
mutable.Buffer(),
entry,
entry.elems,
executionResult,
overriddenFormatter
).map(_.toList)
Expand All @@ -194,7 +194,7 @@ final case class CompiledAgreement(
.map(entry =>
getAgreementElements(
mutable.Buffer(),
entry,
entry.elems,
executionResult,
overriddenFormatter
).map(_.toList)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,12 @@ final case class NumberConstant(
override def toString: String = value.toString()
}

final case class TableBlock(elems: List[TemplatePart])

final case class Table(
header: List[List[TemplatePart]],
header: List[TableBlock],
alignment: List[(Alignment, Border)],
rows: List[List[List[TemplatePart]]]
rows: List[List[TableBlock]]
) extends TemplatePart

trait ConditionalExpression {
Expand Down Expand Up @@ -149,7 +151,7 @@ final case class ForEachBlock(
Success(
CompiledAgreement(
TemplateHeader(),
Block(List(specialCodeBlock) ++ block.elems),
Block(specialCodeBlock :: block.elems),
VariableRedefinition()
),
listType.typeParameter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ import java.util.concurrent.atomic.AtomicInteger

import cats.implicits._
import org.adridadou.openlaw.parser.template._
import org.adridadou.openlaw.parser.template.variableTypes.{
ExternalSignature,
IdentityType
}
import org.adridadou.openlaw.parser.template.variableTypes.{IdentityType}
import scalatags.Text.all._
import slogging._

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ class OpenlawExecutionEngine extends VariableExecutionEngine {

case Table(header, _, rows) =>
val initialValue: Result[OpenlawExecutionState] = Success(executionResult)
(header.flatten ++ rows.flatten.flatten)
(header.flatMap(_.elems) ++ rows.flatten.flatMap(_.elems))
.foldLeft(initialValue)((exec, elem) =>
exec.flatMap(processExecutedElement(_, elem, templates))
)
Expand Down
Loading

0 comments on commit de18f6d

Please sign in to comment.