Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug where .standAloneFiles doesn't work with section indexes #10

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Sources/Publish/API/Item.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,17 @@ private extension Item {
"\(sectionID.rawValue)/\(relativePath)"
}
}

public extension Item {
/// An `Item` location depends on the site's file mode. This function returns the appropriat path for the site.
/// - Parameter site: The site being published
/// - Returns: Path to the `Item` suitable for use in a list of links in a generated section index page.
func linkPath(for site:Site) -> String {
switch site.fileMode {
case .foldersAndIndexFiles:
return "/\(sectionID.rawValue)/\(relativePath)"
case .standAloneFiles:
return "/\(sectionID.rawValue)/\(relativePath).html"
}
}
}
4 changes: 2 additions & 2 deletions Sources/Publish/API/PublishingStep.swift
Original file line number Diff line number Diff line change
Expand Up @@ -333,13 +333,13 @@ public extension PublishingStep {
static func generateHTML(
withTheme theme: Theme<Site>,
indentation: Indentation.Kind? = nil,
fileMode: HTMLFileMode = .foldersAndIndexFiles
fileMode: HTMLFileMode? = nil
) -> Self {
step(named: "Generate HTML") { context in
let generator = HTMLGenerator(
theme: theme,
indentation: indentation,
fileMode: fileMode,
fileMode: fileMode ?? context.site.fileMode,
context: context
)

Expand Down
2 changes: 1 addition & 1 deletion Sources/Publish/API/Theme+Foundation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ private struct ItemList<Site: Website>: Component {
var body: Component {
List(items) { item in
Article {
H1(Link(item.title, url: item.path.absoluteString))
H1(Link(item.title, url: item.linkPath(for: site)))
ItemTagList(item: item, site: site)
Paragraph(item.description)
}
Expand Down
5 changes: 5 additions & 0 deletions Sources/Publish/API/Website.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public protocol Website {
/// The configuration to use when generating tag HTML for the website.
/// If this is `nil`, then no tag HTML will be generated.
var tagHTMLConfig: TagHTMLConfiguration? { get }
/// Preferred file-generation mode. Default is `.foldersAndIndexFiles`
var fileMode: HTMLFileMode { get }
}

// MARK: - Defaults
Expand Down Expand Up @@ -154,4 +156,7 @@ public extension Website {
func url(for location: Location) -> URL {
url(for: location.path)
}

/// Provide a default file mode.
var fileMode: HTMLFileMode { .foldersAndIndexFiles }
}
26 changes: 19 additions & 7 deletions Tests/PublishTests/Infrastructure/PublishTestCase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ class PublishTestCase: XCTestCase {
func publishWebsite(
in folder: Folder? = nil,
using steps: [PublishingStep<WebsiteStub.WithoutItemMetadata>],
content: [Path : String] = [:]
content: [Path : String] = [:],
fileMode: HTMLFileMode = .foldersAndIndexFiles
) throws -> PublishedWebsite<WebsiteStub.WithoutItemMetadata> {
try performWebsitePublishing(
in: folder,
using: steps,
files: content,
filePathPrefix: "Content/"
filePathPrefix: "Content/",
fileMode: fileMode
)
}

Expand All @@ -33,9 +35,12 @@ class PublishTestCase: XCTestCase {
plugins: [Plugin<WebsiteStub.WithoutItemMetadata>] = [],
expectedHTML: [Path : String],
allowWhitelistedOutputFiles: Bool = true,
fileMode: HTMLFileMode = .foldersAndIndexFiles,
file: StaticString = #file,
line: UInt = #line
) throws {
site.fileMode = fileMode

let folder = try folder ?? Folder.createTemporary()

let contentFolderName = "Content"
Expand Down Expand Up @@ -65,14 +70,16 @@ class PublishTestCase: XCTestCase {
in folder: Folder? = nil,
using steps: [PublishingStep<WebsiteStub.WithPodcastMetadata>],
content: [Path : String] = [:],
fileMode: HTMLFileMode = .foldersAndIndexFiles,
file: StaticString = #file,
line: UInt = #line
) throws {
try performWebsitePublishing(
in: folder,
using: steps,
files: content,
filePathPrefix: "Content/"
filePathPrefix: "Content/",
fileMode: fileMode
)
}

Expand Down Expand Up @@ -136,12 +143,14 @@ class PublishTestCase: XCTestCase {
func publishWebsite<T: WebsiteItemMetadata>(
withItemMetadataType itemMetadataType: T.Type,
using steps: [PublishingStep<WebsiteStub.WithItemMetadata<T>>],
content: [Path : String] = [:]
content: [Path : String] = [:],
fileMode: HTMLFileMode = .foldersAndIndexFiles
) throws -> PublishedWebsite<WebsiteStub.WithItemMetadata<T>> {
try performWebsitePublishing(
using: steps,
files: content,
filePathPrefix: "Content/"
filePathPrefix: "Content/",
fileMode: fileMode
)
}

Expand Down Expand Up @@ -197,13 +206,16 @@ private extension PublishTestCase {
in folder: Folder? = nil,
using steps: [PublishingStep<T>],
files: [Path : String],
filePathPrefix: String = ""
filePathPrefix: String = "",
fileMode: HTMLFileMode
) throws -> PublishedWebsite<T> {
let folder = try folder ?? Folder.createTemporary()

try addFiles(withContent: files, to: folder, pathPrefix: filePathPrefix)

return try T().publish(
let site = T()
site.fileMode = fileMode
return try site.publish(
at: Path(folder.path),
using: steps
)
Expand Down
3 changes: 2 additions & 1 deletion Tests/PublishTests/Infrastructure/WebsiteStub.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ class WebsiteStub {
var imagePath: Path? = nil
var faviconPath: Path? = nil
var tagHTMLConfig: TagHTMLConfiguration? = .default

var fileMode: HTMLFileMode = .foldersAndIndexFiles

required init() {}

func title(for sectionID: WebsiteStub.SectionID) -> String {
Expand Down
12 changes: 7 additions & 5 deletions Tests/PublishTests/Tests/HTMLGenerationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -259,11 +259,13 @@ final class HTMLGenerationTests: PublishTestCase {
let folder = try Folder.createTemporary()
let theme = Theme(htmlFactory: htmlFactory)

try publishWebsite(in: folder, using: [
.addItem(Item.stub(withPath: "item").setting(\.tags, to: ["tag"])),
.addItem(Item.stub(withPath: "rawValueItem", sectionID: .customRawValue).setting(\.tags, to: ["tag"])),
.generateHTML(withTheme: theme, fileMode: .standAloneFiles)
])
try publishWebsite(in: folder,
using: [
.addItem(Item.stub(withPath: "item").setting(\.tags, to: ["tag"])),
.addItem(Item.stub(withPath: "rawValueItem", sectionID: .customRawValue).setting(\.tags, to: ["tag"])),
.generateHTML(withTheme: theme)
],
fileMode: .standAloneFiles)

try verifyOutput(
in: folder,
Expand Down