From 801b981b495496311b440a8e6dd7de09da471854 Mon Sep 17 00:00:00 2001 From: Florian Friedrich Date: Fri, 9 Oct 2020 17:37:27 +0200 Subject: [PATCH] Centralize management of tags and views --- .../RouteDocs/DefaultDocsView/doc_list.leaf | 2 +- Sources/RouteDocs/DocsViewContext.swift | 15 ++++++---- ...View.swift => Application+SetupDocs.swift} | 28 +++++++++++++++++-- Sources/RouteDocs/Tags/HashedTag.swift | 16 ----------- 4 files changed, 36 insertions(+), 25 deletions(-) rename Sources/RouteDocs/Extensions/{Request+DefaultDocsView.swift => Application+SetupDocs.swift} (60%) delete mode 100644 Sources/RouteDocs/Tags/HashedTag.swift diff --git a/Sources/RouteDocs/DefaultDocsView/doc_list.leaf b/Sources/RouteDocs/DefaultDocsView/doc_list.leaf index 3ccc2cd..3efa871 100644 --- a/Sources/RouteDocs/DefaultDocsView/doc_list.leaf +++ b/Sources/RouteDocs/DefaultDocsView/doc_list.leaf @@ -1,6 +1,6 @@ #for(groupedDoc in groupedDocumentations): #extend("doc_grouped_documentation"): - #export("groupID"):#escaped(hashed(lowercased(groupedDoc.groupName)))#endexport + #export("groupID"):#(groupedDoc.id)#endexport #endextend #endfor
diff --git a/Sources/RouteDocs/DocsViewContext.swift b/Sources/RouteDocs/DocsViewContext.swift index a55f80a..c046403 100644 --- a/Sources/RouteDocs/DocsViewContext.swift +++ b/Sources/RouteDocs/DocsViewContext.swift @@ -59,6 +59,7 @@ public struct DocsViewContext: Encodable { } public struct GroupedDocumentation: Encodable { + public let id: Int public let groupName: String public let documentations: [Documentation] } @@ -152,13 +153,15 @@ extension DocsViewContext { usingName namePath: KeyPath? = nil) where Docs.Element == EndpointDocumentable { - let allDocsByGroup = Dictionary(grouping: documentables.lazy.compactMap(\.documentation), by: { $0.groupName ?? "" }) - otherDocumentations = allDocsByGroup["", default: []].lazy.contextDocumentation(orderedBy: sortPath, usingName: namePath) + let allDocsByGroup = Dictionary(grouping: documentables.lazy.compactMap(\.documentation), by: \.groupName) + otherDocumentations = allDocsByGroup[nil, default: []].lazy.contextDocumentation(orderedBy: sortPath, usingName: namePath) groupedDocumentations = allDocsByGroup.lazy - .filter { !$0.key.isEmpty } - .map { DocsViewContext.GroupedDocumentation(groupName: $0.key, - documentations: $0.value.contextDocumentation(orderedBy: sortPath, usingName: namePath)) } - .sorted { $0.groupName < $1.groupName } + .compactMap { (key, elem) in key.map { (key: $0, value: elem) } } + .sorted { $0.key < $1.key } + .enumerated() + .map { GroupedDocumentation(id: $0.offset, + groupName: $0.element.key, + documentations: $0.element.value.contextDocumentation(orderedBy: sortPath, usingName: namePath)) } } public init(documentables: Docs, usingName namePath: KeyPath? = nil) diff --git a/Sources/RouteDocs/Extensions/Request+DefaultDocsView.swift b/Sources/RouteDocs/Extensions/Application+SetupDocs.swift similarity index 60% rename from Sources/RouteDocs/Extensions/Request+DefaultDocsView.swift rename to Sources/RouteDocs/Extensions/Application+SetupDocs.swift index 41e2b00..86e6faa 100644 --- a/Sources/RouteDocs/Extensions/Request+DefaultDocsView.swift +++ b/Sources/RouteDocs/Extensions/Application+SetupDocs.swift @@ -28,14 +28,38 @@ extension Application { public var defaultDocsLeafSource: some LeafSource { NIOLeafFiles.defaultDocs(with: fileio) } } +extension LeafSources { + @inlinable + public func addDefaultDocsSource(with fileio: NonBlockingFileIO) throws { + try register(source: "docs", using: NIOLeafFiles.defaultDocs(with: fileio)) + } + + @inlinable + public func addDefaultDocsSource(for app: Application) throws { + try register(source: "docs", using: app.defaultDocsLeafSource) + } +} + extension LeafRenderer { @inlinable public func addDefaultDocsSource(with fileio: NonBlockingFileIO) throws { - try sources.register(using: NIOLeafFiles.defaultDocs(with: fileio)) + try sources.addDefaultDocsSource(with: fileio) } @inlinable public func addDefaultDocsSource(for app: Application) throws { - try sources.register(using: app.defaultDocsLeafSource) + try sources.addDefaultDocsSource(for: app) + } +} + +extension Application.Leaf { + public func registerDocumentationTags() { + tags["escaped"] = EscapeTag() + } + + @inlinable + public func setupRouteDocs() throws { + registerDocumentationTags() + try sources.addDefaultDocsSource(for: application) } } diff --git a/Sources/RouteDocs/Tags/HashedTag.swift b/Sources/RouteDocs/Tags/HashedTag.swift deleted file mode 100644 index cd01587..0000000 --- a/Sources/RouteDocs/Tags/HashedTag.swift +++ /dev/null @@ -1,16 +0,0 @@ -import Leaf - -public struct HashedTag: LeafTag { - public init() {} - - public func render(_ ctx: LeafContext) throws -> LeafData { - try ctx.requireParameterCount(1) - if ctx.body?.isEmpty != true { // A bug in leaf currently gives all tags an empty body. - try ctx.requireNoBody() - } - guard let string = ctx.parameters[0].string else { - return ctx.parameters[0] - } - return .string(string) - } -}