-
Notifications
You must be signed in to change notification settings - Fork 2
/
Kanna.swift
executable file
·386 lines (332 loc) · 10.4 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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/**@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
import libxml2
/*
ParseOption
*/
public enum ParseOption {
// libxml2
case xmlParseUseLibxml(Libxml2XMLParserOptions)
case htmlParseUseLibxml(Libxml2HTMLParserOptions)
}
public let kDefaultXmlParseOption = ParseOption.xmlParseUseLibxml([.RECOVER, .NOERROR, .NOWARNING])
public let kDefaultHtmlParseOption = ParseOption.htmlParseUseLibxml([.RECOVER, .NOERROR, .NOWARNING])
public enum ParseError: Error {
case Empty
case EncodingMismatch
case InvalidOptions
}
/**
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: String, url: String? = nil, encoding: String.Encoding, option: ParseOption = kDefaultXmlParseOption) throws -> XMLDocument {
switch option {
case .xmlParseUseLibxml(let opt):
return try libxmlXMLDocument(xml: xml, url: url, encoding: encoding, option: opt.rawValue)
default:
throw ParseError.InvalidOptions
}
}
// NSData
public func XML(xml: Data, url: String? = nil, encoding: String.Encoding, option: ParseOption = kDefaultXmlParseOption) throws -> XMLDocument {
guard let xmlStr = String(data: xml, encoding: encoding) else {
throw ParseError.EncodingMismatch
}
return try XML(xml: xmlStr, url: url, encoding: encoding, option: option)
}
// NSURL
public func XML(url: URL, encoding: String.Encoding, option: ParseOption = kDefaultXmlParseOption) throws -> XMLDocument {
guard let data = try? Data(contentsOf: url) else {
throw ParseError.EncodingMismatch
}
return try XML(xml: data, url: url.absoluteString, encoding: encoding, option: option)
}
/**
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: String, url: String? = nil, encoding: String.Encoding, option: ParseOption = kDefaultHtmlParseOption) throws -> HTMLDocument {
switch option {
case .htmlParseUseLibxml(let opt):
return try libxmlHTMLDocument(html: html, url: url, encoding: encoding, option: opt.rawValue)
default:
throw ParseError.InvalidOptions
}
}
// NSData
public func HTML(html: Data, url: String? = nil, encoding: String.Encoding, option: ParseOption = kDefaultHtmlParseOption) throws -> HTMLDocument {
guard let htmlStr = String(data: html, encoding: encoding) else {
throw ParseError.EncodingMismatch
}
return try HTML(html: htmlStr, url: url, encoding: encoding, option: option)
}
// NSURL
public func HTML(url: URL, encoding: String.Encoding, option: ParseOption = kDefaultHtmlParseOption) throws -> HTMLDocument {
guard let data = try? Data(contentsOf: url) else {
throw ParseError.EncodingMismatch
}
return try HTML(html: data, url: url.absoluteString, encoding: encoding, option: option)
}
/**
Searchable
*/
public protocol Searchable {
/**
Search for node from current node by XPath.
@param xpath
*/
func xpath(_ xpath: String, namespaces: [String: String]?) -> XPathObject
/**
Search for node from current node by CSS selector.
@param selector a CSS selector
*/
func css(_ selector: String, namespaces: [String: String]?) -> XPathObject
}
public extension Searchable {
func xpath(_ xpath: String, namespaces: [String: String]? = nil) -> XPathObject {
self.xpath(xpath, namespaces: namespaces)
}
func at_xpath(_ xpath: String, namespaces: [String: String]? = nil) -> XMLElement? {
self.xpath(xpath, namespaces: namespaces).nodeSetValue.first
}
func css(_ selector: String, namespaces: [String: String]? = nil) -> XPathObject {
self.css(selector, namespaces: namespaces)
}
func at_css(_ selector: String, namespaces: [String: String]? = nil) -> XMLElement? {
self.css(selector, namespaces: namespaces).nodeSetValue.first
}
}
/**
SearchableNode
*/
public protocol SearchableNode: Searchable {
var text: String? { get }
var toHTML: String? { get }
var toXML: String? { get }
var innerHTML: String? { get }
var className: String? { get }
var tagName: String? { get set }
var content: String? { get set }
}
/**
XMLElement
*/
public protocol XMLElement: SearchableNode {
var parent: XMLElement? { get set }
subscript(attr: String) -> String? { get set }
func attributes() -> [AnyHashable: Any]
func addPrevSibling(_ node: XMLElement)
func addNextSibling(_ node: XMLElement)
func removeChild(_ node: XMLElement)
var nextSibling: XMLElement? { get }
var previousSibling: XMLElement? { get }
}
/**
XMLDocument
*/
public protocol XMLDocument: AnyObject, SearchableNode {
var namespaces: [Namespace] { get }
}
/**
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 {
nodes[index]
}
public var count: Int { nodes.count }
init(nodes: [XMLElement] = []) {
self.nodes = nodes
}
public func at(_ index: Int) -> XMLElement? {
count > index ? nodes[index] : nil
}
public var first: XMLElement? { at(0) }
public var last: XMLElement? { at(count - 1) }
}
extension XMLNodeSet: Sequence {
public typealias Iterator = AnyIterator<XMLElement>
public func makeIterator() -> Iterator {
var index = 0
return AnyIterator {
if index < self.nodes.count {
let n = self.nodes[index]
index += 1
return n
}
return nil
}
}
}
/**
Namespace
*/
public struct Namespace {
public let prefix: String
public let name: String
}
/**
XPathObject
*/
public enum XPathObject {
case none
case NodeSet(nodeset: XMLNodeSet)
case Bool(bool: Swift.Bool)
case Number(num: Double)
case String(text: Swift.String)
}
extension XPathObject {
init(document: XMLDocument?, docPtr: xmlDocPtr, object: xmlXPathObject) {
switch object.type {
case XPATH_NODESET:
guard let nodeSet = object.nodesetval, nodeSet.pointee.nodeNr != 0, let nodeTab = nodeSet.pointee.nodeTab else {
self = .none
return
}
var nodes: [XMLElement] = []
let size = Int(nodeSet.pointee.nodeNr)
for i in 0 ..< size {
let node: xmlNodePtr = nodeTab[i]!
let htmlNode = libxmlHTMLNode(document: document, docPtr: docPtr, node: node)
nodes.append(htmlNode)
}
self = .NodeSet(nodeset: XMLNodeSet(nodes: nodes))
return
case XPATH_BOOLEAN:
self = .Bool(bool: object.boolval != 0)
return
case XPATH_NUMBER:
self = .Number(num: object.floatval)
case XPATH_STRING:
guard let str = UnsafeRawPointer(object.stringval)?.assumingMemoryBound(to: CChar.self) else {
self = .String(text: "")
return
}
self = .String(text: Swift.String(cString: str))
return
default:
self = .none
return
}
}
public subscript(index: Int) -> XMLElement {
nodeSet![index]
}
public var first: XMLElement? {
nodeSet?.first
}
public var count: Int {
nodeSet?.count ?? 0
}
var nodeSet: XMLNodeSet? {
if case let .NodeSet(nodeset) = self {
return nodeset
}
return nil
}
var bool: Swift.Bool? {
if case let .Bool(value) = self {
return value
}
return nil
}
var number: Double? {
if case let .Number(value) = self {
return value
}
return nil
}
var string: Swift.String? {
if case let .String(value) = self {
return value
}
return nil
}
var nodeSetValue: XMLNodeSet {
nodeSet ?? XMLNodeSet()
}
var boolValue: Swift.Bool {
bool ?? false
}
var numberValue: Double {
number ?? 0
}
var stringValue: Swift.String {
string ?? ""
}
}
extension XPathObject: Sequence {
public typealias Iterator = AnyIterator<XMLElement>
public func makeIterator() -> Iterator {
var index = 0
return AnyIterator {
if index < self.nodeSetValue.count {
let obj = self.nodeSetValue[index]
index += 1
return obj
}
return nil
}
}
}