- Introduce a new generic `site` module for web generation - Update `herocmds` to use the new site creation flow - Simplify docusaurus playbook logic with a `docusaurus.play` fn - Refactor site generation to act on `Site` struct directly - Fix playbook find filter to use wildcard `*`
148 lines
3.6 KiB
V
148 lines
3.6 KiB
V
module site
|
|
|
|
import freeflowuniverse.herolib.core.pathlib
|
|
import freeflowuniverse.herolib.web.doctreeclient
|
|
import freeflowuniverse.herolib.data.markdown.tools as markdowntools
|
|
// import freeflowuniverse.herolib.ui.console
|
|
import os
|
|
|
|
pub struct SiteGenerator {
|
|
pub mut:
|
|
siteconfig_name string
|
|
path pathlib.Path
|
|
client &doctreeclient.DocTreeClient
|
|
flat bool // if flat then won't use sitenames as subdir's
|
|
}
|
|
|
|
@[params]
|
|
pub struct SiteGeneratorArgs {
|
|
pub mut:
|
|
path string
|
|
flat bool // if flat then won't use sitenames as subdir's
|
|
}
|
|
|
|
// new creates a new siteconfig and stores it in redis, or gets an existing one
|
|
pub fn (site Site) generate(args SiteGeneratorArgs) ! {
|
|
mut path := args.path
|
|
if path == '' {
|
|
path = '${os.home_dir()}/hero/var/sitegen'
|
|
}
|
|
mut factory := SiteGenerator{
|
|
path: pathlib.get_dir(path: path, create: true)!
|
|
client: doctreeclient.new()!
|
|
flat: args.flat
|
|
}
|
|
|
|
for section in site.sections {
|
|
factory.section_generate(section)!
|
|
}
|
|
|
|
for page in site.pages {
|
|
factory.page_generate(page)!
|
|
}
|
|
}
|
|
|
|
fn (mut site SiteGenerator) page_generate(args_ Page) ! {
|
|
mut args := args_
|
|
|
|
mut content := ['---']
|
|
|
|
mut parts := args.src.split(':')
|
|
if parts.len != 2 {
|
|
return error("Invalid src format for page '${args.src}', expected format: collection:page_name")
|
|
}
|
|
collection_name := parts[0]
|
|
page_name := parts[1]
|
|
|
|
mut page_content := site.client.get_page_content(collection_name, page_name) or {
|
|
return error("Couldn't find page '${page_name}' in collection '${collection_name}' using doctreeclient. Available pages:\n${site.client.list_markdown()!}\nError: ${err}")
|
|
}
|
|
|
|
if args.description.len == 0 {
|
|
descnew := markdowntools.extract_title(page_content)
|
|
if descnew != '' {
|
|
args.description = descnew
|
|
} else {
|
|
args.description = page_name
|
|
}
|
|
}
|
|
|
|
if args.title.len == 0 {
|
|
descnew := markdowntools.extract_title(page_content)
|
|
if descnew != '' {
|
|
args.title = descnew
|
|
} else {
|
|
args.title = page_name
|
|
}
|
|
}
|
|
content << "title: '${args.title}'"
|
|
|
|
if args.description.len > 0 {
|
|
content << "description: '${args.description}'"
|
|
}
|
|
|
|
if args.slug.len > 0 {
|
|
content << "slug: '${args.slug}'"
|
|
}
|
|
|
|
if args.hide_title {
|
|
content << 'hide_title: ${args.hide_title}'
|
|
}
|
|
|
|
if args.draft {
|
|
content << 'draft: ${args.draft}'
|
|
}
|
|
|
|
if args.position > 0 {
|
|
content << 'sidebar_position: ${args.position}'
|
|
}
|
|
|
|
content << '---'
|
|
|
|
mut c := content.join('\n')
|
|
|
|
if args.title_nr > 0 {
|
|
// Set the title number in the page content
|
|
page_content = markdowntools.set_titles(page_content, args.title_nr)
|
|
}
|
|
|
|
c += '\n${page_content}\n'
|
|
|
|
if args.path.ends_with('/') {
|
|
// means is dir
|
|
args.path += page_name
|
|
}
|
|
|
|
if !args.path.ends_with('.md') {
|
|
args.path += '.md'
|
|
}
|
|
|
|
mut pagepath := '${site.path.path}/${args.path}'
|
|
mut pagefile := pathlib.get_file(path: pagepath, create: true)!
|
|
|
|
pagefile.write(c)!
|
|
|
|
// console.print_debug("Copy images in collection '${collection_name}' to ${pagefile.path_dir()}")
|
|
|
|
site.client.copy_images(collection_name, page_name, pagefile.path_dir()) or {
|
|
return error("Couldn't copy images for '${page_name}' in collection '${collection_name}' using doctreeclient. Available pages:\n${site.client.list_markdown()!}\nError: ${err}")
|
|
}
|
|
}
|
|
|
|
fn (mut site SiteGenerator) section_generate(args_ Section) ! {
|
|
mut args := args_
|
|
|
|
mut c := '{
|
|
"label": "${args.label}",
|
|
"position": ${args.position},
|
|
"link": {
|
|
"type": "generated-index"
|
|
}
|
|
}'
|
|
|
|
mut category_path := '${site.path.path}/${args.path}/_category_.json'
|
|
mut catfile := pathlib.get_file(path: category_path, create: true)!
|
|
|
|
catfile.write(c)!
|
|
}
|