forked from tid-kijyun/Kanna
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKanna.swift
254 lines (215 loc) · 7.27 KB
/
Kanna.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/**@file Kanna.swift
Kanna
Copyright (c) 2015 Atsushi Kiwaki (@_tid_)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
import Foundation
/*
ParseOption
*/
public enum ParseOption {
// libxml2
case XmlParseUseLibxml(Libxml2XMLParserOptions)
case HtmlParseUseLibxml(Libxml2HTMLParserOptions)
}
private let kDefaultXmlParseOption = ParseOption.XmlParseUseLibxml([.RECOVER, .NOERROR, .NOWARNING])
private let kDefaultHtmlParseOption = ParseOption.HtmlParseUseLibxml([.RECOVER, .NOERROR, .NOWARNING])
/**
Parse XML
@param xml an XML string
@param url the base URL to use for the document
@param encoding the document encoding
@param options a ParserOption
*/
public func XML(xml xml: String, url: String?, encoding: UInt, option: ParseOption = kDefaultXmlParseOption) -> XMLDocument? {
switch option {
case .XmlParseUseLibxml(let opt):
return libxmlXMLDocument(xml: xml, url: url, encoding: encoding, option: opt.rawValue)
default:
return nil
}
}
public func XML(xml xml: String, encoding: UInt, option: ParseOption = kDefaultXmlParseOption) -> XMLDocument? {
return XML(xml: xml, url: nil, encoding: encoding, option: option)
}
// NSData
public func XML(xml xml: NSData, url: String?, encoding: UInt, option: ParseOption = kDefaultXmlParseOption) -> XMLDocument? {
if let xmlStr = NSString(data: xml, encoding: encoding) as? String {
return XML(xml: xmlStr, url: url, encoding: encoding, option: option)
}
return nil
}
public func XML(xml xml: NSData, encoding: UInt, option: ParseOption = kDefaultXmlParseOption) -> XMLDocument? {
return XML(xml: xml, url: nil, encoding: encoding, option: option)
}
// NSURL
public func XML(url url: NSURL, encoding: UInt, option: ParseOption = kDefaultXmlParseOption) -> XMLDocument? {
if let data = NSData(contentsOfURL: url) {
return XML(xml: data, url: url.absoluteString, encoding: encoding, option: option)
}
return nil
}
/**
Parse HTML
@param html an HTML string
@param url the base URL to use for the document
@param encoding the document encoding
@param options a ParserOption
*/
public func HTML(html html: String, url: String?, encoding: UInt, option: ParseOption = kDefaultHtmlParseOption) -> HTMLDocument? {
switch option {
case .HtmlParseUseLibxml(let opt):
return libxmlHTMLDocument(html: html, url: url, encoding: encoding, option: opt.rawValue)
default:
return nil
}
}
public func HTML(html html: String, encoding: UInt, option: ParseOption = kDefaultHtmlParseOption) -> HTMLDocument? {
return HTML(html: html, url: nil, encoding: encoding, option: option)
}
// NSData
public func HTML(html html: NSData, url: String?, encoding: UInt, option: ParseOption = kDefaultHtmlParseOption) -> HTMLDocument? {
if let htmlStr = NSString(data: html, encoding: encoding) as? String {
return HTML(html: htmlStr, url: url, encoding: encoding, option: option)
}
return nil
}
public func HTML(html html: NSData, encoding: UInt, option: ParseOption = kDefaultHtmlParseOption) -> HTMLDocument? {
return HTML(html: html, url: nil, encoding: encoding, option: option)
}
// NSURL
public func HTML(url url: NSURL, encoding: UInt, option: ParseOption = kDefaultHtmlParseOption) -> HTMLDocument? {
if let data = NSData(contentsOfURL: url) {
return HTML(html: data, url: url.absoluteString, encoding: encoding, option: option)
}
return nil
}
/**
Searchable
*/
public protocol Searchable {
/**
Search for node from current node by XPath.
@param xpath
*/
func xpath(xpath: String, namespaces: [String:String]?) -> XMLNodeSet
func xpath(xpath: String) -> XMLNodeSet
func at_xpath(xpath: String, namespaces: [String:String]?) -> XMLElement?
func at_xpath(xpath: String) -> XMLElement?
/**
Search for node from current node by CSS selector.
@param selector a CSS selector
*/
func css(selector: String, namespaces: [String:String]?) -> XMLNodeSet
func css(selector: String) -> XMLNodeSet
func at_css(selector: String, namespaces: [String:String]?) -> XMLElement?
func at_css(selector: String) -> XMLElement?
}
/**
SearchableNode
*/
public protocol SearchableNode: Searchable {
var text: String? { get }
var toHTML: String? { get }
var innerHTML: String? { get }
var className: String? { get }
var tagName: String? { get }
}
/**
XMLElement
*/
public protocol XMLElement: SearchableNode {
subscript(attr: String) -> String? { get }
}
/**
XMLDocument
*/
public protocol XMLDocument: SearchableNode {
}
/**
HTMLDocument
*/
public protocol HTMLDocument: XMLDocument {
var title: String? { get }
var head: XMLElement? { get }
var body: XMLElement? { get }
}
/**
XMLNodeSet
*/
public final class XMLNodeSet {
private var nodes: [XMLElement] = []
public var toHTML: String? {
let html = nodes.reduce("") {
if let text = $1.toHTML {
return $0 + text
}
return $0
}
return html.isEmpty == false ? html : nil
}
public var innerHTML: String? {
let html = nodes.reduce("") {
if let text = $1.innerHTML {
return $0 + text
}
return $0
}
return html.isEmpty == false ? html : nil
}
public var text: String? {
let html = nodes.reduce("") {
if let text = $1.text {
return $0 + text
}
return $0
}
return html
}
public subscript(index: Int) -> XMLElement {
return nodes[index]
}
public var count: Int {
return nodes.count
}
internal init() {
}
internal init(nodes: [XMLElement]) {
self.nodes = nodes
}
public func at(index: Int) -> XMLElement? {
return count > index ? nodes[index] : nil
}
public var first: XMLElement? {
return at(0)
}
public var last: XMLElement? {
return at(count-1)
}
}
extension XMLNodeSet: SequenceType {
public typealias Generator = AnyGenerator<XMLElement>
public func generate() -> Generator {
var index = 0
return anyGenerator {
if index < self.nodes.count {
return self.nodes[index++]
}
return nil
}
}
}