Skip to content

Commit

Permalink
fix render
Browse files Browse the repository at this point in the history
  • Loading branch information
tib committed Oct 16, 2024
1 parent b283fdb commit 0512522
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 44 deletions.
18 changes: 10 additions & 8 deletions Sources/ToucanSDK/Config/Config.swift
Original file line number Diff line number Diff line change
Expand Up @@ -175,33 +175,34 @@ struct Config {
}

struct Pipeline {

enum Keys {
static let run = "run"
static let render = "render"
}

struct Run {

enum Keys {
static let name = "name"
}

let name: String

init?(_ dict: [String: Any]) {
guard let name = dict.string(Keys.name) else {
return nil
}
self.name = name
}
}

let run: [Run]
let render: Bool

init(_ dict: [String: Any]) {
self.run = dict
self.run =
dict
.array(Keys.run, as: [String: Any].self)
.map { Run($0)! }
self.render = dict.bool(Keys.render) ?? true
Expand All @@ -223,7 +224,8 @@ struct Config {
self.folder =
dict.string(Location.Keys.folder)
?? Config.defaults.transformers.folder
self.pipelines = dict
self.pipelines =
dict
.dict(Keys.pipelines)
.compactMapValues { (item: Any) -> Pipeline? in
guard let dict = item as? [String: Any] else {
Expand Down
46 changes: 14 additions & 32 deletions Sources/ToucanSDK/ContextStore/ContextStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -117,18 +117,6 @@ struct ContextStore {
self.markdownToCParser = .init()
}

// func build() {
// for pageBundle in pageBundles {
// let ctx = standardContext(for: pageBundle)
// print("------------------------------------")
// print(pageBundle.slug)
// print(ctx.keys)
// if pageBundle.slug == "introducing-toucan-a-new-markdown-based-static-site-generator" {
// print(ctx["authors"])
// }
// }
// }

private func baseContext(
for pageBundle: PageBundle
) -> [String: Any] {
Expand Down Expand Up @@ -156,11 +144,11 @@ struct ContextStore {
pageBundle: pageBundle
)
)

let pipelines = sourceConfig.config.transformers.pipelines
let pipeline = pipelines[pageBundle.contentType.id]
var didRenderHTML = false
let shouldRenderMarkdown = pipeline?.render ?? true

if let pipeline, !pipeline.run.isEmpty {
let executor = PipelineExecutor(
pipeline: pipeline,
Expand All @@ -172,31 +160,25 @@ struct ContextStore {
)
do {
contents = try executor.execute()
didRenderHTML = true
}
catch {
logger.error("\(String(describing: error))")
}
}

if didRenderHTML {
let tocElements = htmlToCParser.parse(from: contents) ?? []
return [
"contents": contents,
"readingTime": contents.readingTime(),
"toc": tocElements.buildToCTree(),
]

let tocElements: [TocElement]
if shouldRenderMarkdown {
tocElements = markdownToCParser.parse(from: contents) ?? []
contents = markdownRenderer.renderHTML(markdown: contents)
}
else {
let tocElements = markdownToCParser.parse(from: contents) ?? []
let readingTime = contents.readingTime()
contents = markdownRenderer.renderHTML(markdown: contents)
return [
"contents": contents,
"readingTime": readingTime,
"toc": tocElements.buildToCTree(),
]
tocElements = htmlToCParser.parse(from: contents) ?? []
}
return [
"contents": contents,
"readingTime": contents.readingTime(),
"toc": tocElements.buildToCTree(),
]
}

private func relations(
Expand Down
2 changes: 1 addition & 1 deletion Sources/toucan-cli/Commands/Generate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ extension Entrypoint {
baseUrl: baseUrl,
logger: logger
)

if generator.generateAndLogErrors(logger) {
let metadata: Logger.Metadata = [
"input": "\(input)",
Expand Down
5 changes: 3 additions & 2 deletions Sources/toucan-cli/Commands/Watch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ extension Entrypoint {
logger: logger
)
generator.generateAndLogErrors(logger)

#if os(macOS)
let eventStream = try EonilFSEventStream(
pathsToWatch: [input],
Expand All @@ -68,7 +68,8 @@ extension Entrypoint {
logger.info("Generating site...")
if generator.generateAndLogErrors(logger) {
logger.info("Site re-generated.")
} else {
}
else {
logger.info("Site generation failed.")
}
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/toucan-cli/Extensions/Toucan+UserErrors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ extension Toucan {
catch {
logger.error("\(String(describing: error))")
}

return false
}
}

0 comments on commit 0512522

Please sign in to comment.