PageRenderTime 52ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/example4/example.tcl

https://github.com/jdc8/wubexamples
TCL | 210 lines | 193 code | 16 blank | 1 comment | 12 complexity | a4492083b21d9bc9a4ff67b125f9f333 MD5 | raw file
  1. lappend auto_path ../Wub ../tcllib/modules
  2. package require Site
  3. namespace eval MyDirectDomain {
  4. proc /test { req } {
  5. dict set req -content "Test for MyDirectDomain"
  6. dict set req content-type x-text/html-fragment
  7. dict set req -title "MyDirectDomain: test with query"
  8. return $req
  9. }
  10. proc /test_with_query { req A B C } {
  11. dict set req -content "Test with query for MyDirectDomain A=$A, B=$B, C=$C"
  12. dict set req content-type x-text/html-fragment
  13. dict set req -title "MyDirectDomain: test"
  14. return $req
  15. }
  16. proc /test_without_armour { req } {
  17. dict set req -content "Test without armouring for MyDirectDomain < > ' //"
  18. dict set req content-type x-text/html-fragment
  19. dict set req -title "MyDirectDomain: non-armour test"
  20. return $req
  21. }
  22. proc /test_with_armour { req } {
  23. dict set req -content [armour "Test with armouring for MyDirectDomain < > ' //"]
  24. dict set req content-type x-text/html-fragment
  25. dict set req -title "MyDirectDomain: armour test"
  26. return $req
  27. }
  28. proc /test_with_armour_in_convert { req } {
  29. dict set req -content "Test with armouring using convert for MyDirectDomain < > ' //"
  30. dict set req content-type x-unarmoured-text/html-fragment
  31. dict set req -title "MyDirectDomain: armour convert test"
  32. return $req
  33. }
  34. proc /test_plain_text { req } {
  35. dict set req -content "Test with plain text for MyDirectDomain"
  36. dict set req content-type text/plain
  37. dict set req -title "MyDirectDomain: plain text test"
  38. return $req
  39. }
  40. proc /test_css_javascript { req } {
  41. set C "Test with css and javascript: "
  42. append C [<div> class red id contents {}]
  43. append C "<button type='button' onclick='add_contents();'>Add contents</button>"
  44. dict set req -content "$C"
  45. dict set req -style [list /css/red.css {}]
  46. dict set req -script [list /scripts/contents.js {}]
  47. dict set req content-type x-text/html-fragment
  48. dict set req -title "MyDirectDomain: css and javascript test"
  49. return $req
  50. }
  51. proc /default { req } {
  52. set content "Default function for MyDirectDomain"
  53. set ml {}
  54. foreach m [info command ::MyDirectDomain::/test*] {
  55. lappend ml $m /directns[string range $m 18 end]
  56. }
  57. append content [Html menulist $ml]
  58. dict set req -content $content
  59. dict set req content-type x-text/html-fragment
  60. dict set req -title "MyDirectDomain: default"
  61. return $req
  62. }
  63. }
  64. oo::class create MyOODomain {
  65. constructor {args} {
  66. }
  67. method /test {req args} {
  68. dict set req -content "Test for MyOODomain"
  69. dict set req content-type x-text/html-fragment
  70. dict set req -title "MyOODomain: test"
  71. return $req
  72. }
  73. method /test_with_query {req A B C args} {
  74. dict set req -content "Test with query for MyOODomain A=$A, B=$B, C=$C"
  75. dict set req content-type x-text/html-fragment
  76. dict set req -title "MyOODomain: test with query"
  77. return $req
  78. }
  79. method /test_without_armour { req } {
  80. dict set req -content "Test without armouring for MyOODomain < > ' //"
  81. dict set req content-type x-text/html-fragment
  82. dict set req -title "MyOODomain: non-armour test"
  83. return $req
  84. }
  85. method /test_with_armour { req } {
  86. dict set req -content [armour "Test with armouring for MyOODomain < > ' //"]
  87. dict set req content-type x-text/html-fragment
  88. dict set req -title "MyOODomain: armour test"
  89. return $req
  90. }
  91. method /test_with_armour_in_convert { req } {
  92. dict set req -content "Test with armouring using convert for MyOODomain < > ' //"
  93. dict set req content-type x-unarmoured-text/html-fragment
  94. dict set req -title "MyOODomain: armour convert test"
  95. return $req
  96. }
  97. method /test_plain_text { req } {
  98. dict set req -content "Test with plain text for MyOODomain"
  99. dict set req content-type text/plain
  100. dict set req -title "MyDirectDomain: plain text test"
  101. return $req
  102. }
  103. method /test_html_tags { req } {
  104. set content ""
  105. append content [<h1> "Some test with HTML tags"]
  106. append content [<p> "This is [<b> bold] and this is [<i> italic], this is both [<b> [<i> {bold and italic}]], this is a [<a> href /directoo/test link]"] \n
  107. append content [<p> "My [<tt> tcl_platform] as unordered list:"] \n
  108. append content [<ul> [Foreach {k v} [array get ::tcl_platform] {<li>$k=$v</li>}]] \n
  109. append content [<p> "My [<tt> tcl_platform] as ordered list:"] \n
  110. append content [<ol> [Foreach {k v} [array get ::tcl_platform] {[<li> "$k=$v"]}]] \n
  111. append content [<p> "My [<tt> tcl_platform] as table:"] \n
  112. append content [<table> summary "tcl_platform" [Foreach {k v} [array get ::tcl_platform] {[<tr> "[<td> $k] [<td> $v]"]}]] \n
  113. append content [<p> "A [<tt> menulist]"] \n
  114. set ml {}
  115. foreach m [info object methods [self] -private -all] {
  116. if {[string match /* $m]} {
  117. lappend ml $m /directoo$m
  118. }
  119. }
  120. append content [Html menulist $ml]
  121. append content [<span> class "plain_span" {Non empty span}] \n
  122. append content [<span> class "plain_span" {}] \n
  123. append content [<span>? class "non_empty_span" {Non empty span}] \n
  124. append content [<span>? class "non_empty_span" {}] \n
  125. append content [<div> class "test_div" "My test div"] \n
  126. append content "Using [armour <br>] [<br>] and [armour <hr>] [<hr>] to jump to new lines." \n
  127. append content [<p> [<img> src /images/pwrdLogo100.gif]] [<hr>] \n
  128. set headers {}
  129. lappend headers [<author> "Jos Decoster (jos.decoster@gmail.com)"]
  130. lappend headers [<description> "A test page for HTML tag commands"]
  131. lappend headers [<copyright> "2009 Jos Decoster"]
  132. lappend headers [<generator> "Emacs"]
  133. lappend headers [<keywords> "Tcl Wub"]
  134. lappend headers [<meta> name MyMetaTag content "This is my meta tag"]
  135. lappend headers [<link> rel StyleSheet type text/css media print href /css/sorttable.css]
  136. lappend headers [<stylesheet> /css/sorttable.css handheld]
  137. dict set req -content $content
  138. dict set req -headers $headers
  139. dict set req content-type x-text/html-fragment
  140. dict set req -title "MyOODomain: HTML tag command tests"
  141. return $req
  142. }
  143. method /test_text_html { req } {
  144. set head [<head>]
  145. set body [<body> [divs {a b c d e f g} "Deeply nested div"]]
  146. set content [<html> $head$body]
  147. dict set req -content $content
  148. dict set req content-type text/html
  149. return $req
  150. }
  151. method /test_css_javascript { req } {
  152. set C "Test with css and javascript: "
  153. append C [<div> class red id contents {}]
  154. append C "<button type='button' onclick='add_contents();'>Add contents</button>"
  155. dict set req -content "$C"
  156. dict set req -style [list /css/red.css {}]
  157. dict set req -script [list /scripts/contents.js {}]
  158. dict set req content-type x-text/html-fragment
  159. dict set req -title "MyOODomain: css and javascript test"
  160. return $req
  161. }
  162. method / { req } {
  163. set content [<p> "Default function for MyOODomain"]
  164. set ml {}
  165. foreach m [info object methods [self] -private -all] {
  166. if {[string match /* $m]} {
  167. lappend ml $m /directoo$m
  168. }
  169. }
  170. append content [Html menulist $ml]
  171. dict set req -content $content
  172. dict set req content-type x-text/html-fragment
  173. dict set req -title "MyOODomain: default"
  174. return $req
  175. }
  176. }
  177. set oodomain [MyOODomain new]
  178. package require conversions
  179. set Html::XHTML 1
  180. set ::conversions::htmlhead {<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">}
  181. namespace eval ::conversions {
  182. proc .x-unarmoured-text/html-fragment.x-text/html-fragment { rsp } {
  183. set rspcontent [dict get $rsp -content]
  184. if {[string match "<!DOCTYPE*" $rspcontent]} {
  185. # the content is already fully HTML
  186. set content $rspcontent
  187. } else {
  188. set content [armour $rspcontent]
  189. }
  190. return [Http Ok $rsp $content x-text/html-fragment]
  191. }
  192. }
  193. Site start home . config example.config