feat: Add Atlas Export and AtlasClient example

- Add example for exporting Atlas collections
- Demonstrate using AtlasClient to read exported content
- Include examples for listing collections and pages
- Show reading page content and metadata via AtlasClient
This commit is contained in:
Mahmoud-Emad
2025-11-05 16:08:56 +02:00
parent 04e1e2375f
commit ac09648a5b
2 changed files with 98 additions and 83 deletions

98
examples/data/atlas/example.vsh Executable file
View File

@@ -0,0 +1,98 @@
#!/usr/bin/env -S v -n -w -cg -gc none -cc tcc -d use_openssl -enable-globals run
import incubaid.herolib.data.atlas
import incubaid.herolib.core.pathlib
import incubaid.herolib.web.atlas_client
import os
// Example: Atlas Export and AtlasClient Usage
println('Atlas Export & Client Example')
println('============================================================')
// Setup test directory
test_dir := '/tmp/atlas_example'
export_dir := '/tmp/atlas_export'
os.rmdir_all(test_dir) or {}
os.rmdir_all(export_dir) or {}
os.mkdir_all(test_dir)!
// Create a collection with some content
col_path := '${test_dir}/docs'
os.mkdir_all(col_path)!
mut cfile := pathlib.get_file(path: '${col_path}/.collection', create: true)!
cfile.write('name:docs')!
mut page1 := pathlib.get_file(path: '${col_path}/intro.md', create: true)!
page1.write('# Introduction\n\nWelcome to the docs!')!
mut page2 := pathlib.get_file(path: '${col_path}/guide.md', create: true)!
page2.write('# Guide\n\n!!include docs:intro\n\nMore content here.')!
// Create and scan atlas
println('\n1. Creating Atlas and scanning...')
mut a := atlas.new(name: 'my_docs')!
a.scan(path: test_dir)!
println(' Found ${a.collections.len} collection(s)')
// Validate links
println('\n2. Validating links...')
a.validate_links()!
col := a.get_collection('docs')!
if col.has_errors() {
println(' Errors found:')
col.print_errors()
} else {
println(' No errors found!')
}
// Export collections
println('\n3. Exporting collections to ${export_dir}...')
a.export(
destination: export_dir
include: true // Process includes during export
redis: false // Don't use Redis for this example
)!
println(' Export complete')
// Use AtlasClient to access exported content
println('\n4. Using AtlasClient to read exported content...')
mut client := atlas_client.new(export_dir: export_dir)!
// List collections
collections := client.list_collections()!
println(' Collections: ${collections}')
// List pages in docs collection
pages := client.list_pages('docs')!
println(' Pages in docs: ${pages}')
// Read page content
println('\n5. Reading page content via AtlasClient...')
intro_content := client.get_page_content('docs', 'intro')!
println(' intro.md content:')
println(' ${intro_content}')
guide_content := client.get_page_content('docs', 'guide')!
println('\n guide.md content (with includes processed):')
println(' ${guide_content}')
// Get metadata
println('\n6. Accessing metadata...')
metadata := client.get_collection_metadata('docs')!
println(' Collection name: ${metadata.name}')
println(' Collection path: ${metadata.path}')
println(' Number of pages: ${metadata.pages.len}')
println('\n Example completed successfully!')
println('\nExported files are in: ${export_dir}')
println(' - content/docs/intro.md')
println(' - content/docs/guide.md')
println(' - meta/docs.json')
// Cleanup (commented out so you can inspect the files)
// os.rmdir_all(test_dir) or {}
// os.rmdir_all(export_dir) or {}

View File

@@ -1,83 +0,0 @@
#!/usr/bin/env -S v -n -w -cg -gc none -cc tcc -d use_openssl -enable-globals run
import incubaid.herolib.data.atlas
import incubaid.herolib.core.pathlib
import os
// Example: Save and Load Atlas Collections
println('Atlas Save/Load Example')
println('============================================================')
// Setup test directory
test_dir := '/tmp/atlas_example'
os.rmdir_all(test_dir) or {}
os.mkdir_all(test_dir)!
// Create a collection with some content
col_path := '${test_dir}/docs'
os.mkdir_all(col_path)!
mut cfile := pathlib.get_file(path: '${col_path}/.collection', create: true)!
cfile.write('name:docs')!
mut page1 := pathlib.get_file(path: '${col_path}/intro.md', create: true)!
page1.write('# Introduction\n\nWelcome to the docs!')!
mut page2 := pathlib.get_file(path: '${col_path}/guide.md', create: true)!
page2.write('# Guide\n\n!!include docs:intro\n\nMore content here.')!
// Create and scan atlas
println('\n1. Creating Atlas and scanning...')
mut a := atlas.new(name: 'my_docs')!
a.scan(path: test_dir)!
println(' Found ${a.collections.len} collection(s)')
// Validate links
println('\n2. Validating links...')
a.validate_links()!
col := a.get_collection('docs')!
if col.has_errors() {
println(' Errors found:')
col.print_errors()
} else {
println(' No errors found!')
}
// Save all collections
println('\n3. Saving collections to .collection.json...')
a.save_all()!
println(' Saved to ${col_path}/.collection.json')
// Load in a new atlas
println('\n4. Loading collections in new Atlas...')
mut a2 := atlas.new(name: 'loaded_docs')!
a2.load_from_directory(test_dir)!
println(' Loaded ${a2.collections.len} collection(s)')
// Access loaded data
println('\n5. Accessing loaded data...')
loaded_col := a2.get_collection('docs')!
println(' Collection: ${loaded_col.name}')
println(' Pages: ${loaded_col.pages.len}')
for name, page in loaded_col.pages {
println(' - ${name}: ${page.path.path}')
}
// Read page content
println('\n6. Reading page content...')
mut intro_page := loaded_col.page_get('intro')!
content := intro_page.read_content()!
println(' intro.md content:')
println(' ${content}')
println('\n Example completed successfully!')
println('\nNow you can use the Python loader:')
println(' python3 lib/data/atlas/atlas_loader.py')
// Cleanup
os.rmdir_all(test_dir) or {}