Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/SwiftDocOrg/Markup
A Swift package for working with HTML, XML, and other markup languages, based on libxml2.
https://github.com/SwiftDocOrg/Markup
dtd html libxml2 relax-ng swift xml xpath xslt
Last synced: 3 months ago
JSON representation
A Swift package for working with HTML, XML, and other markup languages, based on libxml2.
- Host: GitHub
- URL: https://github.com/SwiftDocOrg/Markup
- Owner: SwiftDocOrg
- License: mit
- Archived: true
- Created: 2020-03-19T21:09:45.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2021-10-17T13:12:48.000Z (about 3 years ago)
- Last Synced: 2024-07-05T13:46:17.232Z (4 months ago)
- Topics: dtd, html, libxml2, relax-ng, swift, xml, xpath, xslt
- Language: Swift
- Size: 105 KB
- Stars: 112
- Watchers: 5
- Forks: 9
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Markup
![CI][ci badge]
[![Documentation][documentation badge]][documentation]A Swift package for working with HTML, XML, and other markup languages,
based on [libxml2][libxml2].**This project is under active development and is not ready for production use.**
## Features
- [x] XML Support
- [x] XHTML4 Support
- [x] XPath Expression Evaluation
- [ ] HTML5 Support (using [Gumbo][gumbo])
- [ ] CSS Selector to XPath Functionality
- [ ] XML Namespace Support
- [ ] DTD and Relax-NG Validation
- [ ] XInclude Support
- [ ] XSLT Support
- [ ] SAX Parser Interface
- [x] HTML and XML Function Builder Interfaces## Requirements
- Swift 5.1+
- [libxml2][libxml2] _(except for macOS with Xcode 11.4 or later)_## Usage
### XML
#### Parsing & Introspection
```swift
import XMLlet xml = #"""
Hello!
"""#
let document = try XML.Document(string: xml)!
document.root?.name // "greeting"
document.root?.content // "Hello!"document.children.count // 3 (two comment nodes and one element node)
document.root?.children.count // 1 (one text node)
```#### Searching and XPath Expression Evaluation
```swift
document.search("//greeting").count // 1
document.evaluate("//greeting/text()") // .string("Hello!")
```#### Modification
```swift
for case let comment as Comment in document.children {
comment.remove()
}document.root?.name = "valediction"
document.root?["lang"] = "it"
document.root?.content = "Arrivederci!"document.description // =>
/*Arrivederci!
*/
```* * *
### HTML
#### Parsing & Introspection
```swift
import HTMLlet html = #"""
Welcome
Hello, world!
"""#
let document = try HTML.Document(string: html)!
document.body?.children.count // 1 (one element node)
document.body?.children.first?.name // "p"
document.body?.children.first?.text // "Hello, world!"
```#### Searching and XPath Expression Evaluation
```swift
document.search("/body/p").count // 1
document.search("/body/p").first?.xpath // "/body/p[0]"
document.evaluate("/body/p/text()") // .string("Hello, world!")
```#### Creation and Modification
```swift
let div = Element(name: "div")
div["class"] = "wrapper"
if let p = document.search("/body/p").first {
p.wrap(inside: div)
}document.body?.description // =>
/*
Hello, world!
*/
```#### Builder Interface
Available in Swift 5.3+.
```swift
import HTMLlet document = HTML.Document {
html(["lang": "en"]) {
head {
meta(["charset": "UTF-8"])
title { "Hello, world!" }
}body(["class": "beautiful"]) {
div(["class": "wrapper"]) {
span { "Hello," }
tag("span") { "world!" }
}
}
}
}document.description // =>
/*
Hello, world!
Hello,
world!
*/
```## Installation
### Swift Package Manager
If you're on Linux or if you're on macOS and using Xcode < 11.4,
install the [libxml2][libxml2] system library:```terminal
# macOS for Xcode 11.3 and earlier
$ brew install libxml2
$ brew link --force libxml2# Linux (Ubuntu)
$ sudo apt-get install libxml2-dev
```Add the Markup package to your target dependencies in `Package.swift`:
```swift
import PackageDescriptionlet package = Package(
name: "YourProject",
dependencies: [
.package(
url: "https://github.com/SwiftDocOrg/Markup",
from: "0.1.2"
),
]
)
```Add `Markup` as a dependency to your target(s):
```swift
targets: [
.target(
name: "YourTarget",
dependencies: ["Markup"]),
```If you're using Markup in an app,
link `libxml2` to your target.
Open your Xcode project (`.xcodeproj`) or workspace (`.xcworkspace`) file,
select your top-level project entry in the Project Navigator,
and select the target using Markup listed under the Targets heading.
Navigate to the "Build Phases" tab,
expand "Link Binary With Libraries",
and click the + button to add a library.
Enter "libxml2" to the search bar,
select "libxml2.tbd" from the filtered list,
and click the Add button.## License
MIT
## Contact
Mattt ([@mattt](https://twitter.com/mattt))
[libxml2]: http://xmlsoft.org
[gumbo]: https://github.com/google/gumbo-parser
[ci badge]: https://github.com/SwiftDocOrg/Markup/workflows/CI/badge.svg
[documentation badge]: https://github.com/SwiftDocOrg/Markup/workflows/Documentation/badge.svg
[documentation]: https://github.com/SwiftDocOrg/Markup/wiki