From 65ffad74c37fdb8a38b19e72b37dfcc96212567e Mon Sep 17 00:00:00 2001 From: sanketfirodiya Date: Tue, 1 May 2018 17:52:17 -0700 Subject: [PATCH] Improve parsing description of calendar event --- CalendarKit-Swift.xcodeproj/project.pbxproj | 4 +-- .../SwiftCal/ICSEventParser.swift | 30 ++++++++++++------- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/CalendarKit-Swift.xcodeproj/project.pbxproj b/CalendarKit-Swift.xcodeproj/project.pbxproj index 718e013..8c8a34c 100644 --- a/CalendarKit-Swift.xcodeproj/project.pbxproj +++ b/CalendarKit-Swift.xcodeproj/project.pbxproj @@ -737,7 +737,7 @@ ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CODE_SIGN_STYLE = Automatic; - DEVELOPMENT_TEAM = 6UJ7Z72VQ8; + DEVELOPMENT_TEAM = UMWTWMN2C7; INFOPLIST_FILE = "CalendarKit-Swift/Info.plist"; IPHONEOS_DEPLOYMENT_TARGET = 10.0; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; @@ -754,7 +754,7 @@ ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CODE_SIGN_STYLE = Automatic; - DEVELOPMENT_TEAM = 6UJ7Z72VQ8; + DEVELOPMENT_TEAM = UMWTWMN2C7; INFOPLIST_FILE = "CalendarKit-Swift/Info.plist"; IPHONEOS_DEPLOYMENT_TARGET = 10.0; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; diff --git a/CalendarKit-Swift/SwiftCal/ICSEventParser.swift b/CalendarKit-Swift/SwiftCal/ICSEventParser.swift index 22ae30e..e79a4d8 100644 --- a/CalendarKit-Swift/SwiftCal/ICSEventParser.swift +++ b/CalendarKit-Swift/SwiftCal/ICSEventParser.swift @@ -260,23 +260,31 @@ class ICSEventParser: NSObject { } private static func description(from icsString: String) -> String? { - + var descriptionString: NSString? var eventScanner = Scanner(string: icsString) eventScanner.charactersToBeSkipped = newlineCharacterSet() - eventScanner.scanUpTo(ICS.description, into: nil) + + // Handle `DESCRIPTION;LANGUAGE=en-US:Dear Gary, Attached is the ...` format + eventScanner = Scanner(string: icsString) + eventScanner.charactersToBeSkipped = newlineCharacterSet() + eventScanner.scanUpTo(ICS.description2, into: nil) + eventScanner.scanUpTo(":", into: nil) + eventScanner.scanString(":", into: nil) eventScanner.scanUpTo("\n", into: &descriptionString) + if descriptionString != nil { + descriptionString = descriptionString?.replacingOccurrences(of: ICS.description2, with: "").replacingOccurrences(of: "\\n", with: "\n").replacingOccurrences(of: "\\", with: "").trimmingCharacters(in: newlineCharacterSet()) as! NSString + } - // Handle description that has the language tag e.g. - // `DESCRIPTION;LANGUAGE=en-US:Dear Gary, Attached is the ...` - if descriptionString == nil { - eventScanner = Scanner(string: icsString) - eventScanner.charactersToBeSkipped = newlineCharacterSet() - eventScanner.scanUpTo(ICS.description2, into: nil) - eventScanner.scanUpTo(":", into: nil) - eventScanner.scanString(":", into: nil) + // Handle `DESCRIPTION:Dear Gary, Attached is the ...` format + if descriptionString?.length ?? 0 == 0 { + eventScanner.scanLocation = 0 + eventScanner.scanUpTo(ICS.description, into: nil) eventScanner.scanUpTo("\n", into: &descriptionString) + if descriptionString != nil { + descriptionString = descriptionString?.replacingOccurrences(of: ICS.description, with: "").replacingOccurrences(of: "\\n", with: "\n").replacingOccurrences(of: "\\", with: "").trimmingCharacters(in: newlineCharacterSet()) as! NSString + } } // a multi-line description can have newline characters @@ -297,7 +305,7 @@ class ICSEventParser: NSObject { } } - return descriptionString?.replacingOccurrences(of: ICS.description, with: "").replacingOccurrences(of: "\\n", with: "\n").replacingOccurrences(of: "\\", with: "").trimmingCharacters(in: CharacterSet.newlines) + return descriptionString?.replacingOccurrences(of: "\\n", with: "\n").replacingOccurrences(of: "\\", with: "").trimmingCharacters(in: CharacterSet.newlines) } private static func newlineCharacterSet() -> CharacterSet {