Regexp

Go like Regular expressions

regexp

Creates a compiled regular expression object from a pattern string.

Args: 1

Types: 1 [ String ]

pattern String containing a regular expression pattern

returns native regexp object or error if pattern is invalid

regexp "[0-9]" |type?
; returns native
regexp "[0-9]" |kind?
; returns regexp

re

Creates a compiled regular expression object from a pattern string.

Args: 1

Types: 1 [ String ]

regexp//Is-match

Tests if a string matches the regular expression pattern.

Args: 2

Types: 1 [ Native ] 2 [ String ]

regexp Native regexp object

input String to test against the pattern

returns boolean true if the string matches the pattern, false otherwise

regexp "[0-9]" |Is-match "5"
; returns true
regexp "[0-9]" |Is-match "a"
; returns false

regexp//Submatch?

Extracts the first captured group from a string using the regular expression.

Args: 2

Types: 1 [ Native ] 2 [ String ]

regexp Regular expression with capturing groups

input String to search in

returns string containing the first captured group or error if no submatch found

regexp "x([0-9]+)y" |Submatch? "x123y"
; returns "123"

regexp//Submatches?

Extracts all captured groups from the first match as a block of strings.

Args: 2

regexp Regular expression with capturing groups

input String to search in

returns block containing all captured groups from the first match or error if no match found

regexp "x([0-9]+)y" |Submatches? "x123y x234y"
; returns { "123" }

regexp//Submatches\all?

Extracts all captured groups from all matches as a nested block structure.

Args: 2

regexp Regular expression with capturing groups

input String to search in

returns block of blocks, each inner block containing the captured groups from one match

regexp "x([0-9]+)(y+)?" |Submatches\all? "x11yy x22"
; returns { { "11" "yy" } { "22" "" } }

regexp//Find-all

Finds all substrings matching the regular expression and returns them as a block.

Args: 2

regexp Regular expression pattern

input String to search in

returns block containing all matching substrings or error if no matches found

regexp "[0-9]+" |Find-all "x123y x234y"
; returns { "123" "234" }

regexp//Match?

Finds the first substring matching the regular expression.

Args: 2

Types: 1 [ Native ] 2 [ String ]

regexp Regular expression pattern

input String to search in

returns string containing the first match or empty string if no match found

regexp "[0-9]+c+" |Match? "aa33bb55cc"
; returns "55cc"

regexp//Replace-all

Replaces all occurrences of the regular expression pattern with the specified replacement string.

Args: 3

Types: 1 [ Native ] 2 [ String ] 3 [ String ]

regexp Regular expression pattern

input String to modify

replacement String to replace matches with

returns string with all matches replaced by the replacement string

regexp "[0-9]+" |Replace-all "x123y x234y" "XXX"
; returns "xXXXy xXXXy"

JSON

Parsing and generating JSON

parse-json

Parses JSON string into Rye values.

Args: 1

Types: 1 [ String ]

json string containing JSON data

returns parsed Rye value (list, dict, string, integer, etc.)

"[ 1, 2, 3 ]" |parse-json |length?
; returns 3
"[ 1, 2, 3 ]" |parse-json |type?
; returns list
"{"name": "John", "age": 30}" |parse-json -> "name"
; returns "John"

parse-json\lines

Parses JSON string into Rye values.

Args: 1

Types: 1 [ String ]

json string containing consecutive JSON values

returns list of parsed Rye values

"{"a": 2, "b": "x"} 
{"a": 3, "b": "y"} 
" |parse-json\lines |to-table
; returns table { "a" "b" } [ 2 "x" 3 "y" ]

to-json

Converts a Rye value to a JSON string. Supports block (like list) and context (like dict).

Args: 1

value any Rye value to encode (block, list, dict, context, string, integer, etc.)

returns string containing the JSON representation

list { 1 2 3 } |to-json
; returns "[1, 2, 3] "
dict { a: 1 b: 2 c: 3 } |to-json |parse-json |-> "b"
; returns 2
{ 1 2 3 } |to-json
; returns "[1, 2, 3] "
context { a: 1 b: 2 } |to-json |parse-json |-> "a"
; returns 1

to-json\lines

Converts a table to JSON with each row on a separate line.

Args: 1

table table value to encode

returns string containing the JSON representation with each row on a new line

table { "a" "b" } { 2 "x" 3 "y" } |to-json\lines
; returns "{"a": 2, "b": "x"} 
; {"a": 3, "b": "y"} 
; "

BSON

BSON encoding and decoding

from-bson

Decodes BSON data into Rye values.

Args: 1

bytes native bytes object containing BSON data

returns decoded Rye value (string, integer, decimal, block, etc.)

"abc" |to-bson |from-bson
; returns "abc"
123 |to-bson |from-bson
; returns 123
{ 123 "asd" } |to-bson |from-bson
; returns { 123 "asd" }

to-bson

Encodes a Rye value into BSON format.

Args: 1

value any Rye value to encode (string, integer, decimal, block, etc.)

returns native bytes object containing the BSON-encoded data

"abc" |to-bson |type?
; returns native
"abc" |to-bson |kind?
; returns bytes

SXML

Streaming, SAX-like XML processing

reader//do-sxml

Processes XML using a streaming SAX-like approach with tag handlers.

Args: 2

reader XML reader object

block SXML processing block with tag handlers

returns result of processing the XML

"<scene><bot>C3PO</bot><bot>R2D2</bot><jedi>Luke</jedi></scene>" |reader |do-sxml { bot { _ [ .prns ] } }
; prints "C3PO R2D2 "
"<scene><ship>XWing</ship><bot>R2D2</bot><jedi>Luke</jedi></scene>" |reader |do-sxml { bot jedi { _ [ .prns ] } }
; prints "R2D2 Luke "
"<scene><xwing><bot>R2D2</bot><person>Luke</person></xwing><destroyer><person>Vader</person></destroyer></scene>" |reader |do-sxml { xwing { person { _ [ .prns ] } } }
; prints "Luke "

rye-sxml-start//Attr?

Retrieves an attribute by index or name from an XML start element.

Args: 2

Types: 1 [ Native ] 2 [ Integer Word String ]

element XML start element

index Integer index of the Attribute to retrieve

returns list [ namespace tag value ] of the attribute or void if not found

rye-sxml-start//Attrs?

Returns a dict of all attributes from an XML start element.

Args: 1

Types: 1 [ Native ]

element XML start element

returns dict with all attributes where keys are "attrname" or "namespace:attrname"

rye-sxml-start//Name?

Returns the name of an XML start element.

Args: 1

Types: 1 [ Native ]

element XML start element

returns string name of the XML element

HTML

HTML processing functions

unescape\html

Converts HTML entities to their character equivalents.

Args: 1

Types: 1 [ String ]

text HTML-escaped string

returns string with HTML entities converted to their character equivalents

unescape\html "&gt;hello&lt;"
; returns ">hello<"

escape\html

Converts special characters to HTML entities.

Args: 1

Types: 1 [ String ]

text String containing HTML special characters

returns string with special characters converted to HTML entities

escape\html "<hello>"
; returns "&lt;hello&gt;"

html->markdown

Converts HTML text to markdown format.

Args: 1

Types: 1 [ String ]

html HTML string to convert

returns string containing markdown equivalent of the HTML

html->markdown "<h1>title</h1><p>para</p>"
; returns "# title
; 
; para"

reader//Parse-html

Parses HTML using a streaming approach with tag handlers.

Args: 2

reader HTML reader object

block HTML processing block with tag handlers

returns result of processing the HTML

rye-html-start//Attr?

Retrieves an attribute value by name or index from an HTML element.

Args: 2

Types: 1 [ Native ] 2 [ Integer Word String ]

element HTML token element

name-or-index Attribute name (as word or string) or index (as integer)

returns string value of the attribute or void if not found

rye-html-start//Attrs?

Returns a dict of all attributes from an HTML element.

Args: 1

Types: 1 [ Native ]

element HTML token element

returns dict with all attributes where keys are attribute names

rye-html-start//Name?

Returns the name of an HTML element.

Args: 1

Types: 1 [ Native ]

element HTML token element

returns string name of the HTML element

Markdown

Functions for processing and converting Markdown documents

reader//do-markdown

Processes Markdown using a streaming approach with section handlers.

Args: 2

reader File reader or similar object to read markdown from

handlers Block containing section handlers (h1, h2, paragraph, code, etc.)

returns the result of processing the markdown document

markdown->html

Converts Markdown text to HTML.

Args: 1

Types: 1 [ String ]

markdown

Creates a Markdown value from a string.

Args: 1

Types: 1 [ String Markdown ]

markdown//text

Gets the raw markdown text from a Markdown value.

Args: 1

Types: 1 [ Markdown ]

markdown//length

Gets the length of the markdown text in characters.

Args: 1

Types: 1 [ Markdown ]

markdown//to-html

Converts a Markdown value to HTML.

Args: 1

Types: 1 [ Markdown ]

markdown//headings

Extracts all headings from markdown text as a list of strings.

Args: 1

Types: 1 [ Markdown ]

markdown//paragraphs

Extracts all paragraphs from markdown text as a list of strings.

Args: 1

Types: 1 [ Markdown ]

markdown//links

Extracts all links from markdown text as a list of dictionaries with 'text' and 'url' keys.

Args: 1

Types: 1 [ Markdown ]

Conversion

Functions for converting between different types and kinds

convert

Converts value from one kind to another based on a conversion specification block.

Args: 2

value Dict or RyeCtx to convert

spec Block specifying the conversion rules

returns converted value based on the specification

converter

Registers a converter between two kinds of objects.

Args: 3

Types: 1 [ Kind ] 2 [ Kind Block ] 3 [ Block ]

source-kind Kind to convert from

target-kind Kind to convert to

spec Block specifying conversion rules

returns the target kind with the converter registered