Skip to content

Commit

Permalink
[static-hosting] Add support for custom headers and footers
Browse files Browse the repository at this point in the history
  • Loading branch information
ethan-kusters committed Nov 19, 2021
1 parent 5d4a4ea commit 119770f
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,15 @@ public struct ConvertAction: Action, RecreatingContext {
// Process Static Hosting is needed.
if transformForStaticHosting, let templateDirectory = htmlTemplateDirectory {
let dataProvider = try LocalFileSystemDataProvider(rootURL: temporaryFolder.appendingPathComponent("data"))
let transformer = try StaticHostableTransformer(dataProvider: dataProvider, fileManager: fileManager, outputURL: temporaryFolder, htmlTemplate: templateDirectory, staticHostingBasePath: staticHostingBasePath)
let transformer = try StaticHostableTransformer(
dataProvider: dataProvider,
fileManager: fileManager,
outputURL: temporaryFolder,
htmlTemplate: templateDirectory,
staticHostingBasePath: staticHostingBasePath,
enableCustomHeaderFooter: experimentalEnableCustomTemplates,
bundle: context.registeredBundles.first
)
try transformer.transform()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@ struct ConvertFileWritingConsumer: ConvertOutputConsumer {
var indexer: ConvertAction.Indexer?
let enableCustomTemplates: Bool

private enum CustomTemplateIdentifier: String {
case header = "custom-header"
case footer = "custom-footer"
}

init(targetFolder: URL, bundleRootFolder: URL?, fileManager: FileManagerProtocol, context: DocumentationContext, indexer: ConvertAction.Indexer?, enableCustomTemplates: Bool = false) {
self.targetFolder = targetFolder
self.bundleRootFolder = bundleRootFolder
Expand Down Expand Up @@ -171,14 +166,12 @@ struct ConvertFileWritingConsumer: ConvertOutputConsumer {
guard let indexData = fileManager.contents(atPath: index.path),
let indexContents = String(data: indexData, encoding: .utf8),
let templateData = fileManager.contents(atPath: templateURL.path),
let templateContents = String(data: templateData, encoding: .utf8),
let bodyTagRange = indexContents.range(of: "<body[^>]*>", options: .regularExpression) else {
let templateContents = String(data: templateData, encoding: .utf8) else {
return
}

let template = "<template id=\"\(id.rawValue)\">\(templateContents)</template>"
var newIndexContents = indexContents
newIndexContents.replaceSubrange(bodyTagRange, with: indexContents[bodyTagRange] + template)
newIndexContents.injectCustomTemplate(templateContents, identifiedBy: id)
try newIndexContents.write(to: index, atomically: true, encoding: .utf8)
}

Expand All @@ -189,6 +182,31 @@ struct ConvertFileWritingConsumer: ConvertOutputConsumer {
static var buildMetadataFileName = "metadata.json"
}

enum CustomTemplateIdentifier: String {
case header = "custom-header"
case footer = "custom-footer"
}

extension String {
/// Injects the given custom header or footer template into the given string.
mutating func injectCustomTemplate(
_ templateContents: String,
identifiedBy id: CustomTemplateIdentifier
) {
guard let bodyTagRange = range(
of: "<body[^>]*>",
options: .regularExpression
) else {
return
}

let template = """
<template id="\(id.rawValue)">\(templateContents)</template>
"""
replaceSubrange(bodyTagRange, with: self[bodyTagRange] + template)
}
}

enum Digest {
struct Assets: Codable {
let images: [ImageReference]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,13 @@ struct TransformForStaticHostingAction: Action {

// Create a StaticHostableTransformer targeted at the archive data folder
let dataProvider = try LocalFileSystemDataProvider(rootURL: rootURL.appendingPathComponent("data"))
let transformer = try StaticHostableTransformer(dataProvider: dataProvider, fileManager: fileManager, outputURL: outputURL, htmlTemplate: htmlTemplateDirectory, staticHostingBasePath: staticHostingBasePath)
let transformer = try StaticHostableTransformer(
dataProvider: dataProvider,
fileManager: fileManager,
outputURL: outputURL,
htmlTemplate: htmlTemplateDirectory,
staticHostingBasePath: staticHostingBasePath
)
try transformer.transform()

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,38 @@ class StaticHostableTransformer {
/// - fileManager: The FileManager to use for file processes.
/// - outputURL: The folder where the output will be placed
/// - indexHTML: The HTML to be used in the generated index.html file.
init(dataProvider: FileSystemProvider, fileManager: FileManagerProtocol, outputURL: URL, htmlTemplate: URL, staticHostingBasePath: String?) throws {
init(
dataProvider: FileSystemProvider,
fileManager: FileManagerProtocol,
outputURL: URL,
htmlTemplate: URL,
staticHostingBasePath: String?,
enableCustomHeaderFooter: Bool = false,
bundle: DocumentationBundle? = nil
) throws {
self.dataProvider = dataProvider
self.fileManager = fileManager
self.outputURL = outputURL

let indexFileName = staticHostingBasePath != nil ? HTMLTemplate.templateFileName.rawValue : HTMLTemplate.indexFileName.rawValue
let indexFileURL = htmlTemplate.appendingPathComponent(indexFileName)
var indexHTML = try String(contentsOfFile: indexFileURL.path)

if enableCustomHeaderFooter,
let customHeader = bundle?.customHeader,
let templateData = fileManager.contents(atPath: customHeader.path),
let templateContents = String(data: templateData, encoding: .utf8)
{
indexHTML.injectCustomTemplate(templateContents, identifiedBy: .header)
}

if enableCustomHeaderFooter,
let customFooter = bundle?.customFooter,
let templateData = fileManager.contents(atPath: customFooter.path),
let templateContents = String(data: templateData, encoding: .utf8)
{
indexHTML.injectCustomTemplate(templateContents, identifiedBy: .footer)
}


if let staticHostingBasePath = staticHostingBasePath {
Expand Down

0 comments on commit 119770f

Please sign in to comment.