refactor: Centralize Docusaurus site processing logic

- Add central `process_site_from_path` function
- Recursively process heroscript files in `cfg` directory
- Remove duplicated site processing from `run` and `add` commands
- Respect `play` parameter from heroscript `define` block
This commit is contained in:
Mahmoud-Emad
2025-08-07 16:22:35 +03:00
parent 843b20804b
commit b01e6a5a4c
4 changed files with 86 additions and 49 deletions

View File

@@ -10,6 +10,85 @@ import freeflowuniverse.herolib.osal.core as osal
import freeflowuniverse.herolib.core.playbook
// import freeflowuniverse.herolib.data.doctree
// Recursively process heroscript files in a directory
fn process_heroscript_files_recursive(dir_path string) !string {
mut combined_heroscript := ''
files := os.ls(dir_path) or { return combined_heroscript }
for file in files {
file_path := os.join_path(dir_path, file)
if os.is_dir(file_path) {
// Recursively process subdirectories
subdir_content := process_heroscript_files_recursive(file_path)!
combined_heroscript += subdir_content
} else if file.ends_with('.heroscript') {
content := os.read_file(file_path) or { continue }
// Filter out only the play.include lines while keeping all other content
lines := content.split('\n')
mut filtered_lines := []string{}
for line in lines {
trimmed := line.trim_space()
if !trimmed.starts_with('!!play.include') {
filtered_lines << line
}
}
filtered_content := filtered_lines.join('\n')
// Only add if there's meaningful content after filtering
if filtered_content.trim_space().len > 0 {
combined_heroscript += filtered_content + '\n\n'
}
}
}
return combined_heroscript
}
// Central function to process site configuration from a path
// This is the single point of use for all site processing logic
// If sitename is empty, it will return the first available site
pub fn process_site_from_path(path string, sitename string) !&site.Site {
console.print_debug('Processing site configuration from: ${path}')
// Process the site configuration recursively (excluding global includes)
combined_heroscript := process_heroscript_files_recursive('${path}/cfg')!
console.print_debug('Combined heroscript length: ${combined_heroscript.len} characters')
if combined_heroscript.trim_space().len == 0 {
return error('No valid heroscript files found in ${path}/cfg')
}
// Create playbook and process site configuration
mut plbook := playbook.new(text: combined_heroscript)!
console.print_debug('Created playbook with ${plbook.actions.len} actions')
site.play(mut plbook)!
// Check what sites were created
available_sites := site.list()
console.print_debug('Available sites after site.play(): ${available_sites}')
if available_sites.len == 0 {
return error('No sites were created from the configuration')
}
// Determine which site to return
target_sitename := if sitename.len == 0 {
console.print_debug('No specific site requested, using first available: ${available_sites[0]}')
available_sites[0] // Use the first (and likely only) site
} else {
console.print_debug('Looking for specific site: ${sitename}')
sitename
}
mysite := site.get(name: target_sitename) or {
return error('Failed to get site after playing playbook: ${target_sitename}. Available sites: ${available_sites}')
}
console.print_debug('Site processed successfully: ${mysite.siteconfig.name} with ${mysite.pages.len} pages')
return mysite
}
@[params]
pub struct AddArgs {
pub mut:
@@ -80,12 +159,8 @@ pub fn dsite_add(args_ AddArgs) !&DocSite {
if !args.play {
return error('Docusaurus site ${args.sitename} does not exist, please set play to true to create it.')
}
// Create new site and process config files
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}')
}
// Use the centralized site processing function
mysite = process_site_from_path(args.path, args.sitename)!
}
// Create the DocSite instance

View File

@@ -46,7 +46,7 @@ pub fn play(mut plbook PlayBook) ! {
git_root: p.get_default('git_root', '')! // Make git_root optional
git_pull: p.get_default_false('git_pull')
path_publish: p.get_default('path_publish', f.path_publish.path)!
play: false // need to make sure we don't play again
play: p.get_default_false('play') // Respect the play parameter from heroscript
)!
action.done = true
}