Skip to content

Commit

Permalink
Merge branch 'release/1.0.0-beta.9'
Browse files Browse the repository at this point in the history
  • Loading branch information
SergioEstevao committed Jul 27, 2017
2 parents 1f86596 + 48a63a3 commit 2380e07
Show file tree
Hide file tree
Showing 94 changed files with 2,579 additions and 2,297 deletions.
9 changes: 9 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
osx_image: xcode8.3
language: objective-c
xcode_workspace: Aztec.xcworkspace
xcode_scheme: AztecExample
xcode_sdk: iphonesimulator
before_script:
- cd Example && carthage update && cd -
script:
- Scripts/build.sh
128 changes: 43 additions & 85 deletions Aztec.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

5 changes: 2 additions & 3 deletions Aztec/Classes/Constants/Metrics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ import Foundation
enum Metrics {

static let defaultIndentation = CGFloat(12)
static let maxIndentation = CGFloat(200)
static let listBulletIndentation = CGFloat(20)
static let listTextIndentation = CGFloat(24)
static let maxIndentation = CGFloat(200)
static let listTextIndentation = CGFloat(16)
static let tabStepInterval = 8
static let tabStepCount = 12
static let paragraphSpacing = CGFloat(6)
Expand Down
62 changes: 22 additions & 40 deletions Aztec/Classes/Converters/HTMLNodeToNSAttributedString.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,6 @@ import UIKit

class HTMLNodeToNSAttributedString: SafeConverter {

typealias ElementNode = Libxml2.ElementNode
typealias Node = Libxml2.Node
typealias RootNode = Libxml2.RootNode
typealias StringAttribute = Libxml2.StringAttribute
typealias TextNode = Libxml2.TextNode
typealias CommentNode = Libxml2.CommentNode
typealias StandardElementType = Libxml2.StandardElementType

/// The default font descriptor that will be used as a base for conversions.
///
let defaultFontDescriptor: UIFontDescriptor
Expand Down Expand Up @@ -139,7 +131,7 @@ class HTMLNodeToNSAttributedString: SafeConverter {
}

fileprivate func convert(unsupported element: ElementNode, inheriting attributes: [String:Any]) -> NSAttributedString {
let converter = Libxml2.Out.HTMLConverter()
let converter = OutHTMLConverter()
let attachment = HTMLAttachment()

attachment.rootTagName = element.name
Expand Down Expand Up @@ -216,6 +208,8 @@ class HTMLNodeToNSAttributedString: SafeConverter {
.video: VideoFormatter()
]

let attributesToFormattersMap: [StandardHTMLAttribute: AttributeFormatter] = [:]

public let styleToFormattersMap: [String: (AttributeFormatter, (String)->Any?)] = [
"color": (ColorFormatter(), {(value) in return UIColor(hexString: value)}),
"text-decoration": (UnderlineFormatter(), { (value) in return value == "underline" ? NSUnderlineStyle.styleSingle.rawValue : nil})
Expand Down Expand Up @@ -255,42 +249,28 @@ private extension HTMLNodeToNSAttributedString {
return attributes
}

let elementRepresentation = HTMLElementRepresentation(for: element)
return self.attributes(for: elementRepresentation, inheriting: attributes)
}

/// Calculates the attributes for the specified element representation. Returns a dictionary
/// including inherited attributes.
///
/// - Parameters:
/// - elementRepresentation: the element representation.
/// - inheritedAttributes: the attributes that will be inherited.
///
/// - Returns: an attributes dictionary, for use in an NSAttributedString.
///
private func attributes(for elementRepresentation: HTMLElementRepresentation, inheriting attributes: [String: Any]) -> [String: Any] {

let representation = HTMLRepresentation(for: .element(HTMLElementRepresentation(element)))
var finalAttributes = attributes

if let elementFormatter = formatter(for: elementRepresentation) {
finalAttributes = elementFormatter.apply(to: finalAttributes, andStore: elementRepresentation)
} else if elementRepresentation.name == StandardElementType.li.rawValue {
if let elementFormatter = formatter(for: element) {
finalAttributes = elementFormatter.apply(to: finalAttributes, andStore: representation)
} else if element.name == StandardElementType.li.rawValue {
// ^ Since LI is handled by the OL and UL formatters, we can safely ignore it here.

finalAttributes = attributes
} else {
finalAttributes = self.attributes(storing: elementRepresentation, in: finalAttributes)
finalAttributes = self.attributes(storing: element, in: finalAttributes)
}

for attributeRepresentation in elementRepresentation.attributes {
finalAttributes = self.attributes(for: attributeRepresentation, inheriting: finalAttributes)
for attribute in element.attributes {
finalAttributes = self.stringAttributes(for: attribute, inheriting: finalAttributes)
}

return finalAttributes
}


/// Calculates the attributes for the specified element representation. Returns a dictionary
/// Calculates the string attributes for the specified HTML attribute. Returns a dictionary
/// including inherited attributes.
///
/// - Parameters:
Expand All @@ -299,12 +279,14 @@ private extension HTMLNodeToNSAttributedString {
///
/// - Returns: an attributes dictionary, for use in an NSAttributedString.
///
private func attributes(for attributeRepresentation: HTMLAttributeRepresentation, inheriting inheritedAttributes: [String: Any]) -> [String: Any] {
private func stringAttributes(for attribute: Attribute, inheriting inheritedAttributes: [String: Any]) -> [String: Any] {

let attributes: [String:Any]

if let attributeFormatter = formatter(for: attributeRepresentation) {
attributes = attributeFormatter.apply(to: inheritedAttributes, andStore: attributeRepresentation)
if let attributeFormatter = formatter(for: attribute) {
let attributeHTMLRepresentation = HTMLRepresentation(for: .attribute(attribute))

attributes = attributeFormatter.apply(to: inheritedAttributes, andStore: attributeHTMLRepresentation)
} else {
attributes = inheritedAttributes
}
Expand All @@ -321,9 +303,9 @@ private extension HTMLNodeToNSAttributedString {
///
/// - Returns: A collection of NSAttributedString Attributes, including the specified HTMLElementRepresentation.
///
private func attributes(storing elementRepresentation: HTMLElementRepresentation, in attributes: [String: Any]) -> [String: Any] {
private func attributes(storing element: ElementNode, in attributes: [String: Any]) -> [String: Any] {
let unsupportedHTML = attributes[UnsupportedHTMLAttributeName] as? UnsupportedHTML ?? UnsupportedHTML()
unsupportedHTML.add(element: elementRepresentation)
unsupportedHTML.append(element: element)

var updated = attributes
updated[UnsupportedHTMLAttributeName] = unsupportedHTML
Expand All @@ -336,15 +318,15 @@ extension HTMLNodeToNSAttributedString {

// MARK: - Formatters

func formatter(for representation: HTMLAttributeRepresentation) -> AttributeFormatter? {
func formatter(for attribute: Attribute) -> AttributeFormatter? {
// TODO: implement attribute representation formatters
//
return nil
}

func formatter(for representation: HTMLElementRepresentation) -> AttributeFormatter? {
func formatter(for element: ElementNode) -> AttributeFormatter? {

guard let standardType = StandardElementType(rawValue: representation.name) else {
guard let standardType = element.standardName else {
return nil
}

Expand Down
5 changes: 1 addition & 4 deletions Aztec/Classes/Converters/HTMLToAttributedString.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ import UIKit

class HTMLToAttributedString: SafeConverter {

typealias RootNode = Libxml2.RootNode
typealias TextNode = Libxml2.TextNode

/// The default font descriptor that will be used as a base for conversions.
///
let defaultFontDescriptor: UIFontDescriptor
Expand All @@ -15,7 +12,7 @@ class HTMLToAttributedString: SafeConverter {
}

func convert(_ html: String) -> (rootNode: RootNode, attributedString: NSAttributedString) {
let htmlToNode = Libxml2.In.HTMLConverter()
let htmlToNode = InHTMLConverter()
let nodeToAttributedString = HTMLNodeToNSAttributedString(usingDefaultFontDescriptor: defaultFontDescriptor)

let rootNode = htmlToNode.convert(html)
Expand Down
Loading

0 comments on commit 2380e07

Please sign in to comment.