Skip to content

Commit

Permalink
Merge branch 'release/1.14.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
SergioEstevao committed Dec 4, 2019
2 parents e9cee5e + 1aed94a commit dc9e63a
Show file tree
Hide file tree
Showing 15 changed files with 349 additions and 28 deletions.
8 changes: 8 additions & 0 deletions Aztec.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,8 @@
F99D5CF321B989FF0089314A /* StringRangeMultibyteConversionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = F99D5CF221B989FF0089314A /* StringRangeMultibyteConversionTests.swift */; };
FF0714021EFD78AF00E50713 /* Media.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = FF0714001EFD78AF00E50713 /* Media.xcassets */; };
FF17B2D2227A589F0022AECE /* LIElementConverter.swift in Sources */ = {isa = PBXBuildFile; fileRef = FF17B2D1227A589F0022AECE /* LIElementConverter.swift */; };
FF1F56A123955EF600E93A30 /* ColorProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = FF1F56A023955EF600E93A30 /* ColorProvider.swift */; };
FF1F56A3239562B900E93A30 /* html_colors.json in Resources */ = {isa = PBXBuildFile; fileRef = FF1F56A2239562B900E93A30 /* html_colors.json */; };
FF20D6401EDC389A00294B78 /* ShortcodeAttribute.swift in Sources */ = {isa = PBXBuildFile; fileRef = FF20D63D1EDC389A00294B78 /* ShortcodeAttribute.swift */; };
FF20D6411EDC389A00294B78 /* HTMLProcessor.swift in Sources */ = {isa = PBXBuildFile; fileRef = FF20D63E1EDC389A00294B78 /* HTMLProcessor.swift */; };
FF20D6421EDC389A00294B78 /* Processor.swift in Sources */ = {isa = PBXBuildFile; fileRef = FF20D63F1EDC389A00294B78 /* Processor.swift */; };
Expand Down Expand Up @@ -499,6 +501,8 @@
F99D5CF221B989FF0089314A /* StringRangeMultibyteConversionTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StringRangeMultibyteConversionTests.swift; sourceTree = "<group>"; };
FF0714001EFD78AF00E50713 /* Media.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Media.xcassets; sourceTree = "<group>"; };
FF17B2D1227A589F0022AECE /* LIElementConverter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LIElementConverter.swift; sourceTree = "<group>"; };
FF1F56A023955EF600E93A30 /* ColorProvider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ColorProvider.swift; sourceTree = "<group>"; };
FF1F56A2239562B900E93A30 /* html_colors.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = html_colors.json; sourceTree = "<group>"; };
FF20D63D1EDC389A00294B78 /* ShortcodeAttribute.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ShortcodeAttribute.swift; sourceTree = "<group>"; };
FF20D63E1EDC389A00294B78 /* HTMLProcessor.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HTMLProcessor.swift; sourceTree = "<group>"; };
FF20D63F1EDC389A00294B78 /* Processor.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Processor.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -810,6 +814,7 @@
B5A99D831EBA073D00DED081 /* HTMLStorage.swift */,
F9982CF521877663001E606B /* TextViewPasteboardDelegate.swift */,
FF7EAEC3234D253B007A26E0 /* FontProvider.swift */,
FF1F56A023955EF600E93A30 /* ColorProvider.swift */,
);
path = TextKit;
sourceTree = "<group>";
Expand Down Expand Up @@ -1337,6 +1342,7 @@
isa = PBXGroup;
children = (
FF0714001EFD78AF00E50713 /* Media.xcassets */,
FF1F56A2239562B900E93A30 /* html_colors.json */,
);
path = Assets;
sourceTree = "<group>";
Expand Down Expand Up @@ -1486,6 +1492,7 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
FF1F56A3239562B900E93A30 /* html_colors.json in Resources */,
FF0714021EFD78AF00E50713 /* Media.xcassets in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down Expand Up @@ -1540,6 +1547,7 @@
FFD3C1732344DCA900AE8DA0 /* ForegroundColorElementAttributeConverter.swift in Sources */,
F1656FDE2152A6A6009C7E3A /* CiteStringAttributeConverter.swift in Sources */,
599F254B1D8BC9A1002871D6 /* String+RangeConversion.swift in Sources */,
FF1F56A123955EF600E93A30 /* ColorProvider.swift in Sources */,
599F25481D8BC9A1002871D6 /* Metrics.swift in Sources */,
914CD9E72200B2D200DDD5C9 /* BoldFormatter.swift in Sources */,
F1BDDDE72060403B000714E1 /* FigureFormatter.swift in Sources */,
Expand Down
150 changes: 150 additions & 0 deletions Aztec/Assets/html_colors.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
{
"aliceblue": "#f0f8ff",
"antiquewhite": "#faebd7",
"aqua": "#00ffff",
"aquamarine": "#7fffd4",
"azure": "#f0ffff",
"beige": "#f5f5dc",
"bisque": "#ffe4c4",
"black": "#000000",
"blanchedalmond": "#ffebcd",
"blue": "#0000ff",
"blueviolet": "#8a2be2",
"brown": "#a52a2a",
"burlywood": "#deb887",
"cadetblue": "#5f9ea0",
"chartreuse": "#7fff00",
"chocolate": "#d2691e",
"coral": "#ff7f50",
"cornflowerblue": "#6495ed",
"cornsilk": "#fff8dc",
"crimson": "#dc143c",
"cyan": "#00ffff",
"darkblue": "#00008b",
"darkcyan": "#008b8b",
"darkgoldenrod": "#b8860b",
"darkgray": "#a9a9a9",
"darkgreen": "#006400",
"darkgrey": "#a9a9a9",
"darkkhaki": "#bdb76b",
"darkmagenta": "#8b008b",
"darkolivegreen": "#556b2f",
"darkorange": "#ff8c00",
"darkorchid": "#9932cc",
"darkred": "#8b0000",
"darksalmon": "#e9967a",
"darkseagreen": "#8fbc8f",
"darkslateblue": "#483d8b",
"darkslategray": "#2f4f4f",
"darkslategrey": "#2f4f4f",
"darkturquoise": "#00ced1",
"darkviolet": "#9400d3",
"deeppink": "#ff1493",
"deepskyblue": "#00bfff",
"dimgray": "#696969",
"dimgrey": "#696969",
"dodgerblue": "#1e90ff",
"firebrick": "#b22222",
"floralwhite": "#fffaf0",
"forestgreen": "#228b22",
"fuchsia": "#ff00ff",
"gainsboro": "#dcdcdc",
"ghostwhite": "#f8f8ff",
"goldenrod": "#daa520",
"gold": "#ffd700",
"gray": "#808080",
"green": "#008000",
"greenyellow": "#adff2f",
"grey": "#808080",
"honeydew": "#f0fff0",
"hotpink": "#ff69b4",
"indianred": "#cd5c5c",
"indigo": "#4b0082",
"ivory": "#fffff0",
"khaki": "#f0e68c",
"lavenderblush": "#fff0f5",
"lavender": "#e6e6fa",
"lawngreen": "#7cfc00",
"lemonchiffon": "#fffacd",
"lightblue": "#add8e6",
"lightcoral": "#f08080",
"lightcyan": "#e0ffff",
"lightgoldenrodyellow": "#fafad2",
"lightgray": "#d3d3d3",
"lightgreen": "#90ee90",
"lightgrey": "#d3d3d3",
"lightpink": "#ffb6c1",
"lightsalmon": "#ffa07a",
"lightseagreen": "#20b2aa",
"lightskyblue": "#87cefa",
"lightslategray": "#778899",
"lightslategrey": "#778899",
"lightsteelblue": "#b0c4de",
"lightyellow": "#ffffe0",
"lime": "#00ff00",
"limegreen": "#32cd32",
"linen": "#faf0e6",
"magenta": "#ff00ff",
"maroon": "#800000",
"mediumaquamarine": "#66cdaa",
"mediumblue": "#0000cd",
"mediumorchid": "#ba55d3",
"mediumpurple": "#9370db",
"mediumseagreen": "#3cb371",
"mediumslateblue": "#7b68ee",
"mediumspringgreen": "#00fa9a",
"mediumturquoise": "#48d1cc",
"mediumvioletred": "#c71585",
"midnightblue": "#191970",
"mintcream": "#f5fffa",
"mistyrose": "#ffe4e1",
"moccasin": "#ffe4b5",
"navajowhite": "#ffdead",
"navy": "#000080",
"oldlace": "#fdf5e6",
"olive": "#808000",
"olivedrab": "#6b8e23",
"orange": "#ffa500",
"orangered": "#ff4500",
"orchid": "#da70d6",
"palegoldenrod": "#eee8aa",
"palegreen": "#98fb98",
"paleturquoise": "#afeeee",
"palevioletred": "#db7093",
"papayawhip": "#ffefd5",
"peachpuff": "#ffdab9",
"peru": "#cd853f",
"pink": "#ffc0cb",
"plum": "#dda0dd",
"powderblue": "#b0e0e6",
"purple": "#800080",
"rebeccapurple": "#663399",
"red": "#ff0000",
"rosybrown": "#bc8f8f",
"royalblue": "#4169e1",
"saddlebrown": "#8b4513",
"salmon": "#fa8072",
"sandybrown": "#f4a460",
"seagreen": "#2e8b57",
"seashell": "#fff5ee",
"sienna": "#a0522d",
"silver": "#c0c0c0",
"skyblue": "#87ceeb",
"slateblue": "#6a5acd",
"slategray": "#708090",
"slategrey": "#708090",
"snow": "#fffafa",
"springgreen": "#00ff7f",
"steelblue": "#4682b4",
"tan": "#d2b48c",
"teal": "#008080",
"thistle": "#d8bfd8",
"tomato": "#ff6347",
"turquoise": "#40e0d0",
"violet": "#ee82ee",
"wheat": "#f5deb3",
"white": "#ffffff",
"whitesmoke": "#f5f5f5",
"yellow": "#ffff00",
"yellowgreen": "#9acd32"
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class ForegroundColorElementAttributesConverter: ElementAttributeConverter {

guard let cssColor = attribute.firstCSSAttribute(ofType: .foregroundColor),
let colorValue = cssColor.value,
let color = UIColor(hexString: colorValue) else {
let color = ColorProvider.shared.color(named: colorValue) ?? UIColor(hexString: colorValue) else {
return attributes
}

Expand Down
32 changes: 30 additions & 2 deletions Aztec/Classes/Extensions/NSAttributedString+Lists.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,41 @@ extension NSAttributedString {
return resultRange
}

/// Returns the number of items of a list at the given location.
///
/// - Parameters:
/// - list: The list.
/// - location: The location of the item.
///
/// - Returns: Returns the total number of items within the list or NSNotFound if no list is available at location.
///
func numberOfItems(in list: TextList, at location: Int) -> Int {
guard
let paragraphStyle = attribute(.paragraphStyle, at: location, effectiveRange: nil) as? ParagraphStyle
else {
return NSNotFound
}
let listDepth = paragraphStyle.lists.count
guard let rangeOfList = range(of:list, at: location) else {
return NSNotFound
}
var numberInList = 0
let paragraphRanges = self.paragraphRanges(intersecting: rangeOfList)
for (_, enclosingRange) in paragraphRanges {
if let paragraphStyle = attribute(.paragraphStyle, at: enclosingRange.location, effectiveRange: nil) as? ParagraphStyle,
listDepth == paragraphStyle.lists.count {
numberInList += 1
}
}
return numberInList
}
/// Returns the index of the item at the given location within the list.
///
/// - Parameters:
/// - list: The list.
/// - location: The location of the item.
///
/// - Returns: Returns the index within the list.
/// - Returns: Returns the index within the list or NSNotFound is no list is available at location.
///
func itemNumber(in list: TextList, at location: Int) -> Int {
guard
Expand All @@ -86,7 +114,7 @@ extension NSAttributedString {
guard let rangeOfList = range(of:list, at: location) else {
return NSNotFound
}
var numberInList = 1
var numberInList = 0
let paragraphRanges = self.paragraphRanges(intersecting: rangeOfList)

for (_, enclosingRange) in paragraphRanges {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class CodeFormatter: AttributeFormatter {

// MARK: - Init

init(monospaceFont: UIFont = FontProvider.shared.monospaceFont, backgroundColor: UIColor = UIColor.lightGray) {
init(monospaceFont: UIFont = FontProvider.shared.monospaceFont, backgroundColor: UIColor = ColorProvider.shared.codeBackgroungColor) {
self.monospaceFont = monospaceFont
self.backgroundColor = backgroundColor
self.htmlRepresentationKey = .codeHtmlRepresentation
Expand Down
2 changes: 2 additions & 0 deletions Aztec/Classes/Libxml2/DOM/Data/AttributeType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,6 @@ extension AttributeType {
public static let src = AttributeType("src")
public static let style = AttributeType("style")
public static let target = AttributeType("target")
public static let reversed = AttributeType("reversed")
public static let start = AttributeType("start")
}
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,15 @@ private extension AttributedStringParser {
listElement = element.toElementNode()
} else {
listElement = ElementNode(type: listType)
if list.style == .ordered {
if list.reversed {
listElement.updateAttribute(named: "reversed", value: .none)
}

if let start = list.start {
listElement.updateAttribute(named: "start", value: .string("\(start)"))
}
}
}

return listElement
Expand Down
55 changes: 55 additions & 0 deletions Aztec/Classes/TextKit/ColorProvider.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import Foundation
import UIKit

/// A class that provides colors to the formatter in TextKit.
/// By default only the standard html colors are provided, but new color can be added by using the `add(namedColors:)` method.
public class ColorProvider {


private init() {

}

public static var shared = ColorProvider()

private lazy var htmlColors: [String: UIColor] = {
guard let colorDataURL = Bundle.aztecBundle.url(forResource: "html_colors", withExtension: "json"),
let data = try? Data(contentsOf: colorDataURL),
let object = try? JSONSerialization.jsonObject(with: data, options: .allowFragments),
let colorDict = object as? [String: String] else {
return [String:UIColor]()
}

return colorDict.compactMapValues { UIColor(hexString: $0) }
}()

private var extraColors = [String:UIColor]()

/// Adds extra named colors to be used by the `color(named:)` method.
/// If there is a name collision, the value in the added dictionary will overwrite the current color.
/// - Parameter namedColors: a dictionary of colors keyed by name
public func add(namedColors: [String: UIColor]) {
extraColors.merge(namedColors) { (base, other) -> UIColor in
return other
}
}

/// Returns a color designated by name or nil if the name is not available
/// By default the the color available are the ones defined in the HTML specification: https://www.w3schools.com/colors/colors_names.asp
/// - Parameter name: the name of the color
public func color(named name: String) -> UIColor? {
if let color = htmlColors[name.lowercased()] {
return color
}

return extraColors[name]
}

public lazy var codeBackgroungColor: UIColor = {
if #available(iOS 13.0, *) {
return .secondarySystemBackground
} else {
return .lightGray
}
}()
}
12 changes: 10 additions & 2 deletions Aztec/Classes/TextKit/LayoutManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,16 @@ private extension LayoutManager {

let glyphRange = self.glyphRange(forCharacterRange: enclosingRange, actualCharacterRange: nil)
let markerRect = rectForItem(range: glyphRange, origin: origin, paragraphStyle: paragraphStyle)
let markerNumber = textStorage.itemNumber(in: list, at: enclosingRange.location)

var markerNumber = textStorage.itemNumber(in: list, at: enclosingRange.location)
var start = list.start ?? 1
if list.reversed {
markerNumber = -markerNumber
if list.start == nil {
start = textStorage.numberOfItems(in: list, at: enclosingRange.location)
}
}
markerNumber += start

drawItem(number: markerNumber, in: markerRect, from: list, using: paragraphStyle, at: enclosingRange.location)
}
}
Expand Down
Loading

0 comments on commit dc9e63a

Please sign in to comment.