Example #1
0
        public IEnumerable <BlockElement> Parse(string text)
        {
            var content = new List <BlockElement>();

            text = text?.Trim();
            if (string.IsNullOrWhiteSpace(text))
            {
                return(content);
            }

            var textBlocks = SplitBlocks(text);

            bool isBold   = false;
            bool isItalic = false;

            foreach ((string blockText, int blockIndentation) in textBlocks)
            {
                if (string.IsNullOrWhiteSpace(blockText))
                {
                    continue;
                }

                var block = new BlockElement(blockIndentation);
                content.Add(block);

                string currentString = blockText;
                while (!string.IsNullOrWhiteSpace(currentString))
                {
                    bool isText = ExtractNextElement(ref currentString, out string element);
                    if (isText)
                    {
                        block.AddElement(new TextRun(element, isBold, isItalic));
                    }
                    else if (element == _bold)
                    {
                        isBold = !isBold;
                    }
                    else if (element == _italic)
                    {
                        isItalic = !isItalic;
                    }
                    else if (ExtractSymbol(element, out string symbol))
                    {
                        block.AddElement(new Symbol(symbol));
                    }
                }
            }

            return(content);
        }
Example #2
0
        private void AddElement(BlockElement element)
        {
            element.TimeSignature = _timeSignature;
            element.Tempo         = _piece.Tempo;

            if (_midiStrategy.OpenBlocks == 0)
            {
                _piece.AddElement(element, true);
            }
            else
            {
                try
                {
                    BlockElement block = (BlockElement)_piece.Elements[_piece.Elements.Count - 1];
                    for (int i = 1; i < _midiStrategy.OpenBlocks; i++)
                    {
                        block = (BlockElement)block.Elements[block.Elements.Count - 1];
                    }

                    block.AddElement(element, true);
                }
                catch (Exception) { }
            }

            _midiStrategy.OpenBlocks++;
        }