#TODO:
proc pretty { text } {
set originalText $text
# puts $text
# & => &
set text [regsub -all -- "\[\&]" $text {\&}]
# prettify , and
if { [regexp {[<][ ]*[/]?[ ]*([[:alnum:]]+)[ ]*[/]?[ ]*[>]} $text] } {
set result [regsub {[<]([ ]*[/]?[ ]*)([[:alnum:]]*)([ ]*[/]?[ ]*)[>]} $text {\<\1\2\3\>}]
return "$result"
}
#prettify tag from . spanclass is a hack
set text [regsub {[<]([ ]*)([[:alnum:]]+)([^>]*)[>]} $text {\<\1\2\3\>}]
#prettify key and value from
set text [regsub -all -- {[ ]+([[:alnum:]][:[:alnum:]-]*)([ ]*=[ ]*\")([^"]*)(\")} $text { \1\2\3\4}]
# set text [regsub -all -- {[ ]+([[:alnum:]]+)([ ]*=[ ]*\")([^"]*)(\")} $text { \1\2\3\4}]
# Resolve spanclass
set text [regsub -all -- {[<][s][p][a][n][c][l][a][s][s]} $text "]" $text {\>}]
return $text
}
return "$text"
}
proc step { content } {
set result ""
while {[regexp -indices -- "\[\<]\[^\>]*\[\>]" $content indicie]} {
set cbegin [lindex $indicie 0]
set cend [lindex $indicie 1]
set subResult [pretty [string range $content $cbegin $cend]]
# puts $subResult\n
set result "$result[string range $content 0 [expr $cbegin-1]]$subResult"
set content [string range $content [expr $cend+1] end]
}
set result "$result$content"
return $result
}
proc entity { text } {
set text [regsub -all -- "\[\&]" $text {\&}]
set text [regsub -all -- "\[\<]" $text {\<}]
set text [regsub -all -- "\[\>]" $text {\>}]
return $text
}
proc prettify { text filename } {
set file [open $filename]
set content [read $file]
close $file
set pretty [step $text]
if { [regexp -indices -nocase -- "\[\<]\[\!]\[\-]{2}\[ ]*\[=pretty=]\[^\>]*\[\>]" $content indicie] } {
set cbegin [lindex $indicie 0]
set cend [lindex $indicie 1]
set content "[string range $content 0 [expr $cbegin-1]]$pretty[string range $content [expr $cend+1] end]"
}
if { [regexp -indices -nocase -- "\[\<]\[\!]\[\-]{2}\[ ]*\[=entitypretty=]\[^\>]*\[\>]" $content indicie] } {
set cbegin [lindex $indicie 0]
set cend [lindex $indicie 1]
set content "[string range $content 0 [expr $cbegin-1]][entity $pretty][string range $content [expr $cend+1] end]"
}
puts $content
}
#prettify "Test
Line 2
" "prettify.html"
#puts [step "\nTest"]
#puts \n[step "< h1> h1>< / h1>< h1 test=\"this\">"]