From 7745c19cfa7a43820aea3b2aedd295652a748cad Mon Sep 17 00:00:00 2001 From: hummypkg Date: Wed, 29 Jun 2016 20:48:34 +0000 Subject: [PATCH] add regexp matches, inline regsub/replace, asuniqfilename --- CONTROL/control | 2 +- webif/plugin/sweeper/auto.hook | 59 +++++++++++++++++++++++++++++++--- webif/plugin/sweeper/edit.jim | 4 +-- 3 files changed, 57 insertions(+), 8 deletions(-) diff --git a/CONTROL/control b/CONTROL/control index bc599cc..78bdc3f 100644 --- a/CONTROL/control +++ b/CONTROL/control @@ -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) diff --git a/webif/plugin/sweeper/auto.hook b/webif/plugin/sweeper/auto.hook index c5b52d7..af87db5 100644 --- a/webif/plugin/sweeper/auto.hook +++ b/webif/plugin/sweeper/auto.hook @@ -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. diff --git a/webif/plugin/sweeper/edit.jim b/webif/plugin/sweeper/edit.jim index 977e48f..3fcb7b0 100755 --- a/webif/plugin/sweeper/edit.jim +++ b/webif/plugin/sweeper/edit.jim @@ -169,8 +169,8 @@ For pattern matching, the following special sequences may appear in the pattern: