-
Notifications
You must be signed in to change notification settings - Fork 92
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Utility.trim collapse whitespace for adjacent text nodes #73 #113
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,17 +46,28 @@ object Utility extends AnyRef with parsing.TokenTests { | |
*/ | ||
def trim(x: Node): Node = x match { | ||
case Elem(pre, lab, md, scp, child@_*) => | ||
val children = child flatMap trimProper | ||
val children = combineAdjacentTextNodes(child:_*) flatMap trimProper | ||
Elem(pre, lab, md, scp, children.isEmpty, children: _*) | ||
} | ||
|
||
private def combineAdjacentTextNodes(children: Node*): Seq[Node] = { | ||
children.foldLeft(Seq.empty[Node]) { (acc, n) => | ||
(acc.lastOption, n) match { | ||
case (Some(Text(l)), Text(r)) => { | ||
acc.dropRight(1) :+ Text(l + r) | ||
} | ||
case _ => acc :+ n | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
It is pattern matching the front of the list with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oops sorry, I think I wrote that when I was tired this morning and reading
Correct that the order doesn't matter so long as the accumulated list is properly prepended/appended according to the direction it was traversed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, ok. Yeah, it's a lot for a person to keep in their head... between the colons, plus signs and foldings. |
||
} | ||
|
||
/** | ||
* trim a child of an element. `Attribute` values and `Atom` nodes that | ||
* are not `Text` nodes are unaffected. | ||
*/ | ||
def trimProper(x: Node): Seq[Node] = x match { | ||
case Elem(pre, lab, md, scp, child@_*) => | ||
val children = child flatMap trimProper | ||
val children = combineAdjacentTextNodes(child:_*) flatMap trimProper | ||
Elem(pre, lab, md, scp, children.isEmpty, children: _*) | ||
case Text(s) => | ||
new TextBuffer().append(s).toText | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey Ethan,
This pattern match is kind of hairy.
Couldn't you drop the
lastOption
business, usefoldRight
, and have it just be the following?Will it have the same result? Does that improve comprehensibility, as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right,
foldRight
I always use foldLeft so it never comes to mind. That would work,Looks like it would. I wasn't aware you can pattern match the last element in a list but I think that code is more elegant so I can update the PR with that and the tests will inform us if we're getting the same result (answer is probably yes)