генератор html-кода на tcl
Создано: 02-08-2011 00:00:09  Метки: html tcl
Данная библиотека появилась пару лет назад на примерах Печникова. Под себя я правда переписал...
Вот пример кода:
#! /usr/bin/tclsh
source config/cfg.cfg
test_cookie
doctype
html {
 siteheader "Iconez" lite
 body {
  cms-menu
  sidebar
  if ![guest] {
    div -class main -style text-align:center {
      h3 {puts "Прикрепленные ярлычки:"}
      hr
      table {
        foreach lin [exec ls images/ | grep "16x16.png$"] {
          tr {td {img -src /images/$lin -width 32px -height 32px}
            td {puts /images/$lin}
          }
        }
      }
    }
  }
  sitefooter lite
  }
}

А вот библиотека:
package provide html2 1.0
namespace eval ::html2:: {
    namespace export *
}

# шаблон для генерации стандартного html-тэга
proc ::html2::tag {name args} {
  set name [string range $name [expr [string last : $name]+1] end]
  if [expr [llength $args]%2] {
    set opts [lrange $args 0 end-1]
    set b [lindex $args end]
  } else {
    set opts $args
    set b ""
  }
  puts -nonewline "<$name"
  foreach {argname argvalue} $opts {
    if [regexp " $argname " " -checked -hidden -readonly "] {
      if {$argvalue ne "0"} {puts -nonewline " [string range $argname 1 end]"}
    } else {puts -nonewline " [string range $argname 1 end]=\"$argvalue\""}
  }
  ### для незакрывающихся тегов
  if ![regexp " $name " " area base basefont col frame param meta link img input hr br "] {
    puts -nonewline ">"
    uplevel 1 $b
    puts -nonewline "</$name>"
  } else {
    if {$name eq "img" && ![regexp -- "-alt" $opts]} {

      puts -nonewline " alt=\"\""
    }
    puts -nonewline ">"
  }
}

foreach name {html meta title body head style link script form textarea input table thead tfoot tbody tr td th
  center a font button div span p img label ol ul li b i u s strong em h1 h2 h3 h4 h5 h6
  br hr var code kbd tt samp pre acronym embed header footer dd dt video audio} {
  proc ::html2::$name args {
    set name [lindex [info level 0] 0]
    uplevel 1 html2::tag $name $args
  }
}

proc ::html2::doctype {} {
  puts {Content-Type: text/html; charset=utf-8

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">}
}

proc ::html2::html5 {} {
  puts {Content-Type: text/html; charset=utf-8

<!DOCTYPE html>}
}

1498 просмотров комментировать