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

Added logic to allow Metadata values to be split over several lines #70

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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ To define metadata values within a Markdown document, use the following syntax:
```
---
keyA: valueA
keyB: valueB
keyB: "a value must be wrapped in quotes to have a : in it"
keyC: valueC
---

Markdown text...
Expand Down
62 changes: 56 additions & 6 deletions Sources/Ink/Internal/Metadata.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,78 @@ internal struct Metadata: Readable {

var metadata = Metadata()
var lastKey: String?
enum Quotes {
case no, single, double
}
var valueInQuotes = Quotes.no

while !reader.didReachEnd {
reader.discardWhitespacesAndNewlines()

guard reader.currentCharacter != "-" else {
try require(reader.readCount(of: "-") == 3)

//Check if we are in a value marked by quotes.
guard valueInQuotes == .no
else {

var value = trim(reader.readUntilEndOfLine())

if (value.last == "\"" && valueInQuotes == .double) ||
(value.last == "'" && valueInQuotes == .single){
valueInQuotes = .no
value.removeLast()
}

if let lastKey = lastKey {
metadata.values[lastKey]?.append(" " + value)
}
continue
}

//Check for end of metadata.
let currentIndex = reader.currentIndex
guard reader.readCount(of: "-") != 3
else{
return metadata
}
reader.moveToIndex(currentIndex)

//Read until the possible end of a key marked by a colon. This reads until the end of the line if there is no colon.
let key = try trim(reader.read(until: ":", required: false))

guard reader.previousCharacter == ":" else {

//If there is no colon, then this must be an orphan metadata line. Merge it into previous value.
guard reader.previousCharacter == ":"
else {
if let lastKey = lastKey {
metadata.values[lastKey]?.append(" " + key)
}

continue
}

reader.discardWhitespaces()

//Check if we are starting a value wrapped in quotes and flag accordingly
if reader.currentCharacter == "\""
{
reader.advanceIndex()
valueInQuotes = .double
}
if reader.currentCharacter == "'"
{
reader.advanceIndex()
valueInQuotes = .single
}


let value = trim(reader.readUntilEndOfLine())
var value = trim(reader.readUntilEndOfLine())

if !value.isEmpty {

if (value.last == "\"" && valueInQuotes == .double) ||
(value.last == "'" && valueInQuotes == .single){
valueInQuotes = .no
value.removeLast()
}

metadata.values[key] = value
lastKey = key
}
Expand Down
69 changes: 69 additions & 0 deletions Tests/InkTests/MarkdownTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,71 @@ final class MarkdownTests: XCTestCase {
XCTAssertEqual(markdown.metadata, [:])
XCTAssertEqual(markdown.html, "<h1>Title</h1>")
}

func testMultilineQuotedMetadataWithColonInValue(){
let markdown = MarkdownParser().parse("""
---
a: "1
b:2"
---
# Title
""")

XCTAssertEqual(markdown.metadata, ["a": "1 b:2"])
XCTAssertEqual(markdown.html, "<h1>Title</h1>")
}

func testMultilineQuotedMetadataWithEscapedQuotesInValue(){
let markdown = MarkdownParser().parse("""
---
a: "1
\"b\"2"
---
# Title
""")

XCTAssertEqual(markdown.metadata, ["a": "1 \"b\"2"])
XCTAssertEqual(markdown.html, "<h1>Title</h1>")
}

func testMultilineSingleQuotedMetadataWithDoubleQuotesInValue(){
let markdown = MarkdownParser().parse("""
---
a: '1
"b"2'
---
# Title
""")

XCTAssertEqual(markdown.metadata, ["a": "1 \"b\"2"])
XCTAssertEqual(markdown.html, "<h1>Title</h1>")
}

func testSingleLineValueWithQuotes(){
let markdown = MarkdownParser().parse("""
---
a: "1"
b: '2'
---
# Title
""")

XCTAssertEqual(markdown.metadata, ["a": "1", "b": "2"])
XCTAssertEqual(markdown.html, "<h1>Title</h1>")
}

func testOrphanMetadataValueWithDashAtBeginning(){
let markdown = MarkdownParser().parse("""
---
a: 1
-2
---
# Title
""")

XCTAssertEqual(markdown.metadata, ["a": "1 -2"])
XCTAssertEqual(markdown.html, "<h1>Title</h1>")
}

func testMetadataModifiers() {
let parser = MarkdownParser(modifiers: [
Expand Down Expand Up @@ -138,6 +203,10 @@ extension MarkdownTests {
("testDiscardingEmptyMetadataValues", testDiscardingEmptyMetadataValues),
("testMergingOrphanMetadataValueIntoPreviousOne", testMergingOrphanMetadataValueIntoPreviousOne),
("testMissingMetadata", testMissingMetadata),
("testMultilineQuotedMetadataWithColonInValue", testMultilineQuotedMetadataWithColonInValue),
("testMultilineQuotedMetadataWithEscapedQuotesInValue", testMultilineQuotedMetadataWithEscapedQuotesInValue),("testMultilineSingleQuotedMetadataWithDoubleQuotesInValue", testMultilineSingleQuotedMetadataWithDoubleQuotesInValue),
("testSingleLineValueWithQuotes", testSingleLineValueWithQuotes),
("testOrphanMetadataValueWithDashAtBeginning", testOrphanMetadataValueWithDashAtBeginning),
("testMetadataModifiers", testMetadataModifiers),
("testPlainTextTitle", testPlainTextTitle),
("testRemovingTrailingMarkersFromTitle", testRemovingTrailingMarkersFromTitle),
Expand Down