Patitotective / kdl-nim

KDL Nim implementation.

Home Page:https://patitotective.github.io/kdl-nim/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

kdl-nim

Nim Tests

KDL Nim implementation.

Installation

nimble install kdl

Or directly from this repository:

nimble install https://github.com/Patitotective/kdl-nim

Features

Overview

import kdl

var doc = parseKdl("""
// Nodes can be separated into multiple lines
title \
  "Some title"


// Files must be utf8 encoded!
smile (emoji)"๐Ÿ˜" {
  upside-down (emoji)"๐Ÿ™ƒ"
}

// Instead of anonymous nodes, nodes and properties can be wrapped
// in "" for arbitrary node names.
"!@#$@$%Q#$%~@!40" "1.2.3" "!!!!!"=true

// The following is a legal bare identifier:
foo123~!@#$%^&*.:'|?+ "weeee"

// And you can also use unicode!
ใƒŽใƒผใƒ‰ ใŠๅๅ‰="โ˜œ(๏พŸใƒฎ๏พŸโ˜œ)"

// kdl specifically allows properties and values to be
// interspersed with each other, much like CLI commands.
foo bar=true "baz" quux=false 1 2 3
""") # You can also read files using parseKdlFile("file.kdl")

# Nodes are represented like:
# type KdlNode* = object
#   tag*: Option[string]
#   name*: string
#   args*: seq[KdlVal]
#   props*: Table[string, KdlVal]
#   children*: seq[KdlNode]

assert doc[0][0].isString() # title "Some title"

assert doc[1][0] == "๐Ÿ˜" # smile node
assert doc[1][0].tag.isSome and doc[1][0].tag.get == "emoji" # Type annotation
assert doc[1].children[0][0] == "๐Ÿ™ƒ" # smile node's upside-down child

assert doc[2].name == "!@#$@$%Q#$%~@!40"

assert doc[^1]["quux"] == false

doc[0][0].setString("New title")

# toKdlNode is a macro that facilitates the creation of `KdlNode`s, there's also toKdl (to create documents) and toKdlVal
doc[1].children[0] = toKdlNode: sunglasses("๐Ÿ˜Ž"[emoji], 3.14)

assert $doc[1].children[0] == "\"sunglasses\" (\"emoji\")\"๐Ÿ˜Ž\" 3.14"

assert doc[1].children[0][1].get(uint8) == 3u8 # Converts 3.14 into an uint8

doc[^1]["bar"].setTo(false) # Same as setBool(false)

doc.writeFile("doc.kdl")

Docs

Documentation is live at https://patitotective.github.io/kdl-nim/.

TODO

About

Contact me:

About

KDL Nim implementation.

https://patitotective.github.io/kdl-nim/

License:MIT License


Languages

Language:Nim 76.0%Language:HTML 16.8%Language:XSLT 7.2%