This sounds fine, but you could make it a little cleaner; instead of copying 'some' bytes and then looking for </div>, why not search for </div> in the binvar starting from the position of <div ...>. Here's a general-purpose example of this:
alias gettext {
var %b = &gettext $+ $ticks
bread $qt($1) 0 $file($1) %b
if $bfind(%b,1,$2) {
var %pos = $v1 + $len($2)
bcopy -c %b 1 %b %pos $calc($bfind(%b,%pos,$3) - %pos)
; %b stores the name of the binvar holding the desired text
return %b
}
}
You can test it with something like:
//var %binvar = $gettext(file.html,<div id="foo">,</div>) | echo -ag $bvar(%binvar,1-).text
Bear in mind that this won't work if
- there are children divs in the 'foo' one
- there is extra white space inside the div tag you're looking for, eg
<div id = "foo" >or
<div
id="foo">etc. A working version that takes both into account would have to be lengthier/more complex, but it's probably not necessary in this case (about the second issue, if the format changes in the future, you can always pass different parameters to the identifier).