home *** CD-ROM | disk | FTP | other *** search
/ Macwelt 1 / Macwelt DVD 1.toast / Web-Publishing / HTML-Editoren / Alpha ƒ / Tcl / Modes / HTML and CSS Modes / htmlCaches.tcl < prev    next >
Encoding:
Text File  |  2001-01-12  |  9.6 KB  |  310 lines

  1. ## -*-Tcl-*-
  2.  # ###################################################################
  3.  #  HTML mode - tools for editing HTML documents
  4.  # 
  5.  #  FILE: "htmlUtils.tcl"
  6.  #                                    created: 99-07-20 17.52.36 
  7.  #                                last update: 00-12-29 21.55.43 
  8.  #  Author: Johan Linde
  9.  #  E-mail: <alpha_www_tools@go.to>
  10.  #     www: <http://go.to/alpha_www_tools>
  11.  #  
  12.  # Version: 3.0
  13.  # 
  14.  # Copyright 1996-2001 by Johan Linde
  15.  #  
  16.  # This program is free software; you can redistribute it and/or modify
  17.  # it under the terms of the GNU General Public License as published by
  18.  # the Free Software Foundation; either version 2 of the License, or
  19.  # (at your option) any later version.
  20.  # 
  21.  # This program is distributed in the hope that it will be useful,
  22.  # but WITHOUT ANY WARRANTY; without even the implied warranty of
  23.  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  24.  # GNU General Public License for more details.
  25.  # 
  26.  # You should have received a copy of the GNU General Public License
  27.  # along with this program; if not, write to the Free Software
  28.  # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  29.  # 
  30.  # ###################################################################
  31.  ##
  32.  
  33. #===============================================================================
  34. # This file contains the procs for the URLs and Windows submenus plus procs
  35. # for general caches.
  36. #===============================================================================
  37.  
  38. #===============================================================================
  39. # ◊◊◊◊ General caches ◊◊◊◊ #
  40. #===============================================================================
  41.  
  42. proc html::SaveCache {cache text} {
  43.     global html::PrefsFolder htmlVersion
  44.     file::ensureDirExists ${html::PrefsFolder}
  45.     set fid [open [file join ${html::PrefsFolder} $cache] w]
  46.     if {[info exists htmlVersion]} {puts $fid "#$htmlVersion"} else {puts $fid "#1.0"}
  47.     puts $fid $text
  48.     close $fid
  49. }
  50.   
  51. proc html::ReadCache {cache {level #0}} {
  52.     global html::PrefsFolder htmlVersion
  53.     if {![file exists [file join ${html::PrefsFolder} $cache]]} {error "No cache."}
  54.     set fid [open [file join ${html::PrefsFolder} $cache] r]
  55.     gets $fid version
  56.     if {![regexp {^#[0-9]+\.[0-9]+$} $version] || $version != "#$htmlVersion"} {
  57.         close $fid
  58.         html::DeleteCache $cache
  59.         error "Wrong version."
  60.     }
  61.     close $fid
  62.     eval {uplevel $level [list source [file join ${html::PrefsFolder} $cache]]}
  63. }
  64.  
  65. proc html::DeleteCache {cache} {
  66.     global html::PrefsFolder
  67.     catch {file delete [file join ${html::PrefsFolder} $cache]}
  68. }
  69.   
  70.  
  71. #===============================================================================
  72. # ◊◊◊◊ URL and Window Caches ◊◊◊◊ #
  73. #===============================================================================
  74.  
  75. # Adds a URL or window given as input to cache
  76. proc html::AddToCache {cache newurl} {
  77.     global HTMLmodeVars htmlModeIsLoaded
  78.     
  79.     if {$cache == "windows" && [lsearch -exact {_self _top _parent _blank} $newurl] >= 0} {return}
  80.     set URLs $HTMLmodeVars($cache)
  81.     
  82.     if {[string length $newurl] && ![lcontains URLs $newurl]} { 
  83.         set URLs [lsort [lappend URLs $newurl]]
  84.         set HTMLmodeVars($cache) $URLs
  85.         prefs::modifiedModeVar $cache HTML
  86.         if {[set l [llength $URLs]] == 1 && [info exists htmlModeIsLoaded]} {html::Enable$cache on}
  87.         if {$l > 75 && [expr {$l/10 == $l/10.0}]} {
  88.             alertnote "The $cache cache is very large. Consider cleaning it up."
  89.         }
  90.     }
  91. }
  92.  
  93. proc html::CleanUpCache {cache} {
  94.     global HTMLmodeVars 
  95.     
  96.     set URLs $HTMLmodeVars($cache)
  97.  
  98.     if {![llength $URLs]} {
  99.         return
  100.     }
  101.     set urlnumber [llength $URLs]
  102.     set screenHeight [lindex [getMainDevice] 3]
  103.     set maxLines [expr {($screenHeight - 160) / 20}]
  104.     set pages [expr {($urlnumber - 1) / $maxLines}]
  105.     set thispage 0
  106.     for {set i 0} {$i < $urlnumber} {incr i} {
  107.         lappend URLsToSave 1
  108.     }
  109.     set thisbox $URLsToSave
  110.     while {1} {
  111.         if {$thispage < $pages} {
  112.             set thisurlnumber $maxLines
  113.         } else {
  114.             set thisurlnumber [expr {($urlnumber - 1 ) % $maxLines + 1}]
  115.         }
  116.         set height [expr {75 + $thisurlnumber  * 20}]
  117.         set box "-w 440 -h $height -b OK 20 [expr {$height - 30}]  85 [expr {$height - 10}] \
  118.             -b Cancel 100 [expr {$height - 30}] 165 [expr {$height - 10}] \
  119.             -b {Uncheck all} 180 [expr {$height - 30}] 265 [expr {$height - 10}] \
  120.             -t {Uncheck the $cache you want to remove} 10 10 440 30 "
  121.         if {$thispage < $pages} {
  122.             lappend box -b "Next>>" 360 [expr {$height - 30}] 425 [expr {$height - 10}]
  123.         }
  124.         if {$thispage > 0} {
  125.             lappend box -b "<<Prev" 280 [expr {$height - 30}] 345 [expr {$height - 10}]
  126.         }
  127.  
  128.         set hpos 30 
  129.         set thisURLs [lrange $URLs [expr {$thispage * $maxLines}] \
  130.         [expr {$thispage * $maxLines + $maxLines - 1}]]
  131.         set i 0
  132.         foreach url $thisURLs {
  133.             lappend box -c $url [lindex $thisbox $i] 10 $hpos 430 [expr {$hpos + 15}]
  134.             incr i
  135.             incr hpos 20
  136.         }
  137.         set thisbox [eval [concat dialog $box]]
  138.         if {[lindex $thisbox 1]} {
  139.             # cancel
  140.             return
  141.         } elseif {[lindex $thisbox 2]} {
  142.             # uncheck all
  143.             set thisbox {}
  144.             for {set i 0} {$i < [llength $thisbox]} {incr i} {
  145.                 lappend thisbox 0
  146.             }
  147.         } else {
  148.             if {$pages == 0} {
  149.                 set ll 3
  150.             } elseif {$thispage == 0 || $thispage == $pages} {
  151.                 set ll 4
  152.             } else {
  153.                 set ll 5
  154.             }
  155.             set URLsToSave [eval [concat lreplace [list $URLsToSave] [expr {$thispage * $maxLines}] \
  156.             [expr {$thispage * $maxLines + $maxLines - 1}] [lrange $thisbox $ll end]]]
  157.             if {[lindex $thisbox 0]} { 
  158.                 # OK
  159.                 break
  160.             } elseif {$thispage < $pages && [lindex $thisbox 3]} { 
  161.                 # more
  162.                 incr thispage 1
  163.                 set thisbox [lrange $URLsToSave [expr {$thispage * $maxLines}] \
  164.                 [expr {$thispage * $maxLines + $maxLines - 1}]]
  165.             } else {
  166.                 # back
  167.                 incr thispage -1
  168.                 set thisbox [lrange $URLsToSave [expr {$thispage * $maxLines}] \
  169.                 [expr {$thispage * $maxLines + $maxLines - 1}]]
  170.             }
  171.         }
  172.     }
  173.     set newurls {}
  174.     for {set i 0} {$i < $urlnumber} {incr i} {
  175.         if {[lindex $URLsToSave $i]} {
  176.             lappend newurls [lindex $URLs $i]
  177.         }
  178.     }
  179.     set HTMLmodeVars($cache) $newurls
  180.     prefs::modifiedModeVar $cache HTML
  181.     if {![llength $newurls]} {html::Enable$cache off}
  182. }
  183.  
  184. proc html::SelScrapToURL {sel msg1 msg2} {
  185.     set newurl [html::URLunEscape [string trim [eval get$sel]]]
  186.     # Convert tabs and returns.
  187.     if {[regexp {[\t\r\n]} $newurl]} {
  188.         alertnote "$msg1 contains tabs or returns. It will not be added to the URL cache."
  189.         return
  190.     }
  191.     if {[string length $newurl]} {
  192.         html::AddToCache URLs $newurl
  193.         message "$newurl added to URLs."
  194.     } else {
  195.         beep
  196.         message $msg2
  197.     }
  198. }
  199.  
  200. proc html::AddSelection {} {
  201.     html::SelScrapToURL Select Selection "No selection!"
  202. }
  203.  
  204. proc html::AddClipboard {} {
  205.     html::SelScrapToURL Scrap Clipboard "Clipboard empty!"
  206. }
  207.  
  208. proc html::ClearCache {cache} {
  209.     global HTMLmodeVars
  210.     if {[llength $HTMLmodeVars($cache)] && [askyesno "Remove all $cache from [string range $cache 0 [expr {[string length $cache] - 2}]] cache?"] == "yes"} {
  211.         set HTMLmodeVars($cache) {}
  212.         prefs::modifiedModeVar $cache HTML
  213.         html::Enable$cache off
  214.     }
  215. }
  216.  
  217. # Imports all URLs in a file to the cache.
  218. proc html::Import {} {
  219.     global HTMLmodeVars
  220.     set urls $HTMLmodeVars(URLs)
  221.  
  222.     if {[catch {getfile "Import URLs from:"} fil] || ![html::IsTextFile $fil alertnote]} {return}
  223.     set fid [open $fil r]
  224.     set filecont " [read $fid]"
  225.     close $fid
  226.     if {[llength $urls]} {
  227.         set cl [askyesno -c "Clear URL cache before importing?"]
  228.         if {$cl == "cancel"} {
  229.             return
  230.         } elseif {$cl == "yes"} {
  231.             set urls {}
  232.         }
  233.     }
  234.             
  235.     set exp1 "\[ \\t\\n\\r\]+[html::URLregexp]"
  236.     set exp2 {[ \t\r\n]+(url)\([ \t\r\n]*("[^"]+"|'[^']+'|[^ \t\n\r\)]+)[ \t\r\n]*\)}
  237.     for {set i1 1} {$i1 < 3} {incr i1} {
  238.         set fcont $filecont
  239.         set exp [set exp$i1]
  240.         while {[regexp -nocase -indices $exp $fcont a b url]} {
  241.             set link [html::URLunEscape [string trim [string range $fcont [lindex $url 0] [lindex $url 1]] {"'}]]
  242.             set fcont [string range $fcont [lindex $url 1] end]
  243.             if {![lcontains urls $link]} {
  244.                 lappend urls  $link
  245.             }
  246.         }
  247.     }
  248.     set HTMLmodeVars(URLs) [lsort $urls]
  249.     prefs::modifiedModeVar URLs HTML
  250.     html::EnableURLs [expr {[llength $HTMLmodeVars(URLs)] > 0}]
  251.     message "URLs imported."
  252. }
  253.  
  254. # Export URLs in cache to a file.
  255. proc html::Export {} {
  256.     global HTMLmodeVars
  257.     if {![llength $HTMLmodeVars(URLs)]} {
  258.         return
  259.     }
  260.     foreach url $HTMLmodeVars(URLs) {
  261.         lappend out "HREF=\"$url\""
  262.     }
  263.     if {![catch {putfile "Export URL cache to:" "URL cache"} fil]} {
  264.         set fid [open $fil w]
  265.         puts $fid [join $out "\n"]
  266.         close $fid
  267.         message "URLs exported."
  268.     }
  269. }
  270.  
  271. # Add all files in a folder to URL cache.
  272. proc html::AddFolder {} {
  273.     global HTMLmodeVars file::separator
  274.     if {[catch {html::GetDir "Folder to cache:"} folder]} {return}
  275.     set path ""
  276.     foreach hp $HTMLmodeVars(homePages) {
  277.         if {[string match [file join [lindex $hp 0] *] [file join $folder " "]]} {
  278.             set path [string range $folder [expr {[string length [lindex $hp 0]] +1}] end]
  279.             regsub -all ${file::separator} $path {/} path
  280.             if {[string length $path]} {append path /}
  281.         }
  282.     }
  283.     set val [dialog -w 350 -h 80 -t "Path:" 10 10 60 30 -e $path 70 10 340 25 \
  284.     -b OK 20 50 85 70 -b Cancel 110 50 175 70]
  285.     if {[lindex $val 2]} {return}
  286.     set path [string trim [lindex $val 0]]
  287.     if {[string length $path]} {set path "[string trimright $path /]/"}
  288.     set urls $HTMLmodeVars(URLs)
  289.     if {[llength $urls]} {
  290.         set cl [askyesno -c "Clear URL cache first?"]
  291.         if {$cl == "cancel"} {
  292.             return
  293.         } elseif {$cl == "yes"} {
  294.             set urls {}
  295.         }
  296.     }
  297.  
  298.     foreach fil [glob -nocomplain -dir $folder *] {
  299.         set name [file tail $fil]
  300.         if {![file isdirectory $fil] && ![lcontains urls "$path$name"]} {
  301.             lappend urls "$path$name"
  302.         }
  303.     }
  304.     set HTMLmodeVars(URLs) [lsort $urls]
  305.     prefs::modifiedModeVar URLs HTML
  306.     html::EnableURLs [expr {[llength $HTMLmodeVars(URLs)] > 0}]
  307.     message "Files added to URL cache."
  308. }
  309.  
  310.