[swift-users] Generating html code, best practise?
kelvin13ma at gmail.com
Sun May 21 15:05:18 CDT 2017
This problem isn’t all that different in Swift than it is in any other OO
language. As someone who has done a lot of this in the past, a good
strategy that worked is:
Create an “`HTMLNode`” superclass, and have inherited from it individual
tags such as `br`, `p`, `table`, `tr`, etc. In Swift it may be better to
use protocols and you can lower them to structs. Each tag struct should
have a few static properties indicating the tag’s ASCII name, whether or
not it’s allowed to contain text, and if you want, how the tag should be
indented. It should have two instance properties, a dictionary
`attributes:[String: String]`, and a contents vector. The vector is the
hardest thing to get right, since it contains all the children of the node,
which may include textual characters as well as node structs. If you are
not writing an editor, you may find it helpful to create a pseudotag struct
“text” that just contains a `String` so that your vector has a type
`HTMLNode` instead of `Any`. Alternatively you can use an `enum` with an
associated value to achieve a type-safe `Character`/`HTMLNode` union type.
You can easily build such a tree parsing an HTML file with two stacks.
Outputting the tree to a string is a little more complex if you want good
identation, but the main idea is you traverse the tree recursively, bumping
up the indentation level each time, and the emitter function can either
append a new String to the output vector, or append to the last String in
the output vector, depending on if the enclosing tag is allowed to contain
text, or if it can only contain other nodes.
To print the attributes inside the tags themselves, you want to use some
sort of “DNA” system to enforce type safety, and preserve canonical
ordering information, if desired.
On Fri, May 19, 2017 at 4:09 AM, Rien via swift-users <swift-users at swift.org
> I want to generate some HTML code with Swift. Of course there are many way
> to do this, but I am not looking to do an "in-dept all options open”
> Just some ‘get the html out quickly” type of thing.
> So I wonder, how do _you_ do this?
> Simply using Strings is one option, but cumbersome and without any
> Using some general purpose function (e.g. func tagged(tag: String,
> content: String) -> String) helps a little, but not very much.
> Enums are another possibility.
> Or var’s in a struct/class that wrap the html element etc.
> Many possibilities.
> Any suggestion is welcome, suggestions as wel as experiences.
> Of course if there is a solution out there, I’d like to know as well. (I
> didn’t find any though)
> Site: http://balancingrock.nl
> Blog: http://swiftrien.blogspot.com
> Github: http://github.com/Balancingrock
> Project: http://swiftfire.nl - A server for websites build in Swift
> swift-users mailing list
> swift-users at swift.org
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the swift-users