### Construct RSS Feeds in Swift using Plot Source: https://github.com/johnsundell/plot/blob/master/README.md Provides an example of using Plot's DSL to construct an RSS feed. It shows how to define items with properties like guid, title, and link. ```swift let rss = RSS( .item( .guid("https://mysite.com/post", .isPermaLink(true)), .title("My post"), .link("https://mysite.com/post") ) ) ``` -------------------------------- ### Create Podcast Feeds in Swift with Plot Source: https://github.com/johnsundell/plot/blob/master/README.md Demonstrates constructing a podcast feed using Plot's DSL. The example includes setting the podcast title, owner information, and an audio item with URL, size, and title. ```swift let podcastFeed = PodcastFeed( .title("My podcast"), .owner( .name("John Appleseed"), .email("john.appleseed@url.com") ), .item( .title("My first episode"), .audio( url: "https://mycdn.com/episode.mp3", byteSize: 79295410, title: "My first episode" ) ) ) ``` -------------------------------- ### Plot Project Structure Source: https://github.com/johnsundell/plot/blob/master/CONTRIBUTING.md Explains the organizational structure of the Plot project, differentiating between public API code and internal code. It also details the key files used for defining APIs within different document formats. ```General Project Structure: - API: For public-facing API code. - Internal: For purely internal types and functions. Document Format Files (e.g., HTML): - Main document file (e.g., HTML): Defines the format and Context types. - Elements file (e.g., HTMLElements): Defines DSL APIs for constructing elements. - Attributes file (e.g., HTMLAttributes): Defines DSL APIs for constructing attributes. - Components file (e.g., HTMLComponents): Defines higher-level components. This structure applies to other formats like XML, RSS, PodcastFeed, and SiteMap. ``` -------------------------------- ### Import Plot in Swift Source: https://github.com/johnsundell/plot/blob/master/README.md After installing Plot via Swift Package Manager, import the library into your Swift files to start using its functionality for building documents. ```swift import Plot ``` -------------------------------- ### Generate SiteMap XML in Swift using Plot Source: https://github.com/johnsundell/plot/blob/master/README.md Shows how to generate a SiteMap XML file using Plot's DSL. The example includes defining a URL entry with location, last modification date, change frequency, and priority. ```swift let siteMap = SiteMap( .url( .loc("https://mysite.com/post"), .lastmod(Date()), .changefreq(.daily), .priority(1) ) ) ``` -------------------------------- ### Install Plot using Swift Package Manager Source: https://github.com/johnsundell/plot/blob/master/README.md This snippet shows how to add the Plot package as a dependency to your Swift project using the Swift Package Manager. Ensure your Package.swift manifest includes the Plot repository URL and a version constraint. ```swift let package = Package( ... dependencies: [ .package(url: "https://github.com/johnsundell/plot.git", from: "0.9.0") ], ... ) ``` -------------------------------- ### Apply Attributes to HTML Elements with Plot Source: https://github.com/johnsundell/plot/blob/master/README.md This example demonstrates how to apply attributes, such as CSS classes and hrefs, to HTML elements using Plot in Swift. Attributes are added similarly to child elements, allowing for a fluid typing experience. ```Swift let html = HTML( .body( .a(.class("link"), .href("https://github.com"), "GitHub") ) ) ``` -------------------------------- ### Conditional Rendering with .if() in Plot Source: https://github.com/johnsundell/plot/blob/master/README.md Demonstrates how to conditionally render HTML elements using the .if() command in Plot's Node-based API and standard if statements in the Component-based API. Includes examples with and without an else clause for fallback content. ```swift let rating: Rating = ... // When using the Node-based API: let html = HTML(.body( .if(rating.hasEnoughVotes, .span("Average score: \(rating.averageScore)") ) )) // When using the Component API: let html = HTML { if rating.hasEnoughVotes { Span("Average score: \(rating.averageScore)") } } ``` ```swift // When using the Node-based API: let html = HTML(.body( .if(rating.hasEnoughVotes, .span("Average score: \(rating.averageScore)"), else: .span("Not enough votes yet.") ) )) // When using the Component API: let html = HTML { if rating.hasEnoughVotes { Span("Average score: \(rating.averageScore)") } else { Span("Not enough votes yet.") } } ``` -------------------------------- ### List Generation with .forEach() in Plot Source: https://github.com/johnsundell/plot/blob/master/README.md Shows how to generate lists of HTML elements from Swift sequences using Plot's Node-based .forEach() command and the Component-based API's List component or for loops. Demonstrates creating unordered lists with list items. ```swift let names: [String] = ... // When using the Node-based API: let html = HTML(.body( .h2("People"), .ul(.forEach(names) { .li(.class("name"), .text($0)) }) )) // When using the Component API: let html = HTML { H2("People") // Passing our array directly to List: List(names) { name in ListItem(name).class("name") } // Using a manual for loop within a List closure: List { for name in names { ListItem(name).class("name") } } } ``` -------------------------------- ### Generate Basic HTML Structure with Plot Source: https://github.com/johnsundell/plot/blob/master/README.md This snippet demonstrates how to create a basic HTML structure using Plot in Swift. It includes a head with a title and stylesheet, and a body with a heading and paragraph. Plot automatically adds metadata for CSS and social sharing. ```Swift let html = HTML( .head( .title("My website"), .stylesheet("styles.css") ), .body( .div( .h1("My website"), .p("Writing HTML in Swift is pretty great!") ) ) ) ``` -------------------------------- ### Render Individual Nodes and Components in Swift Source: https://github.com/johnsundell/plot/blob/master/README.md Demonstrates rendering single nodes or components independently using Plot. This allows for constructing parts of a larger document or using Plot for specific fragments. ```swift let header = Node.header( .h1("Title"), .span("Description") ) let string = header.render() ``` ```swift let header = Header { H1("Title") Span("Description") } let string = header.render() ``` -------------------------------- ### Define Custom HTML Elements and Attributes in Swift Source: https://github.com/johnsundell/plot/blob/master/README.md Demonstrates how to define and use custom HTML elements and attributes using both the Node-based and Component-based APIs in Swift. This is useful for free-form XML or when Plot lacks built-in support. ```swift let html = HTML(.body( .element(named: "custom", text: "Hello..."), .p( .attribute(named: "custom", value: "...world!") ) )) ``` ```swift let html = HTML { Element(name: "custom") { Text("Hello...") } Paragraph().attribute( named: "custom", value: "...world!" ) } ``` -------------------------------- ### Render HTML Documents with Indentation in Swift Source: https://github.com/johnsundell/plot/blob/master/README.md Illustrates how to render an HTML document constructed with Plot into a String. The rendering can be done without indentation, or indented using spaces or tabs. ```swift let html = HTML(...) let nonIndentedString = html.render() let spacesIndentedString = html.render(indentedBy: .spaces(4)) let tabsIndentedString = html.render(indentedBy: .tabs(1)) ``` -------------------------------- ### Component Integration: Embedding in HTML Source: https://github.com/johnsundell/plot/blob/master/README.md Shows how to integrate a custom 'NewsArticle' component into a Plot-generated HTML structure using the .component API. ```Swift func newsArticlePage(for article: NewsArticle) -> HTML { return HTML(.body( .div( .class("wrapper"), .component(article) ) )) } ``` -------------------------------- ### Component: Mixing Nodes and Components Source: https://github.com/johnsundell/plot/blob/master/README.md Demonstrates how to mix Node-based elements directly within a Component's body, showcasing the flexibility of Plot's API. ```Swift struct Banner: Component { var title: String var imageURL: URLRepresentable var body: Component { Div { Node.h2(.text(title)) Image(imageURL) } .class("banner") } } ``` -------------------------------- ### Optional Unwrapping with .unwrap() in Plot Source: https://github.com/johnsundell/plot/blob/master/README.md Illustrates inline unwrapping of optional values using Plot's Node-based .unwrap() command and Swift's if let construct in the Component-based API. Shows how to handle nil cases with an else clause. ```swift let user: User? = loadUser() // When using the Node-based API: let html = HTML(.body( .unwrap(user) { .p("Hello, \($0.name)") } )) // When using the Component API: let html = HTML { if let user = user { Paragraph("Hello, \(user.name)") } } ``` ```swift let user: User? = loadUser() // When using the Node-based API: let html = HTML(.body( .unwrap(user, { .p("Hello, \($0.name)") }, else: .text("Please log in") ) )) // When using the Component API: let html = HTML { if let user = user { Paragraph("Hello, \(user.name)") } else { Text("Please log in") } } ``` -------------------------------- ### Render HTML with Tables and Lists using Plot Source: https://github.com/johnsundell/plot/blob/master/README.md This Swift code snippet showcases Plot's ability to render more complex HTML structures, including tables and unordered lists. It highlights Plot's composition capabilities, allowing elements and text to be nested and combined seamlessly. ```Swift let html = HTML( .body( .h2("Countries and their capitals"), .table( .tr(.th("Country"), .th("Capital")), .tr(.td("Sweden"), .td("Stockholm")), .tr(.td("Japan"), .td("Tokyo")) ), .h2("List of ", .strong("programming languages")), .ul( .li("Swift"), .li("Objective-C"), .li("C") ) ) ) ``` -------------------------------- ### Component: Reusable News Article Source: https://github.com/johnsundell/plot/blob/master/README.md Defines a reusable 'NewsArticle' component using Plot's Component protocol. This component renders an article with an image, title, and description, and applies CSS classes. ```Swift struct NewsArticle: Component { var imagePath: String var title: String var description: String var body: Component { Article { Image(url: imagePath, description: "Header image") H1(title) Span(description).class("description") } .class("news") } } ``` -------------------------------- ### Swift: Pass Class Name via Environment Source: https://github.com/johnsundell/plot/blob/master/README.md This Swift code demonstrates passing a custom class name down a component hierarchy using Plot's environment API. It defines a custom `EnvironmentKey` for the class name, injects it using `.environmentValue` in a parent component (`Page`), and retrieves it using `@EnvironmentValue` in a child component (`ActionButton`) to apply the class to a `Button`. ```swift extension EnvironmentKey where Value == String { static var actionButtonClass: Self { Self(defaultValue: "action-button") } } struct Page: Component { var body: Component { Div { InfoView(title: "...", text: "...") } .environmentValue("action-button-large", key: .actionButtonClass ) } } struct InfoView: Component { var title: String var text: String var body: Component { Div { H2(title) Paragraph(text) ActionButton(title: "OK") ActionButton(title: "Cancel") } .class("info-view") } } struct ActionButton: Component { var title: String @EnvironmentValue(.actionButtonClass) var className var body: Component { Button(title).class(className) } } ``` -------------------------------- ### Extend Plot for Type-Safe Custom XML Nodes in Swift Source: https://github.com/johnsundell/plot/blob/master/README.md Shows how to extend Plot's XML support to create type-safe custom elements and attributes. This involves defining a new context type and adding DSL APIs to the Node type. ```swift extension XML { enum ProductContext {} } extension Node where Context == XML.DocumentContext { static func product(_ nodes: Node...) -> Self { .element(named: "product", nodes: nodes) } } extension Node where Context == XML.ProductContext { static func name(_ name: String) -> Self { .element(named: "name", text: name) } static func isAvailable(_ bool: Bool) -> Self { .attribute(named: "available", value: String(bool)) } } ``` -------------------------------- ### Type Safety: Invalid Element in List Source: https://github.com/johnsundell/plot/blob/master/README.md Illustrates a compiler error when placing a

element inside a