add regexp matches, inline regsub/replace, asuniqfilename

This commit is contained in:
hummypkg 2016-06-29 20:48:34 +00:00
parent 3ef45af5e0
commit 7745c19cfa
3 changed files with 57 additions and 8 deletions

View File

@ -1,7 +1,7 @@
Package: sweeper
Priority: optional
Section: misc
Version: 2.1.1
Version: 2.1.2
Architecture: mipsel
Maintainer: af123@hummypkg.org.uk
Depends: webif(>=1.3.0-1)

View File

@ -33,6 +33,9 @@ proc ::sweeper::intcomp {ref val} {
# Substring/pattern check
proc ::sweeper::strcontains {ref val} {
if {[string index $val 0] eq "~"} {
return [regexp -nocase -- [string range $val 1 end] $ref]
}
if {[string first "*" $val] > -1} {
return [string match -nocase $val $ref]
}
@ -42,18 +45,19 @@ proc ::sweeper::strcontains {ref val} {
}
set ::sweeper::expand_fns {
replace {2}
regsub {2}
replace {3 {inline fallback}}
regsub {3 {inline fallback}}
asfilename {1 {inline}}
asuniqfilename {1 {inline}}
format {2 {inline}}
var {1 {inline}}
}
proc ::sweeper::expand_replace {ts &ret search replace} {
proc ::sweeper::expand_fb_replace {ts &ret search replace} {
set ret [string map [list $search $replace] $ret]
}
proc ::sweeper::expand_regsub {ts &ret search replace} {
proc ::sweeper::expand_fb_regsub {ts &ret search replace} {
if {[catch {
regsub -all -- $search $ret $replace ret
} msg]} {
@ -61,10 +65,40 @@ proc ::sweeper::expand_regsub {ts &ret search replace} {
}
}
proc ::sweeper::expand_replace {ts &ret arg search replace} {
set arg [string map [list $search $replace] $arg]
return $arg
}
proc ::sweeper::expand_regsub {ts &ret arg search replace} {
if {[catch {
regsub -all -- $search $arg $replace arg
} msg]} {
log "Error. %regsub - $msg"
}
return $arg
}
proc ::sweeper::expand_asfilename {ts &ret arg} {
return [system filename $arg]
}
proc ::sweeper::expand_asuniqfilename {ts &ret arg} {
set arg [system filename $arg]
if {[string index $arg 0] ne "/"} {
set path "[$ts dir]/$arg"
} else {
set path $arg
}
if {[file isfile "$path.ts"]} {
set i 2
while {[file isfile "${path}_$i.ts"]} { incr i }
append arg "_$i"
}
return $arg
}
proc ::sweeper::expand_format {ts &ret format arg} {
if {[catch {set r [format $format $arg]} msg]} {
log "Error. %format - $msg"
@ -109,11 +143,24 @@ proc ::sweeper::expand {ts str {orig ""}} {
# Extract the arguments
set pos $chpos
set fnargs {}
set e -1
while {[llength $fnargs] < $numargs} {
incr pos
set le $e
set e [string first $ch $ret $pos]
if {$e == -1} {
log "Error. %$fn - [llength $fnargs]/$numargs parameters found."
# Insufficient arguments.
set argcnt [llength $fnargs]
if {"fallback" in $flags &&
"inline" in $flags &&
[expr $argcnt + 1] == $numargs} {
log "%$fn - falling back." 2
set fn "fb_$fn"
set flags {}
set e $le
} else {
log "Error. %$fn - $argcnt/$numargs parameters found."
}
break
}
lappend fnargs [::sweeper::expand $ts [
@ -132,7 +179,9 @@ proc ::sweeper::expand {ts str {orig ""}} {
# the result.
set fnret [
::sweeper::expand_$fn $ts ret {*}$fnargs]
set oret $ret
set ret [string replace $ret $s $e $fnret]
log " $fn\($oret) -> \[$ret]" 2
} else {
# otherwise, queue the function up for later
# execution.

View File

@ -169,8 +169,8 @@ For pattern matching, the following special sequences may appear in the pattern:
<div class="hidden edit_textmatch_help blood"
id=edit_textmatch_help_textmatch>
<br>
You can enter either a substring which must be found somewhere or a pattern<br>
which must be matched.
You can enter either a substring which must be found somewhere, a pattern<br>
which must be matched or a regular expression prefixed with ~.
<br>
For pattern matching, the following special sequences may appear in the pattern:
<div style="width: 600px;margin: 1em">