Skip to content

Commit 7536777

Browse files
committed
Migrate project to Xcode 11.1. Support backslash escaping. Approximate support for using link reference definitions.
1 parent 6778c27 commit 7536777

File tree

13 files changed

+306
-47
lines changed

13 files changed

+306
-47
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
# Changelog
22

3+
## 0.2 (2019-10-19)
4+
5+
- Implemented support for backslash escaping
6+
- Added support for using link reference definitions; not fully CommonMark-compliant yet
7+
- Migrated project to Xcode 11.1
8+
39
## 0.1 (2019-08-17)
410
- Initial version

MarkdownKit.xcodeproj/project.pbxproj

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
CC2AF6F7227CD98100BEA420 /* MarkdownBlockTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = CC2AF6F6227CD98100BEA420 /* MarkdownBlockTests.swift */; };
1515
CC2AF6F9227CD98100BEA420 /* MarkdownKit.h in Headers */ = {isa = PBXBuildFile; fileRef = CC2AF6EB227CD98100BEA420 /* MarkdownKit.h */; settings = {ATTRIBUTES = (Public, ); }; };
1616
CC3E4F532309FE9100C8A3DD /* NSColor.swift in Sources */ = {isa = PBXBuildFile; fileRef = CC3E4F522309FE9100C8A3DD /* NSColor.swift */; };
17+
CC5E530C2359FF3A00C72CE2 /* EscapeTransformer.swift in Sources */ = {isa = PBXBuildFile; fileRef = CC5E530B2359FF3A00C72CE2 /* EscapeTransformer.swift */; };
1718
CC6988C4227CDF810021C7E1 /* BlockquoteParser.swift in Sources */ = {isa = PBXBuildFile; fileRef = CC6988C3227CDF810021C7E1 /* BlockquoteParser.swift */; };
1819
CC6D239822C03CBA00BB1302 /* LinkTransformer.swift in Sources */ = {isa = PBXBuildFile; fileRef = CC6D239722C03CBA00BB1302 /* LinkTransformer.swift */; };
1920
CC72627122F318C0001AB6D9 /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = CC72627022F318C0001AB6D9 /* main.swift */; };
@@ -79,6 +80,7 @@
7980
CC2AF6F6227CD98100BEA420 /* MarkdownBlockTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MarkdownBlockTests.swift; sourceTree = "<group>"; };
8081
CC2AF6F8227CD98100BEA420 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
8182
CC3E4F522309FE9100C8A3DD /* NSColor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NSColor.swift; sourceTree = "<group>"; };
83+
CC5E530B2359FF3A00C72CE2 /* EscapeTransformer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EscapeTransformer.swift; sourceTree = "<group>"; };
8284
CC6988C3227CDF810021C7E1 /* BlockquoteParser.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BlockquoteParser.swift; sourceTree = "<group>"; };
8385
CC6988C5227CDFB10021C7E1 /* README.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = "<group>"; };
8486
CC6988C6227CDFB10021C7E1 /* LICENSE */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = LICENSE; sourceTree = "<group>"; };
@@ -249,6 +251,7 @@
249251
CC7B603C22AC6AD70092188C /* CodeLinkHtmlTransformer.swift */,
250252
CC6D239722C03CBA00BB1302 /* LinkTransformer.swift */,
251253
CC7B604022B67F0C0092188C /* EmphasisTransformer.swift */,
254+
CC5E530B2359FF3A00C72CE2 /* EscapeTransformer.swift */,
252255
CC7B603E22AEFDFF0092188C /* ParserUtil.swift */,
253256
);
254257
path = Parser;
@@ -328,7 +331,7 @@
328331
isa = PBXProject;
329332
attributes = {
330333
LastSwiftUpdateCheck = 1030;
331-
LastUpgradeCheck = 1100;
334+
LastUpgradeCheck = 1110;
332335
ORGANIZATIONNAME = "Matthias Zenger";
333336
TargetAttributes = {
334337
CC2AF6E7227CD98100BEA420 = {
@@ -385,6 +388,7 @@
385388
isa = PBXSourcesBuildPhase;
386389
buildActionMask = 2147483647;
387390
files = (
391+
CC5E530C2359FF3A00C72CE2 /* EscapeTransformer.swift in Sources */,
388392
CC6988C4227CDF810021C7E1 /* BlockquoteParser.swift in Sources */,
389393
CC8CBC17227F39C00084683B /* ListItemParser.swift in Sources */,
390394
CCB033D2227CDB2F00E1C4DC /* BlockParser.swift in Sources */,
@@ -587,7 +591,7 @@
587591
"@executable_path/../Frameworks",
588592
"@loader_path/Frameworks",
589593
);
590-
MARKETING_VERSION = 0.1.3;
594+
MARKETING_VERSION = 0.2.0;
591595
PRODUCT_BUNDLE_IDENTIFIER = net.objecthub.MarkdownKit;
592596
PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)";
593597
SKIP_INSTALL = YES;
@@ -616,7 +620,7 @@
616620
"@executable_path/../Frameworks",
617621
"@loader_path/Frameworks",
618622
);
619-
MARKETING_VERSION = 0.1.3;
623+
MARKETING_VERSION = 0.2.0;
620624
PRODUCT_BUNDLE_IDENTIFIER = net.objecthub.MarkdownKit;
621625
PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)";
622626
SKIP_INSTALL = YES;
@@ -665,6 +669,7 @@
665669
CC72627222F318C0001AB6D9 /* Debug */ = {
666670
isa = XCBuildConfiguration;
667671
buildSettings = {
672+
CODE_SIGN_IDENTITY = "-";
668673
CODE_SIGN_STYLE = Automatic;
669674
DEVELOPMENT_TEAM = C72Z63N8M5;
670675
PRODUCT_NAME = "$(TARGET_NAME)";
@@ -675,6 +680,7 @@
675680
CC72627322F318C0001AB6D9 /* Release */ = {
676681
isa = XCBuildConfiguration;
677682
buildSettings = {
683+
CODE_SIGN_IDENTITY = "-";
678684
CODE_SIGN_STYLE = Automatic;
679685
DEVELOPMENT_TEAM = C72Z63N8M5;
680686
PRODUCT_NAME = "$(TARGET_NAME)";

MarkdownKit.xcodeproj/xcshareddata/xcschemes/MarkdownKit.xcscheme

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<Scheme
3-
LastUpgradeVersion = "1100"
3+
LastUpgradeVersion = "1110"
44
version = "1.3">
55
<BuildAction
66
parallelizeBuildables = "YES"

MarkdownKit.xcodeproj/xcshareddata/xcschemes/MarkdownKitProcess.xcscheme

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<Scheme
3-
LastUpgradeVersion = "1100"
3+
LastUpgradeVersion = "1110"
44
version = "1.3">
55
<BuildAction
66
parallelizeBuildables = "YES"

README.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<p>
44
<a href="https://developer.apple.com/osx/"><img src="https://img.shields.io/badge/Platform-macOS-blue.svg?style=flat" alt="Platform: macOS" /></a>
55
<a href="https://developer.apple.com/swift/"><img src="https://img.shields.io/badge/Language-Swift%205.1-green.svg?style=flat" alt="Language: Swift 5.1" /></a>
6-
<a href="https://developer.apple.com/xcode/"><img src="https://img.shields.io/badge/IDE-Xcode%2011.0-orange.svg?style=flat" alt="IDE: Xcode 11.0" /></a>
6+
<a href="https://developer.apple.com/xcode/"><img src="https://img.shields.io/badge/IDE-Xcode%2011.1-orange.svg?style=flat" alt="IDE: Xcode 11.1" /></a>
77
<a href="https://raw.github.com/objecthub/swift-markdownkit/master/LICENSE"><img src="http://img.shields.io/badge/License-Apache-lightgrey.svg?style=flat" alt="License: Apache" /></a>
88
</p>
99

@@ -191,10 +191,9 @@ where: <source> is either a Markdown file or a directory containing Markdown fil
191191

192192
There are a number of limitations and known issues:
193193

194-
- The Markdown parser currently does not fully support _link reference definitions_. It is possible to define a
195-
link reference, but usage of such references is currently not supported.
196-
- Escaping of characters is not handled correctly when generating plain text or when generating HTML or
197-
attributed strings.
194+
- The Markdown parser currently does not fully support _link reference definitions_ in a CommonMark-compliant
195+
fashion. It is possible to define link reference definitions and use them, but for some corner cases, the current
196+
implementation behaves differently from the spec.
198197

199198
## Requirements
200199

@@ -203,7 +202,7 @@ The command-line tool can be compiled with the _Swift Package Manager_, so _Xcod
203202
for that. Similarly, just for compiling the framework and trying the command-line tool in _Xcode_, the
204203
_Swift Package Manager_ is not needed.
205204

206-
- [Xcode 11.0](https://developer.apple.com/xcode/)
205+
- [Xcode 11.1](https://developer.apple.com/xcode/)
207206
- [Swift 5.1](https://developer.apple.com/swift/)
208207
- [Swift Package Manager](https://swift.org/package-manager/)
209208

Sources/MarkdownKit/Info.plist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<key>CFBundlePackageType</key>
1616
<string>FMWK</string>
1717
<key>CFBundleShortVersionString</key>
18-
<string>0.1.3</string>
18+
<string>0.2.0</string>
1919
<key>CFBundleVersion</key>
2020
<string>$(CURRENT_PROJECT_VERSION)</string>
2121
<key>NSHumanReadableCopyright</key>
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
//
2+
// EscapeTransformer.swift
3+
// MarkdownKit
4+
//
5+
// Created by Matthias Zenger on 18/10/2019.
6+
// Copyright © 2019 Google LLC.
7+
//
8+
// Licensed under the Apache License, Version 2.0 (the "License");
9+
// you may not use this file except in compliance with the License.
10+
// You may obtain a copy of the License at
11+
//
12+
// http://www.apache.org/licenses/LICENSE-2.0
13+
//
14+
// Unless required by applicable law or agreed to in writing, software
15+
// distributed under the License is distributed on an "AS IS" BASIS,
16+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
// See the License for the specific language governing permissions and
18+
// limitations under the License.
19+
//
20+
21+
import Foundation
22+
23+
///
24+
/// An inline transformer which removes backslash escapes.
25+
///
26+
public final class EscapeTransformer: InlineTransformer {
27+
28+
public override func transform(_ fragment: TextFragment,
29+
from iterator: inout Text.Iterator,
30+
into res: inout Text) -> TextFragment? {
31+
switch fragment {
32+
case .text(let str):
33+
res.append(fragment: .text(self.resolveEscapes(str)))
34+
case .link(let inner, let uri, let title):
35+
res.append(fragment: .link(self.transform(inner), uri, self.resolveEscapes(title)))
36+
case .image(let inner, let uri, let title):
37+
res.append(fragment: .image(self.transform(inner), uri, self.resolveEscapes(title)))
38+
default:
39+
return super.transform(fragment, from: &iterator, into: &res)
40+
}
41+
return iterator.next()
42+
}
43+
44+
private func resolveEscapes(_ str: String?) -> String? {
45+
if let str = str {
46+
return String(self.resolveEscapes(Substring(str)))
47+
} else {
48+
return nil
49+
}
50+
}
51+
52+
private func resolveEscapes(_ str: Substring) -> Substring {
53+
guard !str.isEmpty else {
54+
return str
55+
}
56+
var res: String? = nil
57+
var i = str.startIndex
58+
while i < str.endIndex {
59+
if str[i] == "\\" {
60+
if res == nil {
61+
res = String(str[str.startIndex..<i])
62+
}
63+
i = str.index(after: i)
64+
guard i < str.endIndex else {
65+
break
66+
}
67+
}
68+
res?.append(str[i])
69+
i = str.index(after: i)
70+
}
71+
guard res == nil else {
72+
return Substring(res!)
73+
}
74+
return str
75+
}
76+
}

Sources/MarkdownKit/Parser/InlineParser.swift

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,62 @@ public class InlineParser {
3030
/// Sequence of inline transformers which implement the inline parsing functionality.
3131
private var inlineTransformers: [InlineTransformer]
3232

33+
/// Blocks of input document
34+
private let block: Block
35+
36+
/// Link reference declarations
37+
public private(set) var linkRefDef: [String : (String, String?)]
38+
3339
/// Initializer
34-
init(inlineTransformers: [InlineTransformer.Type]) {
40+
init(inlineTransformers: [InlineTransformer.Type], input: Block) {
41+
self.block = input
42+
self.linkRefDef = [:]
3543
self.inlineTransformers = []
3644
for transformerType in inlineTransformers {
37-
self.inlineTransformers.append(transformerType.init())
45+
self.inlineTransformers.append(transformerType.init(owner: self))
46+
}
47+
}
48+
49+
/// Traverses the input block and applies all inline transformers to all text.
50+
public func parse() -> Block {
51+
// First, collect all link reference definitions
52+
self.collectLinkRefDef(self.block)
53+
// Second, apply inline transformers
54+
return self.parse(self.block)
55+
}
56+
57+
/// Traverses a Markdown block and enters link reference definitions into `linkRefDef`.
58+
public func collectLinkRefDef(_ block: Block) {
59+
switch block {
60+
case .document(let blocks):
61+
self.collectLinkRefDef(blocks)
62+
case .blockquote(let blocks):
63+
self.collectLinkRefDef(blocks)
64+
case .list(_, _, let blocks):
65+
self.collectLinkRefDef(blocks)
66+
case .listItem(_, _, let blocks):
67+
self.collectLinkRefDef(blocks)
68+
case .referenceDef(let label, let dest, let title):
69+
if title.isEmpty {
70+
let canonical = label.trimmingCharacters(in: .whitespacesAndNewlines).lowercased()
71+
self.linkRefDef[canonical] = (String(dest), nil)
72+
} else {
73+
var str = ""
74+
for line in title {
75+
str += line.description
76+
}
77+
self.linkRefDef[label] = (String(dest), str)
78+
}
79+
default:
80+
break
81+
}
82+
}
83+
84+
/// Traverses an array of Markdown blocks and enters link reference definitions
85+
/// into `linkRefDef`.
86+
public func collectLinkRefDef(_ blocks: Blocks) {
87+
for block in blocks {
88+
self.collectLinkRefDef(block)
3889
}
3990
}
4091

@@ -86,8 +137,4 @@ public class InlineParser {
86137
}
87138
return res
88139
}
89-
90-
public func transform(_ line: Substring) -> Substring {
91-
return line
92-
}
93140
}

Sources/MarkdownKit/Parser/InlineTransformer.swift

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@ import Foundation
2828
///
2929
open class InlineTransformer {
3030

31-
required public init() {}
31+
public unowned let owner: InlineParser
32+
33+
required public init(owner: InlineParser) {
34+
self.owner = owner
35+
}
3236

3337
open func transform(_ text: Text) -> Text {
3438
var res: Text = Text()
@@ -52,12 +56,12 @@ open class InlineTransformer {
5256
res.append(fragment: .emph(self.transform(inner)))
5357
case .strong(let inner):
5458
res.append(fragment: .strong(self.transform(inner)))
55-
case .link(let inner, let label, let title):
56-
res.append(fragment: .link(self.transform(inner), label, title))
59+
case .link(let inner, let uri, let title):
60+
res.append(fragment: .link(self.transform(inner), uri, title))
5761
case .autolink(_):
5862
res.append(fragment: fragment)
59-
case .image(let inner, let label, let title):
60-
res.append(fragment: .image(self.transform(inner), label, title))
63+
case .image(let inner, let uri, let title):
64+
res.append(fragment: .image(self.transform(inner), uri, title))
6165
case .html(_):
6266
res.append(fragment: fragment)
6367
case .delimiter(_, _, _):

0 commit comments

Comments
 (0)