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

allow double [[link]] style #57

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
33 changes: 22 additions & 11 deletions Sources/Ink/Internal/Link.swift
Original file line number Diff line number Diff line change
@@ -1,23 +1,34 @@
/**
* Ink
* Copyright (c) John Sundell 2019
* MIT license, see LICENSE file for details
*/
* Ink
* Copyright (c) John Sundell 2019
* MIT license, see LICENSE file for details
*/

internal struct Link: Fragment {
var modifierTarget: Modifier.Target { .links }

var target: Target
var text: FormattedText

static func read(using reader: inout Reader) throws -> Link {
try reader.read("[")
var double=false
if reader.currentCharacter == "[" {
// double [[link]] style
reader.advanceIndex()
double=true
}
let text = FormattedText.read(using: &reader, terminators: ["]"])
try reader.read("]")

if double{
try reader.read("]")
}

guard !reader.didReachEnd else { throw Reader.Error() }

if reader.currentCharacter == "(" {
if double{
return Link(target: .url(URL("markdown://"+text.plainText())), text: text)
}
else if reader.currentCharacter == "(" {
reader.advanceIndex()
let url = try reader.read(until: ")")
return Link(target: .url(url), text: text)
Expand All @@ -27,14 +38,14 @@ internal struct Link: Fragment {
return Link(target: .reference(reference), text: text)
}
}

func html(usingURLs urls: NamedURLCollection,
modifiers: ModifierCollection) -> String {
let url = target.url(from: urls)
let title = text.html(usingURLs: urls, modifiers: modifiers)
return "<a href=\"\(url)\">\(title)</a>"
}

func plainText() -> String {
text.plainText()
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/Ink/Internal/Reader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ extension Reader {

var didReachEnd: Bool { currentIndex == endIndex }
var previousCharacter: Character? { lookBehindAtPreviousCharacter() }
var currentCharacter: Character { string[currentIndex] }
var currentCharacter: Character { if currentIndex>=endIndex{return Character.init("ƒ")} else {return string[currentIndex]} }
var nextCharacter: Character? { lookAheadAtNextCharacter() }
var endIndex: String.Index { string.endIndex }

Expand Down