Transform text files to XML, JSON, or YAML
Project description
Gelatin
Summary
Gelatin is a parser generator for converting text to a structured format such as XML, JSON or YAML.
Do you need commercial support?
Gelatin is supported by Procedure 8. Get in touch if you need anything!
Converting Text to XML, JSON, or YAML
Gelatin is a combined lexer, parser, and output generator. Gelatin defines a simple language for converting text into a structured formats.
Example
Suppose you want to convert the following text file to XML:
User
----
Name: John, Lastname: Doe
Office: 1st Ave
Birth date: 1978-01-01
User
----
Name: Jane, Lastname: Foo
Office: 2nd Ave
Birth date: 1970-01-01
The following Gelatin syntax does the job:
# Define commonly used data types. This is optional, but
# makes your life a litte easier by allowing to reuse regular
# expressions in the grammar.
define nl /[\r\n]/
define ws /\s+/
define fieldname /[\w ]+/
define value /[^\r\n,]+/
define field_end /[\r\n,] */
grammar user:
match 'Name:' ws value field_end:
out.add_attribute('.', 'firstname', '$2')
match 'Lastname:' ws value field_end:
out.add_attribute('.', 'lastname', '$2')
match fieldname ':' ws value field_end:
out.add('$0', '$3')
match nl:
do.return()
# The grammar named "input" is the entry point for the converter.
grammar input:
match 'User' nl '----' nl:
out.open('user')
user()
Explanation
- "grammar input:" is the entry point for the converter.
- "match" statements in each grammar are executed sequentially. If a match is found, the indented statements in the match block are executed. After reaching the end of a match block, the grammar restarts at the top of the grammar block.
- If the end of a grammar is reached before the end of the input document was reached, an error is raised.
- "out.add('$0', '$3')" creates a node in the XML (or JSON, or YAML) if it does not yet exist. The name of the node is the value of the first matched field (the fieldname, in this case). The data of the node is the value of the fourth matched field.
- "out.open('user')" creates a "user" node in the output and selects it such that all following "add" statements generate output relative to the "user" node. Gelatin leaves the user node upon reaching the out.leave() statement.
- "user()" calls the grammar named "user".
This produces the following output:
<xml>
<user lastname="Doe" firstname="John">
<office>1st Ave</office>
<birth-date>1978-01-01</birth-date>
</user>
<user lastname="Foo" firstname="Jane">
<office>2nd Ave</office>
<birth-date>1970-01-01</birth-date>
</user>
</xml>
Method 1: Starting the transformation using the CLI tool
The following command converts the input to XML:
gel -s mysyntax.gel input.txt
The same for JSON or YAML:
gel -s mysyntax.gel -f json input.txt
gel -s mysyntax.gel -f yaml input.txt
Method 2: Starting the transformation using Gelatin as a Python Module
Gelatin also provides a Python API for transforming the text:
from Gelatin.util import compile, generate
# Parse your .gel file.
syntax = compile('syntax.gel')
# Convert your input file to XML, YAML, and JSON.
print(generate(syntax, 'input.txt'))
print(generate(syntax, 'input.txt', format='yaml'))
print(generate(syntax, 'input.txt', format='json'))
Documentation
For full documentation please refer to
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file gelatin-2.3.tar.gz.
File metadata
- Download URL: gelatin-2.3.tar.gz
- Upload date:
- Size: 15.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
54580a36cdf89a760b0f246184e41117d35178a37945dca5ca279d7d611163f7
|
|
| MD5 |
0d1bdd9c9085927bdefc31f1f676bb08
|
|
| BLAKE2b-256 |
b4658d835e955efa16f1cd8b80582b2f8c42d8fac981288d085b37d3b1c489db
|
Provenance
The following attestation bundles were made for gelatin-2.3.tar.gz:
Publisher:
publish.yml on knipknap/Gelatin
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
gelatin-2.3.tar.gz -
Subject digest:
54580a36cdf89a760b0f246184e41117d35178a37945dca5ca279d7d611163f7 - Sigstore transparency entry: 1036284372
- Sigstore integration time:
-
Permalink:
knipknap/Gelatin@6bfc8258f9628ea04bc061bc30dc5c2be8d45d07 -
Branch / Tag:
refs/tags/v2.3 - Owner: https://github.com/knipknap
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@6bfc8258f9628ea04bc061bc30dc5c2be8d45d07 -
Trigger Event:
push
-
Statement type:
File details
Details for the file gelatin-2.3-py3-none-any.whl.
File metadata
- Download URL: gelatin-2.3-py3-none-any.whl
- Upload date:
- Size: 43.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b8e3118852a1f6226dc69ecbdf543f5d29b63b9e1c6728d54a4c3b300d4d80e6
|
|
| MD5 |
df53593b265b6f3ea658b9f303d484d4
|
|
| BLAKE2b-256 |
eb2ff8d88fc8e2fe0653711494ec6ef2ebcefdf22d2512620d7f7d236c7f4294
|
Provenance
The following attestation bundles were made for gelatin-2.3-py3-none-any.whl:
Publisher:
publish.yml on knipknap/Gelatin
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
gelatin-2.3-py3-none-any.whl -
Subject digest:
b8e3118852a1f6226dc69ecbdf543f5d29b63b9e1c6728d54a4c3b300d4d80e6 - Sigstore transparency entry: 1036284397
- Sigstore integration time:
-
Permalink:
knipknap/Gelatin@6bfc8258f9628ea04bc061bc30dc5c2be8d45d07 -
Branch / Tag:
refs/tags/v2.3 - Owner: https://github.com/knipknap
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@6bfc8258f9628ea04bc061bc30dc5c2be8d45d07 -
Trigger Event:
push
-
Statement type: