This commit is contained in:
2025-08-04 07:05:04 +02:00
parent 52f7a7a2b8
commit 07bd258e54
12 changed files with 223 additions and 296 deletions

View File

@@ -60,7 +60,7 @@ println('Database and table setup completed successfully!')
### Configuration Parameters
| Parameter | Description | Default Value |
|-----------|-------------|---------------|
| --------- | ---------------------------------------- | ------------- |
| name | Unique identifier for this configuration | 'default' |
| user | PostgreSQL user | 'root' |
| port | PostgreSQL server port | 5432 |
@@ -117,3 +117,19 @@ db_client.backup(dest: '/path/to/backup/dir')!
Backups are created in custom PostgreSQL format (.bak files) which can be restored using pg_restore.
## OS supporting
OSX
```
## supporting
brew install postgresql@17
brew services start postgresql@17
#if only the client is needed
brew install libpq
brew link --force libpq
export PATH="/usr/local/opt/libpq/bin:$PATH"
```

View File

@@ -1,5 +1,5 @@
## hero db - OSIS in vlang
```v
// Example usage:
@@ -34,3 +34,4 @@ retrieved_circle := circle_db.get_by_index({
// Search circles by status
active_circles := circle_db.search_by_index("status", "active")!
```

View File

@@ -4,7 +4,6 @@ import freeflowuniverse.herolib.osal.screen
import os
import freeflowuniverse.herolib.core.pathlib
import freeflowuniverse.herolib.web.site as sitemodule
import freeflowuniverse.herolib.develop.gittools
import freeflowuniverse.herolib.osal.core as osal
import freeflowuniverse.herolib.ui.console
import time
@@ -16,35 +15,17 @@ pub mut:
url string
path_src pathlib.Path
path_publish pathlib.Path
args DSiteGetArgs
path_build pathlib.Path
errors []SiteError
config Configuration
siteconfig sitemodule.SiteConfig
factory &DocusaurusFactory @[skip; str: skip] // Reference to the parent
}
@[params]
pub struct DSiteGetArgs {
pub mut:
name string
nameshort string
path string
git_url string
git_reset bool
git_root string
git_pull bool
open bool // Added
watch_changes bool // Added
path_publish string // Added
init bool // Added
update bool // Added (maps to template_update in DocusaurusArgs)
website sitemodule.Site
}
pub fn (mut s DocSite) build() ! {
s.generate()!
osal.exec(
cmd: '
cd ${s.factory.path_build.path}
cd ${s.path_build.path}
exit 1
'
retry: 0
@@ -55,7 +36,7 @@ pub fn (mut s DocSite) build_dev_publish() ! {
s.generate()!
osal.exec(
cmd: '
cd ${s.factory.path_build.path}
cd ${s.path_build.path}
exit 1
'
retry: 0
@@ -66,7 +47,7 @@ pub fn (mut s DocSite) build_publish() ! {
s.generate()!
osal.exec(
cmd: '
cd ${s.factory.path_build.path}
cd ${s.path_build.path}
exit 1
'
retry: 0
@@ -78,6 +59,8 @@ pub struct DevArgs {
pub mut:
host string = 'localhost'
port int = 3000
open bool = true // whether to open the browser automatically
watch_changes bool = false // whether to watch for changes in docs and rebuild automatically
}
pub fn (mut s DocSite) open(args DevArgs) ! {
@@ -90,7 +73,7 @@ pub fn (mut s DocSite) dev(args DevArgs) ! {
s.generate()!
osal.exec(
cmd: '
cd ${s.factory.path_build.path}
cd ${s.path_build.path}
bun run start -p ${args.port} -h ${args.host}
'
retry: 0
@@ -115,8 +98,8 @@ pub fn (mut s DocSite) dev_watch(args DevArgs) ! {
)!
// Send commands to the screen session
console.print_item('To view the server output:: cd ${s.factory.path_build.path}')
scr.cmd_send('cd ${s.factory.path_build.path}')!
console.print_item('To view the server output:: cd ${s.path_build.path}')
scr.cmd_send('cd ${s.path_build.path}')!
// Start script recording in the screen session for log streaming
log_file := '/tmp/docusaurus_${screen_name}.log'
@@ -153,13 +136,13 @@ pub fn (mut s DocSite) dev_watch(args DevArgs) ! {
// tf.wait()!
println('\n')
if s.args.open {
if args.open {
s.open()!
}
if s.args.watch_changes {
if args.watch_changes {
docs_path := '${s.path_src.path}/docs'
watch_docs(docs_path, s.path_src.path, s.factory.path_build.path)!
watch_docs(docs_path, s.path_src.path, s.path_build.path)!
}
}

View File

@@ -7,11 +7,31 @@ import freeflowuniverse.herolib.develop.gittools
import freeflowuniverse.herolib.web.site
import freeflowuniverse.herolib.ui.console
import freeflowuniverse.herolib.osal.core as osal
import freeflowuniverse.herolib.core.playbook
// import freeflowuniverse.herolib.data.doctree
pub fn (mut f DocusaurusFactory) add(args_ DSiteGetArgs) !&DocSite {
console.print_header(' Docusaurus: ${args_.name}')
@[params]
pub struct AddArgs {
pub mut:
sitename string //needs to exist in web.site module
path string //site of the docusaurus site with the config as is needed to populate the docusaurus site
git_url string
git_reset bool
git_root string
git_pull bool
path_publish string
}
pub fn dsite_add(args_ AddArgs) !&DocSite {
mut args := args_
args.sitename = texttools.name_fix(args_.sitename)
console.print_header('Add Docusaurus Site: ${args.sitename}')
if args.sitename in docusaurus_sites {
return error('Docusaurus site ${args.sitename} already exists, returning existing.')
}
mut path := gittools.path(
path: args.path
@@ -19,13 +39,17 @@ pub fn (mut f DocusaurusFactory) add(args_ DSiteGetArgs) !&DocSite {
git_reset: args.git_reset
git_root: args.git_root
git_pull: args.git_pull
currentdir: true
currentdir: false
)!
args.path = path.path
if !path.is_dir() {
return error('path is not a directory')
}
if ! os.exists('${args.path}/cfg') {
return error('config directory for docusaurus does not exist in ${args.path}/cfg.\n${args}')
}
configpath := '${args.path}/cfg'
if !os.exists(configpath) {
return error("can't find config file for docusaurus in ${configpath}")
@@ -39,50 +63,50 @@ pub fn (mut f DocusaurusFactory) add(args_ DSiteGetArgs) !&DocSite {
osal.rm('${args.path}/sync.sh')!
osal.rm('${args.path}/.DS_Store')!
mut myconfig := config_load(configpath)!
mut website := site.get(name: args.sitename)!
mut myconfig := new_configuration(website.siteconfig)! //go from site.SiteConfig to docusaurus.Configuration
if myconfig.main.name.len == 0 {
myconfig.main.name = myconfig.main.base_url.trim_space().trim('/').trim_space()
return error('main.name is not set in the site configuration')
}
if args.name == '' {
args.name = myconfig.main.name
}
if args.nameshort.len == 0 {
args.nameshort = args.name
}
args.name = texttools.name_fix(args.name)
args.nameshort = texttools.name_fix(args.nameshort)
mut f:=factory_get()!
if args.path_publish == '' {
args.path_publish = '${f.path_publish}/${args.name}'
args.path_publish = '${f.path_publish.path}/${args.sitename}'
}
// this will get us the siteconfig run through plbook
mut mysite := site.new(name: args.name)!
mut mysiteconfig := mysite.siteconfig
path_build_ := '${f.path_build.path}/${args.sitename}'
// NOT NEEDED IS DONE FROM HEROSCRIPT BEFORE
// //now run the plbook to get all relevant for the site, {SITENAME} has been set in the context.session
// doctree.play(
// heroscript_path: configpath
// reset: args.update
// )!
//get our website
mut mysite := site.new(name: args.sitename)!
if site.exists(name: args.sitename) {
console.print_debug('Docusaurus site ${args.sitename} already exists, using existing site.')
mysite = site.get(name: args.sitename)!
} else {
console.print_debug('Creating new Docusaurus site ${args.sitename}.')
mut plbook := playbook.new(path: "${args.path}/cfg")!
site.play(mut plbook)!
mysite = site.get(name: args.sitename) or {
return error('Failed to get site after playing playbook: ${args.sitename}')
}
}
mut ds := DocSite{
name: args.name
// Create the DocSite instance
mut dsite := &DocSite{
name: args.sitename
path_src: pathlib.get_dir(path: args.path, create: false)!
path_publish: pathlib.get_dir(path: args.path_publish)!
args: args
config: myconfig
siteconfig: mysiteconfig // comes from the heroconfig
factory: &f
path_publish: pathlib.get_dir(path: args.path_publish, create: true)!
path_build: pathlib.get_dir(path: path_build_, create: true)!
config: new_configuration(website.siteconfig)!
website: mysite
}
ds.check()!
f.sites[args.name] = &ds
return &ds
docusaurus_sites[args.sitename] = dsite
return dsite
}

View File

@@ -2,6 +2,8 @@ module docusaurus
import freeflowuniverse.herolib.web.site
//IS THE ONE AS USED BY DOCUSAURUS
pub struct Configuration {
pub mut:
main Main
@@ -76,7 +78,7 @@ pub mut:
// ... (struct definitions remain the same) ...
// This function is now a pure transformer: site.SiteConfig -> docusaurus.Configuration
pub fn new_configuration(site_cfg site.SiteConfig) !Configuration {
fn new_configuration(site_cfg site.SiteConfig) !Configuration {
// Transform site.SiteConfig to docusaurus.Configuration
mut nav_items := []NavbarItem{}
for item in site_cfg.menu.items {

View File

@@ -3,21 +3,24 @@ module docusaurus
import os
import freeflowuniverse.herolib.core.pathlib
import freeflowuniverse.herolib.core.texttools
import freeflowuniverse.herolib.ui.console
import freeflowuniverse.herolib.web.site
import freeflowuniverse.herolib.develop.gittools
import freeflowuniverse.herolib.osal.core as osal
import freeflowuniverse.herolib.installers.web.bun
__global (
docusaurus_sites map[string]&DocSite
docusaurus_factory ?DocSiteFactory
)
pub struct DocSiteFactory{
pub mut:
path_publish pathlib.Path
path_build pathlib.Path
}
@[params]
pub struct AddArgs {
pub struct DocSiteFactoryArgs {
pub:
site &site.Site
path_src string
path_build string
path_publish string
install bool
@@ -25,97 +28,44 @@ pub:
template_update bool
}
pub fn add(args AddArgs) !&DocSite {
site_name := texttools.name_fix(args.site.siteconfig.name)
if site_name in docusaurus_sites {
console.print_debug('Docusaurus site ${site_name} already exists, returning existing.')
return docusaurus_sites[site_name]
pub fn factory_get(args_ DocSiteFactoryArgs) !DocSiteFactory {
mut args:= args_
mut f:= docusaurus_factory or {
if args.path_build == '' {
args.path_build = '${os.home_dir()}/hero/var/docusaurus/build/${site_name}'
}
if args.path_publish == '' {
args.path_publish = '${os.home_dir()}/hero/var/docusaurus/publish/${site_name}'
}
mut factory := DocSiteFactory{
path_publish: pathlib.get_dir(args.path_publish)!
path_build: pathlib.get_dir(args.path_build)!
}
console.print_debug('Adding docusaurus site ${site_name}')
mut path_build_ := args.path_build
if path_build_ == '' {
path_build_ = '${os.home_dir()}/hero/var/docusaurus/build/${site_name}'
}
mut path_publish_ := args.path_publish
if path_publish_ == '' {
path_publish_ = '${os.home_dir()}/hero/var/docusaurus/publish/${site_name}'
if !os.exists('${f.path_build.path}/node_modules') {
args.install = true
}
// Install template if required
install(path_build_, TemplateInstallArgs{
install: args.install
reset: args.reset
template_update: args.template_update
})!
// Create the DocSite instance
mut dsite := &DocSite{
name: site_name
path_src: pathlib.get_dir(path: args.path_src, create: false)!
path_publish: pathlib.get_dir(path: path_publish_, create: true)!
path_build: pathlib.get_dir(path: path_build_, create: true)!
config: new_configuration(args.site.siteconfig)!
site: args.site
if args.install {
factory.install(args.reset, args.template_update)!
}
factory
}
return f
}
docusaurus_sites[site_name] = dsite
return dsite
}
pub fn get(name_ string) !&DocSite {
pub fn dsite_get(name_ string) !&DocSite {
name := texttools.name_fix(name_)
return docusaurus_sites[name] or {
return error('docusaurus site with name "${name}" does not exist')
}
}
@[params]
struct TemplateInstallArgs {
mut:
install bool
reset bool
template_update bool
pub fn dsite_exists(name_ string) !bool {
name := texttools.name_fix(name_)
d := docusaurus_sites[name] or {
return false
}
return true
}
// copy template in build location
pub fn install(path_build_path string, args_ TemplateInstallArgs) ! {
mut gs := gittools.new()!
mut args := args_
if args.reset {
osal.rm(path_build_path)!
osal.dir_ensure(path_build_path)!
}
template_path := gs.get_path(
pull: args.template_update
reset: args.reset // Changed args.delete to args.reset
url: 'https://github.com/freeflowuniverse/docusaurus_template/src/branch/main/template'
)!
mut template_path0 := pathlib.get_dir(path: template_path, create: false)!
template_path0.copy(dest: path_build_path, delete: args.reset)! // Changed args.delete to args.reset
if !os.exists('${path_build_path}/node_modules') {
args.install = true
}
if args.install {
// install bun
mut installer := bun.get()!
installer.install()!
osal.exec(
// always stay in the context of the build directory
cmd: '
${osal.profile_path_source_and()!}
export PATH=${path_build_path}/node_modules/.bin::${os.home_dir()}/.bun/bin/:\$PATH
cd ${path_build_path}
bun install
'
)!
}
}

View File

@@ -2,47 +2,53 @@ module site
import freeflowuniverse.herolib.core.pathlib
import freeflowuniverse.herolib.web.doctreeclient
import freeflowuniverse.herolib.web.site {Site, Page, Section}
import freeflowuniverse.herolib.data.markdown.tools as markdowntools
// import freeflowuniverse.herolib.ui.console
import os
pub struct SiteGenerator {
pub mut:
//THIS CODE GENERATES A DOCUSAURUS SITE FROM A DOCTREECLIENT AND SITE DEFINITION
struct SiteGenerator {
mut:
siteconfig_name string
path pathlib.Path
client &doctreeclient.DocTreeClient
flat bool // if flat then won't use sitenames as subdir's
site Site
}
@[params]
pub struct SiteGeneratorArgs {
pub mut:
struct SiteGeneratorArgs {
mut:
path string
flat bool // if flat then won't use sitenames as subdir's
site Site
}
// new creates a new siteconfig and stores it in redis, or gets an existing one
pub fn (site Site) generate(args SiteGeneratorArgs) ! {
fn generate(args SiteGeneratorArgs) ! {
mut path := args.path
if path == '' {
path = '${os.home_dir()}/hero/var/sitegen'
if args.path == '' {
return error('Path must be provided to generate site')
}
mut factory := SiteGenerator{
mut gen := SiteGenerator{
path: pathlib.get_dir(path: path, create: true)!
client: doctreeclient.new()!
flat: args.flat
site: args.site
}
for section in site.sections {
factory.section_generate(section)!
for section in gen.site.sections {
gen.section_generate(section)!
}
for page in site.pages {
factory.page_generate(page)!
for page in gen.site.pages {
gen.page_generate(page)!
}
}
fn (mut site SiteGenerator) page_generate(args_ Page) ! {
fn (mut mysite SiteGenerator) page_generate(args_ Page) ! {
mut args := args_
mut content := ['---']
@@ -54,8 +60,8 @@ fn (mut site SiteGenerator) page_generate(args_ Page) ! {
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}")
mut page_content := mysite.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${mysite.client.list_markdown()!}\nError: ${err}")
}
if args.description.len == 0 {
@@ -117,19 +123,19 @@ fn (mut site SiteGenerator) page_generate(args_ Page) ! {
args.path += '.md'
}
mut pagepath := '${site.path.path}/${args.path}'
mut pagepath := '${mysite.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}")
mysite.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${mysite.client.list_markdown()!}\nError: ${err}")
}
}
fn (mut site SiteGenerator) section_generate(args_ Section) ! {
fn (mut mysite SiteGenerator) section_generate(args_ Section) ! {
mut args := args_
mut c := '{
@@ -140,7 +146,7 @@ fn (mut site SiteGenerator) section_generate(args_ Section) ! {
}
}'
mut category_path := '${site.path.path}/${args.path}/_category_.json'
mut category_path := '${mysite.path.path}/${args.path}/_category_.json'
mut catfile := pathlib.get_file(path: category_path, create: true)!
catfile.write(c)!

View File

@@ -0,0 +1,41 @@
module docusaurus
import os
import freeflowuniverse.herolib.core.pathlib
import freeflowuniverse.herolib.develop.gittools
import freeflowuniverse.herolib.osal.core as osal
import freeflowuniverse.herolib.installers.web.bun
fn (mut f DocSiteFactory) install(reset bool, template_update bool) ! {
mut gs := gittools.new()!
if reset {
osal.rm(f.path_build.path)!
osal.dir_ensure(f.path_build.path)!
}
template_path := gs.get_path(
pull: template_update
reset: reset // Changed args.delete to args.reset
url: 'https://github.com/freeflowuniverse/docusaurus_template/src/branch/main/template'
)!
mut template_path0 := pathlib.get_dir(path: template_path, create: false)!
template_path0.copy(dest: f.path_build.path, delete:reset)! // Changed args.delete to args.reset
// install bun
mut installer := bun.get()!
installer.install()!
osal.exec(
// always stay in the context of the build directory
cmd: '
${osal.profile_path_source_and()!}
export PATH=${f.path_build.path}/node_modules/.bin::${os.home_dir()}/.bun/bin/:\$PATH
cd ${f.path_build.path}
bun install
'
)!
}

View File

@@ -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')!

View File

@@ -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()!
}

View File

@@ -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

View File

@@ -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
}