PRETTY: reformat JSON, XML/HTML and JavaScript text for display
Dependency-free lexical formatters aimed at viewers: they re-indent a document so a human can read it, without validating it. More...
Functions
| Name | |
|---|---|
| N_STR * | n_pretty_xml(const char * text) Re-indent an XML or HTML document (one tag or text run per line). |
| N_STR * | n_pretty_json_lenient(const char * text) Re-indent a JSON document by punctuation alone, without parsing it. |
| N_STR * | n_pretty_json(const char * text) Reformat a JSON document with cJSON (parse + print). |
| N_STR * | n_pretty_js(const char * text) Conservatively re-indent JavaScript (newlines at top-level braces and semicolons, two-space indent by brace depth). |
Detailed Description
Dependency-free lexical formatters aimed at viewers: they re-indent a document so a human can read it, without validating it.
Malformed input is reformatted on a best-effort basis; the formatters never crash on it.
Limits by design (these are viewers, not compilers):
- n_pretty_xml() treats HTML like XML with void-element and raw-text (script/style) awareness; whitespace inside text nodes is trimmed at both ends, so whitespace-significant content (pre) is not preserved exactly.
- n_pretty_js() is a conservative re-indenter (newlines at braces and semicolons), not a beautifier. Strings, template literals, comments and regex literals are copied verbatim. A '/' is lexed as a regex when it follows an operator, an opener, a separator, or an expression keyword (return, typeof, case, ...); the remaining ambiguity (a regex right after a closing parenthesis, as in
if (x) /re/.test(y)) is lexed as division, which can only mis-break lines, never corrupt the text. - n_pretty_json() needs the vendored cJSON (HAVE_CJSON); without it the function logs an error and returns NULL. It parses, so it reformats valid documents only: use n_pretty_json_lenient() for the rest.
- n_pretty_json_lenient() re-indents by punctuation without parsing, so it also breaks up a document cJSON rejects (a truncated body, a trailing comma, NDJSON). It cannot report that the input was invalid and it does not normalise anything, so prefer n_pretty_json() first and label what the fallback produces as approximate.
All functions return a freshly allocated N_STR the caller must release with free_nstr(), or NULL when the input is NULL, empty, or does not look like the expected format.
Functions Documentation
function n_pretty_xml
N_STR * n_pretty_xml(
const char * text
)
Re-indent an XML or HTML document (one tag or text run per line).
Parameters:
- text The markup text. NULL, empty, or input whose first non-whitespace character is not '<' returns NULL.
Return: A new indented N_STR (free with free_nstr), or NULL.
function n_pretty_json_lenient
N_STR * n_pretty_json_lenient(
const char * text
)
Re-indent a JSON document by punctuation alone, without parsing it.
Parameters:
- text The JSON text. NULL or empty input returns NULL.
Return: A new indented N_STR (free with free_nstr), or NULL.
String literals are copied verbatim, containers open and close a level, and each comma ends a line, so a document cJSON rejects (truncated, trailing comma, concatenated values) still becomes readable.
function n_pretty_json
N_STR * n_pretty_json(
const char * text
)
Reformat a JSON document with cJSON (parse + print).
Parameters:
- text The JSON text. NULL, empty, or non-JSON input returns NULL.
Return: A new indented N_STR (free with free_nstr), or NULL.
function n_pretty_js
N_STR * n_pretty_js(
const char * text
)
Conservatively re-indent JavaScript (newlines at top-level braces and semicolons, two-space indent by brace depth).
Parameters:
- text The script text. NULL or empty input returns NULL.
Return: A new indented N_STR (free with free_nstr), or NULL.
Modules
- AABB3D
- ACCEPT_POOL
- ALLEGRO4
- ALLEGRO5
- ANIM_LIB
- ASTAR
- ATLAS_LIB
- AVRO
- CLOCK_SYNC
- COMMONS
- CONFIGFILE
- CYPHER_BASE64
- CYPHER_VIGENERE
- DEAD_RECKONING
- ENUMS
- EXCEPTIONS
- GAME_ENV
- GIT
- GUI
- HASH_TABLE
- ISOMETRIC_ENGINE
- LIST
- LOG
- LOGNODUP
- LZ4
- NETWORKING
- NETWORK_MSG
- N_COOKIES
- N_DIFF
- N_DIGEST
- N_DISK_CACHE
- N_DNS
- N_ECC
- N_ENTROPY
- N_FILES
- N_FLUIDS
- N_GUI_COLOR
- N_GUI_COLORWHEEL
- N_GUI_CURVE
- N_GUI_GRID
- N_GUI_POPUP
- N_HEX
- N_HTTP3
- N_HTTP_COOKIE
- N_HTTP_FORM
- N_HTTP_MIME
- N_HTTP_REQ
- N_HTTP_RESP
- N_HTTP_ROUTER
- N_HTTP_SERVER
- N_ICC
- N_JSON
- N_KAFKA
- N_LINALG
- N_MODULE
- N_OPENSSL_COMPAT
- N_PRETTY
- N_PTH
- N_QUERY
- N_RANDOM
- N_SHA256
- N_STR
- N_TIFF
- N_TIME
- N_USER
- N_X509
- N_ZIP
- PARTICLES
- PCRE
- PHYSICS
- SIGNALS
- STACK
- THREADS
- TRAJECTORY
- TREE
- ZLIB