Ejemplo n.º 1
0
        /// <summary>
        /// Converts a Markdown string to XAML and output to the specified writer.
        /// </summary>
        /// <param name="markdown">A Markdown text.</param>
        /// <param name="writer">The destination <see cref="TextWriter"/> that will receive the result of the conversion.</param>
        /// <param name="pipeline">The pipeline used for the conversion.</param>
        /// <returns>The Markdown document that has been parsed</returns>
        /// <exception cref="ArgumentNullException">if reader or writer variable are null</exception>
        public static MarkdownDocument ToXaml([NotNull] string markdown, [NotNull] TextWriter writer,
                                              [CanBeNull] MarkdownPipeline pipeline = null)
        {
            if (markdown == null)
            {
                throw new ArgumentNullException(nameof(markdown));
            }
            if (writer == null)
            {
                throw new ArgumentNullException(nameof(writer));
            }
            pipeline = pipeline ?? new MarkdownPipelineBuilder().Build();

            // We override the renderer with our own writer
            var renderer = new XamlRenderer(writer);

            pipeline.Setup(renderer);

            var document = Markdig.Markdown.Parse(markdown, pipeline);

            renderer.Render(document);
            writer.Flush();

            return(document);
        }
Ejemplo n.º 2
0
        public void Present(Proposal action)
        {
            var needsRefresh = false;

            if (action is SetFile)
            {
                _filename    = ((SetFile)action).FileName;
                _source      = ((SetFile)action).Source;
                needsRefresh = true;
                _lastError   = null;
            }
            else if (action is RefreshFromFile)
            {
                _source      = ((RefreshFromFile)action).Source;
                needsRefresh = true;
                _lastError   = null;
            }
            else if (action is SelectElement)
            {
                var elem = ((SelectElement)action).Element;
                _selected  = elem;
                _lastError = null;
            }
            else if (action is ShowError)
            {
                var act = ((ShowError)action);
                _lastError = new Exception(act.Context, act.Exception);
            }
            else
            {
                throw new Exception("Unsupported action type: " + action.GetType().Name);
            }

            if (needsRefresh)
            {
                try
                {
                    var render = new XamlRenderer(null);

                    byte[] byteArray = Encoding.UTF8.GetBytes(_source);
                    using (var stream = new System.IO.MemoryStream(byteArray))
                    {
                        var tree = render.Render(stream);

                        this._selected = tree;
                        this._tree     = tree;
                    }
                }
                catch (Exception e)
                {
                    // keep everything else intact
                    this._lastError = new Exception("Failed to refresh from file", e);
                }
            }
        }
Ejemplo n.º 3
0
        private void OnPropertyChanged(DependencyObject d, DependencyProperty property)
        {
            if (MarkdownText == null || MarkdownText == "")
            {
                return;
            }
            MarkdownDocument document = MarkdownDocument.Parse(MarkdownText);
            XamlRenderer     renderer = new XamlRenderer();

            Content = renderer.Render(document);
        }
        /// <summary>
        /// Called to preform a render of the current Markdown.
        /// </summary>
        private void RenderMarkdown()
        {
            // Make sure we have something to parse.
            if (string.IsNullOrWhiteSpace(Text))
            {
                return;
            }

            // Leave if we don't have our root yet.
            if (_rootElement == null)
            {
                return;
            }

            // Disconnect from OnClick handlers.
            UnhookListeners();

            MarkdownRenderedEventArgs markdownRenderedArgs = new MarkdownRenderedEventArgs(null);

            try
            {
                // Try to parse the markdown.
                MarkdownDocument markdown = new MarkdownDocument();
                markdown.Parse(Text);

                // Now try to display it
                var renderer = new XamlRenderer(markdown, this, this)
                {
                    Background             = Background,
                    BorderBrush            = BorderBrush,
                    BorderThickness        = BorderThickness,
                    CharacterSpacing       = CharacterSpacing,
                    FontFamily             = FontFamily,
                    FontSize               = FontSize,
                    FontStretch            = FontStretch,
                    FontStyle              = FontStyle,
                    FontWeight             = FontWeight,
                    Foreground             = Foreground,
                    IsTextSelectionEnabled = IsTextSelectionEnabled,
                    Padding                 = Padding,
                    CodeBackground          = CodeBackground,
                    CodeBorderBrush         = CodeBorderBrush,
                    CodeBorderThickness     = CodeBorderThickness,
                    CodeForeground          = CodeForeground,
                    CodeFontFamily          = CodeFontFamily,
                    CodePadding             = CodePadding,
                    CodeMargin              = CodeMargin,
                    Header1FontSize         = Header1FontSize,
                    Header1FontWeight       = Header1FontWeight,
                    Header1Margin           = Header1Margin,
                    Header1Foreground       = Header1Foreground,
                    Header2FontSize         = Header2FontSize,
                    Header2FontWeight       = Header2FontWeight,
                    Header2Margin           = Header2Margin,
                    Header2Foreground       = Header2Foreground,
                    Header3FontSize         = Header3FontSize,
                    Header3FontWeight       = Header3FontWeight,
                    Header3Margin           = Header3Margin,
                    Header3Foreground       = Header3Foreground,
                    Header4FontSize         = Header4FontSize,
                    Header4FontWeight       = Header4FontWeight,
                    Header4Margin           = Header4Margin,
                    Header4Foreground       = Header4Foreground,
                    Header5FontSize         = Header5FontSize,
                    Header5FontWeight       = Header5FontWeight,
                    Header5Margin           = Header5Margin,
                    Header5Foreground       = Header5Foreground,
                    Header6FontSize         = Header6FontSize,
                    Header6FontWeight       = Header6FontWeight,
                    Header6Margin           = Header6Margin,
                    Header6Foreground       = Header6Foreground,
                    HorizontalRuleBrush     = HorizontalRuleBrush,
                    HorizontalRuleMargin    = HorizontalRuleMargin,
                    HorizontalRuleThickness = HorizontalRuleThickness,
                    ListMargin              = ListMargin,
                    ListGutterWidth         = ListGutterWidth,
                    ListBulletSpacing       = ListBulletSpacing,
                    ParagraphMargin         = ParagraphMargin,
                    QuoteBackground         = QuoteBackground,
                    QuoteBorderBrush        = QuoteBorderBrush,
                    QuoteBorderThickness    = QuoteBorderThickness,
                    QuoteForeground         = QuoteForeground,
                    QuoteMargin             = QuoteMargin,
                    QuotePadding            = QuotePadding,
                    TableBorderBrush        = TableBorderBrush,
                    TableBorderThickness    = TableBorderThickness,
                    TableCellPadding        = TableCellPadding,
                    TableMargin             = TableMargin,
                    TextWrapping            = TextWrapping,
                    LinkForeground          = LinkForeground,
                    ImageStretch            = ImageStretch
                };
                _rootElement.Child = renderer.Render();
            }
            catch (Exception ex)
            {
                DebuggingReporter.ReportCriticalError("Error while parsing and rendering: " + ex.Message);
                markdownRenderedArgs = new MarkdownRenderedEventArgs(ex);
            }

            // Indicate that the parse is done.
            MarkdownRendered?.Invoke(this, markdownRenderedArgs);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Fired when the value of a DependencyProperty is changed.
        /// </summary>
        /// <param name="d"></param>
        /// <param name="prop"></param>
        private void OnPropertyChanged(DependencyObject d, DependencyProperty prop)
        {
            // Make sure we have something to parse.
            if (Markdown == null)
            {
                return;
            }

            // Disconnect from OnClick handlers.
            UnhookListeners();

            var args = new OnMarkdownReadyArgs();

            try
            {
                // Try to parse the markdown.
                MarkdownDocument markdown = new MarkdownDocument();
                markdown.Parse(Markdown);

                // Now try to display it
                var renderer = new XamlRenderer(markdown, this);
                renderer.Background             = Background;
                renderer.BorderBrush            = BorderBrush;
                renderer.BorderThickness        = BorderThickness;
                renderer.CharacterSpacing       = CharacterSpacing;
                renderer.FontFamily             = FontFamily;
                renderer.FontSize               = FontSize;
                renderer.FontStretch            = FontStretch;
                renderer.FontStyle              = FontStyle;
                renderer.FontWeight             = FontWeight;
                renderer.Foreground             = Foreground;
                renderer.IsTextSelectionEnabled = IsTextSelectionEnabled;
                renderer.Padding                 = Padding;
                renderer.CodeBackground          = CodeBackground;
                renderer.CodeBorderBrush         = CodeBorderBrush;
                renderer.CodeBorderThickness     = CodeBorderThickness;
                renderer.CodeForeground          = CodeForeground;
                renderer.CodeFontFamily          = CodeFontFamily;
                renderer.CodePadding             = CodePadding;
                renderer.CodeMargin              = CodeMargin;
                renderer.Header1FontSize         = Header1FontSize;
                renderer.Header1FontWeight       = Header1FontWeight;
                renderer.Header1Margin           = Header1Margin;
                renderer.Header2FontSize         = Header2FontSize;
                renderer.Header2FontWeight       = Header2FontWeight;
                renderer.Header2Margin           = Header2Margin;
                renderer.Header3FontSize         = Header3FontSize;
                renderer.Header3FontWeight       = Header3FontWeight;
                renderer.Header3Margin           = Header3Margin;
                renderer.Header4FontSize         = Header4FontSize;
                renderer.Header4FontWeight       = Header4FontWeight;
                renderer.Header4Margin           = Header4Margin;
                renderer.Header5FontSize         = Header5FontSize;
                renderer.Header5FontWeight       = Header5FontWeight;
                renderer.Header5Margin           = Header5Margin;
                renderer.Header6FontSize         = Header6FontSize;
                renderer.Header6FontWeight       = Header6FontWeight;
                renderer.Header6Margin           = Header6Margin;
                renderer.HorizontalRuleBrush     = HorizontalRuleBrush;
                renderer.HorizontalRuleMargin    = HorizontalRuleMargin;
                renderer.HorizontalRuleThickness = HorizontalRuleThickness;
                renderer.ListMargin              = ListMargin;
                renderer.ListGutterWidth         = ListGutterWidth;
                renderer.ListBulletSpacing       = ListBulletSpacing;
                renderer.ParagraphMargin         = ParagraphMargin;
                renderer.QuoteBackground         = QuoteBackground;
                renderer.QuoteBorderBrush        = QuoteBorderBrush;
                renderer.QuoteBorderThickness    = QuoteBorderThickness;
                renderer.QuoteForeground         = QuoteForeground;
                renderer.QuoteMargin             = QuoteMargin;
                renderer.QuotePadding            = QuotePadding;
                renderer.TableBorderBrush        = TableBorderBrush;
                renderer.TableBorderThickness    = TableBorderThickness;
                renderer.TableCellPadding        = TableCellPadding;
                renderer.TableMargin             = TableMargin;
                renderer.TextWrapping            = TextWrapping;
                Content = renderer.Render();
            }
            catch (Exception ex)
            {
                DebuggingReporter.ReportCriticalError("Error while parsing and rendering: " + ex.Message);
                args.WasError  = true;
                args.Exception = ex;
            }

            // #todo indicate if ready
            m_onMarkdownReady.Raise(this, args);
        }