Skip to content

Commit

Permalink
Turn on index.html only mode
Browse files Browse the repository at this point in the history
Seems to work fine now. Tests would be cool :-)
  • Loading branch information
helje5 committed Jul 5, 2021
1 parent 085a7dc commit aa90e60
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 39 deletions.
32 changes: 30 additions & 2 deletions Sources/DocCHTMLExporter/DZRenderingContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ open class DZRenderingContext {
let references : [ String : DocCArchive.Reference ]
let indexLinks : Bool
let isIndex : Bool
let dataFolderPathes : Set<String>
let traits : Set<DocCArchive.ImageReference.Variant.Trait>
= [ .light, .retina ]

Expand All @@ -113,6 +114,7 @@ open class DZRenderingContext {
public init(pathToRoot : String,
references : [ String : DocCArchive.Reference ],
isIndex : Bool,
dataFolderPathes : Set<String>,
indexLinks : Bool,
templates : Templates? = nil,
labels : Labels? = nil,
Expand All @@ -122,6 +124,7 @@ open class DZRenderingContext {
self.pathToRoot = pathToRoot
self.references = references
self.isIndex = isIndex
self.dataFolderPathes = dataFolderPathes
self.indexLinks = indexLinks
self.templates = templates ?? Templates()
self.labels = labels ?? Labels()
Expand All @@ -132,13 +135,38 @@ open class DZRenderingContext {

// MARK: - URLs

func makeRelativeToRoot(_ url: String) -> String {

private func makeRelativeToRoot(_ url: String) -> String {
if url.hasPrefix("/") { return pathToRoot + url.dropFirst() }
return pathToRoot + url
}
func makeRelativeToRoot(_ url: URL) -> String {
private func makeRelativeToRoot(_ url: URL) -> String {
return makeRelativeToRoot(url.path)
}

func linkToResource(_ url: String) -> String {
return makeRelativeToRoot(url)
}

func linkToDocument(_ identifierURL: URL) -> String {
// Note: This is not very clever yet, it essentially goes up the chain
// using `../..` and then appends the "absolute" path, like
// `../../documentation/SwiftBlocksUI/index.html`.
// Note: Those can have path extensions, e.g. "color-swift.property"
assert(identifierURL.scheme == "doc")
assert(!dataFolderPathes.isEmpty)

// This is a little hacky, but yeah. The dataFolderPathes are always
// lowercase. Technically we are supposed to use the reference URL,
// but that doesn't come w/ the `doc` scheme and is probably less safe
// to use.
let isIndexed = dataFolderPathes.contains(identifierURL.path.lowercased())

let url = isIndexed
? identifierURL.appendingPathComponent("index.html")
: identifierURL.appendingPathExtension("html")
return makeRelativeToRoot(url)
}

func externalDocumentBaseURL(for module: String) -> URL? {
return moduleToExternalURL[module]
Expand Down
18 changes: 9 additions & 9 deletions Sources/DocCHTMLExporter/HTML/ReferenceHTML.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ extension DocCArchive.Reference {
switch self {

case .topic(let ref):
guard let url = ref.url ?? idURL else { return "" }
return ctx.makeRelativeToRoot(url) + ".html"

guard let url = idURL ?? ref.url else { return "" }
return ctx.linkToDocument(url)
case .image(let ref):
guard let variant = ref.bestVariant(for: ctx.traits) else { return "" }
return ctx.makeRelativeToRoot(variant.url)
return ctx.linkToResource(variant.url)

case .file(_):
fatalError("unsupported file ref")
Expand Down Expand Up @@ -107,13 +107,13 @@ extension DocCArchive.TopicReference {
{
let idURL = URL(string: identifier)
assert(idURL != nil, "topic refs should always have proper URLs")

assert(idURL?.scheme == "doc", "topic ref w/o a doc:// URL")

let activeClass = isActive ? "" : " class='inactive'"
let title = self.title.htmlEscaped

let url = (self.url ?? idURL).flatMap {
ctx.makeRelativeToRoot($0.appendingPathExtension("html"))
} ?? ""
let url = (idURL ?? self.url).flatMap(ctx.linkToDocument) ?? ""
assert(!url.isEmpty)

var ms = ""
if !url.isEmpty { ms += "<a href='\(url.htmlEscaped)'\(activeClass)>" }
Expand Down Expand Up @@ -155,7 +155,7 @@ extension DocCArchive.ImageReference {
}
}()

let url = ctx.makeRelativeToRoot(variant.url)
let url = ctx.linkToResource(variant.url)
let srcset = url.htmlEscaped
+ (ctx.traits.contains(.retina) ? " 2x" : "")

Expand Down
61 changes: 33 additions & 28 deletions Sources/DocCStaticExporter/DocCStaticExporter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ open class DocCStaticExporter {
public let target : DocCStaticExportTarget
public let archiveURLs : [ URL ]
public let stylesheet = DZRenderingContext.defaultStyleSheet

public var dataFolderPathes = Set<String>()

public init(target: DocCStaticExportTarget, archivePathes: [ String ],
options: Options, logger: Logger = Logger(label: "docc2html"))
Expand All @@ -55,6 +57,11 @@ open class DocCStaticExporter {
}

let archives = try loadArchives(archiveURLs)

for archive in archives {
dataFolderPathes.formUnion(archive.fetchDataFolderPathes())
}

try copyStaticResources(of: archives)
try generatePages (of: archives)
}
Expand Down Expand Up @@ -148,45 +155,43 @@ open class DocCStaticExporter {

for pageURL in folder.pageURLs() {
do {
let document = try folder.document(at: pageURL)
let baseName = pageURL.deletingPathExtension().lastPathComponent
let pathToRoot = String(repeating: "../", count: folder.level)

var indexPath : String? {
let isIndexPage = subfolderNames.contains(baseName)
guard isIndexPage else { return nil }
return relativePath + "/" + baseName + "/index.html"
}
let document = try folder.document(at: pageURL)
let baseName = pageURL.deletingPathExtension().lastPathComponent
let pathToRoot = String(repeating: "../", count: folder.level)
let isIndexPage = subfolderNames.contains(baseName)

do {
let htmlPath = relativePath + "/" +
pageURL
.deletingPathExtension() // JSON
.appendingPathExtension("html")
.lastPathComponent

logger.trace("Build:", document, "\n to:", htmlPath)
if buildIndex, isIndexPage {
let htmlPath = relativePath + "/" + baseName + "/index.html"

logger.trace("Index:", document, "\n to:", htmlPath)

let ctx = DZRenderingContext(
pathToRoot : pathToRoot,
references : document.references,
isIndex : false,
indexLinks : buildIndex
pathToRoot : pathToRoot + "../",
references : document.references,
isIndex : true,
dataFolderPathes : dataFolderPathes,
indexLinks : true
)

let html = try ctx.buildDocument(document, in: folder)

try target.write(html, to: htmlPath)
}

if buildIndex, let htmlPath = indexPath {
logger.trace("Index:", document, "\n to:", htmlPath)
else {
let htmlPath = relativePath + "/" +
pageURL
.deletingPathExtension() // JSON
.appendingPathExtension("html")
.lastPathComponent

logger.trace("Build:", document, "\n to:", htmlPath)

let ctx = DZRenderingContext(
pathToRoot : pathToRoot + "../",
references : document.references,
isIndex : true,
indexLinks : true
pathToRoot : pathToRoot,
references : document.references,
isIndex : false,
dataFolderPathes : dataFolderPathes,
indexLinks : buildIndex
)

let html = try ctx.buildDocument(document, in: folder)
Expand Down

0 comments on commit aa90e60

Please sign in to comment.