This commit is contained in:
2025-07-19 19:51:08 +02:00
parent e798187b89
commit 2c8daf11d8
6 changed files with 146 additions and 54 deletions

View File

@@ -112,7 +112,7 @@ fn (c Collection) export_files(col_name string, dir_src pathlib.Path, reset bool
if reset || !os.exists(d) {
file.copy(d)!
}
redis.hset('doctree:${col_name}', file.name, 'img/${file.name}.${file.ext}')!
redis.hset('doctree:${col_name}', '${file.name}.${file.ext}', 'img/${file.name}.${file.ext}')!
}
}
@@ -121,7 +121,7 @@ fn (c Collection) export_images(col_name string, dir_src pathlib.Path, reset boo
mut redis := context.redis()!
for _, file in c.images {
mut d := '${dir_src.path}/img/${file.name}.${file.ext}'
redis.hset('doctree:${col_name}', file.name, 'img/${file.name}.${file.ext}')!
redis.hset('doctree:${col_name}', '${file.name}.${file.ext}', 'img/${file.name}.${file.ext}')!
if reset || !os.exists(d) {
file.copy(d)!
}

View File

@@ -74,6 +74,12 @@ pub fn (mut c DocTreeClient) get_image_path(collection_name string, image_name s
return error('${DocTreeError.image_not_found}: Image "${image_name}" not found in collection "${collection_name}"')
}
if rel_path == '' {
return error('${DocTreeError.image_not_found}: Image "${image_name}" found in collection "${collection_name}" but its path is empty in Redis.')
}
// console.print_debug('get_image_path: rel_path for "${image_name}": "${rel_path}"')
// Combine the collection path with the relative path
return os.join_path(collection_path, rel_path)
}
@@ -297,7 +303,8 @@ pub fn (mut c DocTreeClient) get_page_paths(collection_name string, page_name st
page_content := c.get_page_content(collection_name, page_name)!
// Extract image names from the page content
image_names := extract_image_links(page_content)!
image_names := extract_image_links(page_content,true)!
// println(image_names)
mut image_paths := []string{}
for image_name in image_names {
@@ -313,42 +320,33 @@ pub fn (mut c DocTreeClient) get_page_paths(collection_name string, page_name st
}
// copy_page copies a page and its linked images to a specified destination.
pub fn (mut c DocTreeClient) copy_page(collection_name string, page_name string, destination_path string) ! {
console.print_debug('copy_page: Copying page "${page_name}" from collection "${collection_name}" to "${destination_path}"')
pub fn (mut c DocTreeClient) copy_images(collection_name string, page_name string, destination_path string) ! {
// Get the page path and linked image paths
page_path, image_paths := c.get_page_paths(collection_name, page_name)!
console.print_debug('copy_page: Page path: "${page_path}"')
console.print_debug('copy_page: Linked image paths: ${image_paths}')
if true{panic("sdsdsd7")}
// println('copy_page: Linked image paths: ${image_paths}')
// Ensure the destination directory exists
console.print_debug('copy_page: Ensuring destination directory "${destination_path}" exists.')
os.mkdir_all(destination_path)!
console.print_debug('copy_page: Destination directory created/exists.')
// Copy the page file
page_file_name := os.base(page_path)
dest_page_path := os.join_path(destination_path, page_file_name)
console.print_debug('copy_page: Copying page file from "${page_path}" to "${dest_page_path}"')
os.cp(page_path, dest_page_path)!
console.print_debug('copy_page: Page file copied.')
// // Copy the page file
// page_file_name := os.base(page_path)
// dest_page_path := os.join_path(destination_path, page_file_name)
// os.cp(page_path, dest_page_path)!
// Create an 'images' subdirectory within the destination
images_dest_path := os.join_path(destination_path, 'images')
console.print_debug('copy_page: Ensuring images directory "${images_dest_path}" exists.')
// Create an 'img' subdirectory within the destination
images_dest_path := os.join_path(destination_path, 'img')
os.mkdir_all(images_dest_path)!
console.print_debug('copy_page: Images directory created/exists.')
// Copy each linked image
for image_path in image_paths {
// println(image_path)
image_file_name := os.base(image_path)
dest_image_path := os.join_path(images_dest_path, image_file_name)
console.print_debug('copy_page: Copying image file from "${image_path}" to "${dest_image_path}"')
// console.print_debug('copy_page: Copying image file from "${image_path}" to "${dest_image_path}"')
os.cp(image_path, dest_image_path)!
console.print_debug('copy_page: Image file "${image_file_name}" copied.')
// console.print_debug('Copy Image file "${image_file_name}" copied.')
}
console.print_debug('copy_page: All files copied successfully.')
}

View File

@@ -1,6 +1,8 @@
module doctreeclient
pub fn extract_image_links(s string) ![]string {
import os
pub fn extract_image_links(s string, exclude_http bool) ![]string {
mut result := []string{}
mut current_pos := 0
for {
@@ -35,7 +37,14 @@ pub fn extract_image_links(s string) ![]string {
// Extract the URL
url := s[url_start_index..url_end_index]
result << url
if exclude_http && (url.starts_with('http://') || url.starts_with('https://')) {
current_pos = url_end_index + 1
continue
}
// Extract only the base name of the image from the URL
image_base_name := os.base(url)
result << image_base_name
// Move current_pos past the found link to continue searching
current_pos = url_end_index + 1

View File

@@ -2,77 +2,117 @@ module doctreeclient
import os
fn test_extract_image_links(exclude_http bool) {
fn test_extract_image_links() {
// Test case 1: Basic case with one image link
mut result := extract_image_links('Some text ![Alt Text](https://example.com/image1.png) more text')!
mut result := extract_image_links('Some text ![Alt Text](https://example.com/image1.png) more text',false)!
assert result.len == 1
assert result[0] == 'https://example.com/image1.png'
// Test case 2: Multiple image links
result = extract_image_links('![Img1](https://example.com/img1.jpg) Text ![Img2](https://example.com/img2.gif)')!
result = extract_image_links('![Img1](https://example.com/img1.jpg) Text ![Img2](https://example.com/img2.gif)',false)!
assert result.len == 2
assert result[0] == 'https://example.com/img1.jpg'
assert result[1] == 'https://example.com/img2.gif'
// Test case 3: No image links
result = extract_image_links('Just some plain text without images.')!
result = extract_image_links('Just some plain text without images.',false)!
assert result.len == 0
// Test case 4: Mixed content with other markdown
result = extract_image_links('A link [Link](https://example.com) and an image ![Photo](https://example.com/photo.jpeg).')!
result = extract_image_links('A link [Link](https://example.com) and an image ![Photo](https://example.com/photo.jpeg).',false)!
assert result.len == 1
assert result[0] == 'https://example.com/photo.jpeg'
// Test case 5: Invalid image link (missing parenthesis)
result = extract_image_links('Invalid ![Broken Link]https://example.com/broken.png')!
result = extract_image_links('Invalid ![Broken Link]https://example.com/broken.png',false)!
assert result.len == 0
// Test case 6: Empty string
result = extract_image_links('')!
result = extract_image_links('',false)!
assert result.len == 0
// Test case 7: Image link at the beginning of the string
result = extract_image_links('![Start](https://example.com/start.png) Some text.')!
result = extract_image_links('![Start](https://example.com/start.png) Some text.',false)!
assert result.len == 1
assert result[0] == 'https://example.com/start.png'
// Test case 8: Image link at the end of the string
result = extract_image_links('Some text ![End](https://example.com/end.png)')!
result = extract_image_links('Some text ![End](https://example.com/end.png)',false)!
assert result.len == 1
assert result[0] == 'https://example.com/end.png'
// Test case 9: Image link with spaces in URL (should not happen in valid markdown, but good to test robustness)
result = extract_image_links('![Space](https://example.com/image with spaces.png)')!
result = extract_image_links('![Space](https://example.com/image with spaces.png)',false)!
assert result.len == 1
assert result[0] == 'https://example.com/image with spaces.png'
// Test case 10: Image link with special characters in URL
result = extract_image_links('![Special](https://example.com/path/to/image?id=1&name=test.png)')!
result = extract_image_links('![Special](https://example.com/path/to/image?id=1&name=test.png)',false)!
assert result.len == 1
assert result[0] == 'https://example.com/path/to/image?id=1&name=test.png'
// Test case 11: Multiple image links without spaces in between
result = extract_image_links('![A](https://a.com)![B](https://b.com)![C](https://c.com)')!
result = extract_image_links('![A](https://a.com)![B](https://b.com)![C](https://c.com)',false)!
assert result.len == 3
assert result[0] == 'https://a.com'
assert result[1] == 'https://b.com'
assert result[2] == 'https://c.com'
// Test case 12: Image link with empty alt text
result = extract_image_links('![](https://example.com/noalt.png)')!
result = extract_image_links('![](https://example.com/noalt.png)',false)!
assert result.len == 1
assert result[0] == 'https://example.com/noalt.png'
// Test case 13: Image link with empty URL (invalid markdown, but test behavior)
result = extract_image_links('![Empty URL]()')!
result = extract_image_links('![Empty URL]()',false)!
assert result.len == 1
assert result[0] == '' // Expecting an empty string for the URL
// Test case 14: Image link with only alt text and no URL part
result = extract_image_links('![Only Alt Text]')!
result = extract_image_links('![Only Alt Text]',false)!
assert result.len == 0
// Test case 15: Image link with only URL part and no alt text
result = extract_image_links('()')!
result = extract_image_links('()',false)!
assert result.len == 0
// --- Test cases for exclude_http = true ---
// Test case 16: Exclude http links, only relative link
result = extract_image_links('Some text ![Relative](image.png) ![Absolute](https://example.com/image.png)', true)!
assert result.len == 1
assert result[0] == 'image.png'
// Test case 17: Exclude http links, multiple relative links
result = extract_image_links('![Rel1](img1.jpg) ![Abs1](http://example.com/img.jpg) ![Rel2](/path/to/img2.gif)', true)!
assert result.len == 2
assert result[0] == 'img1.jpg'
assert result[1] == '/path/to/img2.gif'
// Test case 18: Exclude http links, all absolute links
result = extract_image_links('![Abs1](https://example.com/img1.png) ![Abs2](http://example.com/img2.png)', true)!
assert result.len == 0
// Test case 19: Exclude http links, no links at all
result = extract_image_links('Plain text.', true)!
assert result.len == 0
// Test case 20: Exclude http links, mixed absolute and relative, with other markdown
result = extract_image_links('A link [Link](https://example.com) and an image ![Photo](https://example.com/photo.jpeg) and another ![Local](local.png).', true)!
assert result.len == 1
assert result[0] == 'local.png'
// Test case 21: Exclude http links, empty string
result = extract_image_links('', true)!
assert result.len == 0
// Test case 22: Exclude http links, image with empty URL (should still be included if not http)
result = extract_image_links('![Empty URL]()', true)!
assert result.len == 1
assert result[0] == ''
// Test case 23: Exclude http links, image with data URI (should not be excluded)
result = extract_image_links('![Data URI](data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=)', true)!
assert result.len == 1
assert result[0] == 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII='
}

View File

@@ -47,7 +47,7 @@ pub fn play(args_ PlayArgs) ! {
page_actions := plbook.find(filter: 'site.page')!
mut mypage:=Page{src:"",path:""}
for action in page_actions {
println(action)
// println(action)
mut p := action.params
sitename := p.get('sitename') or { return error("need to specify sitename in site.page") }
mypage.path = p.get_default('path', "")!
@@ -61,4 +61,20 @@ pub fn play(args_ PlayArgs) ! {
mut site := factory.site_get(sitename)!
site.page_add(mypage)!
}
category_actions := plbook.find(filter: 'site.page_category')!
mut section := Section{}
for action in category_actions {
println(action)
mut p := action.params
sitename := p.get('sitename') or { return error("need to specify sitename in site.page") }
section.position = p.get_int_default('position', 20)!
section.label = p.get('label' ) or { return error("need to specify label in site.page_category") }
section.path = p.get('path') or { return error("need to specify path in site.page_category") }
mut site := factory.site_get(sitename)!
site.section_add(section)!
}
}

View File

@@ -66,22 +66,51 @@ pub fn (mut site Site) page_add(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 := 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}")
}
// c+="\n${page_content}\n"
c+="\n${page_content}\n"
mut pagepath:= "${site.path.path}/${args.path}"
// mut pagefile:= pathlib.get_file(path:pagepath,create:true)!
mut pagefile:= pathlib.get_file(path:pagepath,create:true)!
// pagefile.write(c)!
pagefile.write(c)!
console.print_debug("Copy page '${pagepath}' in collection '${collection_name}")
console.print_debug("Copy images in collection '${collection_name}' to ${pagefile.path_dir()}")
site.client.copy_page(collection_name, page_name, pagepath) or {
return error("Couldn't copy page '${page_name}' in collection '${collection_name}' using doctreeclient. Available pages:\n${site.client.list_markdown()!}\nError: ${err}")
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}")
}
}
@[params]
pub struct Section {
pub mut:
position int
path string
label string
}
pub fn (mut site Site) section_add(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)!
}