Skip to content

Commit

Permalink
Centralize management of tags and views
Browse files Browse the repository at this point in the history
  • Loading branch information
ffried committed Oct 9, 2020
1 parent 69e7fb8 commit 801b981
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 25 deletions.
2 changes: 1 addition & 1 deletion Sources/RouteDocs/DefaultDocsView/doc_list.leaf
Original file line number Diff line number Diff line change
@@ -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
<div id="other-docs-accordion-all" class="mb-2 mt-3 mx-3">
Expand Down
15 changes: 9 additions & 6 deletions Sources/RouteDocs/DocsViewContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public struct DocsViewContext: Encodable {
}

public struct GroupedDocumentation: Encodable {
public let id: Int
public let groupName: String
public let documentations: [Documentation]
}
Expand Down Expand Up @@ -152,13 +153,15 @@ extension DocsViewContext {
usingName namePath: KeyPath<DocumentationType, String>? = 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<Docs: Sequence>(documentables: Docs, usingName namePath: KeyPath<DocumentationType, String>? = nil)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
16 changes: 0 additions & 16 deletions Sources/RouteDocs/Tags/HashedTag.swift

This file was deleted.

0 comments on commit 801b981

Please sign in to comment.