-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Additional work on supporting links.
- Loading branch information
1 parent
25c6ae9
commit 2100ae2
Showing
7 changed files
with
235 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
Sources/XMLText/XMLParser/Extensions/Color.InitWithHex.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// | ||
// Color.InitWithHex.swift | ||
// XMLText | ||
// | ||
// Created by cocoatoucher on 2021-12-30. | ||
// | ||
|
||
import SwiftUI | ||
|
||
extension Color { | ||
init(hex: String) { | ||
let hex = hex.trimmingCharacters(in: CharacterSet.alphanumerics.inverted) | ||
var int: UInt64 = 0 | ||
|
||
Scanner(string: hex).scanHexInt64(&int) | ||
|
||
let a, r, g, b: UInt64 | ||
|
||
switch hex.count { | ||
case 3: // RGB (12-bit) | ||
(a, r, g, b) = (255, (int >> 8) * 17, (int >> 4 & 0xF) * 17, (int & 0xF) * 17) | ||
case 6: // RGB (24-bit) | ||
(a, r, g, b) = (255, int >> 16, int >> 8 & 0xFF, int & 0xFF) | ||
case 8: // ARGB (32-bit) | ||
(a, r, g, b) = (int >> 24, int >> 16 & 0xFF, int >> 8 & 0xFF, int & 0xFF) | ||
default: | ||
(a, r, g, b) = (1, 1, 1, 0) | ||
} | ||
|
||
self.init( | ||
.sRGB, | ||
red: Double(r) / 255, | ||
green: Double(g) / 255, | ||
blue: Double(b) / 255, | ||
opacity: Double(a) / 255 | ||
) | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
Sources/XMLText/XMLParser/Extensions/String.EscapeWithUnicodeEntities.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// | ||
// String.EscapeWithUnicodeEntities.swift | ||
// XMLText | ||
// | ||
// Implementation in this file is taken from | ||
// SwiftRichString repository on GitHub. | ||
// https://github.com/malcommac/SwiftRichString/ | ||
// | ||
// SwiftRichString | ||
// Elegant Strings & Attributed Strings Toolkit for Swift | ||
// | ||
// Created by Daniele Margutti. | ||
// Copyright © 2018 Daniele Margutti. All rights reserved. | ||
// | ||
// Web: http://www.danielemargutti.com | ||
// Email: [email protected] | ||
// Twitter: @danielemargutti | ||
|
||
import Foundation | ||
|
||
extension String { | ||
|
||
static let escapeAmpRegExp = try! NSRegularExpression(pattern: "&(?!(#[0-9]{2,4}|[A-z]{2,6});)", options: NSRegularExpression.Options(rawValue: 0)) | ||
|
||
func escapeWithUnicodeEntities() -> String { | ||
let range = NSRange(location: 0, length: self.count) | ||
return String.escapeAmpRegExp.stringByReplacingMatches( | ||
in: self, | ||
options: NSRegularExpression.MatchingOptions(rawValue: 0), | ||
range: range, | ||
withTemplate: "&" | ||
) | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
Sources/XMLText/XMLParser/StandardXMLAttributesResolver.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// | ||
// StandardXMLAttributesResolver.swift | ||
// XMLText | ||
// | ||
// Implementation in this file is taken from | ||
// SwiftRichString repository on GitHub. | ||
// https://github.com/malcommac/SwiftRichString/ | ||
// | ||
// SwiftRichString | ||
// Elegant Strings & Attributed Strings Toolkit for Swift | ||
// | ||
// Created by Daniele Margutti. | ||
// Copyright © 2018 Daniele Margutti. All rights reserved. | ||
// | ||
// Web: http://www.danielemargutti.com | ||
// Email: [email protected] | ||
// Twitter: @danielemargutti | ||
|
||
import Foundation | ||
import SwiftUI | ||
|
||
class StandardXMLAttributesResolver { | ||
|
||
func applyDynamicAttributes( | ||
to attributedString: inout AttributedString, | ||
xmlStyle: XMLDynamicStyle | ||
) { | ||
let finalStyleToApply = Style() | ||
xmlStyle.enumerateAttributes { key, value in | ||
switch key { | ||
case "color": | ||
finalStyleToApply.foregroundColor = Color(hex: value) | ||
default: break | ||
} | ||
} | ||
self.styleForUnknownXMLTag( | ||
xmlStyle.tag, | ||
to: &attributedString, | ||
attributes: xmlStyle.xmlAttributes | ||
) | ||
attributedString = finalStyleToApply.add(to: attributedString) | ||
} | ||
|
||
func styleForUnknownXMLTag( | ||
_ tag: String, | ||
to attributedString: inout AttributedString, | ||
attributes: [String: String]? | ||
) { | ||
let finalStyleToApply = Style() | ||
switch tag { | ||
case "a": // href support | ||
if let href = attributes?["href"] { | ||
finalStyleToApply.link = URL(string: href) | ||
} | ||
default: | ||
break | ||
} | ||
attributedString = finalStyleToApply.add(to: attributedString) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// | ||
// XMLDynamicStyle.swift | ||
// XMLText | ||
// | ||
// Implementation in this file is taken from | ||
// SwiftRichString repository on GitHub. | ||
// https://github.com/malcommac/SwiftRichString/ | ||
// | ||
// SwiftRichString | ||
// Elegant Strings & Attributed Strings Toolkit for Swift | ||
// | ||
// Created by Daniele Margutti. | ||
// Copyright © 2018 Daniele Margutti. All rights reserved. | ||
// | ||
// Web: http://www.danielemargutti.com | ||
// Email: [email protected] | ||
// Twitter: @danielemargutti | ||
|
||
import Foundation | ||
|
||
class XMLDynamicStyle { | ||
|
||
// MARK: - Public Properties | ||
|
||
/// Tag read for this style. | ||
let tag: String | ||
|
||
/// Style found in receiver `TextStyleGroup` instance. | ||
let style: StyleProtocol? | ||
|
||
/// Attributes found in the xml tag. | ||
let xmlAttributes: [String: String]? | ||
|
||
// MARK: - Initialization | ||
|
||
init( | ||
tag: String, | ||
style: StyleProtocol?, | ||
xmlAttributes: [String: String]? | ||
) { | ||
self.tag = tag | ||
self.style = style | ||
self.xmlAttributes = xmlAttributes | ||
} | ||
|
||
func enumerateAttributes(_ handler: ((_ key: String, _ value: String) -> Void)) { | ||
guard let xmlAttributes = xmlAttributes else { | ||
return | ||
} | ||
|
||
xmlAttributes.keys.forEach { | ||
handler($0, xmlAttributes[$0]!) | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters