コード例 #1
0
ファイル: UtilsAdminApi.cs プロジェクト: pekkah/tanka
        public UtilsAdminApi()
            : base("/api/admin/utils")
        {
            this.RequiresHttpsOrXProto();
            this.RequiresAuthentication();
            this.RequiresClaims(new[] {SystemRoles.Administrators});

            Post["/slugs"] = parameters =>
            {
                var slugDto = this.Bind<SlugDto>();

                if (string.IsNullOrWhiteSpace(slugDto.Text))
                {
                    return global::System.Net.HttpStatusCode.BadRequest;
                }

                string slug = Snail.ToSlug(slugDto.Text);

                return new {Slug = slug};
            };

            Post["/markdown/render"] = parameters =>
            {
                string content = Request.Body.ReadAsString();

                if (string.IsNullOrEmpty(content))
                    return global::System.Net.HttpStatusCode.BadRequest;

                var parser = new MarkdownParser();
                var htmlRenderer = new MarkdownHtmlRenderer();

                string html = string.Empty;

                try
                {
                    Document document = parser.Parse(content);
                    html = htmlRenderer.Render(document);
                }
                catch (ParsingException x)
                {
                    html = string.Format(
                        "Markdown parsing error at {0} as block type {1}",
                        x.Position,
                        x.BuilderType);
                }
                catch (RenderingException renderingException)
                {
                    html = string.Format(
                        "Markdown rendering error with block {0} using {1} renderer",
                        renderingException.Block,
                        renderingException.Renderer);
                }

                return Response.AsText(html, "text/html");

            };
        }
コード例 #2
0
            public MarkdownConverter()
            {
                this.parser   = new MarkdownParser();
                this.renderer = new MarkdownHtmlRenderer();

                var para = this.renderer.Options.Renderers.First(r => r is ParagraphRenderer) as ParagraphRenderer;

                para.SpanRenderers.Insert(0, new SafeTextSpanRenderer());
                for (int i = 0; i < this.renderer.Options.Renderers.Count; i++)
                {
                    if (this.renderer.Options.Renderers[i] is OrderedListRenderer)
                    {
                        this.renderer.Options.Renderers[i] = new SafeOrderedListRenderer(para);
                    }
                    else if (this.renderer.Options.Renderers[i] is UnorederedListRenderer)
                    {
                        this.renderer.Options.Renderers[i] = new SafeUnorderedListRenderer(para);
                    }
                }
            }
コード例 #3
0
ファイル: SessionExtensions.cs プロジェクト: pekkah/tanka
        private static BlogPostDto ToDto(BlogPost blogPost)
        {
            string html = string.Empty;

            if (!string.IsNullOrWhiteSpace(blogPost.Content))
            {
                var md = new MarkdownParser();
                var renderer = new MarkdownHtmlRenderer();

                try
                {
                    Document document = md.Parse(blogPost.Content);
                    html = renderer.Render(document);
                }
                catch (ParsingException x)
                {
                    html = string.Format(
                        "Markdown parsing error at {0} as block type {1}",
                        x.Position,
                        x.BuilderType);
                }
                catch (RenderingException renderingException)
                {
                    html = string.Format(
                        "Markdown rendering error with block {0} using {1} renderer",
                        renderingException.Block,
                        renderingException.Renderer);
                }
            }

            return new BlogPostDto
            {
                Content = html,
                Author = blogPost.Author,
                Created = blogPost.Created,
                PublishedOn = blogPost.PublishedOn,
                Title = blogPost.Title,
                Slug = blogPost.Slug,
                Id = Id.WithoutCollection(blogPost.Id),
                Tags = blogPost.Tags ?? new Collection<string>()
            };
        }