sphinx-literalizer¶
sphinx-literalizer is a Sphinx extension for literalizer, which converts JSON, YAML, TOML, and JSON5 data structures to native language literal syntax (Python, TypeScript, Go, etc.).
Installation¶
Requires Python 3.12+.
pip install sphinx-literalizer
Usage¶
Add to your Sphinx conf.py:
extensions = [
# ...
"sphinx_literalizer",
]
Then use the literalizer directive in your .rst files:
.. literalizer:: path/to/data.json
:language: python
This reads the data file and renders its contents as a native Python literal
in a code block. Input format is auto-detected from the file extension
(.json, .yaml/.yml, .toml, .json5), or can be set
explicitly with :input-format:.
Directive options¶
:language:(required)Target language name. Supported values:
ada,bash,c,clojure,cobol,common-lisp,cpp,crystal,csharp,d,dart,dhall,elixir,elm,erlang,fortran,fsharp,gleam,go,groovy,haskell,hcl,java,javascript,json5,jsonnet,julia,kotlin,lua,matlab,mojo,nim,norg,objective-c,ocaml,occam,odin,perl,php,powershell,purescript,python,r,racket,raku,ruby,rust,scala,scheme,swift,systemverilog,toml,typescript,vb.net,yaml,zig.:input-format:(optional)Input data format. If not specified, auto-detected from the file extension. Supported values:
json,json5,yaml,toml.:pre-indent-level:(optional)Number of indent levels to prepend to each output line. Defaults to
0.:indent:(optional)Number of whitespace characters used for one level of indentation. Defaults to
4.:indent-char:(optional)Type of whitespace for indentation:
spaces(default) ortabs.:include-delimiters:(optional flag)Include collection delimiters in the output (
[…]for arrays,{…}for dicts).:date-format:(optional)How to render YAML dates. Not all values are valid for every language. Supported values:
cppstd::chrono::year_month_daytype.csharpnew DateOnly(...)constructor.dartDateTime.parse(...)constructor.gotime.Date(...)call.isoQuoted ISO 8601 string (e.g.
"2024-01-15"). This is the default for most languages.javaLocalDate.of(...)constructor.jsnew Date(...)constructor (JavaScript/TypeScript).juliaDate(...)constructor.kotlinLocalDate.of(...)constructor.objcObjective-C date representation.
phpPHP date representation.
pythondatetime.date(...)constructor.ras.Date(...)call.rubyDate.new(...)constructor.rustNaiveDate::from_ymd_opt(...)call.tomlTOML date literal.
yamlYAML date literal.
:datetime-format:(optional)How to render YAML datetimes. Not all values are valid for every language. Supported values:
cppstd::chronodatetime type.csharpnew DateTime(...)constructor.dartDateTime.parse(...)constructor.epochSeconds since Unix epoch (e.g.
1705314600.0).gotime.Date(...)call.instantInstant.parse(...)(Java).isoQuoted ISO 8601 string. This is the default for most languages.
jsnew Date(...)constructor (JavaScript/TypeScript).juliaDateTime(...)constructor.kotlinLocalDateTime.of(...)constructor.objcObjective-C datetime representation.
phpPHP datetime representation.
pythondatetime.datetime(...)constructor.ras.POSIXct(...)call.rubyTime.new(...)constructor.rustNaiveDateTime::new(...)call.tomlTOML datetime literal.
yamlYAML datetime literal.
zonedZonedDateTime.of(...)(Java).
:sequence-format:(optional)How to render sequences (arrays/lists). Not all values are valid for every language. Supported values:
arrayArray delimiters. Available for Crystal (default), Julia (default), Rust, and many other languages.
cell_arrayCell array delimiters. Available for MATLAB (default).
initializer_listInitializer list. Available for C++ (default).
listList delimiters. Available for Elixir (default), Erlang (default), Python, and many other languages.
sequenceSequence delimiters. Available for COBOL (default) and YAML (default).
sliceSlice delimiters. Available for Go (default).
tableTable delimiters. Available for Lua (default).
tupleTuple delimiters. Available for Crystal, Elixir, Erlang, Julia, Python (default for Python), and Rust.
vecVec macro (
vec![...]). Available for Rust (default).vectorVector delimiters. Available for Clojure (default).
:set-format:(optional)How to render sets (Python only). Supported values:
set{…}set literal (default).frozensetfrozenset({…})constructor.
:bytes-format:(optional)How to render binary data (Python only). Supported values:
hexHex-escaped bytes literal, e.g.
b"\x48\x65"(default).pythonPython bytes literal, e.g.
b"Hello".
:variable-name:(optional)Wrap the output in a variable declaration or assignment using the given name. Use with
:include-delimiters:to include the collection delimiters.:existing-variable:(optional flag)When combined with
:variable-name:, produce an assignment to an existing variable (e.g.x = ...) instead of a new variable declaration (e.g.final x = ...in Dart). Has no effect without:variable-name:.:variable-type-hints:(optional)Whether to add type hints to variable declarations. Supported values:
autoType hints are added when the language requires them (default).
alwaysAlways add type annotation, e.g.
my_var: dict[str, Any] = {...}. Currently available for Python only.
:comment-format:(optional)How to render comments. Not all values are valid for every language. Supported values:
apostrophe'comments. Available for Visual Basic.blockBlock comments (
/* ... */or equivalent). Available for C, C#, C++, Common Lisp, D, Dart, F#, Go, Groovy, Haskell, HCL, Java, JavaScript, Julia, Kotlin, Lua, MATLAB, Nim, Objective-C, PHP, PowerShell, Racket, Rust, Scala, Swift, TypeScript.double_dash--comments. Available for Ada, Haskell, Lua, Occam.double_slash//comments. Available for C, C#, C++, D, Dart, F#, Go, Groovy, Java, JavaScript, Kotlin, Objective-C, PHP, Rust, Scala, Swift, TypeScript, Zig.exclamation!comments. Available for Fortran.hash#comments. Available for Bash, Crystal, Elixir, HCL, Julia, Mojo, Nim, Perl, PowerShell, Python, R, Ruby, TOML, YAML.paren_star(* ... *)comments. Available for OCaml.percent%comments. Available for Erlang, MATLAB, Norg.semicolon;comments. Available for Clojure, Common Lisp, Racket.star_angle*>comments. Available for COBOL.
:declaration-style:(optional)How to declare variables. Not all values are valid for every language. Supported values:
assignPlain assignment (
x = ...). Available for Crystal, Elixir, Erlang, Haskell, HCL, Julia, MATLAB, Mojo, PHP, PowerShell, Python, R, Ruby, TOML, YAML.autoautokeyword. Available for C++, D.blockBlock-level declaration. Available for Norg.
constconstkeyword. Available for JavaScript, TypeScript, Zig.declareLanguage-specific declaration keyword. Available for Ada, Bash.
defdefkeyword. Available for Clojure, Groovy.definedefineform. Available for Racket.defparameterdefparameterform. Available for Common Lisp.dimDimkeyword. Available for Visual Basic.finalfinalkeyword. Available for Dart.letletkeyword. Available for F#, JavaScript, OCaml, Rust, Swift.locallocalkeyword. Available for Lua.mymykeyword. Available for Perl.shortShort variable declaration (
:=). Available for Go.typedTyped declaration. Available for C, COBOL, Fortran, Objective-C.
valvalkeyword. Available for Kotlin, Occam, Scala.varvarkeyword. Available for C#, Java, Nim.
:dict-format:(optional)How to render dictionaries / maps. Not all values are valid for every language. Supported values:
defaultLanguage-default dict syntax. Available for most languages.
dictDict(...)constructor. Available for Julia.dictionaryDictionaryconstructor. Available for C#.hash_mapHashMapconstructor. Available for Rust.mapMapconstructor. Available for C++, JavaScript, Kotlin, Scala.map_of_entriesMap.ofEntries(...)constructor. Available for Java.objectObject literal syntax. Available for JavaScript, TypeScript.
structstructsyntax. Available for MATLAB.
:integer-format:(optional)How to render integer values. Not all values are valid for every language. Supported values:
decimalDecimal integer literal (default for all languages).
hexHexadecimal integer literal. Available for JavaScript.
:numeric-separator:(optional)Whether to use numeric separators in integer literals. Supported values:
noneNo separators (default for all languages).
underscoreUnderscore separators (e.g.
1_000_000). Available for JavaScript.
:string-format:(optional)How to render string values. Supported values:
doubleDouble-quoted strings (default for all languages).
singleSingle-quoted strings. Available for JavaScript.
:trailing-comma:(optional)Whether to include a trailing comma after the last element in collections. Supported values:
yesInclude trailing comma. Available for C, C++, Crystal, D, Dart, Elixir, Go, Groovy, HCL, JavaScript, Julia, Kotlin, Lua, Mojo, Objective-C, Perl, PHP, Python, Ruby, Rust, Scala, Swift, TypeScript, Zig.
noOmit trailing comma. Available for Ada, Bash, C#, Clojure, COBOL, Common Lisp, Erlang, F#, Fortran, Haskell, Java, JavaScript, MATLAB, Nim, Norg, OCaml, Occam, PowerShell, R, Racket, TOML, Visual Basic, YAML.
:line-ending:(optional)Whether to include semicolons at the end of statements. Supported values:
semicolonInclude semicolons. This is the default for all languages.
noneOmit semicolons. Available for JavaScript, TypeScript.
:empty-dict-key:(optional)How to handle empty string keys in dictionaries. Supported values:
positionalUse positional arguments for empty keys (default). Available for R.
errorRaise an error on empty keys. Available for R.
Example¶
Given a file data.json containing:
[true, false, 42, "hello"]
The directive:
.. literalizer:: data.json
:language: python
:include-delimiters:
renders as a code block containing:
[
True,
False,
42,
"hello",
]
Reference¶
- API Reference
- Release Process
- Changelog
- Next
- 2026.04.06
- 2026.03.26.2
- 2026.03.26.1
- 2026.03.26
- 2026.03.25
- 2026.03.23
- 2026.03.22.1
- 2026.03.22
- 2026.03.20.1
- 2026.03.20
- 2026.03.19
- 2026.03.18
- 2026.03.17.1
- 2026.03.17.2
- 2026.03.17
- 2026.03.16.2
- 2026.03.16.1
- 2026.03.16
- 2026.03.15.4
- 2026.03.15.3
- 2026.03.15.2
- 2026.03.15.1
- 2026.03.15
- 2026.03.14.1
- 2026.03.14
- Contributing to sphinx-literalizer