Beispiel #1
0
        /// <summary>
        /// Decode lrc format file
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public override Lyric Decode(string text)
        {
            // All lyric lines
            var lines = new List <string>();

            // Components
            var atTags   = new List <AtTag>();
            var rubyTags = new List <RubyTag>();

            // Get all lines
            var sr = new StringReader(text);

            for (var s = sr.ReadLine(); s != null; s = sr.ReadLine())
            {
                lines.Add(s);
            }

            // Get all atTag(@) in text
            var atTagComponent = new AtTagParserComponent();

            foreach (var line in lines.ToList())
            {
                var atTag = atTagComponent.Decode(line);
                if (atTag == null)
                {
                    continue;
                }

                atTags.Add(atTag.Value);
                lines.Remove(line);
            }

            // Get all lyric from remain text
            var timeTagComponent = new TimeTagParserComponent();

            var lyric = new Lyric
            {
                Lines = lines.Select(line => timeTagComponent.Decode(line)).ToArray()
            };

            // Process ruby tags
            var rubyTagComponent = new RubyTagParserComponent(lyric);

            foreach (var atTag in atTags.ToList())
            {
                var rubyTag = rubyTagComponent.Decode(atTag);
                if (rubyTag == null)
                {
                    continue;
                }

                rubyTags.Add(rubyTag.Value);
                atTags.Remove(atTag);
            }

            lyric.RubyTags = rubyTags.ToArray();
            return(lyric);
        }
        public void TestAtTagEncode(string rubyTag, string parent, string ruby)
        {
            // At tag component
            var atTagComponent = new AtTagParserComponent();
            var result         = atTagComponent.Decode(rubyTag);

            // Ruby tag component(without filter duplicated)
            var atRubyTagComponent = new RubyTagParserComponent(null, false);
            var rubyResult         = atRubyTagComponent.Decode(result.Value);

            // Check is not null;
            Assert.AreEqual(rubyResult.HasValue, true);

            var rubyValue = rubyResult.Value;

            Assert.AreEqual(rubyValue.Parent, parent);
            Assert.AreEqual(rubyValue.Ruby, ruby);
        }