Skip to content
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

Remove an unnecessary trailing line break before end tag. #45

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion src/main/scala/com/tristanhunt/knockoff/MarkdownParsing.scala
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,15 @@ class ChunkParser extends RegexParsers with StringExtras {
horizontalRule | leadingStrongTextBlock | leadingEmTextBlock | bulletItem |
numberedItem | indentedChunk | header | blockquote | linkDefinition |
htmlBlock | textBlockWithBreak | textBlock | emptyLines | emptySpace
} ^^ {
// Remove an unnecessary trailing line break from Chunk.
case BlockquotedChunk(content) => BlockquotedChunk(dropTrailingBreak(content))
case HeaderChunk(level, content) => HeaderChunk(level, dropTrailingBreak(content))
case IndentedChunk(content) => IndentedChunk(dropTrailingBreak(content))
case NumberedLineChunk(content) => NumberedLineChunk(dropTrailingBreak(content))
case TextChunk(content) => TextChunk(dropTrailingBreak(content))
case BulletLineChunk(content) => BulletLineChunk(dropTrailingBreak(content))
case chunk => chunk
}

def emptyLines: Parser[Chunk] =
Expand Down Expand Up @@ -311,6 +320,14 @@ class ChunkParser extends RegexParsers with StringExtras {
/** Take a series of very similar chunks and group them. */
private def foldedString(texts: List[Chunk]): String =
("" /: texts)((current, text) => current + text.content)

private def dropTrailingBreak(content : String) : String =
content match {
case s if s.endsWith("\r\n") => s.stripSuffix("\r\n")
case s if s.endsWith("\r") => s.stripSuffix("\r")
case s if s.endsWith("\n") => s.stripSuffix("\n")
case s => s
}
}

/*
Expand Down Expand Up @@ -415,7 +432,7 @@ case class HeaderChunk(val level: Int, val content: String) extends Chunk {
}

case object HorizontalRuleChunk extends Chunk {
val content = "* * *\n"
val content = "* * *"

def appendNewBlock(list: ListBuffer[Block],
remaining: List[(Chunk, Seq[Span], Position)],
Expand Down
24 changes: 12 additions & 12 deletions src/test/scala/com/tristanhunt/knockoff/ChunkParsersSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,31 @@ class ChunkParsersSpec extends ChunkParser with FunSpecLike with ShouldMatchers
describe("ChunkParser") {
it("should handle simple bullet items") {
val src = "* item 1\n* item 2\n"
parse( chunk, src ).get should equal ( BulletLineChunk("item 1\n") )
parse( chunk, src ).get should equal ( BulletLineChunk("item 1") )
}

it("should group a second line that's not a bullet") {
val src = "* item 1\n more\n"
parse( chunk, src ).get should equal (
BulletLineChunk("item 1\nmore\n")
BulletLineChunk("item 1\nmore")
)
}

it("should ignore whitespace around headers") {
val src = "# Header 1 #"
parse( chunk, src ).get should equal { HeaderChunk(1, "Header 1") }
}

it("should be ok with empty code blocks") {
val src = " "
parse( chunk, src ).get should equal { IndentedChunk("") }
}

it("should not explode on a code block with a trailing line") {
val line = " line\n "
parse( chunk, line ).get should equal { IndentedChunk("line\n") }
parse( chunk, line ).get should equal { IndentedChunk("line") }
}

it("should handle nothin' but code") {
val src = " This is just a code block.\n" +
" \n" +
Expand All @@ -45,15 +45,15 @@ class ChunkParsersSpec extends ChunkParser with FunSpecLike with ShouldMatchers
parse( chunk, src ).get should equal { IndentedChunk(
"This is just a code block.\n" +
"\n" +
"And it has a trailing whitespace line... that's also indented.\n"
"And it has a trailing whitespace line... that's also indented."
) }
}

it("should deal with a CRLF") {
val src = "This is a line \r\nthat is broken in\r\n a couple places.\r\n"
parse( chunk, src ).get should equal { TextChunk(src) }
parse( chunk, src ).get should equal { TextChunk(src.stripSuffix("\r\n")) }
}

it("should deal with just a CRLF") {
val src = "\u000d\u000a"
parse( chunk, src ).get should equal { EmptySpace(src) }
Expand Down