/usr/share/tcltk/tcllib1.14/doctools2idx/container.tcl is in tcllib 1.14-dfsg-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 | # docidx.tcl --
#
# Implementation of docidx objects for Tcl. v2.
#
# Copyright (c) 2009 Andreas Kupries <andreas_kupries@sourceforge.net>
#
# See the file "license.terms" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
#
# RCS: @(#) $Id: container.tcl,v 1.3 2009/08/11 22:52:47 andreas_kupries Exp $
# Each object manages one index, with methods to add and remove keys
# and references, singly, or in bulk. The bulk methods accept various
# forms of textual serializations, among them text using the docidx
# markup language.
# ### ### ### ######### ######### #########
## Requisites
package require Tcl 8.4
package require doctools::idx::structure
package require snit
# ### ### ### ######### ######### #########
## API
snit::type ::doctools::idx {
# Concepts:
# - An index consists of a (possibly empty) set of keys,
# - Each key in the set is identified by its name.
# - Each key has a (possibly empty) set of references.
# - Each reference is identified by its target, specified as
# either url or symbolic filename, depending on the type of
# reference (url, or manpage).
# - A reference can be in the sets of more than one key.
# - A reference outside of the sets of all keys is not possible
# however.
# - A reference carries not only its identifying target, but also
# a descriptive label (*). This label is however not unique per
# reference, but only per a pair of key and reference in that
# key.
# - The type of a reference (url, or manpage) is however bound to
# the reference itself.
# - (*) For keys the identifying feature is identical to its
# label.
# Note: url and manpage references share a namespace for their
# identifiers. This should be no problem with manpage identifiers
# being symbolic filenames and as such they should never look like
# urls.
# ### ### ### ######### ######### #########
## Options
## None
# ### ### ### ######### ######### #########
## Methods
# Default constructor.
# Default destructor.
# ### ### ### ######### ######### #########
method invalidate {} {
array unset myidx *
return
}
# ### ### ### ######### ######### #########
method title {{text {}}} {
if {[llength [info level 0]] == 6} {
set mytitle $text
}
return $mytitle
}
method label {{text {}}} {
if {[llength [info level 0]] == 6} {
set mylabel $text
}
return $mylabel
}
method exporter {{object {}}} {
# TODO :: unlink/link change notification callbacks on the
# config/include components so that we can invalidate our
# cache when the settings change.
if {[llength [info level 0]] == 6} {
set myexporter $object
}
return $myexporter
}
method importer {{object {}}} {
if {[llength [info level 0]] == 6} {
set myimporter $object
}
return $myimporter
}
# ### ### ### ######### ######### #########
## Direct manipulation of the index contents.
method {key add} {key} {
# Ignore addition of an already known key
if {[info exists mykey($key)]} return
set mykey($key) {}
array unset myidx *
return
}
method {key remove} {key} {
# Ignore removal of a key already gone
if {![info exists mykey($key)]} return
set references $mykey($key)
unset mykey($key)
foreach name $references {
# Remove key from the list of users for all references it
# contains.
set pos [lsearch -exact $myrefuse($name) $key]
set myrefuse($name) [lreplace $myrefuse($name) $pos $pos]
if {[llength $myrefuse($name)]} continue
# Last use of this reference is gone, delete it.
unset myrefuse($name)
unset myref($name)
}
array unset myidx *
return
}
method keys {} {
return [array names mykey]
}
method {key references} {key} {
if {![info exists mykey($key)]} {
return -code error "Unknown key '$key'"
}
return $mykey($key)
}
method {reference add} {reftype key name label} {
if {![info exists mykey($key)]} {
return -code error "Unknown key '$key'"
}
if {[info exists myref($name)] && ([lindex $myref($name) 0] ne $reftype)} {
return -code error "Cannot add $reftype reference '$name', is a [lindex $myref($name) 0] reference already"
}
if {($reftype ne "url") && ($reftype ne "manpage")} {
return -code error "Bad reference type '$reftype'"
}
set myref($name) [list $reftype $label]
if {![info exists myrefuse($name)]} {
set myrefuse($name) {}
}
if {![info exists mylink([list $name $key])]} {
# reference was not used by the key yet.
lappend mykey($key) $name
lappend myrefuse($name) $key
set mylink([list $name $key]) .
}
array unset myidx *
return
}
method {reference remove} {name} {
# Ignore removal of already unknown reference
if {![info exists myrefuse($name)]} return
foreach key $myrefuse($name) {
unset mylink([list $name $key])
set pos [lsearch -exact $mykey($key) $name]
set mykey($key) [lreplace $mykey($key) $pos $pos]
}
unset myref($name)
unset myrefuse($name)
array unset myidx *
return
}
method {reference label} {name} {
if {![info exists myref($name)]} {
return -code error "Unknown reference '$name'"
}
return [lindex $myref($name) 1]
}
method {reference type} {name} {
if {![info exists myref($name)]} {
return -code error "Unknown reference '$name'"
}
return [lindex $myref($name) 0]
}
method {reference keys} {name} {
if {![info exists myrefuse($name)]} {
return -code error "Unknown reference '$name'"
}
return $myrefuse($name)
}
method references {} {
return [array names myrefuse]
}
# ### ### ### ######### ######### #########
## Public methods. Bulk loading and merging.
method {deserialize =} {data {format {}}} {
# Default format is the regular index serialization
if {$format eq {}} {
set format serial
}
if {$format ne "serial"} {
set data [$self Import $format $data]
# doctools::idx::structure verify-as-canonical $data
# ImportSerial verifies.
}
$self ImportSerial $data
return
}
method {deserialize +=} {data {format {}}} {
# Default format is the regular index serialization
if {$format eq {}} {
set format serial
}
if {$format ne "serial"} {
set data [$self Import $format $data]
# doctools::idx::structure verify-as-canonical $data
# merge or ImportSerial verify the structure.
}
set data [doctools::idx::structure merge [$self serialize] $data]
# doctools::idx::structure verify-as-canonical $data
# ImportSerial verifies.
$self ImportSerial $data
return
}
# ### ### ### ######### ######### #########
method serialize {{format {}}} {
# Default format is the regular index serialization
if {$format eq {}} {
set format serial
}
# First check the cache for a remebered representation of the
# index for the chosen format, and return it, if such is
# known.
if {[info exists myidx($format)]} {
return $myidx($format)
}
# If there is no cached representation we have to generate it
# from it from our internal representation.
if {$format eq "serial"} {
return [$self GenerateSerial]
} else {
return [$self Generate $format]
}
return -code error "Internal error, reached unreachable location"
}
# ### ### ### ######### ######### #########
## Internal methods
method GenerateSerial {} {
# We can generate the list serialization easily from the
# internal representation.
# Scan and reorder ...
set keywords {}
foreach kw [lsort -dict [array names mykey]] {
# Sort the references in a keyword by their _labels_.
set tmp {}
foreach rid $mykey($kw) { lappend tmp [list $rid [lindex $myref($rid) 1]] }
set refs {}
foreach item [lsort -dict -index 1 $tmp] {
lappend refs [lindex $item 0]
}
lappend keywords $kw $refs
}
set references {}
foreach rid [lsort -dict [array names myrefuse]] {
lappend references $rid $myref($rid)
}
# Construct result
set serial [list doctools::idx \
[list \
label $mylabel \
keywords $keywords \
references $references \
title $mytitle]]
# This is just present to assert that the code above creates
# correct serializations.
doctools::idx::structure verify-as-canonical $serial
set myidx(serial) $serial
return $serial
}
method Generate {format} {
if {$myexporter eq {}} {
return -code error "Unable to export from \"$format\", no exporter configured"
}
set res [$myexporter export object $self $format]
set myidx($format) $res
return $res
}
method ImportSerial {serial} {
doctools::idx::structure verify $serial iscanonical
array unset myidx *
array unset mykey *
array unset myrefuse *
array unset myref *
array unset mylink *
# Unpack the serialization.
array set idx $serial
array set idx $idx(doctools::idx)
unset idx(doctools::idx)
# We are setting the relevant variables directly instead of
# going through the accessor methods.
# I. Label and title
# II. Keys and references
# III. Back index references -> keys.
set mytitle $idx(title)
set mylabel $idx(label)
array set mykey $idx(keywords)
array set myref $idx(references)
foreach k [array names mykey] {
foreach r $mykey($k) {
lappend myrefuse($r) $k
set mylink([list $r $k]) .
}
}
# Extend cache (only if canonical, as we return only canonical
# data).
if {$iscanonical} {
set myidx(serial) $serial
}
return
}
method Import {format data} {
if {$myimporter eq {}} {
return -code error "Unable to import from \"$format\", no importer configured"
}
return [$myimporter import text $data $format]
}
# ### ### ### ######### ######### #########
## State
# References to export/import managers extending the
# (de)serialization abilities of the index.
variable myexporter {}
variable myimporter {}
# Internal representation of the index.
variable mytitle {} ; #
variable mylabel {} ; #
variable mykey -array {} ; # key -> list of references
variable myref -array {} ; # reference -> (type, label)
variable myrefuse -array {} ; # reference -> list of keys using the reference
variable mylink -array {} ; # reference x key -> exists if the reference is used by key.
# Array serving as cache holding alternative representations of
# the index generated via 'serialize', i.e. data export.
variable myidx -array {}
##
# ### ### ### ######### ######### #########
}
# ### ### ### ######### ######### #########
## Ready
package provide doctools::idx 2
return
|