refactor: Update name normalization logic

- Use texttools.name_fix instead of name_fix_no_underscore_no_ext
- Preserve underscores in normalized names
- Update documentation and tests to reflect changes
This commit is contained in:
Mahmoud-Emad
2025-11-05 10:01:18 +02:00
parent 10b9af578a
commit 2150b93a80
7 changed files with 24 additions and 133 deletions

View File

@@ -15,7 +15,7 @@ pub mut:
// Create a new collection
fn (mut self Atlas) add_collection(mut path pathlib.Path) !Collection {
mut name := path.name_fix_no_underscore_no_ext()
mut name := path.name_fix_no_ext()
mut filepath := path.file_get('.collection')!
content := filepath.read()!
if content.trim_space() != '' {
@@ -24,9 +24,8 @@ fn (mut self Atlas) add_collection(mut path pathlib.Path) !Collection {
name = params.get('name')!
}
}
// Normalize collection name to remove underscores and special chars
// This ensures collection_a and collectiona both become 'collectiona'
name = texttools.name_fix_no_underscore_no_ext(name)
// Normalize collection name
name = texttools.name_fix(name)
console.print_item("Adding collection '${name}' to Atlas '${self.name}' at path '${path.path}'")
if name in self.collections {
@@ -120,7 +119,7 @@ fn (mut a Atlas) scan_(mut dir pathlib.Path, ignore_ []string) ! {
// Check if this directory is a collection
if dir.file_exists('.collection') {
collname := dir.name_fix_no_underscore_no_ext()
collname := dir.name_fix_no_ext()
if collname.to_lower() in ignore_ {
return
}

View File

@@ -49,9 +49,8 @@ fn (mut c Collection) init_post() ! {
// Add a page to the collection
fn (mut c Collection) add_page(mut path pathlib.Path) ! {
// Use name_fix_no_underscore_no_ext to ensure consistent naming
// This ensures token_system.md and tokensystem.md both become 'tokensystem'
name := path.name_fix_no_underscore_no_ext()
// Use name_fix_no_ext to normalize the name
name := path.name_fix_no_ext()
if name in c.pages {
return error('Page ${name} already exists in collection ${c.name}')
}

View File

@@ -154,8 +154,8 @@ fn (mut p Page) parse_link_target(mut link Link) {
if target.contains(':') {
parts := target.split(':')
if parts.len >= 2 {
// Normalize collection name to remove underscores
link.target_collection_name = texttools.name_fix_no_underscore_no_ext(parts[0])
// Normalize collection name
link.target_collection_name = texttools.name_fix(parts[0])
link.target_item_name = normalize_page_name(parts[1])
}
} else {
@@ -272,14 +272,12 @@ fn (mut p Page) process_links(mut export_dir pathlib.Path) !string {
/////////////TOOLS//////////////////////////////////
// Normalize page name (remove .md, underscores, and apply name_fix)
// This ensures consistent naming: token_system, token-system, TokenSystem all become tokensystem
// Normalize page name (remove .md and apply name_fix)
fn normalize_page_name(name string) string {
mut clean := name
if clean.ends_with('.md') {
clean = clean[0..clean.len - 3]
}
// Use name_fix_no_underscore_no_ext to remove underscores and normalize
// This ensures token_system and tokensystem both become tokensystem
return texttools.name_fix_no_underscore_no_ext(clean)
// Use name_fix to normalize
return texttools.name_fix(clean)
}

View File

@@ -1,106 +0,0 @@
module atlas
import incubaid.herolib.core.texttools
// Test that normalize_page_name removes underscores and normalizes consistently
fn test_normalize_page_name() {
// Test basic normalization
assert normalize_page_name('token_system') == 'tokensystem'
assert normalize_page_name('token-system') == 'tokensystem'
assert normalize_page_name('TokenSystem') == 'tokensystem'
assert normalize_page_name('Token System') == 'tokensystem'
// Test with .md extension
assert normalize_page_name('token_system.md') == 'tokensystem'
assert normalize_page_name('token-system.md') == 'tokensystem'
assert normalize_page_name('TokenSystem.md') == 'tokensystem'
// Test edge cases
assert normalize_page_name('token__system') == 'tokensystem'
assert normalize_page_name('token___system') == 'tokensystem'
assert normalize_page_name('_token_system_') == 'tokensystem'
// Test special characters
assert normalize_page_name('token@system') == 'tokensystem'
assert normalize_page_name('token!system') == 'tokensystem'
// Note: # is treated specially (truncates at #, like URL anchors)
assert normalize_page_name('token#system') == 'token'
}
// Test collection name normalization
fn test_collection_name_normalization() {
// All these should normalize to the same value
assert texttools.name_fix_no_underscore_no_ext('my_collection') == 'mycollection'
assert texttools.name_fix_no_underscore_no_ext('my-collection') == 'mycollection'
assert texttools.name_fix_no_underscore_no_ext('MyCollection') == 'mycollection'
assert texttools.name_fix_no_underscore_no_ext('My Collection') == 'mycollection'
assert texttools.name_fix_no_underscore_no_ext('my collection') == 'mycollection'
}
// Test that different link formats resolve to the same target
fn test_link_target_normalization() {
// All these should normalize to 'tokensystem'
test_cases := [
'token_system',
'token-system',
'TokenSystem',
'token_system.md',
'token-system.md',
'TokenSystem.md',
'TOKEN_SYSTEM',
'Token_System',
]
for test_case in test_cases {
normalized := normalize_page_name(test_case)
assert normalized == 'tokensystem', 'Expected "${test_case}" to normalize to "tokensystem", got "${normalized}"'
}
}
// Test collection name in links
fn test_collection_name_in_links() {
// All these should normalize to 'collectiona'
test_cases := [
'collection_a',
'collection-a',
'CollectionA',
'Collection_A',
'COLLECTION_A',
]
for test_case in test_cases {
normalized := texttools.name_fix_no_underscore_no_ext(test_case)
assert normalized == 'collectiona', 'Expected "${test_case}" to normalize to "collectiona", got "${normalized}"'
}
}
// Test real-world examples
fn test_real_world_examples() {
// Common documentation page names
assert normalize_page_name('getting_started.md') == 'gettingstarted'
assert normalize_page_name('api_reference.md') == 'apireference'
assert normalize_page_name('user-guide.md') == 'userguide'
assert normalize_page_name('FAQ.md') == 'faq'
assert normalize_page_name('README.md') == 'readme'
// Technical terms
assert normalize_page_name('token_system.md') == 'tokensystem'
assert normalize_page_name('mycelium_cloud.md') == 'myceliumcloud'
assert normalize_page_name('tf_grid.md') == 'tfgrid'
}
// Test that normalization is idempotent (applying it twice gives same result)
fn test_normalization_idempotent() {
test_cases := [
'token_system',
'TokenSystem',
'token-system',
'Token System',
]
for test_case in test_cases {
first := normalize_page_name(test_case)
second := normalize_page_name(first)
assert first == second, 'Normalization should be idempotent: "${test_case}" -> "${first}" -> "${second}"'
}
}

View File

@@ -77,11 +77,12 @@ export_dir/
## Naming Convention
Names are normalized using `name_fix_no_underscore_no_ext()`:
Names are normalized using `name_fix()`:
- `My_Page-Name.md``mypagename`
- Removes: underscores, dashes, special chars, extensions
- `My_Page-Name.md``my_page_name`
- Removes: dashes, special chars
- Converts to lowercase
- Preserves underscores
## Example

View File

@@ -56,9 +56,9 @@ pub mut:
// get_page_path returns the path for a page in a collection
// Pages are stored in {export_dir}/content/{collection}/{page}.md
pub fn (mut c AtlasClient) get_page_path(collection_name string, page_name string) !string {
// Apply name normalization (atlas uses name_fix_no_underscore_no_ext)
fixed_collection_name := texttools.name_fix_no_underscore_no_ext(collection_name)
fixed_page_name := texttools.name_fix_no_underscore_no_ext(page_name)
// Apply name normalization
fixed_collection_name := texttools.name_fix(collection_name)
fixed_page_name := texttools.name_fix(page_name)
// Check if export directory exists
if !os.exists(c.export_dir) {
@@ -83,7 +83,7 @@ pub fn (mut c AtlasClient) get_page_path(collection_name string, page_name strin
// Files are stored in {export_dir}/content/{collection}/{filename}
pub fn (mut c AtlasClient) get_file_path(collection_name string, file_name string) !string {
// Apply name normalization
fixed_collection_name := texttools.name_fix_no_underscore_no_ext(collection_name)
fixed_collection_name := texttools.name_fix(collection_name)
// Files keep their original names with extensions
fixed_file_name := texttools.name_fix_keepext(file_name)
@@ -213,7 +213,7 @@ 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_no_underscore_no_ext(collection_name)
fixed_collection_name := texttools.name_fix(collection_name)
collection_dir := os.join_path(c.export_dir, 'content', fixed_collection_name)
@@ -260,7 +260,7 @@ pub fn (mut c AtlasClient) list_files(collection_name string) ![]string {
// 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_no_underscore_no_ext(collection_name)
fixed_collection_name := texttools.name_fix(collection_name)
collection_dir := os.join_path(c.export_dir, 'content', fixed_collection_name)

View File

@@ -624,7 +624,7 @@ fn test_naming_normalization_underscores() {
defer { cleanup_test_export(test_dir) }
// Create page with underscores
normalized := name_fix_no_underscore_no_ext('test_page_name')
normalized := texttools.name_fix('test_page_name')
os.write_file(os.join_path(test_dir, 'content', 'testcollection', '${normalized}.md'),
'# Test') or { panic(err) }
@@ -641,7 +641,7 @@ fn test_naming_normalization_dashes() {
defer { cleanup_test_export(test_dir) }
// Create page with dashes
normalized := name_fix_no_underscore_no_ext('test-page-name')
normalized := texttools.name_fix('test-page-name')
os.write_file(os.join_path(test_dir, 'content', 'testcollection', '${normalized}.md'),
'# Test') or { panic(err) }
@@ -658,7 +658,7 @@ fn test_naming_normalization_case() {
defer { cleanup_test_export(test_dir) }
// Create page with mixed case
normalized := name_fix_no_underscore_no_ext('TestPageName')
normalized := texttools.name_fix('TestPageName')
os.write_file(os.join_path(test_dir, 'content', 'testcollection', '${normalized}.md'),
'# Test') or { panic(err) }