1# json.tcl -- 2# 3# The json import plugin. Bridge between import management and 4# the parsing of json 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_json.tcl,v 1.3 2009/11/15 05:50:03 andreas_kupries Exp $ 12 13# This package is a plugin for the the doctools::toc v2 system. It 14# takes text in json format and produces the list serialization of a 15# table of contents. 16 17# ### ### ### ######### ######### ######### 18## Requisites 19 20# @mdgen NODEP: doctools::toc::import::plugin 21 22package require Tcl 8.4 23package require doctools::toc::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::toc::structure ; # Verification of the json 32 # parse result as a 33 # proper toc 34 # serialization. 35 36if {[package vcompare [package present Tcl] 8.5] < 0} { 37 if {[catch { 38 package require dict 39 }]} { 40 # Create a pure Tcl implementation of the dict methods 41 # required by json, and fake the presence of the dict package. 42 proc dict {cmd args} { return [uplevel 1 [linsert $args 0 dict/$cmd]] } 43 proc dict/create {} { return {} } 44 proc dict/set {var key val} { 45 upvar 1 $var a 46 array set x $a 47 set x($key) $val 48 set a [array get x] 49 return 50 } 51 package provide dict 1 52 } 53} 54 55package require json ; # The actual json parser used by the plugin. 56# Requires 8.5, or 8.4+dict. 57 58# ### ### ### ######### ######### ######### 59 60# ### ### ### ######### ######### ######### 61## API :: Convert text to canonical toc serialization. 62 63proc import {text configuration} { 64 # Note: We cannot fail here on duplicate keys in the input, 65 # especially for keywords and references, as we do for Tcl-based 66 # canonical toc serializations, because our underlying JSON parser 67 # automatically merges them, by taking only the last found 68 # definition. I.e. of two or more definitions for a key X the last 69 # overwrites all previous occurences. 70 return [doctools::toc::structure canonicalize [json::json2dict $text]] 71} 72 73# ### ### ### ######### ######### ######### 74## Ready 75 76package provide doctools::toc::import::json 0.1 77return 78