refactor: improve collection export

- Remove unnecessary parameter from `errors_report` function.
- Pass `Collection` by mutable reference to `export` function.
- Fix `.collection` file content in export test.
- Update test cases to reflect changes.
- Correct path in test data.
- Use mutable reference for `col` in `Tree.export`.

Co-authored-by: mahmmoud.hassanein <mahmmoud.hassanein@gmail.com>
This commit is contained in:
2025-01-12 14:06:21 +02:00
parent 58a1f04a44
commit 5753e0eb58
6 changed files with 32 additions and 16 deletions

View File

@@ -52,10 +52,10 @@ pub fn (err ObjNotFound) msg() string {
}
// write errors.md in the collection, this allows us to see what the errors are
pub fn (collection Collection) errors_report(dest_ string, errors []CollectionError) ! {
pub fn (collection Collection) errors_report(dest_ string) ! {
// console.print_debug("====== errors report: ${dest_} : ${collection.errors.len}\n${collection.errors}")
mut dest := pathlib.get_file(path: dest_, create: true)!
if errors.len == 0 {
if collection.errors.len == 0 {
dest.delete()!
return
}

View File

@@ -17,14 +17,13 @@ pub mut:
replacer ?regext.ReplaceInstructions
}
pub fn (c Collection) export(args CollectionExportArgs) ! {
pub fn (mut c Collection) export(args CollectionExportArgs) ! {
dir_src := pathlib.get_dir(path: args.destination.path + '/' + c.name, create: true)!
mut cfile := pathlib.get_file(path: dir_src.path + '/.collection', create: true)! // will auto save it
cfile.write("name:${c.name} src:'${c.path.path}'")!
mut errors := c.errors.clone()
errors << export_pages(c.path.path, c.pages.values(),
c.errors << export_pages(c.path.path, c.pages.values(),
dir_src: dir_src
file_paths: args.file_paths
keep_structure: args.keep_structure
@@ -36,7 +35,7 @@ pub fn (c Collection) export(args CollectionExportArgs) ! {
c.export_linked_pages(dir_src)!
if !args.exclude_errors {
c.errors_report('${dir_src.path}/errors.md', errors)!
c.errors_report('${dir_src.path}/errors.md')!
}
}

View File

@@ -39,9 +39,18 @@ fn test_export() {
col1_path := '${export_dir}/src/col1'
expected_col1_path := '${export_expected_dir}/src/col1'
assert os.read_file('${col1_path}/.collection')! == os.read_file('${expected_col1_path}/.collection')!
assert os.read_file('${col1_path}/.collection')! == "name:col1 src:'${tree_dir}/dir1'"
assert os.read_file('${col1_path}/.linkedpages')! == os.read_file('${expected_col1_path}/.linkedpages')!
assert os.read_file('${col1_path}/errors.md')! == os.read_file('${expected_col1_path}/errors.md')!
assert os.read_file('${col1_path}/errors.md')! == '# Errors
## page_not_found
path: ${tree_dir}/dir1/dir2/file1.md
msg: page col3:file5.md not found
'
assert os.read_file('${col1_path}/file1.md')! == os.read_file('${expected_col1_path}/file1.md')!
assert os.read_file('${col1_path}/file2.md')! == os.read_file('${expected_col1_path}/file2.md')!
}

View File

@@ -1 +1 @@
name:col1 src:'/Users/timurgordon/code/github/freeflowuniverse/crystallib/crystallib/data/doctree/collection/testdata/export_test/mytree/dir1'
name:col1 src:'/Users/timurgordon/code/github/freeflowuniverse/herolib/lib/data/doctree/collection/testdata/export_test/mytree/dir1'

View File

@@ -2,7 +2,6 @@ module doctree
import freeflowuniverse.herolib.core.pathlib
import freeflowuniverse.herolib.data.doctree.collection { Collection }
import freeflowuniverse.herolib.data.doctree.collection.data
import freeflowuniverse.herolib.ui.console
import freeflowuniverse.herolib.core.texttools.regext
@@ -43,8 +42,8 @@ pub fn (mut tree Tree) export(args TreeExportArgs) ! {
if args.concurrent {
mut ths := []thread !{}
for _, col in tree.collections {
ths << spawn fn (col Collection, dest_path pathlib.Path, file_paths map[string]string, args TreeExportArgs) ! {
for _, mut col in tree.collections {
ths << spawn fn (mut col Collection, dest_path pathlib.Path, file_paths map[string]string, args TreeExportArgs) ! {
col.export(
destination: dest_path
file_paths: file_paths
@@ -53,7 +52,7 @@ pub fn (mut tree Tree) export(args TreeExportArgs) ! {
exclude_errors: args.exclude_errors
// TODO: replacer: tree.replacer
)!
}(col, dest_path, file_paths, args)
}(mut col, dest_path, file_paths, args)
}
for th in ths {
th.wait() or { panic(err) }

View File

@@ -68,15 +68,24 @@ fn test_export() {
col1_path := '${export_dir}/col1'
expected_col1_path := '${export_expected_dir}/col1'
assert os.read_file('${col1_path}/.collection')! == os.read_file('${expected_col1_path}/.collection')!
assert os.read_file('${col1_path}/.collection')! == "name:col1 src:'${tree_dir}/dir1'"
assert os.read_file('${col1_path}/.linkedpages')! == os.read_file('${expected_col1_path}/.linkedpages')!
assert os.read_file('${col1_path}/errors.md')! == os.read_file('${expected_col1_path}/errors.md')!
assert os.read_file('${col1_path}/errors.md')! == '# Errors
## page_not_found
path: ${tree_dir}/dir1/dir2/file1.md
msg: page col3:file5.md not found
'
assert os.read_file('${col1_path}/file1.md')! == os.read_file('${expected_col1_path}/file1.md')!
assert os.read_file('${col1_path}/file2.md')! == os.read_file('${expected_col1_path}/file2.md')!
col2_path := '${export_dir}/col2'
expected_col2_path := '${export_expected_dir}/col2'
assert os.read_file('${col2_path}/.linkedpages')! == ''
assert os.read_file('${col2_path}/.collection')! == os.read_file('${expected_col2_path}/.collection')!
assert os.read_file('${col2_path}/.collection')! == "name:col2 src:'${tree_dir}/dir3'"
assert os.read_file('${col2_path}/file3.md')! == ''
}