feat: Adds section and section style, needs tests
This commit is contained in:
@@ -11,7 +11,7 @@ public extension ExampleSection {
|
||||
}
|
||||
|
||||
func exampleStyle<S: ExampleStyle>(_ style: S) -> some TextNode {
|
||||
DefaultExamplesStyle().render(content: .init(
|
||||
DefaultExamplesStyle(exampleStyle: style).render(content: .init(
|
||||
header: header,
|
||||
label: label,
|
||||
examples: examples
|
||||
|
||||
37
Sources/CliDoc/Modifiers/SectionStyle.swift
Normal file
37
Sources/CliDoc/Modifiers/SectionStyle.swift
Normal file
@@ -0,0 +1,37 @@
|
||||
public extension Section {
|
||||
|
||||
@inlinable
|
||||
func style<S: SectionStyle>(_ style: S) -> some TextNode {
|
||||
style.render(content: .init(header: header, content: content, footer: footer))
|
||||
}
|
||||
}
|
||||
|
||||
public struct SectionConfiguration {
|
||||
public let header: any TextNode
|
||||
public let content: any TextNode
|
||||
public let footer: any TextNode
|
||||
|
||||
@usableFromInline
|
||||
init(header: any TextNode, content: any TextNode, footer: any TextNode) {
|
||||
self.header = header
|
||||
self.content = content
|
||||
self.footer = footer
|
||||
}
|
||||
}
|
||||
|
||||
public protocol SectionStyle: NodeModifier where Content == SectionConfiguration {}
|
||||
|
||||
public extension SectionStyle where Self == DefaultSectionStyle {
|
||||
static var `default`: Self { DefaultSectionStyle() }
|
||||
}
|
||||
|
||||
public struct DefaultSectionStyle: SectionStyle {
|
||||
|
||||
public func render(content: SectionConfiguration) -> some TextNode {
|
||||
VStack {
|
||||
content.header
|
||||
content.content
|
||||
content.footer.textStyle(.italic)
|
||||
}
|
||||
}
|
||||
}
|
||||
46
Sources/CliDoc/Nodes/Section.swift
Normal file
46
Sources/CliDoc/Nodes/Section.swift
Normal file
@@ -0,0 +1,46 @@
|
||||
public struct Section<Header: TextNode, Content: TextNode, Footer: TextNode>: TextNode {
|
||||
|
||||
@usableFromInline
|
||||
let header: Header
|
||||
|
||||
@usableFromInline
|
||||
let content: Content
|
||||
|
||||
@usableFromInline
|
||||
let footer: Footer
|
||||
|
||||
@inlinable
|
||||
public init(
|
||||
@TextBuilder header: () -> Header,
|
||||
@TextBuilder content: () -> Content,
|
||||
@TextBuilder footer: () -> Footer
|
||||
) {
|
||||
self.header = header()
|
||||
self.content = content()
|
||||
self.footer = footer()
|
||||
}
|
||||
|
||||
public var body: some TextNode {
|
||||
style(.default)
|
||||
}
|
||||
}
|
||||
|
||||
public extension Section where Footer == Empty {
|
||||
@inlinable
|
||||
init(
|
||||
@TextBuilder header: () -> Header,
|
||||
@TextBuilder content: () -> Content
|
||||
) {
|
||||
self.init(header: header, content: content) { Empty() }
|
||||
}
|
||||
}
|
||||
|
||||
public extension Section where Header == Empty {
|
||||
@inlinable
|
||||
init(
|
||||
@TextBuilder content: () -> Content,
|
||||
@TextBuilder footer: () -> Footer
|
||||
) {
|
||||
self.init(header: { Empty() }, content: content, footer: footer)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user