Skip to content

Commit 7e11c47

Browse files
authored
Copy static assets like custom templates before writing out render nodes. (#410)
This copies custom headers and footers into all the index.html files when using --transform-for-static-hosting and --experimental-enable-custom-templates. Adds a test for this as well. Resolves rdar://86298113.
1 parent 74a23a7 commit 7e11c47

File tree

2 files changed

+88
-7
lines changed

2 files changed

+88
-7
lines changed

Sources/SwiftDocC/Infrastructure/DocumentationConverter.swift

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,9 @@ public struct DocumentationConverter: DocumentationConverterProtocol {
246246
let renderContext = RenderContext(documentationContext: context, bundle: bundle)
247247

248248
try outputConsumer.consume(renderReferenceStore: renderContext.store)
249+
250+
// Copy images, sample files, and other static assets.
251+
try outputConsumer.consume(assetsInBundle: bundle)
249252

250253
let converter = DocumentationContextConverter(
251254
bundle: bundle,
@@ -328,13 +331,6 @@ public struct DocumentationConverter: DocumentationConverterProtocol {
328331
// If cancelled, return before producing outputs.
329332
guard !isConversionCancelled() else { return ([], []) }
330333

331-
// Copy images, sample files, and other static assets.
332-
do {
333-
try outputConsumer.consume(assetsInBundle: bundle)
334-
} catch {
335-
recordProblem(from: error, in: &conversionProblems, withIdentifier: "assets")
336-
}
337-
338334
// Write various metadata
339335
if emitDigest {
340336
do {

Tests/SwiftDocCUtilitiesTests/ConvertActionTests.swift

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2456,6 +2456,91 @@ class ConvertActionTests: XCTestCase {
24562456
expectedOutput.assertExist(at: result.outputs[0], fileManager: FileManager.default)
24572457
}
24582458

2459+
// Tests that custom templates are injected into the extra index.html files generated for static hosting.
2460+
func testConvertWithCustomTemplatesForStaticHosting() throws {
2461+
let info = InfoPlist(displayName: "TestConvertWithCustomTemplatesForStaticHosting", identifier: "com.test.example")
2462+
let index = TextFile(name: "index.html", utf8Content: """
2463+
<!DOCTYPE html>
2464+
<html lang="en">
2465+
<head>
2466+
<title>Test</title>
2467+
</head>
2468+
<body data-color-scheme="auto"><p>test for custom templates in static hosting</p></body>
2469+
</html>
2470+
""")
2471+
let template = Folder(name: "template", content: [index])
2472+
let header = TextFile(name: "header.html", utf8Content: """
2473+
<header>custom text for header</header>
2474+
""")
2475+
let footer = TextFile(name: "footer.html", utf8Content: """
2476+
<footer>custom text for footer</footer>
2477+
""")
2478+
2479+
// Adding this page will generate a file named:
2480+
// /documentation/testconvertwithcustomtemplatesforstatichosting/index.html
2481+
// ...which should have the custom header/footer if they're propagated correctly.
2482+
let technologyPage = TextFile(name: "TestConvertWithCustomTemplatesForStaticHosting.md", utf8Content: """
2483+
# TestConvertWithCustomTemplatesForStaticHosting
2484+
2485+
@Metadata {
2486+
@TechnologyRoot
2487+
}
2488+
2489+
An abstract.
2490+
2491+
## Overview
2492+
2493+
Text for a paragraph.
2494+
""")
2495+
let bundle = Folder(name: "TestConvertWithCustomTemplatesForStaticHosting.docc", content: [
2496+
info,
2497+
header,
2498+
footer,
2499+
technologyPage
2500+
])
2501+
2502+
let tempURL = try createTemporaryDirectory()
2503+
let targetURL = tempURL.appendingPathComponent("target", isDirectory: true)
2504+
2505+
let bundleURL = try bundle.write(inside: tempURL)
2506+
let templateURL = try template.write(inside: tempURL)
2507+
2508+
let dataProvider = try LocalFileSystemDataProvider(rootURL: bundleURL)
2509+
2510+
var action = try ConvertAction(
2511+
documentationBundleURL: bundleURL,
2512+
outOfProcessResolver: nil,
2513+
analyze: false,
2514+
targetDirectory: targetURL,
2515+
htmlTemplateDirectory: templateURL,
2516+
emitDigest: false,
2517+
currentPlatforms: nil,
2518+
dataProvider: dataProvider,
2519+
fileManager: FileManager.default,
2520+
temporaryDirectory: createTemporaryDirectory(),
2521+
experimentalEnableCustomTemplates: true,
2522+
transformForStaticHosting: true
2523+
)
2524+
let result = try action.perform(logHandle: .standardOutput)
2525+
2526+
// The custom template contents should be wrapped in <template> tags and
2527+
// prepended to the <body>
2528+
let expectedIndex = TextFile(name: "index.html", utf8Content: """
2529+
<!DOCTYPE html>
2530+
<html lang="en">
2531+
<head>
2532+
<title>Test</title>
2533+
</head>
2534+
<body data-color-scheme="auto"><template id="custom-footer">\(footer.utf8Content)</template><template id="custom-header">\(header.utf8Content)</template><p>test for custom templates in static hosting</p></body>
2535+
</html>
2536+
""")
2537+
2538+
let expectedTechnologyFolder = Folder(name: "TestConvertWithCustomTemplatesForStaticHosting".lowercased(), content: [expectedIndex])
2539+
let expectedDocsFolder = Folder(name: "documentation", content: [expectedTechnologyFolder])
2540+
let expectedOutput = Folder(name: ".docc-build", content: [expectedDocsFolder])
2541+
expectedOutput.assertExist(at: result.outputs[0], fileManager: FileManager.default)
2542+
}
2543+
24592544
func testConvertWithThemeSettings() throws {
24602545
let info = InfoPlist(displayName: "TestConvertWithThemeSettings", identifier: "com.test.example")
24612546
let index = TextFile(name: "index.html", utf8Content: """

0 commit comments

Comments
 (0)