private static void MoveLineBreaksToSeparateSections(AssLine line)
        {
            for (int sectionIdx = line.Sections.Count - 1; sectionIdx >= 0; sectionIdx--)
            {
                AssSection section = (AssSection)line.Sections[sectionIdx];
                Match      match   = Regex.Match(section.Text, @"((?:\r\n)+|[^\r\n]+)+");
                if (!match.Success || match.Groups[1].Captures.Count == 1)
                {
                    continue;
                }

                line.Sections.RemoveAt(sectionIdx);
                for (int i = 0; i < match.Groups[1].Captures.Count; i++)
                {
                    AssSection subSection = (AssSection)section.Clone();
                    subSection.Text = match.Groups[1].Captures[i].Value;
                    line.Sections.Insert(sectionIdx + i, subSection);
                }
            }
        }
Ejemplo n.º 2
0
        protected virtual void WriteLine(AssLine line, StreamWriter writer)
        {
            if (line.Sections.Count == 0)
            {
                return;
            }

            AssStyle style = GetStyleMatchingStructure((AssSection)line.Sections[0]);

            WriteLineMetadata(line, style, writer);

            AssSection prevSection = new AssSection();
            AssStyle   prevStyle   = style;

            ApplyStyle(prevSection, prevStyle);

            AssLineContentBuilder lineContent = new AssLineContentBuilder();

            AppendLineTags(line, style, lineContent);

            RubyPart currentRubyPosition = RubyPart.None;

            for (int i = 0; i < line.Sections.Count; i++)
            {
                AssSection section = (AssSection)line.Sections[i];
                style = GetStyleMatchingStructure(section);
                if (style != prevStyle || (currentRubyPosition != RubyPart.None && section.RubyPart == RubyPart.None))
                {
                    lineContent.AppendTag("r", style);
                    currentRubyPosition = RubyPart.None;
                    prevSection         = (AssSection)prevSection.Clone();
                    ApplyStyle(prevSection, style);
                }

                AppendSectionTags(section, prevSection, lineContent);

                if (section.RubyPart == RubyPart.Text)
                {
                    RubyPart rubyPart;
                    if (i + 4 > line.Sections.Count || ((rubyPart = line.Sections[i + 2].RubyPart) != RubyPart.RubyAbove && rubyPart != RubyPart.RubyBelow))
                    {
                        throw new InvalidDataException("Invalid ruby sequence");
                    }

                    if (rubyPart != currentRubyPosition)
                    {
                        lineContent.AppendTag("ytruby", rubyPart == RubyPart.RubyAbove ? 8 : 2);
                        currentRubyPosition = rubyPart;
                    }
                    lineContent.AppendText($"[{section.Text}/{line.Sections[i + 2].Text}]");
                    i += 3;
                }
                else
                {
                    lineContent.AppendText(section.Text);
                }

                prevSection = section;
                prevStyle   = style;
            }

            writer.WriteLine(lineContent);
        }