doctools::t...import::json(n)

πŸ“¦
version 2.0 Β· 2 versions
, in fedora 43, fedora 44, nixos 26.05, nixpkgs unstable

πŸ’– doctools::toc::import::json β€” the JSON import plugin, bestie edition ✨

okay so you found the manual page for doctools::toc::import::json (that's the n one, Tcl-land πŸŽ€) and honestly? it's giving niche but important. let's get into it πŸ’…


🌸 NAME 🌸

doctools::toc::import::json β€” literally the JSON import plugin. that's the whole vibe. no cap.


✨ SYNOPSIS ✨

package require Tcl 8.5 9

package require doctools::toc::import::json ?0.2.1?

package require doctools::toc::structure

package require json

import string configuration

like, yes, that's four package require lines and then a bare import command sitting there being mysterious. we'll get to it 🫢


πŸ’œ DESCRIPTION πŸ’œ

so this package is the doctools table of contents import plugin for parsing JSON markup. that's the job. that's what it does. iconic behavior honestly.

here's the tea though: it's an internal package of doctools. it exists to be used by the higher-level management packages that handle tables of contents β€” especially doctools::toc::import, the import manager. πŸ‘‘

can you use it from a regular interpreter? ...technically yes. but only with contortions, and it is not recommended. like, you'd be fighting the whole design. the proper way to use this functionality is through the package doctools::toc::import and the import manager objects it provides. do that instead. please. for me πŸ™


πŸ”₯ API πŸ”₯

the API this package provides satisfies the spec of the doctoc import plugin API version 2. slay.

  • import string configuration β€” this command takes the string and parses it as JSON markup encoding a table of contents, in the context of the specified configuration (which is a dictionary, fyi). and the result? the result of the command is the canonical serialization of that table of contents, in the form specified in section ToC serialization format. so yeah, you give it a string, you get back the canonical thing. periodt. πŸ’–

πŸ¦‹ JSON NOTATION OF TABLES OF CONTENTS πŸ¦‹

okay so the JSON format used for tables of contents is a direct translation of the ToC serialization format. the mapping is:

  • Tcl dictionaries β†’ JSON objects
  • Tcl lists β†’ JSON arrays

easy. clean. we love a bijection 🧚

for example, this Tcl serialization:

doctools::toc {
    items {
        {reference {
      desc {DocTools - Tables of Contents}
       id introduction.man
       label doctools::toc::introduction
  }}
  {division {
       id processing.man
       items {
           {reference {
          desc {doctoc serialization utilities}
          id structure.man
          label doctools::toc::structure
      }}
      {reference {
          desc {Parsing text in doctoc format}
          id parse.man
          label doctools::toc::parse
      }}
       }
             label Processing
        }}
    }
    label {Table of Contents}
    title TOC
}

is equivalent to the JSON string:

{
    "doctools::toc" : {
        "items" : [{
            "reference" : {
                "desc"  : "DocTools - Tables of Contents",
                "id"    : "introduction.man",
                "label" : "doctools::toc::introduction"
            }
        },{
            "division" : {
                "id"    : "processing.man",
                "items" : [{
                    "reference" : {
                        "desc"  : "doctoc serialization utilities",
                        "id"    : "structure.man",
                        "label" : "doctools::toc::structure"
                    }
                },{
                    "reference" : {
                        "desc"  : "Parsing text in doctoc format",
                        "id"    : "parse.man",
                        "label" : "doctools::toc::parse"
                    }
                }],
                "label" : "Processing"
            }
        }],
        "label" : "Table of Contents",
        "title" : "TOC"
    }
}

same thing. different outfit. πŸ’…


🌈 TOC SERIALIZATION FORMAT 🌈

here's where we specify the format used by the doctools v2 packages to serialize tables of contents as immutable values for transport, comparison, etc. y'know, the boring-but-essential infrastructure stuff.

we distinguish between regular and canonical serializations. a table of contents may have more than one regular serialization β€” but only exactly one of them will be canonical. only one. the main character. πŸ‘‘

⭐ regular serialization ⭐

  1. the serialization of any table of contents is a nested Tcl dictionary.
  2. this dictionary holds a single key, doctools::toc, and its value. this value holds the contents of the table of contents.
  3. the contents of the table of contents are a Tcl dictionary holding the title of the table of contents, a label, and its elements. the relevant keys and their values are:
    • title β€” the value is a string containing the title of the table of contents.
    • label β€” the value is a string containing a label for the table of contents.
    • items β€” the value is a Tcl list holding the elements of the table, in the order they are to be shown.

each element is a Tcl list holding the type of the item, and its description, in this order. an alternative description (fancy!) would be that it is a Tcl dictionary holding a single key β€” the item type β€” mapped to the item description.

the two legal item types and their descriptions are:

  • reference β€” this item describes a single entry in the table of contents, referencing a single document. to this end its value is a Tcl dictionary containing an id for the referenced document, a label, and a longer textual description which can be associated with the entry. the relevant keys and their values are:
    • id β€” the value is a string containing the id of the document associated with the entry.
    • label β€” the value is a string containing a label for this entry. this string also identifies the entry, and no two entries (references and divisions) in the containing list are allowed to have the same label. ⚠️
    • desc β€” the value is a string containing a longer description for this entry.
  • division β€” this item describes a group of entries in the table of contents, inducing a hierarchy of entries. to this end its value is a Tcl dictionary containing a label for the group, an optional id to a document for the whole group, and the list of entries in the group. the relevant keys and their values are:
    • id β€” the value is a string containing the id of the document associated with the whole group. this key is optional.
    • label β€” the value is a string containing a label for the group. this string also identifies the entry, and no two entries (references and divisions) in the containing list are allowed to have the same label. ⚠️
    • items β€” the value is a Tcl list holding the elements of the group, in the order they are to be shown. this list has the same structure as the value for the keyword items used to describe the whole table of contents, see above. this closes the recursive definition of the structure, with divisions holding the same type of elements as the whole table of contents, including other divisions. it's turtles all the way down bestie 🐒

πŸ’« canonical serialization πŸ’«

the canonical serialization of a table of contents has the format as specified in the previous item, and then additionally satisfies the constraints below, which make it unique among all the possible serializations of this table of contents.

  1. the keys found in all the nested Tcl dictionaries are sorted in ascending dictionary order, as generated by Tcl's builtin command lsort -increasing -dict.

and that's it. that's the whole constraint. one line. absolute queen behavior ✨


πŸ’€ BUGS, IDEAS, FEEDBACK πŸ’€

ngl, this document β€” and the package it describes β€” will undoubtedly contain bugs and other problems. it's software, babes. 😭

please report such in the category doctools of the Tcllib Trackers [http://core.tcl.tk/tcllib/reportlist]. please also report any ideas for enhancements you may have for either the package and/or the documentation. we want them fr fr 🫢

when proposing code changes, please provide unified diffs, i.e the output of diff -u.

note further that attachments are strongly preferred over inlined patches. attachments can be made by going to the Edit form of the ticket immediately after its creation, and then using the left-most button in the secondary navigation bar. yes, that specific button. that one. πŸ’…


πŸŽ€ KEYWORDS πŸŽ€

JSON, deserialization, doctools, import, table of contents, toc


🌸 CATEGORY 🌸

Text formatter plugin


Copyright (c) 2009-2019 Andreas Kupries <andreas_kupries@users.sourceforge.net>


okay bestie that's the whole page πŸ’– this one's brought to you by tcllib, version 0.2.1, and it lives under doctools::t...import::json(n) β€” now go use doctools::toc::import like a normal person and stop trying to call the internal package directly 😭 πŸ¦‹ ✨

πŸ“¦ where this page comes from