Skip to content

Commit

Permalink
Merge pull request #7 from greatbeyond/master
Browse files Browse the repository at this point in the history
Fix for issue #2
  • Loading branch information
kristopherjohnson committed Dec 7, 2015
2 parents 175a973 + 60eac26 commit 94ab9b6
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 20 deletions.
40 changes: 20 additions & 20 deletions Markingbird/Markdown.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1650,7 +1650,7 @@ public struct Markdown {

return sb
}

/// convert all tabs to _tabWidth spaces;
/// standardizes line endings from DOS (CR LF) or Mac (CR) to UNIX (LF);
/// makes sure text ends with a couple of newlines;
Expand All @@ -1659,42 +1659,42 @@ public struct Markdown {
var output = ""
var line = ""
var valid = false

let str = text as NSString
for i in 0..<str.length {
let c = str.characterAtIndex(i)

for i in text.startIndex..<text.endIndex {
let c = text[i]
switch (c) {
case U16_NEWLINE:
case "\n":
if (valid) { output += line }
output += "\n"
line = ""
valid = false
case U16_RETURN:
if (i < str.length - 1) && (str.characterAtIndex(i + 1) != 10) {
if (valid) { output += line }
output += "\n"
line = ""
valid = false
}
case U16_TAB:
case "\r":
if (valid) { output += line }
output += "\n"
line = ""
valid = false
case "\r\n":
if (valid) { output += line }
output += "\n"
line = ""
valid = false
case "\t":
let width = Markdown._tabWidth - line.characters.count % Markdown._tabWidth
for _ in 0..<width {
line += " "
}
case 0x1A:
break
default:
if !valid && c != U16_SPACE /* ' ' */ {
if !valid && c != " " /* ' ' */ {
valid = true
}
line += String(count: 1, repeatedValue: UnicodeScalar(c))
line += String(count: 1, repeatedValue: c)
break
}
}

if (valid) { output += line }
output += "\n"

// add two newlines to the end before return
return output + "\n\n"
}
Expand Down
30 changes: 30 additions & 0 deletions MarkingbirdTests/SimpleTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -193,4 +193,34 @@ class SimpleTests: XCTestCase {

XCTAssertEqual(expected, actual)
}

func testNormalizeCR()
{
let input = "# Header\r\rBody"
let expected = "<h1>Header</h1>\n\n<p>Body</p>\n"

let actual = m.transform(input)

XCTAssertEqual(expected, actual)
}

func testNormalizeCRLF()
{
let input = "# Header\r\n\r\nBody"
let expected = "<h1>Header</h1>\n\n<p>Body</p>\n"

let actual = m.transform(input)

XCTAssertEqual(expected, actual)
}

func testNormalizeLF()
{
let input = "# Header\n\nBody"
let expected = "<h1>Header</h1>\n\n<p>Body</p>\n"

let actual = m.transform(input)

XCTAssertEqual(expected, actual)
}
}

0 comments on commit 94ab9b6

Please sign in to comment.