Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 5 additions & 62 deletions Modules/Sources/WordPressKitModels/NSString+Summary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,76 +5,19 @@
/// and convert HTML into plain text.
///
extension NSString {

static let PostDerivedSummaryLength = 150

/// Create a summary for the post based on the post's content.
///
/// - Returns: A summary for the post.
///
@objc
public func wpkit_summarized() -> String {
let characterSet = CharacterSet(charactersIn: "\n")

return (self as String).strippingGutenbergContentForExcerpt()
.strippingShortcodes()
.makePlainText()
.trimmingCharacters(in: characterSet)
.wp_stringByEllipsizing(withMaxLength: NSString.PostDerivedSummaryLength, preserveWords: true)
GutenbergExcerptGenerator.firstParagraph(from: (self as String))
}
}

private extension String {
func makePlainText() -> String {
let characterSet = NSCharacterSet.whitespacesAndNewlines

return self.strippingHTML()
@objc
public func wpkit_makePlainText() -> String {

Check warning on line 18 in Modules/Sources/WordPressKitModels/NSString+Summary.swift

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Rename function "wpkit_makePlainText" to match the regular expression ^[a-z][a-zA-Z0-9]*$.

See more on https://sonarcloud.io/project/issues?id=wordpress-mobile_WordPress-iOS&issues=AZ0Hp13sYjv1li-UOHGd&open=AZ0Hp13sYjv1li-UOHGd&pullRequest=25392
self.strippingHTML()
.stringByDecodingXMLCharacters()
.trimmingCharacters(in: characterSet)
}

/// Creates a new string by stripping all shortcodes from this string.
///
func strippingShortcodes() -> String {
let pattern = "\\[[^\\]]+\\]"

return removingMatches(pattern: pattern, options: .caseInsensitive)
}

/// This method is the main entry point to generate excerpts for Gutenberg content.
///
func strippingGutenbergContentForExcerpt() -> String {
return strippingGutenbergGalleries().strippingGutenbergVideoPress()
}

/// Strips Gutenberg galleries from strings.
///
func strippingGutenbergGalleries() -> String {
let pattern = "(?s)<!--\\swp:gallery?(.*?)wp:gallery\\s-->"

return removingMatches(pattern: pattern, options: .caseInsensitive)
}

/// Strips VideoPress references from Gutenberg VideoPress and Video blocks.
///
func strippingGutenbergVideoPress() -> String {
let pattern = "(?s)\n?<!--\\swp:video.*?(.*?)wp:video.*?\\s-->"

return removingMatches(pattern: pattern, options: .caseInsensitive)
}

/// Creates a new string by removing all matches of the specified regex.
///
func removingMatches(pattern: String, options: NSRegularExpression.Options = []) -> String {
let range = NSRange(location: 0, length: self.utf16.count)
let regex: NSRegularExpression

do {
regex = try NSRegularExpression(pattern: pattern, options: options)
} catch {
return self
}

return regex.stringByReplacingMatches(in: self, options: .reportCompletion, range: range, withTemplate: "")
.trimmingCharacters(in: .whitespacesAndNewlines)
}
}
2 changes: 1 addition & 1 deletion Modules/Sources/WordPressKitObjC/RemoteReaderPost.m
Original file line number Diff line number Diff line change
Expand Up @@ -707,7 +707,7 @@ - (NSString *)createSummaryFromContent:(NSString *)string
*/
- (NSString *)makePlainText:(NSString *)string
{
return [string wpkit_summarized];
return [string wpkit_makePlainText];
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ public struct GutenbergExcerptGenerator {
return ""
}

// Extract content
// Extract content while convering <br>, <br/>, <br /> to newlines first
let rawText = String(content[tagEnd.upperBound..<pEnd.lowerBound])
.replacingOccurrences(of: "<br\\s*/?>", with: " ", options: .regularExpression)

// Remove HTML tags AND shortcodes in one pass
let range = NSRange(rawText.startIndex..., in: rawText)
let text = (regex?.stringByReplacingMatches(in: rawText, options: [], range: range, withTemplate: "") ?? rawText)
.stringByDecodingXMLCharacters()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,29 @@ struct GutenbergPostExcerptGeneratorTests {
let content = "<!-- wp:gallery {\"ids\":[2315,2309,2308]} --><figure class=\"wp-block-gallery columns-3 is-cropped\"><ul class=\"blocks-gallery-grid\"><li class=\"blocks-gallery-item\"><figure><img src=\"https://diegotest4.files.wordpress.com/2020/01/img_0005-1-1.jpg\" data-id=\"2315\" class=\"wp-image-2315\"/><figcaption class=\"blocks-gallery-item__caption\">Asdasdasd</figcaption></figure></li><li class=\"blocks-gallery-item\"><figure><img src=\"https://diegotest4.files.wordpress.com/2020/01/img_0111-1-1.jpg\" data-id=\"2309\" class=\"wp-image-2309\"/><figcaption class=\"blocks-gallery-item__caption\">Asdasdasd</figcaption></figure></li><li class=\"blocks-gallery-item\"><figure><img src=\"https://diegotest4.files.wordpress.com/2020/01/img_0004-1.jpg\" data-id=\"2308\" class=\"wp-image-2308\"/><figcaption class=\"blocks-gallery-item__caption\">Adsasdasdasd</figcaption></figure></li></ul></figure><!-- /wp:gallery --><p>Some Content</p>"

let summary = GutenbergExcerptGenerator.firstParagraph(from: content, maxLength: 150)
#expect(summary == "Some Content")
#expect(summary == "Some Content")
}

@Test func summaryForContentWithGallery2() {
let content = "<p>Before</p>\n<!-- wp:gallery {\"ids\":[2315,2309,2308]} --><figure class=\"wp-block-gallery columns-3 is-cropped\"><ul class=\"blocks-gallery-grid\"><li class=\"blocks-gallery-item\"><figure><img src=\"https://diegotest4.files.wordpress.com/2020/01/img_0005-1-1.jpg\" data-id=\"2315\" class=\"wp-image-2315\"/><figcaption class=\"blocks-gallery-item__caption\">Asdasdasd</figcaption></figure></li><li class=\"blocks-gallery-item\"><figure><img src=\"https://diegotest4.files.wordpress.com/2020/01/img_0111-1-1.jpg\" data-id=\"2309\" class=\"wp-image-2309\"/><figcaption class=\"blocks-gallery-item__caption\">Asdasdasd</figcaption></figure></li><li class=\"blocks-gallery-item\"><figure><img src=\"https://diegotest4.files.wordpress.com/2020/01/img_0004-1.jpg\" data-id=\"2308\" class=\"wp-image-2308\"/><figcaption class=\"blocks-gallery-item__caption\">Adsasdasdasd</figcaption></figure></li></ul></figure><!-- /wp:gallery --><p>After</p>"

let summary = GutenbergExcerptGenerator.firstParagraph(from: content, maxLength: 150)
#expect(summary == "Before")
#expect(summary == "Before")
}

@Test
func testVideoPressBlock() {
let content = "<p>Before</p>\n<!-- wp:videopress/video {\"title\":\"demo\",\"description\":\"\",\"id\":5297,\"guid\":\"AbCDe\",\"videoRatio\":56.333333333333336,\"privacySetting\":2,\"allowDownload\":false,\"rating\":\"G\",\"isPrivate\":true,\"duration\":1673} -->\n<figure class=\"wp-block-videopress-video wp-block-jetpack-videopress jetpack-videopress-player\"><div class=\"jetpack-videopress-player__wrapper\">\nhttps://videopress.com/v/AbCDe?resizeToParent=true&amp;cover=true&amp;preloadContent=metadata&amp;useAverageColor=true\n</div></figure>\n<!-- /wp:videopress/video -->\n<p>After</p>"

let summary = GutenbergExcerptGenerator.firstParagraph(from: content, maxLength: 150)
#expect(summary == "Before")
#expect(summary == "Before…")
}

@Test func testPostWithBRTags() {
let content = #"<p class="wp-block-paragraph">Yes,<br>look behind<br>in remembrance and with gratitude.</p><p class="wp-block-paragraph">Then,<br>stay present,<br>or miss memories in the making.</p>"#

let summary = GutenbergExcerptGenerator.firstParagraph(from: content, maxLength: 150)
print(summary)
#expect(summary == "Yes, look behind in remembrance and with gratitude.")
}
}