forked from JohnSundell/Plot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Document.swift
59 lines (52 loc) · 2.46 KB
/
Document.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/**
* Plot
* Copyright (c) John Sundell 2019
* MIT license, see LICENSE file for details
*/
import Foundation
/// Protocol used to define a document format. Plot ships with a number
/// of different implementations of this protocol, such as `HTML`, `RSS`,
/// and `XML`, but you can also create your own types by conforming to it.
///
/// Built-in document types are created simply by initializing them, while
/// custom ones can be created using the `Document.custom` APIs.
public protocol DocumentFormat {
/// The root context of the document, which all top-level elements are
/// bound to. Each document format is free to define any number of contexts
/// in order to limit where an element or attribute may be placed.
associatedtype RootContext
}
/// A representation of a document, which is the root element for all formats
/// that can be expressed using Plot. For example, an HTML document will have
/// the type `Document<HTML>`, and an RSS feed `Document<RSS>`.
///
/// You normally don't have to interact with this type directly, unless you want
/// to define your own custom document format, since the built-in formats (such
/// as `HTML`, `RSS` and `XML`) completely wrap this type. To create custom
/// `Document` values, use the `.custom` static factory methods.
public struct Document<Format: DocumentFormat> {
/// The root elements that make up this document. See `Element` for more info.
public var elements: [Element<Format.RootContext>]
internal init(elements: [Element<Format.RootContext>]) {
self.elements = elements
}
}
public extension Document {
/// Create a `Document` value using a custom `DocumentFormat` type, with a list
/// of elements that make up the root of the document.
/// - parameter elements: The new document's root elements
static func custom(_ elements: Element<Format.RootContext>...) -> Self {
Document(elements: elements)
}
/// Create a `Document` value using an explicitly passed custom `DocumentFormat`
/// type, with a list of elements that make up the root of the document.
/// - parameter format: The `DocumentFormat` type to bind the document to.
/// - parameter elements: The new document's root elements
static func custom(withFormat format: Format.Type,
elements: [Element<Format.RootContext>] = []) -> Self {
Document(elements: elements)
}
}
extension Document: NodeConvertible {
public var node: Node<Format> { .document(self) }
}