This commit is contained in:
2025-11-07 07:24:38 +04:00
parent ea1a49ffd5
commit 0d3b4357ac
2 changed files with 48 additions and 119 deletions

View File

@@ -93,7 +93,7 @@ pub fn (mut c AtlasClient) get_file_path(collection_name string, file_name strin
}
// Construct the file path
file_path := os.join_path(c.export_dir, 'content', 'files', fixed_collection_name,
file_path := os.join_path(c.export_dir, 'content', fixed_collection_name, 'files',
fixed_file_name)
// Check if the file exists
@@ -121,7 +121,7 @@ pub fn (mut c AtlasClient) get_image_path(collection_name string, image_name str
}
// Construct the image path
image_path := os.join_path(c.export_dir, 'content', 'img', fixed_collection_name,
image_path := os.join_path(c.export_dir, 'content', fixed_collection_name, 'img',
fixed_image_name)
// Check if the image exists
@@ -214,85 +214,26 @@ pub fn (mut c AtlasClient) list_pages(collection_name string) ![]string {
// list_files returns a list of all file names in a collection (excluding pages and images)
pub fn (mut c AtlasClient) list_files(collection_name string) ![]string {
// Apply name normalization
fixed_collection_name := texttools.name_fix(collection_name)
collection_dir := os.join_path(c.export_dir, 'content', fixed_collection_name)
// Check if collection directory exists
if !os.exists(collection_dir) {
return c.error_collection_not_found(collection_name: collection_name)
}
// Get all files that are not .md and not images
metadata := c.get_collection_metadata(collection_name)!
mut file_names := []string{}
entries := os.ls(collection_dir)!
for entry in entries {
entry_path := os.join_path(collection_dir, entry)
// Skip directories
if os.is_dir(entry_path) {
continue
}
// Skip .md files (pages)
if entry.ends_with('.md') {
continue
}
// Check if it's an image
mut is_image := false
for ext in image_extensions {
if entry.ends_with(ext) {
is_image = true
break
}
}
// Add to file_names if it's not an image
if !is_image {
file_names << entry
for file_name, file_meta in metadata.files {
if !file_meta.path.starts_with('img/') { // Exclude images
file_names << file_name
}
}
return file_names
}
// list_images returns a list of all image names in a collection
pub fn (mut c AtlasClient) list_images(collection_name string) ![]string {
// Apply name normalization
fixed_collection_name := texttools.name_fix(collection_name)
collection_dir := os.join_path(c.export_dir, 'content', fixed_collection_name)
// Check if collection directory exists
if !os.exists(collection_dir) {
return c.error_collection_not_found(collection_name: collection_name)
}
// Get all image files
mut image_names := []string{}
entries := os.ls(collection_dir)!
for entry in entries {
entry_path := os.join_path(collection_dir, entry)
// Skip directories
if os.is_dir(entry_path) {
continue
}
// Check if it's an image
for ext in image_extensions {
if entry.ends_with(ext) {
image_names << entry
break
}
metadata := c.get_collection_metadata(collection_name)!
mut images := []string{}
for file_name, file_meta in metadata.files {
if file_meta.path.starts_with('img/') {
images << file_name
}
}
return image_names
return images
}
// list_pages_map returns a map of collection names to a list of page names within that collection.
@@ -390,47 +331,20 @@ pub fn (mut c AtlasClient) has_errors(collection_name string) bool {
return errors.len > 0
}
// get_page_paths returns the path of a page and the paths of its linked images.
// Returns (page_path, image_paths)
// This is compatible with the doctreeclient API
// pub fn (mut c AtlasClient) get_page_paths(collection_name string, page_name string) !(string, []string) {
// // Get the page path
// page_path := c.get_page_path(collection_name, page_name)!
// page_content := c.get_page_content(collection_name, page_name)!
// // Extract image names from the page content
// image_names := extract_image_links(page_content, true)!
// mut image_paths := []string{}
// for image_name in image_names {
// // Get the path for each image
// image_path := c.get_image_path(collection_name, image_name) or {
// // If an image is not found, log a warning and continue, don't fail the whole operation
// return error('Error: Linked image "${image_name}" not found in collection "${collection_name}". Skipping.')
// }
// image_paths << image_path
// }
// return page_path, image_paths
// }
// copy_images copies all images linked in a page to a destination directory
// This is compatible with the doctreeclient API
// pub fn (mut c AtlasClient) copy_images(collection_name string, page_name string, destination_path string) ! {
// // Get the page path and linked image paths
// _, image_paths := c.get_page_paths(collection_name, page_name)!
// // Ensure the destination directory exists
// os.mkdir_all(destination_path)!
// // Create an 'img' subdirectory within the destination
// images_dest_path := os.join_path(destination_path, 'img')
// os.mkdir_all(images_dest_path)!
// // Copy each linked image
// for image_path in image_paths {
// image_file_name := os.base(image_path)
// dest_image_path := os.join_path(images_dest_path, image_file_name)
// os.cp(image_path, dest_image_path)!
// }
// }
pub fn (mut c AtlasClient) copy_images(collection_name string, page_name string, destination_path string) ! {
// Get page links from metadata
links := c.get_page_links(collection_name, page_name)!
// Create img subdirectory
mut img_dest := pathlib.get_dir(path: '${destination_path}/img', create: true)!
// Copy only image links
for link in links {
if !link.is_image_link { continue }
// Get image path and copy
img_path := c.get_image_path(link.target_collection_name, link.target_item_name)!
mut src := pathlib.get_file(path: img_path)!
src.copy(dest: '${img_dest.path}/${src.name_fix()}.${src.extension_lower()}')!
}
}

View File

@@ -28,15 +28,17 @@ fn setup_test_export() string {
'# Intro\n\nWelcome!') or { panic(err) }
// Create test images
os.write_file(os.join_path(test_dir, 'content', 'testcollection', 'logo.png'), 'fake png data') or {
os.mkdir_all(os.join_path(test_dir, 'content', 'testcollection', 'img')) or { panic(err) }
os.write_file(os.join_path(test_dir, 'content', 'testcollection', 'img', 'logo.png'), 'fake png data') or {
panic(err)
}
os.write_file(os.join_path(test_dir, 'content', 'testcollection', 'banner.jpg'), 'fake jpg data') or {
os.write_file(os.join_path(test_dir, 'content', 'testcollection', 'img', 'banner.jpg'), 'fake jpg data') or {
panic(err)
}
// Create test files
os.write_file(os.join_path(test_dir, 'content', 'testcollection', 'data.csv'), 'col1,col2\nval1,val2') or {
os.mkdir_all(os.join_path(test_dir, 'content', 'testcollection', 'files')) or { panic(err) }
os.write_file(os.join_path(test_dir, 'content', 'testcollection', 'files', 'data.csv'), 'col1,col2\nval1,val2') or {
panic(err)
}
@@ -70,7 +72,20 @@ fn setup_test_export() string {
]
}
},
"files": {},
"files": {
"logo.png": {
"name": "logo.png",
"path": "img/logo.png"
},
"banner.jpg": {
"name": "banner.jpg",
"path": "img/banner.jpg"
},
"data.csv": {
"name": "data.csv",
"path": "files/data.csv"
}
},
"errors": []
}'
os.write_file(os.join_path(test_dir, 'meta', 'testcollection.json'), metadata1) or {