Skip to content
Permalink
Browse files

Initial commit.

  • Loading branch information...
frankrausch committed Feb 16, 2017
0 parents commit 5b3dc67a23bfd3e2dc1d24cae9012030803d018d
Showing with 471 additions and 0 deletions.
  1. +13 −0 .gitignore
  2. +7 −0 LICENSE
  3. +97 −0 README.md
  4. +18 −0 Typographizer/String+Typographizer.swift
  5. +336 −0 Typographizer/Typographizer.swift
@@ -0,0 +1,13 @@
# macOS
.DS_Store
profile

# Xcode
build/*
*.mode1
*.mode1v3
*.mode2v3
*.perspective
*.perspectivev3
*.pbxuser
xcuserdata
@@ -0,0 +1,7 @@
Copyright (c) 2017 Frank Rausch

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.
@@ -0,0 +1,97 @@
## Swift Typographizer

Typographizer turns those pesky dumb quotes `""`/`''` and apostrophes into their beautiful, curly, localized counterparts. It’s smart enough to skip HTML tags and everything that’s inside certain tags (like `<code>` and `<pre>`).

Typographizer was initially created to typographize Wikipedia articles in the [V for Wiki](http://v-for-wiki.com/) app, a nice reader for iOS and Apple Watch.

Written in Swift 3, it’s been tested on macOS, iOS, and watchOS.

## 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.
```

If your strings may contain any HTML, set the `isHTML` parameter to `true`:

```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>
```

Activate the debug mode to highlight the characters that have been changed—Typographizer will add this tag around them: `<span class="typographizerDebug">` `</span>`

```swift
var s = "This is a string with \"dumb\" quotes."
s.typographized(language: "en", isHTML: true, debug: true)
print(s) // This is a string with <span class="typographizerDebug">“</span>dumb<span class="typographizerDebug">”</span> quotes.
```

## Supported languages

| Language Code | Single Quotes | Double Quotes | Comment |
| --- | --- | --- | --- |
|||
`bs` | ”” | ’’ | |
`cs` | „“ | ‚‘ | |
`da` | „“ | ‚‘ | |
`de` | „“ | ‚‘ | There should be a `de_CH` option for Swiss German with guillemets («»/‹›)|
`en` | “” | ‘’ | |
`et` | „“ | ‚‘ | |
`fi` | ”” | ’’ | |
`fr` | «`\u{00A0}` `\u{00A0}`» | ‹`\u{00A0}` `\u{00A0}`› | French Quotes are set with a non-breaking space (`\u{00A0}`)|
`hu` | „” | ’’ | |
`is` | „“ | ‚‘ | |
`ja` | 「」 | 『』 | |
`lt` | „“ | ‚‘ | |
`lv` | „“ | ‚‘ | |
`nl` | “” | ‘’ | |
`nn` | «» | ’’ | |
`no` | «» | ’’ | |
`pl` | „” | ’’ | |
`ro` | „” | ’’ | |
`ru` | «» | ’’ | |
`sk` | „“ | ‚‘ | |
`sl` | „“ | ‚‘ | |
`sv` | ”” | ’’ | |


## To Do

- Add a demo app for macOS
- Build a Swift Framework project (or maybe this is overkill?)
- Add Carthage/CocoaPods support (or maybe this is overkill?)
- Analyze HTML tags to verify correct quotes (opening and closing `<p>` tags make a good indicator for opening and closing quotation marks)
- Add special cases like ’80s, ’Twas, Rock ’n’ Roll, etc.
- Add support for Hebrew and Swiss German.
- If there is only one dumb single quote in a string, it’s definitely an apostrophe
- If the isHTML flag is not explicitly set, we could still check automatically whether the string contains any HTML tags
- Add more typographic refinements.

---

## Credits

Typographizer was created by [Frank Rausch](http://frankrausch.com).

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).

---

## License
Swift Typographizer is released under the MIT License. Please view the LICENSE file for details.
@@ -0,0 +1,18 @@
//
// String+Typographizer.swift
// Typographizer
//
// Created by Frank Rausch on 2017-02-04.
// Copyright © 2017 Frank Rausch.
//
import Foundation

extension String {
func typographized(language: String, isHTML: Bool = true, debug: Bool = false) -> String {
var t = Typographizer(language: language, text: self)
t.isDebugModeEnabled = debug
t.isHTML = isHTML
return t.typographize()
}
}
Oops, something went wrong.

0 comments on commit 5b3dc67

Please sign in to comment.
You can’t perform that action at this time.