summaryrefslogtreecommitdiff
path: root/viewhtml
diff options
context:
space:
mode:
authorAki <please@ignore.pl>2020-08-05 23:20:00 +0200
committerAki <please@ignore.pl>2020-08-05 23:20:19 +0200
commit72fa2618ec508cf191f6d9ca0fab3ce2d24c4766 (patch)
tree0fb7c717aa47f3f2cbdf1e8ac5fcf41d65bf1348 /viewhtml
parent09fad3455b734ce8108afc4cc3df9248c539a713 (diff)
downloadbrowse-72fa2618ec508cf191f6d9ca0fab3ce2d24c4766.zip
browse-72fa2618ec508cf191f6d9ca0fab3ce2d24c4766.tar.gz
browse-72fa2618ec508cf191f6d9ca0fab3ce2d24c4766.tar.bz2
Moved in html5 viewer from sketches
Diffstat (limited to 'viewhtml')
-rwxr-xr-xviewhtml98
1 files changed, 98 insertions, 0 deletions
diff --git a/viewhtml b/viewhtml
new file mode 100755
index 0000000..f853dc3
--- /dev/null
+++ b/viewhtml
@@ -0,0 +1,98 @@
+#!/usr/bin/env tclsh
+package require tclgumbo
+package require Tk
+
+
+text .t -yscrollcommand {.s set} -relief flat -font {Times 12} -wrap word -border 4
+scrollbar .s -command {.t yview}
+pack .s -side right -fill y
+pack .t -side left -fill both -expand 1
+
+set w .t
+
+$w tag config title -elide true
+$w tag config href -elide true
+$w tag config a -foreground blue -underline 1
+$w tag config strong -font {Times 12 bold}
+$w tag config b -font {Times 12 bold}
+$w tag config i -font {Times 12 italic}
+$w tag config em -font {Times 12 italic}
+$w tag config pre -font {Courier 12}
+$w tag config code -font {Courier 12}
+$w tag config h1 -font {Times 18}
+$w tag config h2 -font {Times 18}
+$w tag config ul -lmargin1 40 -lmargin2 40
+$w tag config dd -lmargin1 40 -lmargin2 40
+$w tag config p
+$w tag bind a <Enter> "$w config -cursor hand2"
+$w tag bind a <Leave> "$w config -cursor {}"
+$w tag bind a <ButtonRelease-1> "click $w %x %y"
+
+# TODO:
+set baseAddress [lindex $argv 1]
+set file [open [lindex $argv 0]]
+set html [read $file]
+close $file
+set output [gumbo::parse $html]
+
+proc click {w x y} {
+ global baseAddress
+ set range [$w tag prevrange href [$w index @$x,$y]]
+ set address [eval $w get $range]
+
+ # TODO:
+ if {0 == [regexp {^https?.*} $address]} {
+ exec "./html.tcl" "$baseAddress/$address" $baseAddress
+ exit
+ } else {
+ exec "./html.tcl" $address $address
+ exit
+ }
+}
+
+proc displayNode {node tagList} {
+ # TODO: Avoid using global variables.
+ global w
+ global baseAddress
+
+ set type [gumbo::node_get_type $node]
+ if {$type == $gumbo::GUMBO_NODE_ELEMENT} {
+ set tag [gumbo::element_get_tag_name $node]
+ set attributes [gumbo::element_get_attributes $node]
+
+ # TODO:
+ if {$tag == "a"} {
+ $w insert end [lindex [array get $attributes] 1] [concat $tagList href]
+ } elseif {$tag == "img"} {
+ $w insert end "\n"
+ set lattr [array get $attributes]
+ set path "$baseAddress/[lindex $lattr [expr [lsearch -exact $lattr src] + 1]]"
+ image create photo $path -file $path
+ $w image create end -image $path
+ $w insert end "\n"
+ }
+
+ foreach child_node [gumbo::element_get_children $node] {
+ displayNode $child_node [concat $tag $tagList]
+ }
+
+ # TODO:
+ if {$tag == "h1" || $tag == "h2" || $tag == "p"} {
+ $w insert end "\n\n" {}
+ } elseif {$tag == "pre" || $tag == "li" || $tag == "dt" || $tag == "ul" || $tag == "dd" || $tag == "dl"} {
+ $w insert end "\n" {}
+ }
+ } elseif {$type == $gumbo::GUMBO_NODE_TEXT} {
+ # TODO:
+ if {0 <= [lsearch $tagList pre]} {
+ $w insert end [gumbo::text_get_text $node] $tagList
+ } else {
+ $w insert end [regsub -all {\s+} [gumbo::text_get_text $node] " "] $tagList
+ }
+ }
+}
+
+displayNode [gumbo::output_get_root $output] [list]
+$w config -state disabled
+gumbo::destroy_output $output
+