diff --git a/VisualCard.Calendar/Parsers/VCalendarParser.cs b/VisualCard.Calendar/Parsers/VCalendarParser.cs index f043853..86f5a01 100644 --- a/VisualCard.Calendar/Parsers/VCalendarParser.cs +++ b/VisualCard.Calendar/Parsers/VCalendarParser.cs @@ -69,14 +69,12 @@ public Parts.Calendar Parse() var calendar = new Parts.Calendar(CalendarVersion); // Iterate through all the lines - bool constructing = false; - StringBuilder valueBuilder = new(); List<(string, Parts.Calendar)> begins = []; for (int i = 0; i < CalendarContent.Length; i++) { // Get line var content = CalendarContent[i]; - string _value = content.Item2; + string _value = VcardCommonTools.ConstructBlocks(CalendarContent, ref i); int lineNumber = content.Item1; if (string.IsNullOrEmpty(_value)) continue; @@ -86,24 +84,6 @@ public Parts.Calendar Parse() if (begins.Count > 0) subPart = begins[begins.Count - 1].Item2; - // First, check to see if we need to construct blocks - string secondLine = i + 1 < CalendarContent.Length ? CalendarContent[i + 1].Item2 : ""; - bool firstConstructedLine = !_value.StartsWith(VcardConstants._spaceBreak) && !_value.StartsWith(VcardConstants._tabBreak); - constructing = secondLine.StartsWithAnyOf([VcardConstants._spaceBreak, VcardConstants._tabBreak]); - secondLine = secondLine.Length > 1 ? secondLine.Substring(1) : ""; - if (constructing) - { - if (firstConstructedLine) - valueBuilder.Append(_value); - valueBuilder.Append(secondLine); - continue; - } - else if (!firstConstructedLine) - { - _value = valueBuilder.ToString(); - valueBuilder.Clear(); - } - try { // Now, parse a property diff --git a/VisualCard.Tests/ContactData.cs b/VisualCard.Tests/ContactData.cs index e91feb7..03ddde1 100644 --- a/VisualCard.Tests/ContactData.cs +++ b/VisualCard.Tests/ContactData.cs @@ -143,8 +143,8 @@ public static class ContactData VERSION:3.0 TEL:495-522-3560 EMAIL:john.s@acme.co - FN:John Sanders NOTE:Note test for VisualCard + FN:John Sanders N:Sanders;John;;; ADR:;;Los Angeles;;;;USA END:VCARD @@ -159,11 +159,11 @@ public static class ContactData TEL:495-522-3560 TEL;TYPE=VIDEO:495-522-3550 EMAIL:john.s@acme.co - FN:John Sanders NOTE:Note test for VisualCard + FN:John Sanders N:Sanders;John;;; - ADR:;;Los Angeles;;;;USA X-VISUALCARD-KANA:Saunders;John + ADR:;;Los Angeles;;;;USA END:VCARD """ diff --git a/VisualCard/Parsers/VcardCommonTools.cs b/VisualCard/Parsers/VcardCommonTools.cs index c36cabf..ddb4721 100644 --- a/VisualCard/Parsers/VcardCommonTools.cs +++ b/VisualCard/Parsers/VcardCommonTools.cs @@ -24,6 +24,7 @@ using System.Linq; using System.Text; using System.Text.RegularExpressions; +using Textify.General; using VisualCard.Parsers.Arguments; using VisualCard.Parsers.Recurrence; using VisualCard.Parts.Enums; @@ -696,5 +697,38 @@ internal static CardKind GetKindEnum(string kind) => "location" => CardKind.Location, _ => CardKind.Others, }; + + internal static string ConstructBlocks((int, string)[] cardContent, ref int i) + { + StringBuilder valueBuilder = new(); + bool constructing = false; + int idx; + for (idx = i; idx < cardContent.Length; idx++) + { + // Get line + var content = cardContent[idx]; + string _value = content.Item2; + if (string.IsNullOrEmpty(_value)) + continue; + + // First, check to see if we need to construct blocks + string secondLine = idx + 1 < cardContent.Length ? cardContent[idx + 1].Item2 : ""; + bool firstConstructedLine = !_value.StartsWith(VcardConstants._spaceBreak) && !_value.StartsWith(VcardConstants._tabBreak); + constructing = secondLine.StartsWithAnyOf([VcardConstants._spaceBreak, VcardConstants._tabBreak]); + secondLine = secondLine.Length > 1 ? secondLine.Substring(1) : ""; + if (constructing) + { + if (firstConstructedLine) + valueBuilder.Append(_value); + valueBuilder.Append(secondLine); + continue; + } + else if (firstConstructedLine && !constructing) + valueBuilder.Append(_value); + break; + } + i = idx; + return valueBuilder.ToString(); + } } } diff --git a/VisualCard/Parsers/VcardParser.cs b/VisualCard/Parsers/VcardParser.cs index 309551b..42ba6db 100644 --- a/VisualCard/Parsers/VcardParser.cs +++ b/VisualCard/Parsers/VcardParser.cs @@ -75,7 +75,6 @@ public Card Parse() } // Iterate through all the lines - bool constructing = false; StringBuilder valueBuilder = new(); string[] allowedTypes = ["HOME", "WORK", "PREF"]; string kind = "individual"; @@ -83,29 +82,11 @@ public Card Parse() { // Get line var content = CardContent[i]; - string _value = content.Item2; + string _value = VcardCommonTools.ConstructBlocks(CardContent, ref i); int lineNumber = content.Item1; if (string.IsNullOrEmpty(_value)) continue; - // First, check to see if we need to construct blocks - string secondLine = i + 1 < CardContent.Length ? CardContent[i + 1].Item2 : ""; - bool firstConstructedLine = !_value.StartsWith(VcardConstants._spaceBreak) && !_value.StartsWith(VcardConstants._tabBreak); - constructing = secondLine.StartsWithAnyOf([VcardConstants._spaceBreak, VcardConstants._tabBreak]); - secondLine = secondLine.Length > 1 ? secondLine.Substring(1) : ""; - if (constructing) - { - if (firstConstructedLine) - valueBuilder.Append(_value); - valueBuilder.Append(secondLine); - continue; - } - else if (!firstConstructedLine) - { - _value = valueBuilder.ToString(); - valueBuilder.Clear(); - } - try { // Now, parse a property