• This repository has been archived on 17/Oct/2021
  • Stars
    star
    113
  • Rank 300,478 (Top 7 %)
  • Language
    Swift
  • License
    MIT License
  • Created about 4 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A Swift package for working with HTML, XML, and other markup languages, based on libxml2.

Markup

CI Documentation

A Swift package for working with HTML, XML, and other markup languages, based on libxml2.

This project is under active development and is not ready for production use.

Features

  • XML Support
  • XHTML4 Support
  • XPath Expression Evaluation
  • HTML5 Support (using Gumbo)
  • CSS Selector to XPath Functionality
  • XML Namespace Support
  • DTD and Relax-NG Validation
  • XInclude Support
  • XSLT Support
  • SAX Parser Interface
  • HTML and XML Function Builder Interfaces

Requirements

  • Swift 5.1+
  • libxml2 (except for macOS with Xcode 11.4 or later)

Usage

XML

Parsing & Introspection

import XML

let xml = #"""
<?xml version="1.0" encoding="UTF-8"?>
<!-- begin greeting -->
<greeting>Hello!</greeting>
<!-- end greeting -->
"""#

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

document.search("//greeting").count // 1
document.evaluate("//greeting/text()") // .string("Hello!")

Modification

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 // =>
/*
<?xml version="1.0" encoding="UTF-8"?>
<valediction lang="it">Arrivederci!</valediction>

*/

HTML

Parsing & Introspection

import HTML

let html = #"""
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Welcome</title>
</head>
<body>
    <p>Hello, world!</p>
</body>
</html>
"""#

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

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

let div = Element(name: "div")
div["class"] = "wrapper"
if let p = document.search("/body/p").first {
    p.wrap(inside: div)
}

document.body?.description // =>
/*
<div class="wrapper">
    <p>Hello, world!</p>
</div>
*/

Builder Interface

Available in Swift 5.3+.

import HTML

let 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 // =>
/*
<html lang="en">
  <head>
      <meta charset="UTF-8">
      <title>Hello, world!</title>
  </head>
  <body class="beautiful">
      <?greeter start>
      <div class="wrapper">
          <span>Hello,</span>
          <span>world!</span>
      </div>
      <?greeter end>
  </body>
</html>

*/

Installation

Swift Package Manager

If you're on Linux or if you're on macOS and using Xcode < 11.4, install the libxml2 system library:

# 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:

import PackageDescription

let 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):

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.

Add libxml2 library to your target

License

MIT

Contact

Mattt (@mattt)

More Repositories

1

swift-doc

A documentation generator for Swift projects
Swift
1,682
star
2

DocTest

An experimental tool for testing Swift example code in documentation.
Swift
358
star
3

GraphViz

A Swift package for working with GraphViz
Swift
292
star
4

SwiftSemantics

Uses SwiftSyntax to parse Swift code into its constituent declarations
Swift
222
star
5

swiftdoc.org

Auto-generated documentation for Swift. Command-click no more.
HTML
217
star
6

CommonMark

Create, parse, and render Markdown text according to the CommonMark specification
Swift
180
star
7

github-wiki-publish-action

GitHub Action that publishes the contents of a directory to your project's wiki
Shell
81
star
8

SwiftMarkup

Parses Swift documentation comments into structured entities
Swift
56
star
9

Git

A Swift package for working with Git, built on top of libgit2.
Swift
47
star
10

swiftdoc-parser

Header parsing and HTML generating utilities for SwiftDoc.org
JavaScript
42
star
11

TAP

A Swift package for the Test Anything Protocol (v13)
Swift
21
star
12

DocSetUtil

Apple developer tool for working with .docset bundles
Makefile
20
star
13

Swift-Semantic-Icons

Icons used by swift-doc to represent Swift code symbols.
19
star
14

StringLocationConverter

Converts String index values into their corresponding line & column numbers
Swift
16
star
15

SwiftPackageManifest

A package for decoding the output of `swift package dump-package`
Swift
15
star
16

Inflection

Swift
10
star
17

api.swiftdoc.org

A JSON API for SwiftDoc.org
JavaScript
8
star
18

swift-api-inventory

Swift
6
star
19

homebrew-formulae

Collection of Homebrew Formulae
Ruby
3
star
20

docsetutil-validate-action

JavaScript
2
star