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

Fix for issue #2 #7

Merged
merged 3 commits into from
Dec 7, 2015
Merged
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
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)
}
}