-
Notifications
You must be signed in to change notification settings - Fork 147
Auto-Capitalize First Word #880
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
Merged
emilyychenn
merged 27 commits into
swiftlang:main
from
emilyychenn:capitalize-first-word
Apr 15, 2024
Merged
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
f82b1bb
WIP: first word in parameters and returns is capitalized, all asides …
emilyychenn c7ecfa6
more WIP not fully functional
emilyychenn 5a63151
WIP
emilyychenn b7f2429
working version of auto-capitalization
emilyychenn 44538f6
clean up PR
emilyychenn 9f2c6e9
add unit tests
emilyychenn 6a9b916
update documentation year
emilyychenn 97c6557
add unit tests for different alphabets
emilyychenn 4cd7293
change computed property to func to indicate to the caller that this …
emilyychenn a7e788c
added headings to autocapitalized cases
emilyychenn 11ee2c5
wip resolving some PR comments
emilyychenn 271874c
added end-to-end unit tests
emilyychenn 7dd5823
rename end-to-end test class
emilyychenn ba4b59b
wip
emilyychenn c7a9aa3
cleaned up capitalizeFirstWord()
emilyychenn 9dd7ecd
remove unused file
emilyychenn 8e1e8e4
Merge branch 'main' into capitalize-first-word
emilyychenn da1e76f
fix end-to-end integration tests
emilyychenn 0bfa1a2
remove redundant test
emilyychenn e3bcdc7
fix failing unit tests
emilyychenn 688f4fa
Merge branch 'main' into capitalize-first-word
emilyychenn 634005d
Update Sources/SwiftDocC/Model/Rendering/RenderSectionTranslator/Para…
emilyychenn 38d881f
resolve PR comments
emilyychenn 1e059be
resolve PR comments
emilyychenn 1663871
remove capitalization from extract tag
emilyychenn a197d5f
remove whitespace
emilyychenn 6b3479b
fix tests for capitalization by locale
emilyychenn 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
92 changes: 92 additions & 0 deletions
92
Sources/SwiftDocC/Model/Rendering/Content/RenderBlockContent+Capitalization.swift
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,92 @@ | ||
/* | ||
This source file is part of the Swift.org open source project | ||
|
||
Copyright (c) 2024 Apple Inc. and the Swift project authors | ||
Licensed under Apache License v2.0 with Runtime Library Exception | ||
|
||
See https://swift.org/LICENSE.txt for license information | ||
See https://swift.org/CONTRIBUTORS.txt for Swift project authors | ||
*/ | ||
|
||
/// For auto capitalizing the first letter of a sentence following a colon (e.g. asides, sections such as parameters, returns). | ||
protocol AutoCapitalizable { | ||
|
||
/// Any type that conforms to the AutoCapitalizable protocol will have the first letter of the first word capitalized (if applicable). | ||
var withFirstWordCapitalized: Self { | ||
get | ||
} | ||
emilyychenn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
} | ||
|
||
extension AutoCapitalizable { | ||
var withFirstWordCapitalized: Self { return self } | ||
emilyychenn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
extension RenderInlineContent: AutoCapitalizable { | ||
/// Capitalize the first word for normal text content, as well as content that has emphasis or strong applied. | ||
var withFirstWordCapitalized: Self { | ||
switch self { | ||
case .text(let text): | ||
return .text(text.capitalizeFirstWord()) | ||
case .emphasis(inlineContent: let embeddedContent): | ||
return .emphasis(inlineContent: [embeddedContent[0].withFirstWordCapitalized] + embeddedContent[1...]) | ||
case .strong(inlineContent: let embeddedContent): | ||
return .strong(inlineContent: [embeddedContent[0].withFirstWordCapitalized] + embeddedContent[1...]) | ||
default: | ||
return self | ||
} | ||
} | ||
} | ||
|
||
|
||
extension RenderBlockContent: AutoCapitalizable { | ||
/// Capitalize the first word for paragraphs, asides, headings, and small content. | ||
var withFirstWordCapitalized: Self { | ||
switch self { | ||
case .paragraph(let paragraph): | ||
emilyychenn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return .paragraph(paragraph.withFirstWordCapitalized) | ||
case .aside(let aside): | ||
return .aside(aside.withFirstWordCapitalized) | ||
case .small(let small): | ||
return .small(small.withFirstWordCapitalized) | ||
case .heading(let heading): | ||
return .heading(.init(level: heading.level, text: heading.text.capitalizeFirstWord(), anchor: heading.anchor)) | ||
default: | ||
return self | ||
} | ||
} | ||
} | ||
|
||
extension RenderBlockContent.Paragraph: AutoCapitalizable { | ||
var withFirstWordCapitalized: RenderBlockContent.Paragraph { | ||
guard !self.inlineContent.isEmpty else { | ||
return self | ||
} | ||
|
||
let inlineContent = [self.inlineContent[0].withFirstWordCapitalized] + self.inlineContent[1...] | ||
emilyychenn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return .init(inlineContent: inlineContent) | ||
} | ||
} | ||
|
||
extension RenderBlockContent.Aside: AutoCapitalizable { | ||
var withFirstWordCapitalized: RenderBlockContent.Aside { | ||
guard !self.content.isEmpty else { | ||
return self | ||
} | ||
|
||
let content = [self.content[0].withFirstWordCapitalized] + self.content[1...] | ||
emilyychenn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return .init(style: self.style, content: content) | ||
} | ||
} | ||
|
||
extension RenderBlockContent.Small: AutoCapitalizable { | ||
var withFirstWordCapitalized: RenderBlockContent.Small { | ||
guard !self.inlineContent.isEmpty else { | ||
return self | ||
} | ||
|
||
let inlineContent = [self.inlineContent[0].withFirstWordCapitalized] + self.inlineContent[1...] | ||
return .init(inlineContent: inlineContent) | ||
} | ||
} | ||
|
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
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
38 changes: 38 additions & 0 deletions
38
Sources/SwiftDocC/Utility/FoundationExtensions/String+Capitalization.swift
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,38 @@ | ||
/* | ||
This source file is part of the Swift.org open source project | ||
|
||
Copyright (c) 2024 Apple Inc. and the Swift project authors | ||
Licensed under Apache License v2.0 with Runtime Library Exception | ||
|
||
See https://swift.org/LICENSE.txt for license information | ||
See https://swift.org/CONTRIBUTORS.txt for Swift project authors | ||
*/ | ||
|
||
import Foundation | ||
|
||
extension String { | ||
|
||
// Precomputes the CharacterSet to use in capitalizeFirstWord(). | ||
private static let charactersPreventingWordCapitalization = CharacterSet.lowercaseLetters.union(.punctuationCharacters).inverted | ||
|
||
/// Returns the string with the first letter capitalized. | ||
/// This auto-capitalization only occurs if the first word is all lowercase and contains only lowercase letters. | ||
/// The first word can also contain punctuation (e.g. a period, comma, hyphen, semi-colon, colon). | ||
func capitalizeFirstWord() -> String { | ||
guard let firstWordStartIndex = self.firstIndex(where: { !$0.isWhitespace && !$0.isNewline }) else { return self } | ||
let firstWord = self[firstWordStartIndex...].prefix(while: { !$0.isWhitespace && !$0.isNewline}) | ||
|
||
guard firstWord.rangeOfCharacter(from: Self.charactersPreventingWordCapitalization) == nil else { | ||
return self | ||
} | ||
Comment on lines
+22
to
+27
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I find the mix of This could be either something like
or something like
|
||
|
||
var resultString = String() | ||
resultString.reserveCapacity(self.count) | ||
resultString.append(contentsOf: self[..<firstWordStartIndex]) | ||
resultString.append(contentsOf: String(firstWord).localizedCapitalized) | ||
let restStartIndex = self.index(firstWordStartIndex, offsetBy: firstWord.count) | ||
resultString.append(contentsOf: self[restStartIndex...]) | ||
|
||
return resultString | ||
} | ||
} |
Oops, something went wrong.
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.