Skip to content

Commit 752b2f7

Browse files
Keep playground structure (#153)
* keep the consistency between render and playgrounds structure * unified path component from an playground and page renderurl minor changes minor changes * comments in PR about allow only aphanumeric chars * comments in PR
1 parent f19b8a1 commit 752b2f7

File tree

7 files changed

+60
-32
lines changed

7 files changed

+60
-32
lines changed

project/Component/NefCarbon/NefCarbon.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,12 @@ public struct Carbon {
114114
}
115115

116116
private func writePlayground(playground: RenderingURL, content: PlaygroundOutput, output: URL) -> EnvIO<Environment, RenderError, URL> {
117-
content.traverse { info in self.writePage(pagePathComponent: "\(playground.escapedTitle)/\(info.page.escapedTitle)", content: info.output, output: output) }
118-
.map { _ in playground.url }^
117+
content.traverse { info -> EnvIO<Environment, RenderError, URL> in
118+
let pathComponent = RenderEnvironmentInfo.info(playground: playground, page: info.page).pathComponent
119+
return self.writePage(pagePathComponent: !pathComponent.isEmpty ? pathComponent : info.page.escapedTitle,
120+
content: info.output,
121+
output: output)
122+
}.map { _ in playground.url }^
119123
}
120124

121125
// MARK: private <utils>

project/Component/NefCommon/Models/RenderingURL.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ public struct RenderingURL {
99
public let title: String
1010

1111
public var escapedTitle: String {
12-
title.lowercased().replacingOccurrences(of: "?", with: "-")
13-
.replacingOccurrences(of: " ", with: "-")
12+
String(title.lowercased().unicodeScalars.filter { $0.isAlphanumeric || $0 == " " || $0 == "-" })
13+
.replacingOccurrences(of: " ", with: "-")
1414
}
1515

1616
public init(url: URL, title: String) {
@@ -19,6 +19,12 @@ public struct RenderingURL {
1919
}
2020
}
2121

22+
extension Unicode.Scalar {
23+
var isAlphanumeric: Bool {
24+
CharacterSet.alphanumerics.contains(self)
25+
}
26+
}
27+
2228
// MARK: - public <helpers>
2329
extension RenderingURL: CustomStringConvertible {
2430
public var description: String { title }

project/Component/NefJekyll/Models/RenderJekyllEnvironment.swift

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,9 @@ public struct RenderJekyllEnvironment<A> {
4242
static var data: String { "_data" }
4343

4444
static func permalink(info: RenderEnvironmentInfo) -> IO<CoreRenderError, String> {
45-
switch info {
46-
case let .info(playground, page):
47-
return IO.pure("/\(docs)/\(playground.escapedTitle)/\(page.escapedTitle)/")^
48-
default:
49-
return IO.raiseError(.renderEmpty)^
50-
}
45+
let pathComponent = info.pathComponent
46+
return pathComponent.isEmpty ? IO.raiseError(.renderEmpty)^
47+
: IO.pure("/\(docs)/\(info.pathComponent)/")^
5148
}
5249

5350
// MARK: - init <helper>

project/Component/NefJekyll/NefJekyll.swift

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,12 @@ public struct Jekyll {
6464
}
6565

6666
private func writePage(page: RenderingURL, content: RenderingOutput, output: URL) -> EnvIO<Environment, RenderError, URL> {
67+
writePage(pathComponent: page.escapedTitle, content: content, output: output)
68+
}
69+
70+
private func writePage(pathComponent: String, content: RenderingOutput, output: URL) -> EnvIO<Environment, RenderError, URL> {
6771
EnvIO { env in
68-
let file = output.appendingPathComponent(page.escapedTitle).appendingPathComponent("README.md")
72+
let file = output.appendingPathComponent(pathComponent).appendingPathComponent("README.md")
6973
return env.persistence.writePage(content, file).provide(env.fileSystem)
7074
.map { _ in file }^.mapError { _ in .page(file) }
7175
}^
@@ -76,8 +80,12 @@ public struct Jekyll {
7680
}
7781

7882
private func writePlayground(playground: RenderingURL, content: PlaygroundOutput, output: URL) -> EnvIO<Environment, RenderError, URL> {
79-
content.traverse { info in self.writePage(page: info.page, content: info.output, output: output) }
80-
.map { _ in playground.url }^
83+
content.traverse { info -> EnvIO<Environment, RenderError, URL> in
84+
let pathComponent = RenderEnvironmentInfo.info(playground: playground, page: info.page).pathComponent
85+
return self.writePage(pathComponent: !pathComponent.isEmpty ? pathComponent : info.page.escapedTitle,
86+
content: info.output,
87+
output: output)
88+
}.map { _ in playground.url }^
8189
}
8290

8391
private func writePage(_ page: RenderingOutput, into file: URL) -> EnvIO<Environment, RenderError, Void> {
@@ -137,9 +145,7 @@ public struct Jekyll {
137145
info.traverse { (page, _, _) in sidebarPage(playground: playground, page: page) }.map { sidebarPages in
138146
"""
139147
- title: \(playground)
140-
141148
nested_options:
142-
143149
\(sidebarPages.all().joined(separator: "\n\n"))
144150
"""
145151
}^

project/Component/NefMarkdown/NefMarkdown.swift

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,12 @@ public struct Markdown {
5959
}
6060

6161
private func writePage(page: RenderingURL, content: RenderingOutput, output: URL) -> EnvIO<Environment, RenderError, URL> {
62+
writePage(pathComponent: page.escapedTitle, content: content, output: output)
63+
}
64+
65+
private func writePage(pathComponent: String, content: RenderingOutput, output: URL) -> EnvIO<Environment, RenderError, URL> {
6266
EnvIO { env in
63-
let file = output.appendingPathComponent(page.escapedTitle).appendingPathExtension("md")
67+
let file = output.appendingPathComponent(pathComponent).appendingPathExtension("md")
6468
return env.persistence.writePage(content, file).provide(env.fileSystem)
6569
.map { _ in file }^.mapError { _ in .page(file) }
6670
}^
@@ -71,8 +75,12 @@ public struct Markdown {
7175
}
7276

7377
private func writePlayground(playground: RenderingURL, content: PlaygroundOutput, output: URL) -> EnvIO<Environment, RenderError, URL> {
74-
content.traverse { info in self.writePage(page: info.page, content: info.output, output: output) }
75-
.map { _ in playground.url }^
78+
content.traverse { info -> EnvIO<Environment, RenderError, URL> in
79+
let pathComponent = RenderEnvironmentInfo.info(playground: playground, page: info.page).pathComponent
80+
return self.writePage(pathComponent: !pathComponent.isEmpty ? pathComponent : info.page.escapedTitle,
81+
content: info.output,
82+
output: output)
83+
}.map { _ in playground.url }^
7684
}
7785

7886
private func write(page: RenderingOutput, into file: URL) -> EnvIO<Environment, RenderError, Void> {

project/Component/NefRender/Models/RenderEnvironment.swift

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,17 @@ public enum RenderEnvironmentInfo {
2828
case info(playground: RenderingURL, page: RenderingURL)
2929
case empty
3030

31-
public var data: (playground: RenderingURL, page: RenderingURL)? {
31+
public var pathComponent: String {
32+
guard let data = data else { return "" }
33+
return "\(data.playground.escapedTitle)/\(data.page.escapedTitle)"
34+
}
35+
36+
var data: (playground: RenderingURL, page: RenderingURL)? {
3237
switch self {
33-
case let .info(playground, page): return (playground, page)
34-
default: return nil
38+
case let .info(playground, page):
39+
return (playground, page)
40+
default:
41+
return nil
3542
}
3643
}
3744
}

project/Component/NefRender/Render.swift

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public struct Render<A> {
9999
|<-env.progressReport.inProgress(step),
100100
rendered <- env.nodePrinter(content).provide(info)
101101
.mapError { e in .content(info: e) },
102-
yield: rendered.get)^
102+
yield: rendered.get)^
103103
.step(step, reportCompleted: env.progressReport)
104104
}
105105
}
@@ -112,12 +112,12 @@ public struct Render<A> {
112112
let step = RenderEvent.gettingPagesFromPlayground(playground.description)
113113

114114
return binding(
115-
|<-env.progressReport.inProgress(step),
116-
pages <- env.xcodePlaygroundSystem.pages(in: playground.url)
117-
.provide(env.fileSystem)
118-
.mapError { _ in .getPages(playground: playground.url) },
115+
|<-env.progressReport.inProgress(step),
116+
pages <- env.xcodePlaygroundSystem.pages(in: playground.url)
117+
.provide(env.fileSystem)
118+
.mapError { _ in .getPages(playground: playground.url) },
119119
rendererPages <- pages.get.traverse { url in RenderingURL(url: url, title: self.pageName(url)).io() },
120-
yield: rendererPages.get)^
120+
yield: rendererPages.get)^
121121
.step(step, reportCompleted: env.progressReport)
122122
}
123123
}
@@ -129,12 +129,12 @@ public struct Render<A> {
129129
let step = RenderEvent.gettingPlaygrounds(folder.lastPathComponent.removeExtension)
130130

131131
return binding(
132-
|<-env.progressReport.inProgress(step),
133-
playgrounds <- env.xcodePlaygroundSystem.linkedPlaygrounds(at: folder)
134-
.provide(env.fileSystem)
135-
.mapError { _ in .getPlaygrounds(folder: folder) },
132+
|<-env.progressReport.inProgress(step),
133+
playgrounds <- env.xcodePlaygroundSystem.linkedPlaygrounds(at: folder)
134+
.provide(env.fileSystem)
135+
.mapError { _ in .getPlaygrounds(folder: folder) },
136136
rendered <- playgrounds.get.traverse { url in RenderingURL(url: url, title: self.playgroundName(url)).io() },
137-
yield: rendered.get)^
137+
yield: rendered.get)^
138138
.step(step, reportCompleted: env.progressReport)
139139
}
140140
}

0 commit comments

Comments
 (0)