Skip to content

Commit 8645db8

Browse files
committed
Initial commit
0 parents  commit 8645db8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+3393
-0
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.DS_Store
2+
/.build
3+
/Packages
4+
/*.xcodeproj
5+
Package.resolved

CODE_OF_CONDUCT.md

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# Splash Code of Conduct
2+
3+
Below is the Code of Conduct that all contributors and participants in the Splash community are expected to adhere to.
4+
It's adopted from the [Contributor Covenant Code of Conduct][homepage].
5+
6+
## Our Pledge
7+
8+
In the interest of fostering an open and welcoming environment, we as
9+
contributors and maintainers pledge to making participation in our project and
10+
our community a harassment-free experience for everyone, regardless of age, body
11+
size, disability, ethnicity, gender identity and expression, level of experience,
12+
nationality, personal appearance, race, religion, or sexual identity and
13+
orientation.
14+
15+
## Our Standards
16+
17+
Examples of behavior that contributes to creating a positive environment
18+
include:
19+
20+
* Using welcoming and inclusive language
21+
* Being respectful of differing viewpoints and experiences
22+
* Gracefully accepting constructive criticism
23+
* Focusing on what is best for the community
24+
* Showing empathy towards other community members
25+
26+
Examples of unacceptable behavior by participants include:
27+
28+
* The use of sexualized language or imagery and unwelcome sexual attention or
29+
advances
30+
* Trolling, insulting/derogatory comments, and personal or political attacks
31+
* Public or private harassment
32+
* Publishing others' private information, such as a physical or electronic
33+
address, without explicit permission
34+
* Other conduct which could reasonably be considered inappropriate in a
35+
professional setting
36+
37+
## Our Responsibilities
38+
39+
Project maintainers are responsible for clarifying the standards of acceptable
40+
behavior and are expected to take appropriate and fair corrective action in
41+
response to any instances of unacceptable behavior.
42+
43+
Project maintainers have the right and responsibility to remove, edit, or
44+
reject comments, commits, code, wiki edits, issues, and other contributions
45+
that are not aligned to this Code of Conduct, or to ban temporarily or
46+
permanently any contributor for other behaviors that they deem inappropriate,
47+
threatening, offensive, or harmful.
48+
49+
## Scope
50+
51+
This Code of Conduct applies both within project spaces and in public spaces
52+
when an individual is representing the project or its community. Examples of
53+
representing a project or community include using an official project e-mail
54+
address, posting via an official social media account, or acting as an appointed
55+
representative at an online or offline event. Representation of a project may be
56+
further defined and clarified by project maintainers.
57+
58+
## Enforcement
59+
60+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
61+
reported by contacting the project leader at [email protected]. All
62+
complaints will be reviewed and investigated and will result in a response that
63+
is deemed necessary and appropriate to the circumstances. The project team is
64+
obligated to maintain confidentiality with regard to the reporter of an incident.
65+
Further details of specific enforcement policies may be posted separately.
66+
67+
Project maintainers who do not follow or enforce the Code of Conduct in good
68+
faith may face temporary or permanent repercussions as determined by other
69+
members of the project's leadership.
70+
71+
## Attribution
72+
73+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
74+
available at [http://contributor-covenant.org/version/1/4][version]
75+
76+
[homepage]: http://contributor-covenant.org
77+
[version]: http://contributor-covenant.org/version/1/4/

CONTRIBUTING.md

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# Splash Contribution Guide
2+
3+
Welcome to the *Splash Contribution Guide* - a document that aims to give you all the information you need to contribute to the Splash project.
4+
5+
Before you continue, make sure that you've read & agree to [the Code of Conduct](https://github.com/JohnSundell/Splash/blob/master/CODE_OF_CONDUCT.md).
6+
7+
## Bugs, feature requests and support
8+
9+
Splash 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.
10+
11+
The idea behind this workflow is to encourage more people using Splash 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 more fluid experience for everyone involved 😊.
12+
13+
*This workflow is still very much an experiment, so please be patient and try to keep an open mind as we work things out together* 😉
14+
15+
**🐞 I found a bug, how do I report it?**
16+
17+
If you find a bug, for example a piece of code that doesn't highlight correctly, here's the recommended workflow:
18+
19+
1. Come up with the simplest code possible that reproduces the issue (`SplashTokenizer` can be a great tool to use in order to quickly test how Splash tokenizes a string of code).
20+
2. Write a test case using the code that reproduces the issue. [See Splash's existing tests for inspiration on how to get started](https://github.com/JohnSundell/Splash/tree/master/Tests/SplashTests/Tests). When writing a test, you essentially give `SyntaxHighlighter` a string to highlight, and then perform an `XCTAssertEqual` against an array of expected components.
21+
3. Either fix the bug yourself, or simply submit your failing test as a Pull Request, and we can work on a solution together.
22+
23+
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.
24+
25+
**💡 I have an idea for a feature request!**
26+
27+
First of all, that's awesome! 👍 Your ideas on how to make Splash better and more powerful are super welcome. Here's the recommended workflow for feature requests:
28+
29+
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 and your idea on how it could be added to Splash.
30+
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.
31+
3. Together we'll discuss the feature and your sample implementation, and either accept it as-is, use it as a starting point for a new implementation, or decide that the idea is not worth implementing as this time.
32+
33+
**🤔 I have a question that the documentation doesn't yet answer**
34+
35+
With Splash, 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.
36+
37+
Here's the recommended workflow for getting your question answered:
38+
39+
1. Start by looking through the code. Splash is a normal Swift package that uses standard Swift conventions with a (hopefully 😅) well-defined structure. 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.
40+
2. If you found out the answer to your question (congrats! 🎉) - 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 👍), write down your question as a comment - either in the code or in one of the Markdown documents.
41+
3. Submit your new documentation or your comment as a Pull Request, and we'll work on improving the documentation together.
42+
43+
## Design and technical decisions
44+
45+
Like most programs & frameworks, Splash could've been written in many different ways. Specifically for the task of Swift syntax highlighting, there were three main options to consider:
46+
47+
1. Apply regular expressions to the code in order to tokenize it. This is how most JavaScript-based syntax highlighters work. It's a common and proven approach, but it usually doesn't yield the most accurate results (writing really granular regular expressions is really hard), and can be a bit alienating for people who haven't used advanced regular expressions before.
48+
2. Hook into Apple's SourceKit service. SourceKit is what powers Xcode's syntax highlighting, and works in tandem with the Swift compiler to tokenize and highlight code. SourceKit is awesome, but using it is quite complicated and requires cross-process communication.
49+
3. Simply parse the code manually. Like all programming languages, Swift has a well-defined syntax and clear grammar that we can model in code, in order to parse and tokenize code by iterating through it.
50+
51+
When I first started exploring the idea of a custom Swift syntax highlighter, I built quick prototypes using all of the above three techniques - and the one that I liked the most (by far) was option number 3. Writing Splash as a normal Swift package, using normal Swift code, with standard Swift conventions turned out (at least for me) to be the most easy to understand and easy to work with solution.
52+
53+
The next challenge then became to decide exactly *how* to write such a Swift package. Swift's syntax changes over time, so Splash required a flexible setup in order to avoid becoming hard to maintain due to complicated logic and lots of different conditions scattered all over the code.
54+
55+
## Architectural overview
56+
57+
Splash's architecture was designed to enable easy tweaking of how it parses and tokenizes code, and to make bugs and edge cases easier to debug - but also to hide all those implementation details from the API user.
58+
59+
**SyntaxHighlighter**
60+
61+
The most top level API that most API users will interact with is `SyntaxHighlighter`. It doesn't do much itself, but instead works as the *"middleman"* between the internal `Tokenizer` type and user-configurable implementations of `Grammar` and `OutputFormat`.
62+
63+
So as an API user, all you have to know in order to use Splash is this:
64+
65+
```swift
66+
let highlighter = SyntaxHighlighter(format: HTMLOutputFormat())
67+
let code = highlighter.highlight("func hello() -> Int")
68+
```
69+
70+
**Tokenizer**
71+
72+
The `Tokenizer` type enables `SyntaxHighlighter` to ask for a sequence of code segments for a given string, using a set of delimiters to use to split the code up. It then iterates through each character of the given string and uses the set of delimiters to check when each segment should begin and end.
73+
74+
The reason `Tokenizer` doesn't simply *split* the string is to enable Splash to have as close to `O(N)` performance characteristics as possible. If we first had to split the string, *then* iterate through it, we would always make at least two passes through the code. With the current approach, only one full pass has to be made.
75+
76+
**Grammar**
77+
78+
What delimiters that `Tokenizer` should use to split the code up into segments is determined by the given language `Grammar`. The default implementation is called `SwiftGrammar`, which aims to mimic the behavior of the Swift compiler as close as possible without actually having to compile the code (which is what enables Splash to be so fast).
79+
80+
The decision to not simply hardcode `SwitftGrammar` across the code base was to [decouple the code using a protocol](https://www.swiftbysundell.com/posts/separation-of-concerns-using-protocols-in-swift) (in this case `Grammar`) to achieve a much more flexible solution. If the Swift grammar changes a lot in the future, we can always add a second implementation while still maintaining backward compatibility, and it also opens up the possibility of using Splash with languages other than Swift - since it doesn't make many (if any) hard assumptions about Swift itself (Objective-C support, anyone? 😉).
81+
82+
Apart from supplying `Tokenizer` with delimiters, the most important role of a `Grammar` implementation is to provide an array of `SyntaxRule` implementations. When `SyntaxHighlighter` iterates through the segments that its `Tokenizer` gave it, it applies the syntax rules from its `Grammar` to each one of them to figure out each token's type. Each rule is asked if it matches a given segment, and as soon as a match is found that rule's `TokenType` is used to determine the type of that token.
83+
84+
Have a look at `SwiftGrammar` to see all of its `SyntaxRule` implementations and how they decide how to classify each token using code segments.
85+
86+
**OutputFormat**
87+
88+
The final piece of the puzzle is `OutputFormat`, which determines how the result of tokenizing a string of code should be transformed into its final form. Splash ships with two implementations of this protocol `HTMLOutputFormat` and `AttributedStringOutputFormat`, but the framework makes no assumptions about what output format that the API user may want, since the output format can be fully customized.
89+
90+
An `OutputFormat` has two responsibilities. The first is to define what type that the output will actually be (through its `Output` associated type). The second is to construct an `OutputBuilder` to build up a value of that output format type. Splash uses the *[builder pattern](https://www.swiftbysundell.com/posts/using-the-builder-pattern-in-swift)* to be able to continuously build up the output as it iterates through each token, and at the end call `build()` on the builder to output the final result.
91+
92+
**Conclusion**
93+
94+
Hopefully this document has given you an introduction to how Splash works, both in terms of its recommended project workflow and its technical implementation. Feel free to submit Pull Requests to improve this document, and I look forward to working with you on Splash and seeing how you use it 😀

Images/Code.png

12.8 KB
Loading

Images/Logo.png

214 KB
Loading

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2018 John Sundell
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
install:
2+
swift package update
3+
swift build -c release -Xswiftc -static-stdlib
4+
install .build/Release/SplashHTMLGen /usr/local/bin/SplashHTMLGen
5+
install .build/Release/SplashImageGen /usr/local/bin/SplashImageGen
6+
install .build/Release/SplashTokenizer /usr/local/bin/SplashTokenizer

Package.swift

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// swift-tools-version:4.1
2+
3+
/**
4+
* Splash
5+
* Copyright (c) John Sundell 2018
6+
* MIT license - see LICENSE.md
7+
*/
8+
9+
import PackageDescription
10+
11+
let package = Package(
12+
name: "Splash",
13+
products: [
14+
.library(name: "Splash", targets: ["Splash"])
15+
],
16+
targets: [
17+
.target(name: "Splash"),
18+
.target(
19+
name: "SplashHTMLGen",
20+
dependencies: ["Splash"]
21+
),
22+
.target(
23+
name: "SplashImageGen",
24+
dependencies: ["Splash"]
25+
),
26+
.target(
27+
name: "SplashTokenizer",
28+
dependencies: ["Splash"]
29+
),
30+
.testTarget(
31+
name: "SplashTests",
32+
dependencies: ["Splash"]
33+
)
34+
]
35+
)

0 commit comments

Comments
 (0)