From 58a8419984e7444c3000f61e6bf097e832ce3b04 Mon Sep 17 00:00:00 2001 From: Isaac Paul Date: Sat, 25 Oct 2025 00:04:37 -0400 Subject: [PATCH] Updates and fixes --- CLA.txt | 97 ++++++++++++++++++++++++++++++++ LICENSE | 2 +- Package.resolved | 14 +++++ Package.swift | 20 +++++-- README.txt | 13 +++-- Sources/HRW/BaseComponents.swift | 37 ++++++++++++ Sources/HRW/HTMLParser.swift | 2 +- Sources/HRW/swiftxml.swift | 2 +- 8 files changed, 174 insertions(+), 13 deletions(-) create mode 100644 CLA.txt create mode 100644 Package.resolved diff --git a/CLA.txt b/CLA.txt new file mode 100644 index 0000000..903865f --- /dev/null +++ b/CLA.txt @@ -0,0 +1,97 @@ +You accept and agree to the following terms and conditions for Your +Contributions (present and future) that you submit to the Grantee. In +return, the Grantee shall not use Your Contributions in a way that +is contrary to the public benefit. Except for the license granted +herein to the Grantee and recipients of software distributed by the +Grantee, You reserve all right, title, and interest in and to Your +Contributions. + +1. Definitions. +"The Grantee" shall mean Isaac Roy Paul (a.k.a izackp@gmail.com), a +individual with its principal place of business in Florida, USA. +"You" (or "Your") shall mean the copyright owner or legal entity +authorized by the copyright owner that is making this Agreement +with the Grantee. For legal entities, the entity making a +Contribution and all other entities that control, are controlled +by, or are under common control with that entity are considered to +be a single Contributor. For the purposes of this definition, +"control" means (i) the power, direct or indirect, to cause the +direction or management of such entity, whether by contract or +otherwise, or (ii) ownership of fifty percent (50%) or more of the +outstanding shares, or (iii) beneficial ownership of such entity. +"Contribution" shall mean any original work of authorship, +including any modifications or additions to an existing work, that +is intentionally submitted by You to the Grantee for inclusion +in, or documentation of, any of the products owned or managed by +the Grantee (the "Work"). For the purposes of this definition, +"submitted" means any form of electronic, verbal, or written +communication sent to the Grantee or its representatives, +including but not limited to communication on electronic mailing +lists, source code control systems, and issue tracking systems that +are managed by, or on behalf of, the Grantee for the purpose of +discussing and improving the Work, but excluding communication that +is conspicuously marked or otherwise designated in writing by You +as "Not a Contribution." + +2. Grant of Copyright License. Subject to the terms and conditions of +this Agreement, You hereby grant to the Grantee and to +recipients of software distributed by the Grantee a perpetual, +worldwide, non-exclusive, no-charge, royalty-free, irrevocable +copyright license to reproduce, prepare derivative works of, +publicly display, publicly perform, sublicense, and distribute Your +Contributions and such derivative works. + +3. Grant of Patent License. Subject to the terms and conditions of +this Agreement, You hereby grant to the Grantee and to +recipients of software distributed by the Grantee a perpetual, +worldwide, non-exclusive, no-charge, royalty-free, irrevocable +(except as stated in this section) patent license to make, have +made, use, offer to sell, sell, import, and otherwise transfer the +Work, where such license applies only to those patent claims +licensable by You that are necessarily infringed by Your +Contribution(s) alone or by combination of Your Contribution(s) +with the Work to which such Contribution(s) was submitted. If any +entity institutes patent litigation against You or any other entity +(including a cross-claim or counterclaim in a lawsuit) alleging +that your Contribution, or the Work to which you have contributed, +constitutes direct or contributory patent infringement, then any +patent licenses granted to that entity under this Agreement for +that Contribution or Work shall terminate as of the date such +litigation is filed. + +4. You represent that you are legally entitled to grant the above +license. If your employer(s) has rights to intellectual property +that you create that includes your Contributions, you represent +that you have received permission to make Contributions on behalf +of that employer, that your employer has waived such rights for +your Contributions to the Grantee, or that your employer has +executed a separate Corporate CLA with the Grantee. + +5. You represent that each of Your Contributions is Your original +creation (see section 7 for submissions on behalf of others). You +represent that Your Contribution submissions include complete +details of any third-party license or other restriction (including, +but not limited to, related patents and trademarks) of which you +are personally aware and which are associated with any part of Your +Contributions. + +6. You are not expected to provide support for Your Contributions, +except to the extent You desire to provide support. You may provide +support for free, for a fee, or not at all. Unless required by +applicable law or agreed to in writing, You provide Your +Contributions on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS +OF ANY KIND, either express or implied, including, without +limitation, any warranties or conditions of TITLE, NON- +INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. + +7. Should You wish to submit work that is not Your original creation, +You may submit it to the Grantee separately from any +Contribution, identifying the complete details of its source and of +any license or other restriction (including, but not limited to, +related patents, trademarks, and license agreements) of which you +are personally aware, and conspicuously marking the work as +"Submitted on behalf of a third-party: [named here]". + +8. You agree to notify the Grantee of any facts or circumstances of +which you become aware that would make these representations +inaccurate in any respect. diff --git a/LICENSE b/LICENSE index cc6f753..393fc6f 100644 --- a/LICENSE +++ b/LICENSE @@ -33,7 +33,7 @@ URL for them above, as well as copies of any plain-text lines beginning with `Required Notice:` that the licensor provided with the software. For example: -> Required Notice: Copyright Yoyodyne, Inc. (http://example.com) +> Required Notice: Copyright Isaac Paul ## Changes and New Works License diff --git a/Package.resolved b/Package.resolved new file mode 100644 index 0000000..49d3304 --- /dev/null +++ b/Package.resolved @@ -0,0 +1,14 @@ +{ + "pins" : [ + { + "identity" : "swift-argument-parser", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-argument-parser", + "state" : { + "revision" : "309a47b2b1d9b5e991f36961c983ecec72275be3", + "version" : "1.6.1" + } + } + ], + "version" : 2 +} diff --git a/Package.swift b/Package.swift index f93606c..78baf41 100644 --- a/Package.swift +++ b/Package.swift @@ -1,4 +1,4 @@ -// swift-tools-version:5.9 +// swift-tools-version:6.1.0 // // Package.swift @@ -16,19 +16,29 @@ let package = Package( .library( name: "HRW", targets: ["HRW"] - ) + ), ], dependencies: [ + .package(path: "/Users/isaacpaul/Projects/swift-projects/GenHTML5") ], targets: [ .target( name: "HRW", - dependencies: [ - ] + dependencies: [ ] ), .testTarget( name: "HRWTests", - dependencies: ["HRW"] + dependencies: [ + "HRW"], + sources: ["example.html"], + plugins: [.plugin(name: "BindingPlugin", package: "genhtml5")] ) ] ) +/* + + Showing All Messages + product 'BindingGenerator' required by package 'hrw' target 'HRWTests' not found. Did you mean ''? + + + */ diff --git a/README.txt b/README.txt index c238f59..c966851 100644 --- a/README.txt +++ b/README.txt @@ -1,10 +1,10 @@ ## HTML Reader Writer (H-RW) -The goal is to be able to load html into swift from a file, manipulate it, then spit out html while retaining the benefits of a type safe language which serves as an alternative to templating. +The goal is to be able to load html into swift from a file, manipulate it, then spit out html while retaining the benefits of a type safe language which serves as an alternative to templating. A BindingPlugin is also provided, this will analyze the html files and produce a swift file that will let you access html elements with IDs while retaining type infomation. Similar to Android's ViewBinding. #### Thoughts -I suppose I wanted to be able to use plain html as is. In hindsight, it was too much effort for the payoff. This is mostly so I can build multipage websites with swift without the use of JS as a silly experiement. +I suppose I wanted to be able to use plain html as is. In hindsight, it was too much effort for the payoff. This is mostly so I can build multipage websites with swift without the use of JS as a silly experiment. There is also a HTML library called Plot that also lets you manipulate html, but it doesn't let you load it in from a file and treats HTML generations similarly to SwiftUI. @@ -12,11 +12,14 @@ Perhaps this project can be repurposed as an LSP for html, but that's not much o There is also an HTML parser in LadyBird thats partially written in Swift. It seems to call into c++ for a handful of things, so it doesn't seem to be reusable without that. - ## License -The license is a modified version of the PolyForm Noncommercial License (1.0.0) to add more non-commerical and non-ai use stipulations. I am open relicensing. +The license is a modified version of the PolyForm Noncommercial License (1.0.0) to add more non-commercial and non-ai use stipulations. Basically, if you're not making money then it's free to use. Depending on the project, I am open to distributing this source under a different license. ## Contributions -All contributors must sign an CLA as I do not wish to restrict myself in the use of the code or future relicensing endevors. +All contributors must agree to the CLA within. It is primarily based on Apache's ICLA. + +By signing off your git commits you are agreeing the CLA within the repository inside the file CLA.txt. + +You can sign off your commits via the signoff flag `git commit --signoff` diff --git a/Sources/HRW/BaseComponents.swift b/Sources/HRW/BaseComponents.swift index dc3cd7c..69bc6c9 100644 --- a/Sources/HRW/BaseComponents.swift +++ b/Sources/HRW/BaseComponents.swift @@ -147,6 +147,43 @@ public class HTMLNode : XMLNode, IGlobalContainer { return nil } + public func iterate(_ index:Int, _ skipTextNodes:Bool, _ cb:(HTMLNode, Int)->()) -> Int { + if (skipTextNodes && self is HTMLText) { + return index + } + cb(self, index) + var newIndex = index + 1 + for eachChild in children { + newIndex = eachChild.iterate(newIndex, skipTextNodes, cb) + } + return newIndex + } + + public func flatten() -> [HTMLNode] { + let count = countElements() + let result:[HTMLNode] = Array(unsafeUninitializedCapacity: count+1) { buffer, initializedCount in + initializedCount = self.addTo(array: &buffer, index: 0) + } + return result + } + + public func countElements() -> Int { + var result = 1 + for eachChild in children { + result += eachChild.countElements() + } + return result + } + + private func addTo(array:inout UnsafeMutableBufferPointer, index:Int) -> Int { + array.initializeElement(at: index, to: self) + var newIndex = index + 1 + for eachChild in children { + newIndex = eachChild.addTo(array: &array, index: newIndex) + } + return newIndex + } + public override func renderAttributes() -> String { var result = super.renderAttributes() var first = result.count == 0 diff --git a/Sources/HRW/HTMLParser.swift b/Sources/HRW/HTMLParser.swift index 5dbfc9c..a30ad20 100644 --- a/Sources/HRW/HTMLParser.swift +++ b/Sources/HRW/HTMLParser.swift @@ -27,7 +27,7 @@ public class HTMLParser { } */ //TODO: Not really an html node.. -public class HTMLText : HTMLNode, IFlowContent { +public class HTMLText : HTMLNode, IFlowContent, IPhrasing, IFlow { public var content: String diff --git a/Sources/HRW/swiftxml.swift b/Sources/HRW/swiftxml.swift index 0ecffd7..86d0e03 100644 --- a/Sources/HRW/swiftxml.swift +++ b/Sources/HRW/swiftxml.swift @@ -385,7 +385,7 @@ public class XMLParser return String(cString: buffer) }*/ - init?(str:String) { + public init?(str:String) { iterator = str.unicodeScalars.makeIterator() position = Position() }