diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c12edaf --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.DS_Store +/.build +/.swiftpm +/*.xcodeproj diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..6992f1d --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,68 @@ +# Plot Contribution Guide + +Welcome to the *Plot Contribution Guide* — a document that aims to give you all the information you need to contribute to the Plot project. Thanks for your interest in contributing to this project, and hopefully this document will help make it as smooth and enjoyable as possible to do so. + +## Bugs, feature requests and support + +Plot doesn’t use GitHub issues, so all form of support — whether that’s asking a question, reporting a bug, or discussing a feature request — takes place in Pull Requests. + +The idea behind this workflow is to encourage more people using Plot to dive into the source code and familiarize themselves with how it works, in order to better be able to *self-service* on bugs and issues — hopefully leading to a better and smoother experience for everyone involved. + +### I found a bug, how do I report it? + +If you find a bug, for example an element or attribute that doesn’t render correctly, here’s the recommended workflow: + +1. Come up with the simplest code possible that reproduces the issue in isolation. +2. Write a test case using the code that reproduces the issue. See [Plot’s existing test suite](Tests/PlotTests) for inspiration on how to get started, or [this article](https://www.swiftbysundell.com/basics/unit-testing) if you want to learn some of the basics of unit testing in Swift. +3. Either fix the bug yourself, or simply submit your failing test as a Pull Request, and we can work on a solution together. + +While doing the above does require a bit of extra work for the person who found the bug, it gives us as a community a very nice starting point for fixing issues — hopefully leading to quicker fixes and a more constructive workflow. + +### I have an idea for a feature request + +First of all, that’s awesome! Your ideas on how to make Plot better and more powerful are super welcome. Here’s the recommended workflow for feature requests: + +1. Do some prototyping and come up with a sample implementation of your idea or feature request. Note that this doesn’t have to be a fully working, complete implementation, just something that illustrates the feature/idea and how it could be added to Plot. +2. Submit your sample implementation as a Pull Request. Use the description field to write down why you think the feature should be added and some initial discussion points. +3. Together we’ll discuss the feature and your sample implementation, and either accept it as-is, or use it as a starting point for a new implementation, or decide that the idea is not worth implementing as this time. + +### I have a question that the documentation doesn’t yet answer + +With Plot, the goal is to end up with state of the art documentation that answers most of the questions that both users and developers of the tool might have — and the only way to get there is through continued improvement, with your help. Here’s the recommended workflow for getting your question answered: + +1. Start by looking through the code. Chances are high that you’ll be able to answer your own question by reading through the implementation, the tests, and the inline code documentation. +2. If you found out the answer to your question — then don’t stop there. Other people will probably ask themselves the same question at some point, so let’s improve the documentation! Find an appropriate place where your question could’ve been answered by clearer documentation or a better structure (for example this document, or inline in the code), and add the documentation you wish would’ve been there. If you didn’t manage to find an answer (no worries, we're all always learning), then write down your question as a comment — either in the code or in one of the Markdown documents. +3. Submit your new documentation or your comment as a Pull Request, and we'll work on improving the documentation together. + +## Project structure + +Plot’s code is structured into two main folders — `API` and `Internal`. Any code that has a public-facing API should go into `API`, and purely internal types and functions should go into `Internal`. Note that you shouldn’t split a type up into two files, but rather find one place for it depending on if it has *any* public-facing components. + +For each document type, for example `HTML`, there are several key files in which APIs are defined: + +- The main document file, `HTML`, defines the format itself as well as all of its `Context` types. +- The elements file, `HTMLElements`, defines all DSL APIs for constructing elements within a document. +- The attributes file, `HTMLAttributes`, defines all DSL APIs for constructing attributes for elements of that format. +- Finally, the components file, `HTMLComponents`, defines higher-level components that are compositions of elements and attributes. + +*The same structure is also used for all other document formats — `XML`, `RSS`, `PodcastFeed`, and `SiteMap`.* + +## Unit testing + +Plot has 100% unit testing coverage, and the goal is to keep it that way. All changes to Plot should be fully covered with tests, both to make sure that the new implementation works as expected, and to ensure that it keeps working as the code base is being iterated on. + +If you’re new to unit testing and want to learn more about it, then Swift by Sundell has [lots of articles on the topic](https://www.swiftbysundell.com/tags/unit-testing), starting with [this Basics article](https://www.swiftbysundell.com/basics/unit-testing). + +## Adding a new node type + +If you’ve encountered an element or attribute that’s missing from Plot’s DSL, then feel free to add it. Here’s how to do that: + +1. Start by finding the file in which your new API should be added. The above [project structure section](#project-structure) should help you identify the file that’d be the most appropriate. For example, HTML elements should be defined within `HTMLElements.swift`, and attributes within `HTMLAttributes.swift`. +2. Either add your new API to an existing `Node` extension, or create a new one constrained to the type of `Context` that your element or attribute belongs in. For example, if it’s an attribute for an `` HTML element, then it should be defined in the `Node` extension `where Context == HTML.AnchorContext`. +3. Write a test that verifies that your new API is working. See the above [unit testing section](#unit-testing) for more information. +4. Submit your new API and your test as a Pull Request. +5. Your Pull Request will be reviewed by a maintainer as soon as possible. + +## Conclusion + +Hopefully this document has given you an introduction to how Plot works, both in terms of its recommended project workflow and its structure. Feel free to submit Pull Requests to improve this document, and hope you’ll have a nice experience contributing to Plot. \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..c49eda8 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2019 John Sundell + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/Logo.png b/Logo.png new file mode 100644 index 0000000..fdfc6fe Binary files /dev/null and b/Logo.png differ diff --git a/Package.swift b/Package.swift new file mode 100644 index 0000000..c3fff33 --- /dev/null +++ b/Package.swift @@ -0,0 +1,26 @@ +// swift-tools-version:5.1 + +/** +* Plot +* Copyright (c) John Sundell 2019 +* MIT license, see LICENSE file for details +*/ + +import PackageDescription + +let package = Package( + name: "Plot", + products: [ + .library( + name: "Plot", + targets: ["Plot"] + ) + ], + targets: [ + .target(name: "Plot"), + .testTarget( + name: "PlotTests", + dependencies: ["Plot"] + ) + ] +) diff --git a/README.md b/README.md new file mode 100644 index 0000000..78b675b --- /dev/null +++ b/README.md @@ -0,0 +1,398 @@ +

+ Plot +

+ +

+ + + Swift Package Manager + + Mac + Linux + + Twitter: @johnsundell + +

+ +Welcome to **Plot**, a domain-specific language (DSL) for writing type-safe HTML, XML and RSS in Swift. It can be used to build websites, documents and feeds, as a templating tool, or as a renderer for higher-level components and tools. It’s primary focus is on static site generation and Swift-based web development. + +Plot is used to build and render all of [swiftbysundell.com](https://swiftbysundell.com). + +## Write HTML — in Swift! + +Plot enables you to write HTML using native, fully compiled Swift code, by modeling the HTML5 standard’s various elements as Swift APIs. The result is a very lightweight DSL that lets you build complete web pages in a highly expressive way: + +```swift +let html = HTML( + .head( + .title("My website"), + .stylesheet("styles.css") + ), + .body( + .div( + .h1("My website"), + .p("Writing HTML in Swift is pretty great!") + ) + ) +) +``` + +Looking at the above, it may at first seem like Plot simply maps each function call directly to an equivalent HTML element — and while that’s the case for *some* elements, Plot also inserts many kinds of highly valuable metadata automatically. For example, the above expression will result in this HTML: + +```html + + + + My website + + + + + +
+

My website

+

Writing HTML in Swift is pretty great!

+
+ + +``` + +As you can see above, Plot added both all of the necessary attributes to load the requested CSS stylesheet, along with additional metadata for the page’s title as well — improving page rendering, social media sharing, and search engine optimization. + +Plot ships with a very wide coverage of the HTML5 standard, enabling all sorts of elements to be defined using the same lightweight syntax — such as tables, lists, and inline text styling: + +```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") + ) + ) +) +``` + +Above we’re also using Plot’s powerful composition capabilities, which lets us express all sorts of HTML hierarchies by simply adding new elements as comma-separated values. + +## Applying attributes + +Attributes can also be applied the exact same way as child elements are added, by simply adding another entry to an element’s comma-separated list of content. For example, here’s how an anchor element with both a CSS class and a URL can be defined: + +```swift +let html = HTML( + .body( + .a(.class("link"), .href("https://github.com"), "GitHub") + ) +) +``` + +The fact that attributes, elements and inline text are all defined the same way both makes Plot’s API easier to learn, and also enables a really fast and fluid typing experience — as you can simply type `.` within any context to keep defining new attributes and elements. + +## Type safety built-in + +Plot makes heavy use of Swift’s advanced generics capabilities to not only make it *possible* to write HTML and XML using native code, but to also make that process completely type-safe as well. + +All of Plot’s elements and attributes are implemented as context-bound *nodes*, which both enforces valid HTML semantics, and also enables Xcode and other IDEs to provide rich autocomplete suggestions when writing code using Plot’s DSL. + +For example, above the `href` attribute was added to an `` element, which is completely valid. However, if we instead attempted to add that same attribute to a `

` element, we’d get a compiler error: + +```swift +let html = HTML(.body( + // Compiler error: Referencing static method 'href' on + // 'Node' requires that 'HTML.BodyContext' conform to + // 'HTMLLinkableContext'. + .p(.href("https://github.com")) +)) +``` + +Plot also leverages the Swift type system to verify each document’s element structure as well. For example, within HTML lists (such as `

    ` and `