Source Code for workspace.img2imgRef
on img2imgRef (s="") {
local (start, end, x, ttr);
local {
imgATR = "";
altATR = "";
borderATR = "";
attrATR = "";
usemapATR = ""};
on makeAttr (attribute, theValue) {
return ( " " +string.lower(attribute) + ":\"" + theValue + "\"")};
if s == "" {
wp.setTextMode (true);
wp.getSelect (@start, @end);
if start == end { // Nothing selected
window.msg ("Hey, nothing selected...");
speaker.ouch (); window.msg("");return}};
local (source, part, ix, attr, val );
if s == "" {
source = wp.getSelText ()}
else {
source = s};
source = string.nthfield (source, '>', string.countfields (source, '>'));
if not string.lower (source) contains "<img " {
window.msg ("This ain't an image tag.");
speaker.ouch ();speaker.ouch ();
window.msg("");
return};
ix = string.patternmatch (" ", source);
source = string. mid (source, ix + 1, infinity); // delete '<img '
«while source != "" «loop over source, pulling out attributes one by one
«part = string.nthfield (source, ' ', 1)
«source = string.mid (source, sizeOf(part)+1, infinity)
«source = string.popleading (source, ' ')
«
«attr = string.nthfield (part, '=', 1)
«val = string.nthfield (part, '=', 2)
«case string.lower(attr)
«"height"
«"width"
«continue
«"src"
«val = string.nthfield (val, '/', string.countfields (val, '/'))
«val = string.poptrailing (val, '"');val = string.popsuffix (val, '.')
«imgATR ="\"" + val + "\""
«"border"
«val = string.popTrailing (val, '"');val = string.popLeading (val, '"')
«borderATR = makeAttr (attr, val)
«"usemap"
«val = string.popTrailing (val, '"');val = string.popLeading (val, '"')
«usemapATR = makeAttr (attr, val)
«"alt"
«val = string.popTrailing (val, '"');val = string.popLeading (val, '"')
«altATR = makeAttr (attr, val)
«else
«if attrATR == "" «first time
«attrATR = part
«else
«attrATR = attrATR + " " + part
while source != "" { «loop over source, pulling out attributes one by one
local (markerChar, flSpace, ixend, testChar);
ix = string.patternmatch ('=', source);
markerChar = string.nthChar (source, ++ix);
case markerChar {
'\'';
'"' {
flSpace = false};
' ' {
flSpace=true;
«ignore whitespace in next line
while (markerChar == ' ') && (string.nthChar (source, ix + 1) == ' ') {
markerChar = string.nthChar (source, ++ix)};
while (markerChar == ' ') && (string.nthChar (source, ix + 1) == '\'') {
markerChar = string.nthChar (source, ++ix)};
while (markerChar == ' ') && (string.nthChar (source, ix + 1) == '"') {
markerChar = string.nthChar (source, ++ix)}}}
else {
flSpace = true};
loop {
testChar = string.nthChar (source, ++ix);
if testChar == "" {
break};
if testChar == ' ' && flSpace {
break};
if testChar == markerChar {
ix++;
break}};
«part = string.nthfield (source, ' ', 1)
part = string.mid (source, 1, ix - 1);
source = string.mid (source, sizeOf(part)+1, infinity);
source = string.popleading (source, ' ');
attr = string.nthfield (part, '=', 1);
attr = string.poptrailing (attr, ' ');
val = string.nthfield (part, '=', 2);
val = string.popleading (val, ' ');
case string.lower(attr) {
"height";
"width" {
continue};
"src" {
val = string.nthfield (val, '/', string.countfields (val, '/'));
val = string.poptrailing (val, '"');val = string.popsuffix (val, '.');
imgATR ="\"" + val + "\""};
"border" {
val = string.popTrailing (val, '"');val = string.popLeading (val, '"');
borderATR = makeAttr (attr, val)};
"usemap" {
val = string.popTrailing (val, '"');val = string.popLeading (val, '"');
usemapATR = makeAttr (attr, val)};
"alt" {
val = string.popTrailing (val, '"');val = string.popLeading (val, '"');
if markerChar == '\'' {
val = string.popTrailing (val, '\'');val = string.popLeading (val, '\'')};
altATR = makeAttr (attr, val)}}
else {
if attrATR == "" { «first time
attrATR = part}
else {
attrATR = attrATR + " " + part}}};
bundle { «build the macro string
If imgATR == "" {
dialog.alert ("Couldn't find a imageSpec for this <img> tag.");
return};
ttr = "{imgRef ( " + imgATR;
if altATR != "" {
ttr = ttr + ", " + altAtr};
if borderATR != "" {
ttr = ttr + ", " + borderAtr};
if attrATR != "" {
attrATR = "\"" + string.replaceAll (attrATR, "\"", "\\\"") + "\"";
ttr = ttr + ", " + attrAtr};
if usemapATR != "" {
ttr = ttr + ", " + usemapAtr};
ttr = ttr + ")}"};
if s == "" {
wp.insert (ttr);
wp.setSelect (start, start + sizeOf (ttr));
msg (ttr);
return (true)}
else {
return (ttr)}};
bundle { «test code
«dialog.notify(workspace.img2imgRef ("<IMG SRC=\"images/nav1spacer.GIF\" WIDTH=\"15\" HEIGHT=\"15\" BORDER=\"0\" alt=Thisistext>"))
«dialog.notify(workspace.img2imgRef ("<IMG SRC=\"images/nav1spacer.GIF\" WIDTH=\"15\" HEIGHT=\"15\" BORDER=\"0\" alt=\"This is text\">"))
dialog.notify(workspace.img2imgRef ("<IMG SRC=\"images/nav1spacer.GIF\" WIDTH=\"15\" HEIGHT=\"15\" BORDER=\"0\" alt = ' This is text '>"));
}«dialog.notify(workspace.img2imgRef ("<IMG SRC=\"images/nav1spacer.GIF\" WIDTH=\"15\" HEIGHT=\"15\" BORDER=\"0\" alt = \" This is text \" >"))
Go back to the main page for this script