Esempio n. 1
0
        private VmTextBlock ParseMarkdownToTextBlock(string text)
        {
            VmTextBlock textBlock = new VmTextBlock();
            var         lines     = text?.Split('\n') ?? new string[0] {
            };

            var typePatterns = new Dictionary <TextFormatClientTypeEnum, string>()
            {
                { TextFormatClientTypeEnum.unorderedListItem, @"\*" },  //ordered
                { TextFormatClientTypeEnum.orderedListItem, @"\d+\." }, //unoredered
                { TextFormatClientTypeEnum.unstyled, @"[^\d+\.\*\s]+" } //rest
            };

            foreach (var line in lines)
            {
                int index      = int.MaxValue;
                int textLength = 0;
                var lineText   = line;
                TextFormatClientTypeEnum type = TextFormatClientTypeEnum.unstyled;

                foreach (KeyValuePair <TextFormatClientTypeEnum, string> valueTypePattern in typePatterns)
                {
                    if (Regex.IsMatch(lineText, valueTypePattern.Value)) //find first occurence
                    {
                        var match = Regex.Match(lineText, valueTypePattern.Value);
                        if (match.Index < index)
                        {
                            index      = match.Index;
                            textLength = match.Length;
                            type       = valueTypePattern.Key;
                        }
                    }
                }

                if (type == TextFormatClientTypeEnum.unorderedListItem)
                {
                    lineText = line.Remove(index, 1);
                }

                if (type == TextFormatClientTypeEnum.orderedListItem)
                {
                    lineText = line.Remove(index, textLength);
                }

                textBlock.Blocks.Add(new VmTextLine()
                {
                    Text = lineText, Type = type, Key = GetUniqueKey(5)
                });
            }

            return(textBlock);
        }
Esempio n. 2
0
        private VmTextBlock ParseLineBreaksToTextBlock(string text)
        {
            VmTextBlock textBlock = new VmTextBlock();
            var         lines     = text?.Split('\n') ?? new string[0] {
            };

            foreach (var line in lines)
            {
                TextFormatClientTypeEnum type = TextFormatClientTypeEnum.unstyled;
                textBlock.Blocks.Add(new VmTextLine()
                {
                    Text = line, Type = type, Key = GetUniqueKey(5)
                });
            }

            return(textBlock);
        }
Esempio n. 3
0
        private VmTextBlock DeserializeJson(string json)
        {
            VmTextBlock result = null;

            json = json?.Trim();

            try
            {
                if (IsValidJson(json))
                {
                    result = JsonConvert.DeserializeObject <VmTextBlock>(json);
                }
            }
            catch (JsonSerializationException ex)
            {
                throw new Exception($"Deserialize failed on JSON: {json}. {ex.Message}", ex);
            }

            return(result ?? new VmTextBlock());
        }