Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/frankrausch/typographizer
Fix dumb quotation marks and apostrophes in Swift on iOS, macOS, watchOS, and tvOS
https://github.com/frankrausch/typographizer
dumb-quotes smart-quotes swift typography
Last synced: about 3 hours ago
JSON representation
Fix dumb quotation marks and apostrophes in Swift on iOS, macOS, watchOS, and tvOS
- Host: GitHub
- URL: https://github.com/frankrausch/typographizer
- Owner: frankrausch
- License: mit
- Created: 2017-02-16T11:01:59.000Z (almost 8 years ago)
- Default Branch: main
- Last Pushed: 2021-03-03T18:31:51.000Z (over 3 years ago)
- Last Synced: 2023-10-28T11:28:51.670Z (about 1 year ago)
- Topics: dumb-quotes, smart-quotes, swift, typography
- Language: Swift
- Homepage:
- Size: 105 KB
- Stars: 269
- Watchers: 10
- Forks: 17
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## Typographizer · Smart Quotes for Swift Apps
Typographizer turns those pesky dumb quotes (`""`/`''`) and apostrophes (`'`) into their beautiful, curly, localized counterparts. Because [good typography uses smart quotes, not dumb quotes](http://smartquotesforsmartpeople.com/) and we should not let [the internet kill smart quotes](https://www.theatlantic.com/technology/archive/2016/12/quotation-mark-wars/511766/). Speaking of smartness: Typographizer is smart enough to skip HTML tags and everything between certain tags (like `
` and `
`).Typographizer has a small footprint, was written in pure Swift, and has been tested on macOS, iOS, watchOS, and tvOS.
I started building Typographizer to typographize Wikipedia articles in my [V for Wiki](http://v-for-wiki.com) app.
## Installation
Put the `.swift` files into your app’s Xcode project.
## How to Use
The easiest way to use Typographizer is the String extension (`String+Typographizer.swift`):
```swift
var s = "This is a string with \"dumb\" quotes."s = s.typographized(language: "en")
print(s) // This is a string with “dumb” quotes.
```### Ignoring HTML Tags
If your string may contain HTML, set the `isHTML` parameter to `true`. Typographizer will then ignore the quotes inside tags and anything between `
`, ``, ``, ``, ``, ``, ``, and `<style>` tags:
```swift
var s = "This is a \"string\" with HTML. <code class="">print(\"hello world\")</code>"s = s.typographized(language: "en", isHTML: true)
print(s) // This is a “string” with HTML. <code class="">print("hello world")</code>
```### Debug Mode
Activate the debug mode to highlight the characters that have been changed—Typographizer will add this tag around them: `<span class="typographizer-debug typographizer-debug--XXX">` `</span>`
```swift
var s = "This is a string with \"dumb\" quotes."s = s.typographized(language: "en", isHTML: true, debug: true)
print(s) // This is a string with <span class="typographizer-debug typographizer-debug--opening-double">“</span>dumb<span class="typographizer-debug typographizer-debug--closing-double">”</span> quotes.
```(Yes, the class names are a little wordy, but that’s [on purpose](https://csswizardry.com/2013/01/mindbemding-getting-your-head-round-bem-syntax/).)
Use CSS to visualize the changes:
```css
.typographizer-debug {
font-weight: bold;
}.typographizer-debug--apostrophe {
color: red;
}/* … */
```### Measuring Performance
Pass `measurePerformance: true` to log performance stats:
```swift
s = s.typographized(language: "en", isHTML: true, debug: false, measurePerformance: true)
```You’ll see something like this in the Xcode console:
```
Typographizing took 0.00582303 seconds
```## Features
- [x] Fixes double quotes: `""` → `“”` (localized)
- [x] Fixes single quotes: `''` → `‘’` (localized)
- [x] Fixes apostrophes: `'` → `’`
- [x] Fixes hyphens that are used as en dashes: `… - …` → `… – …`
- [x] Demo app project for macOS## Supported Languages
| Language Code | Double Quotes | Single Quotes | Comment |
| :---: | :---: | :---: | --- |
| `bs` | `”` `”` | `’` `’` | |
| `cs` | `„` `“` | `‚` `‘` | |
| `da` | `„` `“` | `‚` `‘` | |
| `de` | `„` `“` | `‚` `‘` | |
| `de_CH` | `«` `»` | `‹` `›` | Swiss Standard German |
| `en` | `“` `”` | `‘` `’` | |
| `et` | `„` `“` | `‚` `‘` | |
| `fi` | `”` `”` | `’` `’` | |
| `fr` | `«\u{00A0}` `\u{00A0}»` | `‹\u{00A0}` `\u{00A0}›` | French Quotes are set with a non-breaking space (`\u{00A0}`). A thin non-breaking space would be better, but it’s not supported in most browsers.|
| `hu` | `„` `”` | `’` `’` | |
| `is` | `„` `“` | `‚` `‘` | |
| `ja` | `「` `」` |`『` `』` | |
| `lt` | `„` `“` | `‚` `‘` | |
| `lv` | `„` `“` | `‚` `‘` | |
| `nl` | `“` `”` | `‘` `’` | |
| `nn` | `«` `»` | `’` `’` | |
| `no` | `«` `»` | `’` `’` | |
| `pl` | `„` `”` | `’` `’` | |
| `ro` | `„` `”` | `’` `’` | |
| `ru` | `«` `»` | `’` `’` | |
| `sk` | `„` `“` | `‚` `‘` | |
| `sl` | `„` `“` | `‚` `‘` | |
| `sv` | `”` `”` | `’` `’` | |## Demo App
To get started, try the included demo app for macOS.![](https://cloud.githubusercontent.com/assets/6783714/24149232/0b818208-0e42-11e7-9257-b2afda792f2b.png "Screenshot of Typographizer Demo App")
## To Do
- [ ] Handle special cases like ’80s, ’Twas, Rock ’n’ Roll, etc.
- [ ] Handle primes in coordinates properly:
52° 27' 20" N, 13° 19' 11" E → 52° 27′ 20″ N, 13° 19′ 11″ E
- [ ] Add support for Hebrew
- [ ] If there is only one dumb single quote in a string, it’s probably an apostrophe
- [ ] Analyze HTML tags to verify correct quotes (opening and closing `<p>` tags make a good indicator for opening and closing quotation marks)
- [ ] Track open/closed state while iterating over the text to make more informed decisions
- [ ] Add more typographic refinements (e.g. prime symbols, thin spaces)## Credits
Typographizer was created by [Frank Rausch](http://frankrausch.com) ([@frankrausch](https://twitter.com/frankrausch)).
Thanks to Tony Allevato for the great article on [Strings, characters, and performance in Swift—a deep dive](https://medium.com/@tonyallevato/strings-characters-and-performance-in-swift-a-deep-dive-b7b5bde58d53).
## Contribution Guidelines
Please always use `self` when accessing methods and properties.
Preferred:
```swift
self.coolMethod()
```Not preferred:
```swift
coolMethod()
```## License
The Typographizer source code is released under the MIT License. Please view the LICENSE file for details.The Typographizer logo is © 2017 Frank Rausch; all rights reserved.