Needed something to breakup a string into a simple list. My first thought was to use arrays but then thought about using the dict command. This will produce an enumerated list maintaining the creation order of the extracted text markup substrings.
set str "abc deg ghi <b>Bold</b> italic <i>italic</i>"
## parse markup string string into an enumerated list of text and tags
# @param str
# @returns enumerated list
proc parseMarkupStr {str} {
set idx 0
set res ""
set t 0
foreach ch [split $str {}] {
# detect markup start and end
if { $ch == "<" } {
set t 1
incr idx }
if { $ch == ">" } {
set res [dict append res $idx $ch]
set t 0
incr idx
continue }
set res [dict append res $idx $ch]
}
return $res
}
puts [parseMarkup $str]
0 {abc deg ghi } 1 <b> 2 Bold 3 </b> 4 { italic } 5 <i> 6 italic 7 </i>
Comments