<commit_before>import Foundation
import UIKit


/// HTML Attachments: Represents unknown HTML
///
open class HTMLAttachment: NSTextAttachment {

    /// Internal Cached Image
    ///
    fileprivate var glyphImage: UIImage?

    /// Delegate
    ///
    weak var delegate: RenderableAttachmentDelegate?

    /// Name of the Root "Unknown" Tag
    ///
    open var rootTagName: String = "" {
        didSet {
            glyphImage = nil
        }
    }

    /// Raw Unknown HTML to be rendered
    ///
    open var rawHTML: String = "" {
        didSet {
            glyphImage = nil
        }
    }


    // MARK: - Initializers

    init() {
        super.init(data: nil, ofType: nil)
    }

    public required init?(coder aDecoder: NSCoder) {
        super.init(data: nil, ofType: nil)

        guard let rootTagName = aDecoder.decodeObject(forKey: Keys.rootTagName) as? String,
            let rawHTML = aDecoder.decodeObject(forKey: Keys.rawHTML) as? String
            else {
                return
        }

        self.rootTagName = rootTagName
        self.rawHTML = rawHTML
    }


    /// Returns the Pretty Printed version of the contained HTML
    ///
    open func prettyHTML() -> String {
        let inParser = Libxml2.In.HTMLConverter()
        let outParser = Libxml2.Out.HTMLConverter(prettyPrint: true)
        do {
            let inNode = try inParser.convert(rawHTML)
            return outParser.convert(inNode)
        } catch {
            NSLog("Error converting Raw HTML to Pretty Format: \(error)")
        }

        return rawHTML
    }

    // MARK: - NSCoder Methods

    open override func encode(with aCoder: NSCoder) {
        super.encode(with: aCoder)

        aCoder.encode(rootTagName, forKey: Keys.rootTagName)
        aCoder.encode(rawHTML, forKey: Keys.rawHTML)
    }



    // MARK: - NSTextAttachmentContainer

    override open func image(forBounds imageBounds: CGRect, textContainer: NSTextContainer?, characterIndex charIndex: Int) -> UIImage? {
        if let cachedImage = glyphImage, imageBounds.size.equalTo(cachedImage.size) {
            return cachedImage
        }

        glyphImage = delegate?.attachment(self, imageForSize: imageBounds.size)

        return glyphImage
    }

    override open func attachmentBounds(for textContainer: NSTextContainer?, proposedLineFragment lineFrag: CGRect, glyphPosition position: CGPoint, characterIndex charIndex: Int) -> CGRect {
        guard let bounds = delegate?.attachment(self, boundsForLineFragment: lineFrag) else {
            assertionFailure("Could not determine HTML Attachment Size")
            return .zero
        }

        return bounds
    }
}


// MARK: - Private Helpers
//
private extension HTMLAttachment {

    struct Keys {
        static let rootTagName  = "rootTagName"
        static let rawHTML      = "rawHTML"
    }
}
<commit_msg>HTMLAttachment: Nukes NSLog<commit_after>import Foundation
import UIKit


/// HTML Attachments: Represents unknown HTML
///
open class HTMLAttachment: NSTextAttachment {

    /// Internal Cached Image
    ///
    fileprivate var glyphImage: UIImage?

    /// Delegate
    ///
    weak var delegate: RenderableAttachmentDelegate?

    /// Name of the Root "Unknown" Tag
    ///
    open var rootTagName: String = "" {
        didSet {
            glyphImage = nil
        }
    }

    /// Raw Unknown HTML to be rendered
    ///
    open var rawHTML: String = "" {
        didSet {
            glyphImage = nil
        }
    }


    // MARK: - Initializers

    init() {
        super.init(data: nil, ofType: nil)
    }

    public required init?(coder aDecoder: NSCoder) {
        super.init(data: nil, ofType: nil)

        guard let rootTagName = aDecoder.decodeObject(forKey: Keys.rootTagName) as? String,
            let rawHTML = aDecoder.decodeObject(forKey: Keys.rawHTML) as? String
            else {
                return
        }

        self.rootTagName = rootTagName
        self.rawHTML = rawHTML
    }


    /// Returns the Pretty Printed version of the contained HTML
    ///
    open func prettyHTML() -> String {
        let inParser = Libxml2.In.HTMLConverter()
        let outParser = Libxml2.Out.HTMLConverter(prettyPrint: true)
        do {
            let inNode = try inParser.convert(rawHTML)
            return outParser.convert(inNode)
        } catch {
            assertionFailure("Error converting Raw HTML to Pretty Format: \(error)")
        }

        return rawHTML
    }

    // MARK: - NSCoder Methods

    open override func encode(with aCoder: NSCoder) {
        super.encode(with: aCoder)

        aCoder.encode(rootTagName, forKey: Keys.rootTagName)
        aCoder.encode(rawHTML, forKey: Keys.rawHTML)
    }



    // MARK: - NSTextAttachmentContainer

    override open func image(forBounds imageBounds: CGRect, textContainer: NSTextContainer?, characterIndex charIndex: Int) -> UIImage? {
        if let cachedImage = glyphImage, imageBounds.size.equalTo(cachedImage.size) {
            return cachedImage
        }

        glyphImage = delegate?.attachment(self, imageForSize: imageBounds.size)

        return glyphImage
    }

    override open func attachmentBounds(for textContainer: NSTextContainer?, proposedLineFragment lineFrag: CGRect, glyphPosition position: CGPoint, characterIndex charIndex: Int) -> CGRect {
        guard let bounds = delegate?.attachment(self, boundsForLineFragment: lineFrag) else {
            assertionFailure("Could not determine HTML Attachment Size")
            return .zero
        }

        return bounds
    }
}


// MARK: - Private Helpers
//
private extension HTMLAttachment {

    struct Keys {
        static let rootTagName  = "rootTagName"
        static let rawHTML      = "rawHTML"
    }
}

{'fraction_non_alphanumeric': 0.08818925030440077, 'fraction_numerical': 0.0006957731779439903, 'mean_word_length': 2.1820697288323188, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2967158', 'n_tokens_mistral': 1659, 'n_tokens_neox': 1566, 'n_words': 436}