wobsite.Site
type pub Site
A type that represents a website to build.
Fields
files
let pub @files: Files
The files and directories that make up the website.
Static methods
build
Show source codeHide source code
fn pub static build(func: fn (mut Site)) {
let source = Path.new('source')
let output = Path.new('public')
let stderr = Stderr.new
let site = match Site.new(source, output) {
case Ok(site) -> site
case Error(e) -> {
let _ = stderr.print("failed to get the site's source files: ${e}")
exit(1)
}
}
func.call(site)
match site.wait {
case Ok(_) -> {}
case Error(e) -> {
let _ = stderr.print(e.to_string)
exit(1)
}
}
}
fn pub static build(func: fn (mut Site))
Creates a new Site
, builds it, and presents the user with the results.
The func
argument is used to set up what files to build, copy, etc.
If any errors are produced, they're written to STDERR and this method terminates the program with exit code 1.
Examples
Building a website that consists of simple text and CSS files:
import wobsite (Site)
Site.build(fn (site) {
site.copy('*.txt')
site.copy('*.css')
})
new
Show source codeHide source code
fn pub static new(source: Path, output: Path) -> Result[Site, io.Error] {
Files.new(source, output).map(fn (files) {
Site(files: files, pending: 0, status: Channel.new)
})
}
fn pub static new(source: Path, output: Path) -> Result[Site, Error]
Returns a new Site
instance.
The source
argument specifies the path to the source files. The output
argument is the path to write the built files to.
This method also recursively gets all the source files to (potentially)
build. If this fails, an Error(std.io.Error)
is returned.
Instance methods
copy
Show source codeHide source code
fn pub mut copy(pattern: String) {
@files.matching(pattern).each(fn (path) {
@pending += 1
spawn.copy(recover path.clone)
})
}
fn pub mut copy(pattern: String)
Copies a source file to the output directory, using the same hierarchy as the source file.
The pattern
argument specifies the fnmatch(3)
pattern to use for finding
the files to copy.
Examples
import wobsite (Site)
Site.build(fn (site) { site.copy('*.css') })
generate
Show source codeHide source code
fn pub mut generate(
path: String,
builder: uni fn (ref Files) -> Result[String, String],
) {
@pending += 1
spawn.generate(recover @files.output.join(path), builder)
}
fn pub mut generate(path: String, builder: uni fn (ref Files) -> Result[String, String])
Schedule a job that generates an arbitrary file.
The path
argument specifies the output path, relative to the output
directory.
The builder
argument is a closure that's called to generate the content of
the file.
Examples
import wobsite (Site)
Site.build(fn (site) {
site.generate('feed.xml') fn (files) { 'Example content' }
})
page
Show source codeHide source code
fn pub mut page(
pattern: String,
builder: fn -> uni fn (ref Files, Page) -> Result[html.Document, String],
) {
@files.matching(pattern).each(fn (path) {
@pending += 1
spawn.page(recover path.clone, index: true, builder: builder.call)
})
}
fn pub mut page(pattern: String, builder: fn -> uni fn (ref Files, Page) -> Result[Document, String])
Generates an HTML file from a Markdown file.
The pattern
argument specifies the fnmatch(3)
pattern to use for
determining the files to process.
The builder
argument is a closure called for every file to process,
returning another closure used to build the final HTML document. In a
typical setting, this closure converts the Markdown to HTML and wraps it in
a layout of sorts.
Input files are mapped to output files as follows:
./source/index.md
becomes./public/index.html
./source/foo.md
becomes./public/foo/index.html
./source/foo/bar/index.md
becomes./public/foo/bar/index.html
./source/foo/bar.md
becomes./public/foo/bar/index.html
page_without_index
Show source codeHide source code
fn pub mut page_without_index(
pattern: String,
builder: fn -> uni fn (ref Files, Page) -> Result[html.Document, String],
) {
@files.matching(pattern).each(fn (path) {
@pending += 1
spawn.page(recover path.clone, index: false, builder: builder.call)
})
}
fn pub mut page_without_index(pattern: String, builder: fn -> uni fn (ref Files, Page) -> Result[Document, String])
Generates an HTML file from a Markdown file, without generating an
index.html
file.
See Site.page
for more details.
Input files are mapped to output files as follows:
./source/index.md
becomes./public/index.html
./source/foo.md
becomes./public/foo.html
./source/foo/bar/index.md
becomes./public/foo/bar.html
./source/foo/bar.md
becomes./public/foo/bar.html
wait
Show source codeHide source code
fn pub move wait -> Result[Nil, Errors] {
let errors = Errors([])
while @pending > 0 {
match @status.receive {
case Ok -> {}
case Error(path, err) -> errors.add(path, err)
}
@pending -= 1
}
if errors.empty? { Result.Ok(nil) } else { Result.Error(errors) }
}
fn pub move wait -> Result[Nil, Errors]
Waits for the site to be built, returning once all pages have been processed.
If one or more pages failed to build, a Result.Error
is returned.