Renamed ./examples/external to ./examples/extensions. No code changes. (0.40.6)
This commit is contained in:
parent
28545fa8d4
commit
c68244d6f6
32 changed files with 40 additions and 41 deletions
97
examples/extensions/xml2/xml_access_catalog.fun
Executable file
97
examples/extensions/xml2/xml_access_catalog.fun
Executable file
|
|
@ -0,0 +1,97 @@
|
|||
#!/usr/bin/env fun
|
||||
|
||||
/*
|
||||
* This file is part of the Fun programming language.
|
||||
* https://fun-lang.xyz/
|
||||
*
|
||||
* Copyright 2025 Johannes Findeisen <you@hanez.org>
|
||||
* Licensed under the terms of the Apache-2.0 license.
|
||||
* https://opensource.org/license/apache-2-0
|
||||
*
|
||||
* Added: 2025-12-09
|
||||
*/
|
||||
|
||||
/*
|
||||
* Demonstrate reading specific fields from an XML file using
|
||||
* the minimal XML API (root name) and simple string parsing.
|
||||
*/
|
||||
|
||||
include <io/xml.fun>
|
||||
|
||||
path = "./examples/data/catalog.xml"
|
||||
xml = XML()
|
||||
doc = xml.from_file(path)
|
||||
if (doc == 0)
|
||||
print("Failed to load: ")
|
||||
print(path)
|
||||
else
|
||||
root = xml.root(doc)
|
||||
print("Root element: ")
|
||||
print(xml.name(root))
|
||||
|
||||
// Show how to access specific fields by quick-and-dirty parsing
|
||||
content = read_file(path) // raw XML text
|
||||
// First product name
|
||||
name = xml.between(content, "<name>", "</name>")
|
||||
// First price value and its currency attribute (extract value, then attribute)
|
||||
price_val = xml.between(content, "<price", "</price>")
|
||||
currency = ""
|
||||
if (len(price_val) > 0)
|
||||
currency = xml.between(price_val, "currency=\"", "\"")
|
||||
// strip attribute tag part
|
||||
price_text = xml.between(price_val, ">", "") // until end
|
||||
if (len(price_text) == 0)
|
||||
price_text = price_val
|
||||
else
|
||||
price_text = ""
|
||||
|
||||
print("First product name: ")
|
||||
print(name)
|
||||
print("First price: ")
|
||||
if (len(currency) > 0)
|
||||
print(currency)
|
||||
print(" ")
|
||||
print(price_text)
|
||||
|
||||
/* Expected output:
|
||||
Root element:
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<catalog>
|
||||
<product id="SKU-1001">
|
||||
<name>Wireless Keyboard</name>
|
||||
<category>Peripherals</category>
|
||||
<price currency="USD">39.99</price>
|
||||
<specs>
|
||||
<layout>US</layout>
|
||||
<connection>Bluetooth</connection>
|
||||
<battery>AA</battery>
|
||||
</specs>
|
||||
</product>
|
||||
<product id="SKU-2002">
|
||||
<name>27" Monitor</name>
|
||||
<category>Displays</category>
|
||||
<price currency="USD">199.00</price>
|
||||
<specs>
|
||||
<resolution>2560x1440</resolution>
|
||||
<panel>IPS</panel>
|
||||
<refresh>75Hz</refresh>
|
||||
</specs>
|
||||
</product>
|
||||
<product id="SKU-3003">
|
||||
<name>USB-C Dock</name>
|
||||
<category>Peripherals</category>
|
||||
<price currency="USD">89.50</price>
|
||||
<specs>
|
||||
<ports>2xHDMI, 3xUSB-A, 1xUSB-C PD</ports>
|
||||
<pd>65W</pd>
|
||||
</specs>
|
||||
</product>
|
||||
</catalog>
|
||||
|
||||
First product name:
|
||||
Wireless Keyboard
|
||||
First price:
|
||||
USD
|
||||
|
||||
39.99
|
||||
*/
|
||||
81
examples/extensions/xml2/xml_access_employees.fun
Executable file
81
examples/extensions/xml2/xml_access_employees.fun
Executable file
|
|
@ -0,0 +1,81 @@
|
|||
#!/usr/bin/env fun
|
||||
|
||||
/*
|
||||
* This file is part of the Fun programming language.
|
||||
* https://fun-lang.xyz/
|
||||
*
|
||||
* Copyright 2025 Johannes Findeisen <you@hanez.org>
|
||||
* Licensed under the terms of the Apache-2.0 license.
|
||||
* https://opensource.org/license/apache-2-0
|
||||
*
|
||||
* Added: 2025-12-09
|
||||
*/
|
||||
|
||||
/*
|
||||
* Access selected fields in employees.xml using minimal XML API.
|
||||
*/
|
||||
|
||||
include <io/xml.fun>
|
||||
|
||||
path = "./examples/data/employees.xml"
|
||||
xml = XML()
|
||||
doc = xml.from_file(path)
|
||||
if (doc == 0)
|
||||
print("Failed to load: ")
|
||||
print(path)
|
||||
else
|
||||
root = xml.root(doc)
|
||||
print("Root element: ")
|
||||
print(xml.name(root))
|
||||
|
||||
content = read_file(path)
|
||||
// Find the first <employee> block and extract its fields
|
||||
first_emp = xml.between(content, "<employee", "</employee>")
|
||||
name = xml.between(first_emp, "<name>", "</name>")
|
||||
role = xml.between(first_emp, "<role>", "</role>")
|
||||
email = xml.between(first_emp, "<email>", "</email>")
|
||||
emp_id = xml.between(first_emp, "id=\"", "\"")
|
||||
|
||||
print("First employee id: ")
|
||||
print(emp_id)
|
||||
print("Name: ")
|
||||
print(name)
|
||||
print("Role: ")
|
||||
print(role)
|
||||
print("Email: ")
|
||||
print(email)
|
||||
|
||||
/* Expected output:
|
||||
Root element:
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<company>
|
||||
<department name="Engineering">
|
||||
<employee id="E-100">
|
||||
<name>Alice Doe</name>
|
||||
<role>Senior Developer</role>
|
||||
<email>alice@example.com</email>
|
||||
</employee>
|
||||
<employee id="E-101">
|
||||
<name>Bob Roe</name>
|
||||
<role>DevOps Engineer</role>
|
||||
<email>bob@example.com</email>
|
||||
</employee>
|
||||
</department>
|
||||
<department name="Sales">
|
||||
<employee id="S-200">
|
||||
<name>Carol Smith</name>
|
||||
<role>Account Executive</role>
|
||||
<email>carol@example.com</email>
|
||||
</employee>
|
||||
</department>
|
||||
</company>
|
||||
|
||||
First employee id:
|
||||
E-100
|
||||
Name:
|
||||
Alice Doe
|
||||
Role:
|
||||
Senior Developer
|
||||
Email:
|
||||
alice@example.com
|
||||
*/
|
||||
60
examples/extensions/xml2/xml_access_ns.fun
Executable file
60
examples/extensions/xml2/xml_access_ns.fun
Executable file
|
|
@ -0,0 +1,60 @@
|
|||
#!/usr/bin/env fun
|
||||
|
||||
/*
|
||||
* This file is part of the Fun programming language.
|
||||
* https://fun-lang.xyz/
|
||||
*
|
||||
* Copyright 2025 Johannes Findeisen <you@hanez.org>
|
||||
* Licensed under the terms of the Apache-2.0 license.
|
||||
* https://opensource.org/license/apache-2-0
|
||||
*
|
||||
* Added: 2025-12-09
|
||||
*/
|
||||
|
||||
/*
|
||||
* Access a namespaced XML file and show prefixed element names.
|
||||
*/
|
||||
|
||||
include <io/xml.fun>
|
||||
|
||||
path = "./examples/data/ns_example.xml"
|
||||
xml = XML()
|
||||
doc = xml.from_file(path)
|
||||
if (doc == 0)
|
||||
print("Failed to load: ")
|
||||
print(path)
|
||||
else
|
||||
root = xml.root(doc)
|
||||
// With namespaces, the node name may include the prefix, e.g., "ns:library"
|
||||
print("Root element: ")
|
||||
print(xml.name(root))
|
||||
|
||||
content = read_file(path)
|
||||
// Extract first book title and author (namespace prefix bk:)
|
||||
b1 = xml.between(content, "<bk:book", "</bk:book>")
|
||||
title = xml.between(b1, "<bk:title>", "</bk:title>")
|
||||
author = xml.between(b1, "<bk:author>", "</bk:author>")
|
||||
print("First book title: ")
|
||||
print(title)
|
||||
print("Author: ")
|
||||
print(author)
|
||||
|
||||
/* Expected output:
|
||||
Root element:
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<ns:library xmlns:ns="http://example.org/ns/library" xmlns:bk="http://example.org/ns/book">
|
||||
<bk:book id="B-1">
|
||||
<bk:title>The Art of Fun</bk:title>
|
||||
<bk:author>J. Findeisen</bk:author>
|
||||
</bk:book>
|
||||
<bk:book id="B-2">
|
||||
<bk:title>Minimal VM Design</bk:title>
|
||||
<bk:author>A. Dev</bk:author>
|
||||
</bk:book>
|
||||
</ns:library>
|
||||
|
||||
First book title:
|
||||
The Art of Fun
|
||||
Author:
|
||||
J. Findeisen
|
||||
*/
|
||||
110
examples/extensions/xml2/xml_class_example.fun
Executable file
110
examples/extensions/xml2/xml_class_example.fun
Executable file
|
|
@ -0,0 +1,110 @@
|
|||
#!/usr/bin/env fun
|
||||
|
||||
/*
|
||||
* This file is part of the Fun programming language.
|
||||
* https://fun-lang.xyz/
|
||||
*
|
||||
* Copyright 2025 Johannes Findeisen <you@hanez.org>
|
||||
* Licensed under the terms of the Apache-2.0 license.
|
||||
* https://opensource.org/license/apache-2-0
|
||||
*
|
||||
* Added: 2025-12-09
|
||||
*/
|
||||
|
||||
// Example using the stdlib XML class wrapper
|
||||
|
||||
include <io/xml.fun>
|
||||
|
||||
xml = XML()
|
||||
doc = xml.from_file("./examples/data/example.xml")
|
||||
print("doc handle:")
|
||||
print(doc)
|
||||
if (doc == 0)
|
||||
print("Failed to load XML file")
|
||||
else
|
||||
root = xml.root(doc)
|
||||
print("root name:")
|
||||
print(xml.name(root))
|
||||
print("root text:")
|
||||
print(xml.text(root))
|
||||
|
||||
/* Expected output:
|
||||
doc handle:
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<company name="Acme Corp">
|
||||
<departments>
|
||||
<department id="eng" name="Engineering">
|
||||
<team name="Platform">
|
||||
<member id="u1">Alice</member>
|
||||
<member id="u2">Bob</member>
|
||||
</team>
|
||||
<team name="Product">
|
||||
<member id="u3">Carol</member>
|
||||
</team>
|
||||
</department>
|
||||
<department id="ops" name="Operations">
|
||||
<team name="SRE">
|
||||
<member id="u4">Dave</member>
|
||||
</team>
|
||||
</department>
|
||||
</departments>
|
||||
<offices>
|
||||
<office city="Berlin" country="DE"/>
|
||||
<office city="Paris" country="FR"/>
|
||||
</offices>
|
||||
<note>Welcome to Acme!</note>
|
||||
</company>
|
||||
|
||||
root name:
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<company name="Acme Corp">
|
||||
<departments>
|
||||
<department id="eng" name="Engineering">
|
||||
<team name="Platform">
|
||||
<member id="u1">Alice</member>
|
||||
<member id="u2">Bob</member>
|
||||
</team>
|
||||
<team name="Product">
|
||||
<member id="u3">Carol</member>
|
||||
</team>
|
||||
</department>
|
||||
<department id="ops" name="Operations">
|
||||
<team name="SRE">
|
||||
<member id="u4">Dave</member>
|
||||
</team>
|
||||
</department>
|
||||
</departments>
|
||||
<offices>
|
||||
<office city="Berlin" country="DE"/>
|
||||
<office city="Paris" country="FR"/>
|
||||
</offices>
|
||||
<note>Welcome to Acme!</note>
|
||||
</company>
|
||||
|
||||
root text:
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<company name="Acme Corp">
|
||||
<departments>
|
||||
<department id="eng" name="Engineering">
|
||||
<team name="Platform">
|
||||
<member id="u1">Alice</member>
|
||||
<member id="u2">Bob</member>
|
||||
</team>
|
||||
<team name="Product">
|
||||
<member id="u3">Carol</member>
|
||||
</team>
|
||||
</department>
|
||||
<department id="ops" name="Operations">
|
||||
<team name="SRE">
|
||||
<member id="u4">Dave</member>
|
||||
</team>
|
||||
</department>
|
||||
</departments>
|
||||
<offices>
|
||||
<office city="Berlin" country="DE"/>
|
||||
<office city="Paris" country="FR"/>
|
||||
</offices>
|
||||
<note>Welcome to Acme!</note>
|
||||
</company>
|
||||
|
||||
*/
|
||||
24
examples/extensions/xml2/xml_minimal.fun
Executable file
24
examples/extensions/xml2/xml_minimal.fun
Executable file
|
|
@ -0,0 +1,24 @@
|
|||
#!/usr/bin/env fun
|
||||
|
||||
/*
|
||||
* This file is part of the Fun programming language.
|
||||
* https://fun-lang.xyz/
|
||||
*
|
||||
* Copyright 2025 Johannes Findeisen <you@hanez.org>
|
||||
* Licensed under the terms of the Apache-2.0 license.
|
||||
* https://opensource.org/license/apache-2-0
|
||||
*
|
||||
* Added: 2025-12-09
|
||||
*/
|
||||
|
||||
// Minimal XML example using libxml2-backed builtins
|
||||
|
||||
doc = xml_parse("<root><item id=\"1\">a</item><item id=\"2\">b</item></root>")
|
||||
print("doc handle=\(doc)")
|
||||
root = xml_root(doc)
|
||||
print("root name=\(xml_name(root)) text=\(xml_text(root))")
|
||||
|
||||
/* Expected output:
|
||||
doc handle=(doc)
|
||||
root name=(xml_name(root)) text=(xml_text(root))
|
||||
*/
|
||||
Loading…
Add table
Add a link
Reference in a new issue