1# docidx.tcl --
2#
3#	The docidx import plugin. Bridge between import management and
4#	the parsing of docidx markup.
5#
6# Copyright (c) 2009 Andreas Kupries <andreas_kupries@sourceforge.net>
7#
8# See the file "license.terms" for information on usage and redistribution
9# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
10#
11# RCS: @(#) $Id: import_docidx.tcl,v 1.3 2009/08/07 18:53:11 andreas_kupries Exp $
12
13# This package is a plugin for the the doctools::idx v2 system.  It
14# takes text in docidx format and produces the list serialization of a
15# keyword index.
16
17# ### ### ### ######### ######### #########
18## Requisites
19
20# @mdgen NODEP: doctools::idx::import::plugin
21
22package require Tcl 8.4
23package require doctools::idx::import::plugin ; # The presence of this
24						# pseudo package
25						# indicates execution
26						# of this code inside
27						# of an interpreter
28						# which was properly
29						# initialized for use
30						# by import plugins.
31package require doctools::idx::parse          ; # The actual docidx
32						# parser used by the
33						# plugin.
34
35# ### ### ### ######### ######### #########
36
37## We redefine the command 'doctools::idx::parse::GetFile' to use the
38## 'include' alias provided by the plugin manager, as reguar file
39## commands are not allowed in this 'safe' environment. However this
40## is done if and only if we truly are in the plugin environment. The
41## testsuite, for example, will leave out the definition of 'include',
42## signaling in this way that the regular file operations can still be
43## used.
44
45if {[llength [info commands include]]} {
46
47    # Note: We are poking directly into the implementation of the
48    #       class. Any changes to the interface here have to reviewed
49    #       for their impact on doctools::idx::parse, and possibly
50    #       ported over.
51
52    proc ::doctools::idx::parse::GetFile {currentfile path dv pv ev mv} {
53	upvar 1 $dv data $pv fullpath $ev error $mv emessage
54	foreach {ok data fullpath error emessage} [include $currentfile $path] break
55	return $ok
56    }
57}
58
59# ### ### ### ######### ######### #########
60## API :: Convert text to canonical index serialization.
61
62proc import {text configuration} {
63    global errorInfo errorCode
64
65    doctools::idx::parse var load $configuration
66
67    # Could be done better using a try/finally
68    set code [catch {
69	doctools::idx::parse text $text
70    } serial]
71
72    # Save error state if there was any.
73    set ei $errorInfo
74    set ec $errorCode
75
76    # Cleanup parser configuration, regardless of errors or not.
77    doctools::idx::parse var unset *
78
79    # Rethrow any error, using the captured state.
80    if {$code} {
81	return -code $code -errorinfo $ei -errorcode $ec $serial
82    }
83
84    return $serial
85}
86
87# ### ### ### ######### ######### #########
88## Ready
89
90package provide doctools::idx::import::docidx 0.1
91return
92