...
This commit is contained in:
@@ -30,6 +30,15 @@ pub fn get(args FactoryArgs) !&Site {
|
||||
return sc
|
||||
}
|
||||
|
||||
|
||||
pub fn exists(args FactoryArgs) bool {
|
||||
name := texttools.name_fix(args.name)
|
||||
mut sc := websites[name] or {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
pub fn default() !&Site {
|
||||
if websites.len == 0 {
|
||||
return new(name:'default')!
|
||||
|
||||
@@ -1,147 +0,0 @@
|
||||
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)!
|
||||
}
|
||||
@@ -1,103 +0,0 @@
|
||||
module site
|
||||
|
||||
import os
|
||||
import freeflowuniverse.herolib.core.pathlib
|
||||
import freeflowuniverse.herolib.data.doctree
|
||||
|
||||
fn test_page_add() ! {
|
||||
// Setup a dummy doctree.Tree and pathlib.Path
|
||||
// Setup a temporary directory for site output
|
||||
mut site_output_dir := pathlib.get_dir(
|
||||
path: os.join_path(os.temp_dir(), 'sitegen_test_output')
|
||||
create: true
|
||||
)!
|
||||
site_output_dir.delete()! // Clean up previous test runs
|
||||
|
||||
// Setup a temporary directory for doctree source content
|
||||
mut doctree_source_dir := pathlib.get_dir(
|
||||
path: os.join_path(os.temp_dir(), 'doctree_test_source')
|
||||
create: true
|
||||
)!
|
||||
doctree_source_dir.delete()! // Clean up previous test runs
|
||||
|
||||
// Create a collection directory and a .collection file
|
||||
mut collection_dir_path := os.join_path(doctree_source_dir.path, 'my_collection')
|
||||
os.mkdir_all(collection_dir_path)!
|
||||
os.write_file(os.join_path(collection_dir_path, '.collection'), '')!
|
||||
|
||||
// Write a dummy markdown file to the collection directory
|
||||
mut dummy_md_file_path := os.join_path(collection_dir_path, 'dummy_page.md')
|
||||
os.write_file(dummy_md_file_path, '# My Dummy Page\n\nThis is some content for the dummy page.')!
|
||||
|
||||
// Initialize doctree.Tree and scan the source directory
|
||||
mut tree := doctree.new(name: 'test_tree')!
|
||||
tree.scan(path: doctree_source_dir.path)!
|
||||
|
||||
// Debug prints
|
||||
println('Tree collections after scan: ${tree.collections.keys()}')
|
||||
if 'my_collection' in tree.collections {
|
||||
println('my_collection exists in tree.collections')
|
||||
println('Pages in my_collection: ${tree.collections['my_collection'].pages.keys()}')
|
||||
} else {
|
||||
println('my_collection DOES NOT exist in tree.collections')
|
||||
}
|
||||
|
||||
// The dummy page path in doctree will be collection_name:page_name
|
||||
mut dummy_page_doctree_path := 'my_collection:dummy_page'
|
||||
println('Dummy page doctree path: ${dummy_page_doctree_path}')
|
||||
|
||||
mut site := Site{
|
||||
name: 'TestSite'
|
||||
path: site_output_dir
|
||||
tree: tree // Pass the pointer directly
|
||||
}
|
||||
|
||||
// Test Case 1: Basic page addition
|
||||
mut page1 := Page{
|
||||
title: 'Test Page 1'
|
||||
description: 'A simple test page.'
|
||||
src: dummy_page_doctree_path
|
||||
path: 'pages/test_page_1.md'
|
||||
}
|
||||
site.page_add(page1)!
|
||||
|
||||
mut expected_content_page1 := "---\ntitle: 'Test Page 1'\ndescription: 'A simple test page.'\n---\n# My Dummy Page\n\nThis is some content for the dummy page.\n"
|
||||
mut output_file_page1 := pathlib.get_file(path: os.join_path(site_output_dir.path,
|
||||
'pages/test_page_1.md'))!
|
||||
assert output_file_page1.exists()
|
||||
assert output_file_page1.read()! == expected_content_page1
|
||||
|
||||
// Test Case 2: Page with draft, no description, hide_title, and position
|
||||
mut page2 := Page{
|
||||
title: 'Test Page 2'
|
||||
draft: true
|
||||
position: 5
|
||||
hide_title: true
|
||||
src: dummy_page_doctree_path
|
||||
path: 'articles/test_page_2.md'
|
||||
}
|
||||
site.page_add(page2)!
|
||||
|
||||
mut expected_content_page2 := "---\ntitle: 'Test Page 2'\nhide_title: true\ndraft: true\nsidebar_position: 5\n---\n# My Dummy Page\n\nThis is some content for the dummy page.\n"
|
||||
mut output_file_page2 := pathlib.get_file(path: os.join_path(site_output_dir.path,
|
||||
'articles/test_page_2.md'))!
|
||||
assert output_file_page2.exists()
|
||||
assert output_file_page2.read()! == expected_content_page2
|
||||
|
||||
// Test Case 3: Page with no title (should use filename)
|
||||
mut page3 := Page{
|
||||
src: dummy_page_doctree_path
|
||||
path: 'blog/my_blog_post.md'
|
||||
}
|
||||
site.page_add(page3)!
|
||||
|
||||
mut expected_content_page3 := "---\ntitle: 'my_blog_post.md'\n---\n# My Dummy Page\n\nThis is some content for the dummy page.\n"
|
||||
mut output_file_page3 := pathlib.get_file(path: os.join_path(site_output_dir.path,
|
||||
'blog/my_blog_post.md'))!
|
||||
assert output_file_page3.exists()
|
||||
assert output_file_page3.read()! == expected_content_page3
|
||||
|
||||
// Clean up
|
||||
site_output_dir.delete()!
|
||||
doctree_source_dir.delete()!
|
||||
}
|
||||
@@ -1,5 +1,4 @@
|
||||
module site
|
||||
import os
|
||||
|
||||
|
||||
@[heap]
|
||||
@@ -18,7 +17,7 @@ pub mut:
|
||||
draft bool
|
||||
position int
|
||||
hide_title bool
|
||||
src string @[required]
|
||||
src string @[required] //always in format collection:page_name
|
||||
path string @[required]
|
||||
title_nr int
|
||||
slug string
|
||||
|
||||
@@ -30,9 +30,7 @@ pub fn play(mut plbook PlayBook) ! {
|
||||
fn play_config_single(action Action) !&Site {
|
||||
mut p := action.params
|
||||
name := p.get('name') or {
|
||||
// If name is not specified, try to derive it from title or use a default
|
||||
title := p.get_default('title', 'default-site')!
|
||||
texttools.name_fix(title)
|
||||
return error('need to specify name in site.config.\n${action}')
|
||||
}
|
||||
|
||||
mut website := new(name: name)!
|
||||
@@ -47,6 +45,7 @@ fn play_config_single(action Action) !&Site {
|
||||
config.url = p.get_default('url', config.url)!
|
||||
config.base_url = p.get_default('base_url', config.base_url)!
|
||||
config.url_home = p.get_default('url_home', config.url_home)!
|
||||
config.name = name
|
||||
|
||||
return website
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user