yml2vocab

Generate RDFS vocabulary files from YAML

This script in this module converts a simple RDF vocabulary, described in YAML, into a formal RDFS in JSON-LD, Turtle, and HTML+RDFa. Optionally, a simple JSON-LD @context is also generated for the vocabulary. Neither the script nor the YAML format is prepared for complex vocabularies; its primary goal is to simplify the generation of simple, straightforward RDFS vocabularies not requiring, for instance, sophisticated OWL statements.

When running, the script relies on two files:

  1. The vocabulary.yml file, containing the definition for the vocabulary entries. (It is also possible to use a different name for the YAML file, see below.)
  2. The template.html file, used to create the HTML file version of the vocabulary. (It is also possible to use a different name for the template file, see below.)

Definition of the vocabulary in the YAML file

The vocabulary is defined in a YAML file, which contains several block sequences with the following keys: vocab, prefix, ontology, class, property, individual, and datatype. Only the vocab and ontology blocks are required, all others are optional.

Each block sequence consists of blocks with the following keys:id, property, value, label, upper_value, domain, range, deprecated, comment, status, defined_by, context, and see_also. The interpretation of these key/value pairs may depend on the top level block where they reside, but some have a common interpretation.

There are some examples in the example directory on github that illustrate all of these terms.

Installation and use

The script is in TypeScript (version 5.0.2 and beyond) running on top of node.js (version 16 and beyond).

Beyond the YAML file itself, the script relies on an HTML template file, i.e., a skeleton file in HTML that is completed by the vocabulary entries. The example template file on github provides a good starting point for a template that also makes use of respec. The script relies on the existing id values and section structures to be modified/extended by the script. Unused subsections (e.g., when there are no deprecated classes) are removed from the final HTML file.

Installation from npm

The script can be used as a standard npm module via:

npm install yml2vocab

Running on a command line

The npm installation installs the node_modules/.bin/yml2vocab script. The script can be used as:

yml2vocab [-v vocab_file_name] [-t template_file_name] [-c]

Running this script generates the vocab_file_name.ttl, vocab_file_name.jsonld, and vocab_file_name.html files for the Turtle, JSON-LD, and HTML+RDFa versions, respectively. The script relies on the vocab_file_name.yml file for the vocabulary specification in YAML and a template_file_name file for a template file. The defaults are vocabulary and template.html, respectively.

If the -c flag is also set, the additional vocab_file_name_context.jsonld is also generated, containing a simple @context structure that can be used as a separate @context file or embedded in a JSON file. Note that this is a “minimal” JSON-LD file, which does not necessarily use all the sophistication that JSON-LD defines for @context; these may have to be added manually.

Running from a Javascript/TypeScript program

The simplest way of using the module from Javascript is:

const yml2vocab = require('yml2vocab');
async function main() {
    await yml2vocab.generateVocabularyFiles("vocabulary","template.html",false);
}
main();

This reads (asynchronously) the YAML and template files and stores the generated vocabulary representations (see the command line interface for details) in the directory alongside the YAML file. By setting the last argument to true a @context is also generated.

The somewhat lower level yml2vocab.VocabGeneration class can also be used:

const yml2vocab = require('yml2vocab');
const vocabGeneration = new yml2vocab.VocabGeneration(yml_content);     // YAML content is text form, before parsing
const turtle: string  = vocabGeneration.getTurtle();                    // returns the turtle content as a string
const jsonld: string  = vocabGeneration.getJSONLD();                    // returns the JSON-LD content as a string
const html: string    = vocabGeneration.getHTML(template_file_content); // returns the HTML+RDFa content as a string
const html: string    = vocabGeneration.getContext();                   // returns the minimal @context file for the vocabulary

If TypeScript is used instead of Javascript the same works, except that the require must be replaced by:

import yml2vocab from 'yml2vocab';

There is no need to install any extra typing, it is included in the package. The interfaces are simply using strings, no extra TypeScript type definitions have been defined.

Cloning the repository

The repository may also be cloned. For a complete installation:

  1. If necessary, install node.js on your local machine. Installation of node.js should automatically install the npm package manager.
  2. Clone the repository (i.e., https://github.com/w3c/yml2vocab/) to your local machine.
  3. In the directory of the repository clone, run npm install on the command line. This installs all the necessary packages in the node_modules subdirectory.
  4. Create a directory for the vocabulary definition; this should include
    1. A vocabulary.yml file. You can start with the YAML file in the example directory of the repository, and change the cells for your vocabulary.
    2. A template.html file. You can start with the HTML file in the example directory of the repository, and adapt/change it as you wish.
  5. Run the main.ts file in the directory vocabulary definition. This generates the vocabulary.ttl, vocabulary.jsonld, and vocabulary.html files for, respectively, the Turtle, JSON-LD, and HTML representations.

    “Running” may be done in two different ways:

    1. Run, via node, the file dist/main.js of the repository
    2. Run, via node_modules/.bin/ts-node, the file main.ts of the repository

    The script also accepts a single argument to be used instead of vocabulary to name the various files (see above).

Content of the directory

The following files and directories are generated/modified by either the script or npm; better not to touch these directly:

Acknowledgement

The original idea, structure, and script (in Ruby) was created by Gregg Kellogg for v1 of the Credentials Vocabulary and with a vocabulary definition using CSV. The CSV definitions have been changed to YAML, and the script itself has been re-written in TypeScript (and developed further since).