Skip to content

Commit

Permalink
Merge pull request #110 from tid-kijyun/linux
Browse files Browse the repository at this point in the history
Added support for linux
  • Loading branch information
tid-kijyun authored Oct 4, 2016
2 parents fdd68eb + a63e88a commit 0bad55e
Show file tree
Hide file tree
Showing 32 changed files with 306 additions and 268 deletions.
511 changes: 255 additions & 256 deletions Kanna.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import PackageDescription
let package = Package(
name: "Kanna",
dependencies: [
.Package(url: "../Kanna", majorVersion: 1)
.Package(url: "https://github.com/tid-kijyun/SwiftClibxml2.git", majorVersion: 1)
]
)

36 changes: 30 additions & 6 deletions Sources/CSS.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ SOFTWARE.
*/
import Foundation

#if os(Linux)
import SwiftClibxml2

typealias AKTextCheckingResult = TextCheckingResult
typealias AKRegularExpression = RegularExpression
#else
typealias AKRegularExpression = NSRegularExpression
typealias AKTextCheckingResult = NSTextCheckingResult
#endif

/**
CSS
*/
Expand All @@ -45,7 +55,7 @@ public struct CSS {
var combinator: String = ""

if let result = matchBlank(str) {
str = (str as NSString).substring(from: result.range.length)
str = str.substring(from: str.index(str.startIndex, offsetBy: result.range.length))
}

// element
Expand Down Expand Up @@ -84,11 +94,11 @@ public struct CSS {
}
}

private func firstMatch(_ pattern: String) -> (String) -> NSTextCheckingResult? {
private func firstMatch(_ pattern: String) -> (String) -> AKTextCheckingResult? {
return { str in
let length = str.utf16.count
do {
let regex = try NSRegularExpression(pattern: pattern, options: .caseInsensitive)
let regex = try AKRegularExpression(pattern: pattern, options: .caseInsensitive)
if let result = regex.firstMatch(in: str, options: .reportProgress, range: NSRange(location: 0, length: length)) {
return result
}
Expand Down Expand Up @@ -136,11 +146,17 @@ private let matchSubNthOfType = firstMatch("nth-of-type\\((odd|even|\\d+)\\)")
private let matchSubContains = firstMatch("contains\\([\"\'](.*?)[\"\']\\)")
private let matchSubBlank = firstMatch("^\\s*$")

private func substringWithRangeAtIndex(_ result: NSTextCheckingResult, str: String, at: Int) -> String {
private func substringWithRangeAtIndex(_ result: AKTextCheckingResult, str: String, at: Int) -> String {
if result.numberOfRanges > at {
#if os(Linux)
let range = result.range(at: at)
#else
let range = result.rangeAt(at)
#endif
if range.length > 0 {
return (str as NSString).substring(with: range)
let startIndex = str.index(str.startIndex, offsetBy: range.location)
let endIndex = str.index(startIndex, offsetBy: range.length)
return str.substring(with: startIndex..<endIndex)
}
}
return ""
Expand Down Expand Up @@ -300,7 +316,15 @@ private func getAttrNot(_ str: inout String, skip: Bool = true) -> String? {
if let attr = getAttribute(&one, skip: false) {
return attr
} else if let sub = matchElement(one) {
let elem = (one as NSString).substring(with: sub.rangeAt(1))
#if os(Linux)
let range = sub.range(at: 1)
#else
let range = sub.rangeAt(1)
#endif
let startIndex = one.index(one.startIndex, offsetBy: range.location)
let endIndex = one.index(startIndex, offsetBy: range.length)

let elem = one.substring(with: startIndex ..< endIndex)
return "self::\(elem)"
} else if let attr = getClassId(&one) {
return attr
Expand Down
8 changes: 6 additions & 2 deletions Sources/Kanna.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ SOFTWARE.
*/
import Foundation

#if os(Linux)
import SwiftClibxml2
#endif

/*
ParseOption
*/
Expand Down Expand Up @@ -59,7 +63,7 @@ public func XML(xml: String, encoding: String.Encoding, option: ParseOption = kD

// NSData
public func XML(xml: Data, url: String?, encoding: String.Encoding, option: ParseOption = kDefaultXmlParseOption) -> XMLDocument? {
if let xmlStr = NSString(data: xml, encoding: encoding.rawValue) as? String {
if let xmlStr = String(data: xml, encoding: encoding) {
return XML(xml: xmlStr, url: url, encoding: encoding, option: option)
}
return nil
Expand Down Expand Up @@ -128,7 +132,7 @@ public func HTML(html: String, encoding: String.Encoding, option: ParseOption =

// NSData
public func HTML(html: Data, url: String?, encoding: String.Encoding, option: ParseOption = kDefaultHtmlParseOption) -> HTMLDocument? {
if let htmlStr = NSString(data: html, encoding: encoding.rawValue) as? String {
if let htmlStr = String(data: html, encoding: encoding) {
return HTML(html: htmlStr, url: url, encoding: encoding, option: option)
}
return nil
Expand Down
8 changes: 6 additions & 2 deletions Sources/libxmlHTMLDocument.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
import Foundation
import CoreFoundation

#if os(Linux)
import SwiftClibxml2
#endif

/*
libxmlHTMLDocument
Expand Down Expand Up @@ -98,9 +103,9 @@ internal final class libxmlHTMLDocument: HTMLDocument {
if html.lengthOfBytes(using: encoding) <= 0 {
return nil
}

let cfenc : CFStringEncoding = CFStringConvertNSStringEncodingToEncoding(encoding.rawValue)
let cfencstr = CFStringConvertEncodingToIANACharSetName(cfenc)

if let cur = html.cString(using: encoding) {
let url : String = ""
docPtr = htmlReadDoc(UnsafeRawPointer(cur).assumingMemoryBound(to: xmlChar.self), url, (cfencstr as? String) ?? "", CInt(option))
Expand Down Expand Up @@ -227,7 +232,6 @@ internal final class libxmlXMLDocument: XMLDocument {
}
let cfenc : CFStringEncoding = CFStringConvertNSStringEncodingToEncoding(encoding.rawValue)
let cfencstr = CFStringConvertEncodingToIANACharSetName(cfenc)

if let cur = xml.cString(using: encoding) {
let url : String = ""
docPtr = xmlReadDoc(UnsafeRawPointer(cur).assumingMemoryBound(to: xmlChar.self), url, (cfencstr as? String) ?? "", CInt(option))
Expand Down
4 changes: 4 additions & 0 deletions Sources/libxmlHTMLNode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ SOFTWARE.
*/
import Foundation

#if os(Linux)
import SwiftClibxml2
#endif

/**
libxmlHTMLNode
*/
Expand Down
4 changes: 4 additions & 0 deletions Sources/libxmlParserOption.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ SOFTWARE.
*/
import Foundation

#if os(Linux)
import SwiftClibxml2
#endif

/*
Libxml2HTMLParserOptions
*/
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 0bad55e

Please sign in to comment.