...
This commit is contained in:
@@ -120,27 +120,3 @@ ver := texttools.version("v0.4.36")
|
|||||||
ver := texttools.version("v1.4.36")
|
ver := texttools.version("v1.4.36")
|
||||||
// Result: 1004036
|
// Result: 1004036
|
||||||
```
|
```
|
||||||
|
|
||||||
## Error Handling
|
|
||||||
|
|
||||||
Many functions in the module return a Result type (indicated by `!` in the function signature). These functions can return errors that should be handled appropriately:
|
|
||||||
|
|
||||||
```v
|
|
||||||
// Example of error handling
|
|
||||||
name := texttools.name_fix_keepspace("some@name") or {
|
|
||||||
println("Error: ${err}")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
## Best Practices
|
|
||||||
|
|
||||||
1. Always use appropriate error handling for functions that return Results
|
|
||||||
2. Consider using `dedent()` before processing multiline text to ensure consistent formatting
|
|
||||||
3. When working with filenames, use the appropriate name_fix variant based on your needs
|
|
||||||
4. For command line parsing, be aware of quote handling and escaping rules
|
|
||||||
5. When using tokenization, consider the context and whether smart splitting is needed
|
|
||||||
|
|
||||||
## Contributing
|
|
||||||
|
|
||||||
The TextTools module is part of the heroLib project. Contributions are welcome through pull requests.
|
|
||||||
|
|||||||
@@ -41,6 +41,19 @@ ri.add_item('^Stest 1', 'TTT') or { panic(err) } //will be case insensitive sear
|
|||||||
|
|
||||||
mut text_out2 := ri.replace(text: text, dedent: true) or { panic(err) }
|
mut text_out2 := ri.replace(text: text, dedent: true) or { panic(err) }
|
||||||
|
|
||||||
|
//pub struct ReplaceDirArgs {
|
||||||
|
//pub mut:
|
||||||
|
// path string
|
||||||
|
// extensions []string
|
||||||
|
// dryrun bool
|
||||||
|
//}
|
||||||
|
// if dryrun is true then will not replace but just show
|
||||||
|
ri.replace_in_dir(path:"/tmp/mypath",extensions:["md"])!
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -58,7 +58,7 @@ pub fn regex_rewrite(r string) !string {
|
|||||||
// regex string see https://github.com/vlang/v/blob/master/vlib/regex/README.md .
|
// regex string see https://github.com/vlang/v/blob/master/vlib/regex/README.md .
|
||||||
// find_str is a normal search (text) .
|
// find_str is a normal search (text) .
|
||||||
// replace is the string we want to replace the match with
|
// replace is the string we want to replace the match with
|
||||||
fn (mut self ReplaceInstructions) add_item(regex_find_str string, replace_with string) ! {
|
pub fn (mut self ReplaceInstructions) add_item(regex_find_str string, replace_with string) ! {
|
||||||
mut item := regex_find_str
|
mut item := regex_find_str
|
||||||
if item.starts_with('^R') {
|
if item.starts_with('^R') {
|
||||||
item = item[2..] // remove ^r
|
item = item[2..] // remove ^r
|
||||||
@@ -226,13 +226,11 @@ fn (mut self ReplaceInstructions) replace_in_dir_recursive(path1 string, extensi
|
|||||||
mut pathnew := ''
|
mut pathnew := ''
|
||||||
mut count := 0
|
mut count := 0
|
||||||
|
|
||||||
|
console.print_debug(" - replace in dir: ${path1}")
|
||||||
|
|
||||||
for item in items {
|
for item in items {
|
||||||
|
// println(item)
|
||||||
pathnew = os.join_path(path1, item)
|
pathnew = os.join_path(path1, item)
|
||||||
// CAN DO THIS LATER IF NEEDED
|
|
||||||
// if pathnew in done{
|
|
||||||
// continue
|
|
||||||
// }
|
|
||||||
// done << pathnew
|
|
||||||
if os.is_dir(pathnew) {
|
if os.is_dir(pathnew) {
|
||||||
if item.starts_with('.') {
|
if item.starts_with('.') {
|
||||||
continue
|
continue
|
||||||
@@ -243,17 +241,15 @@ fn (mut self ReplaceInstructions) replace_in_dir_recursive(path1 string, extensi
|
|||||||
|
|
||||||
self.replace_in_dir_recursive(pathnew, extensions, dryrun, mut done)!
|
self.replace_in_dir_recursive(pathnew, extensions, dryrun, mut done)!
|
||||||
} else {
|
} else {
|
||||||
ext := os.file_ext(pathnew)[1..].to_lower()
|
tmpext:=os.file_ext(pathnew)[1..] or { ""}
|
||||||
|
ext := tmpext.to_lower()
|
||||||
if extensions == [] || ext in extensions {
|
if extensions == [] || ext in extensions {
|
||||||
// means we match a file
|
// means we match a file
|
||||||
|
|
||||||
txtold := os.read_file(pathnew)!
|
txtold := os.read_file(pathnew)!
|
||||||
txtnew := self.replace(text: txtold, dedent: false)!
|
txtnew := self.replace(text: txtold, dedent: false)!
|
||||||
if txtnew.trim(' \n') == txtold.trim(' \n') {
|
if txtnew.trim(' \n') == txtold.trim(' \n') {
|
||||||
// panic("need to move this file to other lib can't use print_header")
|
|
||||||
console.print_header(' nothing to do : ${pathnew}')
|
console.print_header(' nothing to do : ${pathnew}')
|
||||||
} else {
|
} else {
|
||||||
// panic("need to move this file to other lib can't use print_header")
|
|
||||||
console.print_header(' replace done : ${pathnew}')
|
console.print_header(' replace done : ${pathnew}')
|
||||||
count++
|
count++
|
||||||
if !dryrun {
|
if !dryrun {
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import json
|
|||||||
import os
|
import os
|
||||||
import freeflowuniverse.herolib.osal
|
import freeflowuniverse.herolib.osal
|
||||||
import freeflowuniverse.herolib.ui.console
|
import freeflowuniverse.herolib.ui.console
|
||||||
|
import freeflowuniverse.herolib.core.texttools.regext
|
||||||
|
|
||||||
pub fn (mut site DocSite) generate() ! {
|
pub fn (mut site DocSite) generate() ! {
|
||||||
console.print_header(' site generate: ${site.name} on ${site.factory.path_build.path}')
|
console.print_header(' site generate: ${site.name} on ${site.factory.path_build.path}')
|
||||||
@@ -61,8 +62,16 @@ pub fn (mut site DocSite) download_collections() ! {
|
|||||||
)!
|
)!
|
||||||
mut mypatho := pathlib.get(mypath)
|
mut mypatho := pathlib.get(mypath)
|
||||||
mypatho.copy(dest: '${site.factory.path_build.path}/docs/${item.dest}', delete: true)!
|
mypatho.copy(dest: '${site.factory.path_build.path}/docs/${item.dest}', delete: true)!
|
||||||
println(mypath)
|
// println(item)
|
||||||
// site.process_md(mut mypatho, item)!
|
//replace: {'NAME': 'MyName', 'URGENCY': 'red'}
|
||||||
|
mut ri := regext.regex_instructions_new()
|
||||||
|
for key,val in item.replace {
|
||||||
|
ri.add_item("\{${key}\}",val)!
|
||||||
|
}
|
||||||
|
// println(ri)
|
||||||
|
ri.replace_in_dir(path:"${site.factory.path_build.path}/docs/${item.dest}",extensions:["md"])!
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user