View/Edit #29:slice_tagged

Switch to edit mode.

Name(s): slice_tagged
1:  "Copied from MattC (#9466):html_Substr Jan 11 21:11:07 1999"
2:  ":slice_tagged(string, length[, cap_tags])"
3:  "Slices 'string' after 'length' characters.  Is HTML aware."
4:  "eg. $su:slice_tagged('1<b>23</b>4', 2) => '1<b>2</b>'"
5:  "eg. $su:slice_tagged('1&amp;2', 2) => '1&amp;'"
6:  {string, length, ?cap_tags = 1} = args
7:  rest = string
8:  tags = {}
9:  while (rest)
10:   if ("<" == rest[1])
11:     chunksize = index(rest, ">") || length(rest)
12:   elseif (">" == rest[1])
13:     chunksize = 1
14:   elseif ("&" == rest[1] && length(rest) > 1 && " " == rest[2])
15:     "Just a lone & in a sentance."
16:     chunksize = 1
17:     length = length - 1
18:   elseif ("&" == rest[1])
19:     "An &entity; in a sentance."
20:     chunksize = $math_utils:min_positive(index(rest, ";"), index(rest, " "), length(rest))
21:     length = length - 1
22:   else
23:     i1 = index(rest, "<")
24:     i2 = index(rest, ">")
25:     i3 = index(rest, "&")
26:     chunksize = $math_utils:min_positive(i1 - 1, i2 - 1, i3 - 1, length(rest), length)
27:     length = length - chunksize
28:   endif
29:   if (length < 1)
30:     string = string[1..$ - length(rest) + chunksize]
31:     break
32:   endif
33:   tag = rest[1..chunksize]
34:   if (tag[1] == "<")
35:     if (tag[2] == "/")
36:       for x in (tags)
37:         if ($su:explode(x)[1] == tag[3..$ - 1])
38:           tags = listdelete(tags, x in tags)
39:           break
40:         endif
41:       endfor
42:     else
43:       tags = {tag[2..$ - 1], @tags}
44:     endif
45:   elseif (tag[1] == "&")
46:   endif
47:   rest = rest[chunksize + 1..$]
48: endwhile
49: if (cap_tags)
50:   for x in (tags)
51:     if (x && !($su:explode(x)[1] in $html_utils.single_tags))
52:       string = tostr(string, "</", $su:explode(x)[1], ">")
53:     endif
54:   endfor
55: endif
56: return string
57: "Last modified by Dax (#789) on Tue Aug 20 12:56:43 2002 MDT."

Verb arguments: this none this
Verb perms: Readable eXecutable NOT-Debug
Verb owned by: Hacker (#60)

You are not logged in.

[home | help | who | search | setup | code]