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: Update the documentation and incontext template functions #137

Merged
merged 1 commit into from
Sep 9, 2023
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -53,23 +53,23 @@
<CommandLineArguments>
<CommandLineArgument
argument = "build"
isEnabled = "NO">
isEnabled = "YES">
</CommandLineArgument>
<CommandLineArgument
argument = "version"
isEnabled = "NO">
</CommandLineArgument>
<CommandLineArgument
argument = "serve"
isEnabled = "YES">
isEnabled = "NO">
</CommandLineArgument>
<CommandLineArgument
argument = "--site /Users/jbmorley/Projects/jbmorley.co.uk"
isEnabled = "YES">
</CommandLineArgument>
<CommandLineArgument
argument = "--watch"
isEnabled = "YES">
isEnabled = "NO">
</CommandLineArgument>
</CommandLineArguments>
</LaunchAction>
Expand Down
49 changes: 27 additions & 22 deletions Sources/InContextCore/Builder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,29 @@ public class Builder {

static func context(for site: Site, document: Document, renderTracker: RenderTracker) -> [String: Any] {

let titlecase = Function { (string: String) -> String in
return string.toTitleCase()
}

// TODO: Can I do this without performing yet another query? Can I pass the DocumentContext back?
// TODO: As long as we also cache the render status with this query, we can cache the render output per URL.
// TODO: Maybe this should just take a string and let the template to do the lookup and render.
let thumbnail = Function { (url: String) -> String? in
guard let document = try renderTracker.documentContexts(query: QueryDescription(url: url)).first else {
return nil
}
let html = try document.render()
let dom = try SwiftSoup.parse(html)
if let openGraphImage = try dom.select("meta[property=og:image]").first() {
// Use the Open Graph image tag if it exists.
return try openGraphImage.attr("content")
} else if let img = try dom.select("img[src]").first() {
// Select the first image tag with source.
return try img.attr("src")
}
return nil
}

// TODO: Inline the config loaded from the settings file
// TODO: Consider separating the store and the site metadata.
// TODO: These top-level methods should probably be namespaced.
Expand Down Expand Up @@ -55,9 +78,7 @@ public class Builder {
"generate_uuid": Function {
return UUID().uuidString
},
"titlecase": Function { (string: String) -> String in
return string.toTitleCase()
},
"titlecase": titlecase,
"document": DocumentContext(renderTracker: renderTracker, document: document),
"distant_past": Function { (timezoneAware: Bool) in
return Date.distantPast
Expand All @@ -81,25 +102,9 @@ public class Builder {
}
return data.base64EncodedString()
},
"ic": [
// TODO: Can I do this without performing yet another query? Can I pass the DocumentContext back?
// TODO: As long as we also cache the render status with this query, we can cache the render output per URL
// TODO: This should indeed probably just take a string and we should leave it up to the template to do the lookup and render.
"thumbnail": Function { (url: String) -> String? in
guard let document = try renderTracker.documentContexts(query: QueryDescription(url: url)).first else {
return nil
}
let html = try document.render()
let dom = try SwiftSoup.parse(html)
if let openGraphImage = try dom.select("meta[property=og:image]").first() {
// Use the Open Graph image tag if it exists.
return try openGraphImage.attr("content")
} else if let img = try dom.select("img[src]").first() {
// Select the first image tag with source.
return try img.attr("src")
}
return nil
},
"incontext": [
"titlecase": titlecase,
"thumbnail": thumbnail,
] as [String: Any]
]
}
Expand Down
6 changes: 3 additions & 3 deletions templates.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ The simplest template renders the HTML contents of a single document, processing

``` html
<html>
<head>
<title>{{ site.title }}</title>
<head>
<title>{{ site.title }}</title>
</head>
<body>
<h1>{{ incontext.titlecase(document.title) }}</h1>
Expand All @@ -35,7 +35,7 @@ It's very common to want to list all documents within a specific category, with
{{ incontext.renderDocumentHTML(document) }}
<ul>
{% for _, child in ipairs(document.children) do %}
<li><a href="{{ document.url }}">{{ incontext.titlecase(child.title) }}</a></li>
<li><a href="{{ document.url }}">{{ incontext.titlecase(child.title) }}</a></li>
{% end %}
</ul>
</body>
Expand Down