-
Notifications
You must be signed in to change notification settings - Fork 79
Added Strong & Emphasis formatter #68
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
stephanheilner
wants to merge
3
commits into
laptobbe:master
Choose a base branch
from
stephanheilner:strong_emphasis
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -53,7 +53,8 @@ Emphasis | |
| _Em_ | ||
| **Strong** | ||
| __Strong__ | ||
|
|
||
| ***Strong Emphasis*** | ||
| ___Strong Emphasis___ | ||
| ```` | ||
|
|
||
| # Requirements | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| // | ||
| // NSAttributedString+Markdown.swift | ||
| // TSMarkdownParserLib | ||
| // | ||
| // Created by Stephan Heilner on 10/30/17. | ||
| // Copyright © 2017 Computertalk Sweden. All rights reserved. | ||
| // | ||
|
|
||
| #if !os(OSX) | ||
|
|
||
| import UIKit | ||
| import Foundation | ||
|
|
||
| public extension NSAttributedString { | ||
|
|
||
| public func markdownString() -> String { | ||
| let bulletCharacter = Character("\u{2022}") | ||
| let nonBreakingSpaceCharacter = Character("\u{00A0}") | ||
|
|
||
| var markdownString = "" | ||
|
|
||
| enum FormattingChange { | ||
| case enable | ||
| case disable | ||
| case keep | ||
|
|
||
| static func getFormattingChange(_ before: Bool, after: Bool) -> FormattingChange { | ||
| if !before && after { return .enable } | ||
| if before && !after { return .disable } | ||
| return .keep | ||
| } | ||
| } | ||
|
|
||
| var stringHasBoldEnabled = false | ||
| var stringHasItalicEnabled = false | ||
| var closingString = "" | ||
| var characterOnBulletedListLine = false | ||
| var openedNumberedListStarter = false | ||
| var characterOnNumberedListLine = false | ||
| var numberedListIsFirstLine = false | ||
| var previousCharacter: Character? | ||
|
|
||
| enumerateAttributes(in: NSRange(location: 0, length: length), options: []) { attributes, range, shouldStop in | ||
| if let traits = (attributes[NSAttributedStringKey.font] as? UIFont)?.fontDescriptor.symbolicTraits { | ||
| let boldChange = FormattingChange.getFormattingChange(stringHasBoldEnabled, after: traits.contains(.traitBold)) | ||
| let italicChange = FormattingChange.getFormattingChange(stringHasItalicEnabled, after: traits.contains(.traitItalic)) | ||
| var formatString = "" | ||
| switch boldChange { | ||
| case .enable: | ||
| formatString += "**" | ||
| closingString = "**\(closingString)" | ||
| case .disable: | ||
| if stringHasItalicEnabled && italicChange == .keep { | ||
| formatString += "_**_" | ||
| closingString = "_" | ||
| } else { | ||
| formatString += "**" | ||
| closingString = "" | ||
| } | ||
| case .keep: | ||
| break | ||
| } | ||
|
|
||
| switch italicChange { | ||
| case .enable: | ||
| formatString += "_" | ||
| closingString = "_\(closingString)" | ||
| case .disable: | ||
| if stringHasBoldEnabled && boldChange == .keep { | ||
| formatString = "**_**\(formatString)" | ||
| closingString = "**" | ||
| } else { | ||
| formatString = "_\(formatString)" | ||
| closingString = "" | ||
| } | ||
| case .keep: | ||
| break | ||
| } | ||
|
|
||
| markdownString += formatString | ||
|
|
||
| stringHasBoldEnabled = traits.contains(.traitBold) | ||
| stringHasItalicEnabled = traits.contains(.traitItalic) | ||
| } | ||
|
|
||
| let preprocessedString = (self.string as NSString).substring(with: range) | ||
| let processedString = preprocessedString.characters.reduce("") { resultString, character in | ||
| var stringToAppend = "" | ||
|
|
||
| switch character { | ||
| case "\\", "`", "*", "_", "{", "}", "[", "]", "(", ")", "#", "+", "-", "!": | ||
| stringToAppend = "\\\(character)" | ||
| case "\n", "\u{2028}": | ||
| stringToAppend = "\(closingString)\(character)" | ||
| if !characterOnBulletedListLine && !characterOnNumberedListLine { | ||
| stringToAppend += String(closingString.characters.reversed()) | ||
| } | ||
|
|
||
| characterOnBulletedListLine = false | ||
| characterOnNumberedListLine = false | ||
| case "1", "2", "3", "4", "5", "6", "7", "8", "9", "0": | ||
| if previousCharacter == "\n" || previousCharacter == nil || previousCharacter == nonBreakingSpaceCharacter { | ||
| openedNumberedListStarter = true | ||
| } | ||
|
|
||
| numberedListIsFirstLine = previousCharacter == nil ? true : numberedListIsFirstLine | ||
| stringToAppend = "\(character)" | ||
| case bulletCharacter: | ||
| characterOnBulletedListLine = true | ||
| stringToAppend = "+ \(previousCharacter != nil ? String(closingString.characters.reversed()) : markdownString)" | ||
| markdownString = previousCharacter == nil ? "" : markdownString | ||
| case ".": | ||
| if openedNumberedListStarter { | ||
| openedNumberedListStarter = false | ||
| characterOnNumberedListLine = true | ||
|
|
||
| stringToAppend = "\(character) \(!numberedListIsFirstLine ? String(closingString.characters.reversed()) : markdownString)" | ||
|
|
||
| if numberedListIsFirstLine { | ||
| markdownString = "" | ||
| numberedListIsFirstLine = false | ||
| } | ||
| break | ||
| } | ||
| stringToAppend = "\\\(character)" | ||
| case nonBreakingSpaceCharacter: | ||
| if characterOnBulletedListLine || characterOnNumberedListLine { | ||
| break | ||
| } | ||
| stringToAppend = " " | ||
| default: | ||
| if (previousCharacter == "\n" || previousCharacter == "\u{2028}") && characterOnBulletedListLine { | ||
| characterOnBulletedListLine = false | ||
| stringToAppend = "\(String(closingString.characters.reversed()))\(character)" | ||
| } else { | ||
| stringToAppend = "\(character)" | ||
| } | ||
| } | ||
|
|
||
| previousCharacter = character | ||
| return "\(resultString)\(stringToAppend)" | ||
| } | ||
| markdownString += processedString | ||
| } | ||
| markdownString += closingString | ||
| markdownString = markdownString.replacingOccurrences(of: "**__**", with: "").replacingOccurrences(of: "****", with: "") | ||
| .replacingOccurrences(of: "__", with: "") | ||
| // Help the user because they probably didn't intend to have empty bullets and it will make markdown have a + if we leave them | ||
| markdownString = markdownString.replacingOccurrences(of: "+ \n", with: "") | ||
| if markdownString.hasSuffix("+ ") { | ||
| markdownString = (markdownString as NSString).substring(to: markdownString.characters.count - 2) | ||
| } | ||
|
|
||
| return markdownString | ||
| } | ||
|
|
||
| } | ||
|
|
||
| #endif | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I may move this line before Strong and Emphasis.