<commit_before>import Foundation

// let read = try! String(contentsOfFile: "/Users/zef/code/Fly/HTMLTags.swift")
func write(text: String) {
    let path = "HTMLViewTags.swift"
    do {
        try text.writeToFile(path, atomically: false, encoding: NSUTF8StringEncoding)
    } catch {
        print("file write failed")
    }
}

extension String {
    mutating func addLine(content: String, indent: Int = 0) {
        var indentation = ""
        for _ in 0..<indent where !content.isEmpty {
            indentation += "    "
        }
        self += "\n" + indentation + content
    }
    var quoted: String {
        return "\"\(self)\""
    }
}

struct TagDefinition {
    let tag: String

    static let voidElements = ["area", "base", "br", "col", "embed", "hr", "img", "input", "keygen", "link", "meta", "param", "source", "track", "wbr"]
    var isVoid: Bool {
        return TagDefinition.voidElements.contains(tag)
    }

    var methodDefinition: String {
        var def = ""

        def.addLine("public func \(tag.capitalizedString)(attributes attributes: HTMLAttributes = HTMLAttributes(), id: String? = nil, classes: [String]? = nil, data: HTMLAttributes? = nil, _ content: [HTMLElement]) -> Tag {")
        def.addLine("return Tag(\(tag.quoted), id: id, classes: classes, data: data, attributes: attributes, content)", indent: 1)
        def.addLine("}")

        def.addLine("public func \(tag.capitalizedString)(content: HTMLElement, id: String? = nil, classes: [String]? = nil, data: HTMLAttributes? = nil,  attributes: HTMLAttributes = HTMLAttributes()) -> Tag {")
        def.addLine("return Tag(\(tag.quoted), id: id, classes: classes, data: data, attributes: attributes, [content])", indent: 1)
        def.addLine("}")

        def.addLine("")
        return def
    }
}


// to handle manually:
// a link img video input

let basicTagGroups = [
    "html head body",
    "div span",
    "article aside header nav main section",
    "h1 h2 h3 h4 h5 h6",
    "p strong em i",
    "ul ol li",
]

var tags = basicTagGroups.reduce([TagDefinition]()) { tags, group in
    var tags = tags
    tags.appendContentsOf(group.componentsSeparatedByString(" ").map { TagDefinition(tag: $0) } )
    return tags
}

var code = "// This file is auto-generated, editing by hand is not recommended"
code.addLine("")
code.addLine("extension HTMLView {")
for tag in tags {
    for line in tag.methodDefinition.componentsSeparatedByString("\n") {
        code.addLine(line, indent: 1)
    }
}
code.addLine("}")

write(code)
<commit_msg>Write HTMLTags file to playground sources too<commit_after>import Foundation

// let read = try! String(contentsOfFile: "/Users/zef/code/Fly/HTMLTags.swift")
func write(text: String, path: String) {
    do {
        try text.writeToFile(path, atomically: false, encoding: NSUTF8StringEncoding)
    } catch {
        print("file write failed")
    }
}

extension String {
    mutating func addLine(content: String, indent: Int = 0) {
        var indentation = ""
        for _ in 0..<indent where !content.isEmpty {
            indentation += "    "
        }
        self += "\n" + indentation + content
    }
    var quoted: String {
        return "\"\(self)\""
    }
}

struct TagDefinition {
    let tag: String

    static let voidElements = ["area", "base", "br", "col", "embed", "hr", "img", "input", "keygen", "link", "meta", "param", "source", "track", "wbr"]
    var isVoid: Bool {
        return TagDefinition.voidElements.contains(tag)
    }

    var methodDefinition: String {
        var def = ""

        def.addLine("public func \(tag.capitalizedString)(attributes attributes: HTMLAttributes = HTMLAttributes(), id: String? = nil, classes: [String]? = nil, data: HTMLAttributes? = nil, _ content: [HTMLElement]) -> Tag {")
        def.addLine("return Tag(\(tag.quoted), id: id, classes: classes, data: data, attributes: attributes, content)", indent: 1)
        def.addLine("}")

        def.addLine("public func \(tag.capitalizedString)(content: HTMLElement, id: String? = nil, classes: [String]? = nil, data: HTMLAttributes? = nil,  attributes: HTMLAttributes = HTMLAttributes()) -> Tag {")
        def.addLine("return Tag(\(tag.quoted), id: id, classes: classes, data: data, attributes: attributes, [content])", indent: 1)
        def.addLine("}")

        def.addLine("")
        return def
    }
}


// to handle manually:
// a link img video input

let basicTagGroups = [
    "html head body",
    "div span",
    "article aside header nav main section",
    "h1 h2 h3 h4 h5 h6",
    "p strong em i",
    "ul ol li",
]

var tags = basicTagGroups.reduce([TagDefinition]()) { tags, group in
    var tags = tags
    tags.appendContentsOf(group.componentsSeparatedByString(" ").map { TagDefinition(tag: $0) } )
    return tags
}

var code = "// This file is auto-generated, editing by hand is not recommended"
code.addLine("")
code.addLine("extension HTMLView {")
for tag in tags {
    for line in tag.methodDefinition.componentsSeparatedByString("\n") {
        code.addLine(line, indent: 1)
    }
}
code.addLine("}")

write(code, path: "HTMLViewTags.swift")
write(code, path: "../HTMLBuilder.playground/Sources/HTMLTags.swift")


{'fraction_non_alphanumeric': 0.1480053960300636, 'fraction_numerical': 0.005010599344767778, 'mean_word_length': 3.125596184419714, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '2342448', 'n_tokens_mistral': 1613, 'n_tokens_neox': 1581, 'n_words': 462}