コード例 #1
0
        private static void AdjustImages(ref string html, ConverterOptions options, string rootPath = null)
        {
            HtmlDocument doc = new HtmlDocument();

            doc.LoadHtml(html);
            HtmlNodeCollection imgNodes = doc.DocumentNode.SelectNodes("//img[@src]");
            string             size     = "full";

            if (imgNodes == null || imgNodes.Count == 0)
            {
                return;
            }

            for (var i = 0; i < imgNodes.Count; i++)
            {
                HtmlNode node = imgNodes[i];

                // If root path is known, check if images are too big for full size class
                if (rootPath != null && File.Exists(DragonUtil.GetFullFilePath(node.Attributes["src"].Value, rootPath)))
                {
                    var imageSize = ImageHelper.GetDimensions(DragonUtil.GetFullFilePath(node.Attributes["src"].Value, rootPath));

                    if (imageSize.x > 700)
                    {
                        size = "large";
                    }
                    else
                    {
                        size = "full";
                    }

                    HtmlAttribute width = doc.CreateAttribute("width", imageSize.x.ToString());
                    node.Attributes.Add(width);

                    HtmlAttribute height = doc.CreateAttribute("height", imageSize.y.ToString());
                    node.Attributes.Add(height);
                }

                if (i == 0 && options.FirstImageIsAlignedRight) // First image should be right aligned, it's the 250x250 image
                {
                    HtmlAttribute classAttribute = doc.CreateAttribute("class", "alignright size-" + size);
                    node.Attributes.Add(classAttribute);
                }
                else
                {
                    HtmlAttribute classAttribute = doc.CreateAttribute("class", "aligncenter size-" + size);
                    node.Attributes.Add(classAttribute);
                }

                if (options.AddBordersToImages)
                {
                    //HtmlAttribute attr = doc.CreateAttribute("bordered");
                    //node.Attributes.Add(attr);
                    node.Attributes["class"].Value += " bordered";
                }
            }

            html = doc.DocumentNode.OuterHtml;
        }
コード例 #2
0
 public static void ConvertMarkdownFileToHtmlFile(string markdownFilePath, string htmlFilePath,
                                                  ConverterOptions options = null)
 {
     using (StreamReader sr = new StreamReader(markdownFilePath))
     {
         string html = ConvertMarkdownStringToHtml(sr.ReadToEnd(), options, Path.GetDirectoryName(markdownFilePath));
         using (StreamWriter sw = new StreamWriter(htmlFilePath))
         {
             sw.Write(html);
             sw.Flush();
         }
     }
 }
コード例 #3
0
        public static string ConvertMarkdownStringToHtml(string markdown, ConverterOptions options = null, string rootPath = null, bool prepareForPreview = false)
        {
            if (options == null)
            {
                options = new ConverterOptions();
            }

            //MarkdownPipeline pipeline = new MarkdownPipelineBuilder().UseAdvancedExtensions().Build();
            MarkdownPipeline pipeline = new MarkdownPipelineBuilder().UseEmphasisExtras().UseCustomContainers().Build();


            string output = Markdown.ToHtml(markdown, pipeline);

            //Console.WriteLine(output);

            // HTML readability improvements & RW specific changes

            // Code
            if (!prepareForPreview)
            {
                output = output.Replace("<pre><code class=", "\r\n<pre lang=");
                output = output.Replace("lang-", "");
                output = output.Replace("language-", "");
                output = output.Replace("</code></pre>", "</pre>\r\n");
            }

            // Add attributes
            AddClassToImages(ref output, options.FirstImageIsAlignedRight, rootPath);
            AddExtraAttributesToLinks(ref output);

            // Make new lines consistent across platforms
            output = output.Replace("\r\n", "|||");
            output = output.Replace("|||", "\n");

            // Text
            output = output.Replace("<p>", "\n");
            output = output.Replace("<br>", "\n");
            output = output.Replace("</p>", "");
            output = output.Replace("<h1", "\n<h1");
            output = output.Replace("<h2", "\n<h2");
            output = output.Replace("<h3", "\n<h3");
            output = output.Replace("<h4", "\n<h4");
            output = output.Replace("<em>", "<i>");
            output = output.Replace("</em>", "</i>");
            output = output.Replace("<strong>", "<em>");
            output = output.Replace("</strong>", "</em>");

            // List
            //output = output.Replace("<ul>", "\n<ul>");
            //output = output.Replace("<ol>", "\n<ol>");

            //// Note
            output = output.Replace("</blockquote>", "</div>");
            output = output.Replace("<blockquote>\n", "\n<blockquote>");
            output = output.Replace("<blockquote>\n<em>Note", "<div class=\"note\">\n<em>Note");
            output = output.Replace("<blockquote>", "<div>");

            // Spoiler
            ConvertSpoilers(ref output);

            if (options.ReplaceImageWithAltWithCaption && !prepareForPreview)
            {
                ConvertImagesWithAltToCaptions(ref output);
            }

            // Final cleanup
            output = output.Replace("<div></div>", "");
            if (!prepareForPreview)
            {
                output = WebUtility.HtmlDecode(output);
            }


            output = output.Trim();
            return(output);
        }
コード例 #4
0
        public static string ConvertMarkdownStringToHtml(string markdown, ConverterOptions options = null, string rootPath = null, bool prepareForPreview = false)
        {
            if (options == null)
            {
                options = new ConverterOptions();
            }

            if (rootPath != null)
            {
                rootPath = Path.GetDirectoryName(rootPath);
            }

            //MarkdownPipeline pipeline = new MarkdownPipelineBuilder().UseAdvancedExtensions().Build();
            MarkdownPipeline pipeline = new MarkdownPipelineBuilder().UseEmphasisExtras().UseCustomContainers().Build();

            string output = Markdown.ToHtml(markdown, pipeline);

            // HTML readability improvements & RW specific changes

            // Code
            if (!prepareForPreview)
            {
                output = new StringBuilder(output)
                         .Replace("<pre><code class=", "\r\n<pre lang=")
                         .Replace("lang-", "")
                         .Replace("language-", "")
                         .Replace("</code></pre>", "</pre>\r\n")
                         .ToString();
            }

            // Add attributes
            AdjustImages(ref output, options, rootPath);

            AddExtraAttributesToLinks(ref output);

            output = new StringBuilder(output)
                     .Replace("\r\n", "|||")
                     .Replace("|||", "\n")
                     .Replace("<p>", "\n")
                     .Replace("<br>", "\n")
                     .Replace("</p>", "")
                     .Replace("<h1", "\n<h1")
                     .Replace("<h2", "\n<h2")
                     .Replace("<h3", "\n<h3")
                     .Replace("<h4", "\n<h4")
                     .Replace("<h5", "\n<h5")
                     .Replace("<h6", "\n<h6")
                     .Replace("<em>", "<i>")
                     .Replace("</em>", "</i>")
                     .Replace("<strong>", "<em>")
                     .Replace("</strong>", "</em>")
                     .Replace("</blockquote>", "</div>")
                     .Replace("<blockquote>\n", "\n<blockquote>")
                     .Replace("<blockquote>\n<em>Note", "<div class=\"note\">\n<em>Note")
                     .Replace("<blockquote>", "<div>")
                     .ToString();

            // Spoiler
            ConvertSpoilers(ref output);

            if (!prepareForPreview)
            {
                AddCaptionsToImages(ref output);
            }

            // Final cleanup
            output = output.Replace("<div></div>", "");
            if (!prepareForPreview)
            {
                output = WebUtility.HtmlDecode(output);
            }

            output = output.Trim();
            return(output);
        }