Пример #1
0
        protected override void Write(AttributedTextRenderer renderer, HtmlInline obj)
        {
            var tag = obj.Tag;

            if (tag.StartsWith("<span ", StringComparison.Ordinal) &&
                !tag.EndsWith("/>", StringComparison.Ordinal))
            {
                var styleIndex = tag.IndexOf("style", StringComparison.Ordinal);
                if (styleIndex >= 0)
                {
                    var firstQuote = tag.IndexOf('"', styleIndex);
                    if (firstQuote > styleIndex)
                    {
                        firstQuote += 1;
                        var secondQuote = tag.IndexOf('"', firstQuote);
                        if (secondQuote > firstQuote)
                        {
                            var style    = tag.Substring(firstQuote, secondQuote - firstQuote);
                            var start    = renderer.Count;
                            var spanData = new SpanData {
                                Start = start, Style = style
                            };
                            _spans.Push(spanData);
                        }
                    }
                }
            }
            else if (tag.Equals("</span>", StringComparison.Ordinal))
            {
                if (_spans.Count > 0)
                {
                    var span   = _spans.Pop();
                    var end    = renderer.Count;
                    var length = end - span.Start;

                    var values = SimpleCssParser.Parse(span.Style);
                    if (values != null)
                    {
                        var attributes = new TextAttributes();

                        string color = null;
                        if (values.TryGetValue("color", out color))
                        {
                            attributes[TextAttribute.Color] = color;
                        }

                        string background = null;
                        if (values.TryGetValue("background", out background))
                        {
                            attributes[TextAttribute.Background] = background;
                        }

                        if (attributes.Count > 0)
                        {
                            renderer.AddTextRun(span.Start, length, attributes);
                        }
                    }
                }
            }
        }
Пример #2
0
        protected override void Write(
            AttributedTextRenderer renderer,
            ListBlock listBlock)
        {
            var start = renderer.Count;

            renderer.EnsureLine();

            foreach (var item in listBlock)
            {
                var listItem = (ListItemBlock)item;

                renderer.EnsureLine();
                renderer.Write("• ");
                renderer.WriteChildren(listItem);
            }

            var length = renderer.Count - start;

            if (length > 0)
            {
                var attributes = new TextAttributes();
                if (!listBlock.IsOrdered)
                {
                    attributes[TextAttribute.UnorderedList] = "True";
                }
                renderer.AddTextRun(start, length, attributes);
            }
        }
Пример #3
0
        protected override void Write(AttributedTextRenderer renderer, EmphasisInline obj)
        {
            var start = renderer.Count;

            renderer.WriteChildren(obj);
            var length = renderer.Count - start;

            var bold          = IsBold(obj);
            var underline     = IsUnderline(obj);
            var italic        = IsItalic(obj);
            var strikethrough = IsStrikethrough(obj);
            var subscript     = IsSubscript(obj);
            var superscript   = IsSuperscript(obj);

            if (length > 0 && (bold || italic || strikethrough || subscript || superscript || underline))
            {
                var attributes = new TextAttributes();
                attributes.SetBold(bold);
                attributes.SetItalic(italic);
                attributes.SetStrikethrough(strikethrough);
                attributes.SetSubscript(subscript);
                attributes.SetSuperscript(superscript);
                attributes.SetUnderline(underline);
                renderer.AddTextRun(start, length, attributes);
            }
        }