Beispiel #1
0
        public override void ApplyStyles(DocumentStyle style, DependencyObject ctrl, HtmlFragment fragment)
        {
            var li = ctrl as Paragraph;
            var currentStyle = style.Li;

            if (!string.IsNullOrEmpty(currentStyle?.Bullet))
            {
                currentStyle.RegisterPropertyChangedCallback(ListStyle.BulletProperty, (sender, dp) =>
                {
                    var r = li.Inlines[0] as Run;
                    if (r != null)
                    {
                        r.Text = currentStyle?.Bullet;
                    }
                });

                li.Inlines.Insert(0, new Run
                {
                    Text = currentStyle.Bullet
                });
                li.Inlines.Insert(1, new Run
                {
                    Text = " "
                });
            }
            ApplyParagraphStyles(li, currentStyle);
        }
Beispiel #2
0
        private static string AddFragments(TagReader reader, HtmlFragment parentFragment, Stack<string> openTagStack)
        {
            while (reader.Read())
            {
                parentFragment.TryToAddText(HtmlText.Create(reader.Document, reader.PreviousTag, reader.CurrentTag));

                if (reader.CurrentTag.TagType == TagType.Close && openTagStack.Any(t => t.Equals(reader.CurrentTag.Name, StringComparison.CurrentCultureIgnoreCase)))
                {
                    return reader.CurrentTag.Name;
                }

                var node = parentFragment.AddNode(reader.CurrentTag);
                node.Parent = parentFragment;

                if (reader.CurrentTag.TagType == TagType.Open)
                {
                    openTagStack.Push(reader.CurrentTag.Name);

                    var lastClosed = AddFragments(reader, node, openTagStack);
                    if (lastClosed != openTagStack.Pop())
                    {
                        return lastClosed;
                    }
                }
            }
            parentFragment.TryToAddText(HtmlText.Create(reader.Document, reader.CurrentTag, null));
            return null;
        }
Beispiel #3
0
        public override void ApplyStyles(DocumentStyle style, DependencyObject ctrl, HtmlFragment fragment)
        {
            var grid = ctrl as Grid;
            var vb = grid.GetChild<Viewbox>(0, 0);

            ApplyImageStyles(vb, GetStyle(style));
        }
Beispiel #4
0
 public override void ApplyStyles(DocumentStyle style, DependencyObject ctrl, HtmlFragment fragment)
 {
     if (!IsInline(fragment))
     {
         ApplyImageStyles(ctrl as Viewbox, style.Img);
     }
 }
Beispiel #5
0
        private static string AddFragments(TagReader reader, HtmlFragment parentFragment, Stack <string> openTagStack)
        {
            while (reader.Read())
            {
                parentFragment.TryToAddText(HtmlText.Create(reader.Document, reader.PreviousTag, reader.CurrentTag));

                if (reader.CurrentTag.TagType == TagType.Close && openTagStack.Any(t => t.Equals(reader.CurrentTag.Name, StringComparison.CurrentCultureIgnoreCase)))
                {
                    return(reader.CurrentTag.Name);
                }

                var node = parentFragment.AddNode(reader.CurrentTag);
                node.Parent = parentFragment;

                if (reader.CurrentTag.TagType == TagType.Open)
                {
                    openTagStack.Push(reader.CurrentTag.Name);

                    var lastClosed = AddFragments(reader, node, openTagStack);
                    if (lastClosed != openTagStack.Pop())
                    {
                        return(lastClosed);
                    }
                }
            }
            return(null);
        }
Beispiel #6
0
        public override DependencyObject GetControl(HtmlFragment fragment)
        {
            var node = fragment as HtmlNode;
            var src = GetImageSrc(node);
            if (node != null && !string.IsNullOrEmpty(src))
            {
                Uri uri;

                if (Uri.TryCreate(src, UriKind.Absolute, out uri))
                {
                    try
                    {
                        var viewbox = CreateImage(node, src);

                        if (IsInline(fragment))
                        {
                            return new InlineUIContainer
                            {
                                Child = viewbox
                            };
                        }
                        else
                        {
                            return viewbox;
                        }
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine($"Error loading img@src '{uri?.ToString()}': {ex.Message}");
                    }
                }
            }
            return null;
        }
Beispiel #7
0
        public override void ApplyStyles(DocumentStyle style, DependencyObject ctrl, HtmlFragment fragment)
        {
            var column = ctrl as GridColumn;
            var border = column.Row.Container.GetChild<Border>(column.Index, column.Row.Index);
            if (border != null)
            {
                var tableBorder = GetTableBorder(fragment.AsNode());
                if (tableBorder.HasValue)
                {
                    border.BorderThickness = new Thickness(tableBorder.Value);
                    border.BorderBrush = style.Table?.BorderForeground ?? new SolidColorBrush(Colors.Black);
                }
                else if (style.Table != null && !double.IsNaN(style.Table.Border.Top))
                {
                    BindingOperations.SetBinding(border, Border.BorderThicknessProperty, CreateBinding(style.Table, "Border"));
                    BindingOperations.SetBinding(border, Border.BorderBrushProperty, CreateBinding(style.Table, "BorderForeground"));
                }

                if (style.Table != null)
                {
                    BindingOperations.SetBinding(border, Border.MarginProperty, CreateBinding(style.Td, "Margin"));
                    BindingOperations.SetBinding(border, Border.PaddingProperty, CreateBinding(style.Td, "Padding"));
                }
            }
        }
Beispiel #8
0
        public override void ApplyStyles(DocumentStyle style, DependencyObject ctrl, HtmlFragment fragment)
        {
            var caption = ctrl as Paragraph;
            caption.TextAlignment = Parse(style.Img.HorizontalAlignment);

            ApplyParagraphStyles(caption, style.FigCaption);
        }
Beispiel #9
0
 public override DependencyObject GetControl(HtmlFragment fragment)
 {
     return new GridColumn()
     {
         ColSpan = GetSpan(fragment.AsNode(), "colspan"),
         RowSpan = GetSpan(fragment.AsNode(), "rowspan"),
     };
 }
Beispiel #10
0
 public override DependencyObject GetControl(HtmlFragment fragment)
 {
     //LineBreak doesn't work with hyperlink
     return new Run
     {
         Text = Environment.NewLine
     };
 }
Beispiel #11
0
 public override DependencyObject GetControl(HtmlFragment fragment)
 {
     var text = fragment?.AsText();
     if (text != null && !string.IsNullOrEmpty(text.Content))
     {
         return new Run
         {
             Text = text.Content
         };
     }
     return null;
 }
Beispiel #12
0
 protected static string GetIframeSrc(HtmlFragment fragment)
 {
     var node = fragment.AsNode();
     if (node != null)
     {
         if (node.Attributes.ContainsKey("src"))
         {
             return node.Attributes["src"];
         }
     }
     return string.Empty;
 }
Beispiel #13
0
        public override void ApplyStyles(DocumentStyle style, DependencyObject ctrl, HtmlFragment fragment)
        {
            if (style?.Table != null)
            {
                var grid = ctrl as Grid;

                BindingOperations.SetBinding(grid, Grid.HorizontalAlignmentProperty, CreateBinding(style.Table, "HorizontalAlignment"));

                foreach (var columnDefinition in grid.ColumnDefinitions)
                {
                    BindingOperations.SetBinding(columnDefinition, ColumnDefinition.WidthProperty, CreateBinding(style.Table, "ColumnWidth"));
                }

                ApplyContainerStyles(grid, style.Table);
            }
        }
Beispiel #14
0
        public override DependencyObject GetControl(HtmlFragment fragment)
        {
            var node = fragment as HtmlNode;
            if (node != null)
            {
                var grid = new Grid
                {
                    HorizontalAlignment = HorizontalAlignment.Stretch,
                };

                grid.Tapped += (sender, e) =>
                {
                    Launcher.LaunchUriAsync(new Uri(GetIframeSrc(node))).AsTask().FireAndForget();
                };

                grid.PointerEntered += (sender, e) =>
                {
                    Window.Current.CoreWindow.PointerCursor = _handCursor;
                };
                grid.PointerExited += (sender, e) =>
                {
                    Window.Current.CoreWindow.PointerCursor = _arrowCursor;
                };

                AddColumn(grid);
                AddColumn(grid);
                AddColumn(grid);

                var screenShot = GetImageControl((i) => SetScreenshot(i, node));

                Grid.SetColumn(screenShot, 0);
                Grid.SetColumnSpan(screenShot, 3);
                grid.Children.Add(screenShot);

                var player = GetImageControl((i) => i.Source = GetPlayerImage());

                Grid.SetColumn(player, 1);
                grid.Children.Add(player);

                return grid;
            }

            return null;
        }
Beispiel #15
0
 private static ContainerStyle GetDocumentStyle(HtmlFragment fragment, DocumentStyle style)
 {
     if (style == null)
     {
         return null;
     }
     switch (fragment.Name.ToLowerInvariant())
     {
         case "div":
         case "tbody":
             return style.Div;
         case "ul":
             return style.Ul;
         case "ol":
             return style.Ol;
         case "dl":
             return style.Dl;
         case "section":
             return style.Section;
         case "article":
             return style.Article;
         case "header":
             return style.Header;
         case "footer":
             return style.Footer;
         case "main":
             return style.Main;
         case "figure":
             return style.Figure;
         case "details":
             return style.Details;
         case "summary":
             return style.Summary;
         default:
             return null;
     }
 }
Beispiel #16
0
 private static ParagraphStyle GetDocumentStyle(HtmlFragment fragment, DocumentStyle style)
 {
     if (style == null)
     {
         return null;
     }
     switch (fragment.Name.ToLowerInvariant())
     {
         case "h1":
             return style.H1;
         case "h2":
             return style.H2;
         case "h3":
             return style.H3;
         case "h4":
             return style.H4;
         case "h5":
             return style.H5;
         case "h6":
             return style.H6;
         default:
             return null;
     }
 }
Beispiel #17
0
 public abstract DependencyObject GetControl(HtmlFragment fragment);
Beispiel #18
0
 public override DependencyObject GetControl(HtmlFragment fragment)
 {
     return new Paragraph();
 }
 public override void ApplyStyles(DocumentStyle style, DependencyObject ctrl, HtmlFragment fragment)
 {
     ApplyParagraphStyles(ctrl as Paragraph, style.Dt);
 }
Beispiel #20
0
 public override bool Match(HtmlFragment fragment)
 {
     var src = GetIframeSrc(fragment);
     return fragment.Name == "iframe" && !string.IsNullOrWhiteSpace(src) && src.ToLowerInvariant().Contains("channel9.msdn.com");
 }
Beispiel #21
0
 public override DependencyObject GetControl(HtmlFragment fragment)
 {
     return new GridRow();
 }
Beispiel #22
0
        private void WriteFragments(HtmlFragment fragment, DocumentContainer parentContainer)
        {
            if (parentContainer != null)
            {
                foreach (var childFragment in fragment.Fragments)
                {
                    var writer = HtmlWriterFactory.Find(childFragment);

                    var ctrl = writer?.GetControl(childFragment);

                    if (ctrl != null)
                    {
                        if (!parentContainer.CanContain(ctrl))
                        {
                            var antecesorContainer = parentContainer.Find(ctrl);

                            if (antecesorContainer == null)
                            {
                                continue;
                            }
                            else
                            {
                                parentContainer = antecesorContainer;
                            }
                        }

                        var currentContainer = parentContainer.Append(ctrl);

                        WriteFragments(childFragment, currentContainer);

                        writer?.ApplyStyles(_docStyles, ctrl, childFragment);
                    }
                }
            }
        }
Beispiel #23
0
 private static bool IsInline(HtmlFragment fragment)
 {
     return fragment.Parent != null && fragment.Parent.Name == "p";
 }
Beispiel #24
0
        public static HtmlWriter Find(HtmlFragment fragment)
        {
            EnsureWriters();

            return _writers.FirstOrDefault(w => w.Match(fragment)); ;
        }
Beispiel #25
0
 public override void ApplyStyles(DocumentStyle style, DependencyObject ctrl, HtmlFragment fragment)
 {
     ApplyTextStyles(ctrl as Span, style.Q);
 }
Beispiel #26
0
 public override void ApplyStyles(DocumentStyle style, DependencyObject ctrl, HtmlFragment fragment)
 {
     ApplyContainerStyles(ctrl as Grid, GetDocumentStyle(fragment, style));
 }
Beispiel #27
0
 public virtual bool Match(HtmlFragment fragment)
 {
     return fragment != null && !string.IsNullOrEmpty(fragment.Name) && TargetTags.Any(t => t.Equals(fragment.Name, StringComparison.CurrentCultureIgnoreCase));
 }
Beispiel #28
0
 public virtual void ApplyStyles(DocumentStyle style, DependencyObject ctrl, HtmlFragment fragment)
 {
 }
Beispiel #29
-1
        public override DependencyObject GetControl(HtmlFragment fragment)
        {
            var node = fragment as HtmlNode;
            if (node != null && node.Attributes.ContainsKey("href"))
            {
                Hyperlink a = new Hyperlink();

                Uri uri;

                if (Uri.TryCreate(node.Attributes["href"], UriKind.Absolute, out uri))
                {
                    try
                    {
                        a.NavigateUri = uri;
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine($"Error loading a@href '{uri?.ToString()}': {ex.Message}");
                    }
                }

                return a;
            }
            return null;
        }