Beispiel #1
0
        public void ReverseMarkdown_FromClipboard()
        {
            var html = ClipboardHelper.GetHtmlFromClipboard();

            if (string.IsNullOrEmpty(html))
            {
                Console.WriteLine("You need to have some HTML on the clipboard for this test to do something useful.");

                return;
            }

            var config = new ReverseMarkdown.Config
            {
                GithubFlavored = true,
                UnknownTags    =
                    ReverseMarkdown.Config.UnknownTagsOption
                    .PassThrough,        // Include the unknown tag completely in the result (default as well)
                SmartHrefHandling = true // remove markdown output for links where appropriate
            };

            var    converter = new ReverseMarkdown.Converter(config);
            string markdown  = converter.Convert(html);

            Console.WriteLine(html);

            Console.WriteLine("\n---\n");

            Console.WriteLine(markdown);
        }
Beispiel #2
0
        public static void ProcessFileWithReverseMarkdown(string path)
        {
            Console.WriteLine("Processed file '{0}'.", path);

            // with config
            string unknownTagsConverter = "pass_through";
            bool   githubFlavored       = true;
            var    config = new ReverseMarkdown.Config(unknownTagsConverter, githubFlavored);

            var    converter = new ReverseMarkdown.Converter(config);
            string markdown  = converter.Convert(File.ReadAllText(path));

            // Replace html <u> and </u> tag with empty space
            // markdown = markdown.Replace("<u>", "").Replace("</u>", "");

            string directoryPath = path.Substring(0, path.LastIndexOf('\\'));
            string htmlFileName  = path.Substring(path.LastIndexOf('\\') + 1);
            string mdFileName    = path.Substring(path.LastIndexOf('\\') + 1).Replace(".html", ".md");

            // Before writing markdown to the md file some small modifications
            // Replace '$' with '\$'
            markdown = markdown.Replace("$", "\\$");

            // Replace '<' html tag with '&lt;'
            // Replace '>' html tag with '&gt;'
            //markdown = markdown.Replace("<", "&lt;");
            //markdown = markdown.Replace(">", "&gt;");

            TextWriter txt = new StreamWriter(Path.Combine(directoryPath, mdFileName));

            txt.Write(markdown);
            txt.Close();
        }
Beispiel #3
0
        public static String ToMarkdown(this string html)
        {
            var config = new ReverseMarkdown.Config()
            {
                UnknownTags = ReverseMarkdown.Config.UnknownTagsOption.Bypass
            };
            var converter = new ReverseMarkdown.Converter(config);

            return(converter.Convert(html).Replace("<br>", "\r\n"));
        }
Beispiel #4
0
        /// <summary>
        /// Converts an HTML string to Markdown.
        /// </summary>
        /// <remarks>
        /// This routine relies on a browser window
        /// and an HTML file that handles the actual
        /// parsing: Editor\HtmlToMarkdown.htm
        /// </remarks>
        /// <param name="html"></param>
        /// <returns></returns>
        public static string HtmlToMarkdown(string html, bool noErrorUI = false)
        {
            if (string.IsNullOrEmpty(html))
            {
                return("");
            }
#if false
            var config = new ReverseMarkdown.Config {
                GithubFlavored    = true,
                UnknownTags       = ReverseMarkdown.Config.UnknownTagsOption.PassThrough, // Include the unknown tag completely in the result (default as well)
                SmartHrefHandling = true                                                  // remove markdown output for links where appropriate
            };
            var    converter = new ReverseMarkdown.Converter(config);
            string markdown  = converter.Convert(html);

//            if (!string.IsNullOrEmpty(markdown))
//                markdown = markdown.Replace("\n\n", "\r\n").Replace("\r\n\r\n", "\r\n");
            return(markdown ?? html);
#else
            // Old code that uses JavaScript in a WebBrowser Control
            string markdown = null;
            string htmlFile = Path.Combine(App.InitialStartDirectory, "Editor\\htmltomarkdown.htm");

            var form = new BrowserDialog();
            form.ShowInTaskbar = false;
            form.Width         = 1;
            form.Height        = 1;
            form.Left          = -10000;
            form.Show();

            bool exists = File.Exists(htmlFile);
            form.NavigateAndWaitForCompletion(htmlFile);

            WindowUtilities.DoEvents();

            try
            {
                dynamic doc = form.Browser.Document;
                markdown = doc.ParentWindow.htmltomarkdown(html);
            }
            catch (Exception ex)
            {
                mmApp.Log("Failed to convert Html to Markdown", ex);
                if (!noErrorUI)
                {
                    MessageBox.Show("Unable to convert Html to Markdown. Returning Html.", "Html to Markdown Conversion Failed.", MessageBoxButton.OK, MessageBoxImage.Warning);
                }
            }

            form.Close();
            form = null;

            return(markdown ?? html);
#endif
        }
Beispiel #5
0
        /// <summary>
        /// Converts an HTML string to Markdown.
        /// </summary>
        /// <remarks>
        /// This routine relies on a browser window
        /// and an HTML file that handles the actual
        /// parsing: Editor\HtmlToMarkdown.htm
        /// </remarks>
        /// <param name="html"></param>
        /// <returns></returns>
        public static string HtmlToMarkdown(string html)
        {
            if (string.IsNullOrEmpty(html))
            {
                return("");
            }
#if false
            var    config    = new ReverseMarkdown.Config(githubFlavored: true);
            var    converter = new ReverseMarkdown.Converter(config);
            string markdown  = converter.Convert(html);
            return(markdown ?? html);
#else
            // Old code that uses JavaScript in a WebBrowser Control
            string markdown = null;
            string htmlFile = Path.Combine(Environment.CurrentDirectory, "Editor\\htmltomarkdown.htm");

            var form = new BrowserDialog();
            form.ShowInTaskbar = false;
            form.Width         = 1;
            form.Height        = 1;
            form.Left          = -10000;
            form.Show();

            bool exists = File.Exists(htmlFile);
            form.Navigate(htmlFile);

            WindowUtilities.DoEvents();

            for (int i = 0; i < 200; i++)
            {
                dynamic doc = form.Browser.Document;

                if (!form.IsLoaded)
                {
                    Task.Delay(10);
                    WindowUtilities.DoEvents();
                }
                else
                {
                    markdown = doc.ParentWindow.htmltomarkdown(html);
                    break;
                }
            }

            form.Close();
            form = null;

            return(markdown ?? html);
#endif
        }
        public void When_FencedCodeBlocks_Shouldnt_Have_Trailing_Line()
        {
            var html =
                $@"<pre><code class=""language-xml hljs""><span class=""hljs-tag"">&lt;<span class=""hljs-name"">AspNetCoreHostingModel</span>&gt;</span>InProcess<span class=""hljs-tag"">&lt;/<span class=""hljs-name"">AspNetCoreHostingModel</span>&gt;</span>{Environment.NewLine}</code></pre>";
            var expected = $@"{Environment.NewLine}```{Environment.NewLine}<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>{Environment.NewLine}```{Environment.NewLine}";

            var config = new ReverseMarkdown.Config
            {
                GithubFlavored = true,
            };
            var converter = new Converter(config);
            var result    = converter.Convert(html);

            Assert.Equal(expected, result, StringComparer.OrdinalIgnoreCase);
        }
Beispiel #7
0
        /// <summary>
        /// Converts an HTML string to Markdown.
        /// </summary>
        /// <remarks>
        /// This routine relies on a browser window
        /// and an HTML file that handles the actual
        /// parsing: Editor\HtmlToMarkdown.htm
        /// </remarks>
        /// <param name="html"></param>
        /// <returns></returns>
        public static string HtmlToMarkdown(string html, bool noErrorUI = false)
        {
            if (string.IsNullOrEmpty(html))
            {
                return("");
            }
#if false
            var    config    = new ReverseMarkdown.Config(githubFlavored: true);
            var    converter = new ReverseMarkdown.Converter(config);
            string markdown  = converter.Convert(html);
            return(markdown ?? html);
#else
            // Old code that uses JavaScript in a WebBrowser Control
            string markdown = null;
            string htmlFile = Path.Combine(Environment.CurrentDirectory, "Editor\\htmltomarkdown.htm");

            var form = new BrowserDialog();
            form.ShowInTaskbar = false;
            form.Width         = 1;
            form.Height        = 1;
            form.Left          = -10000;
            form.Show();

            bool exists = File.Exists(htmlFile);
            form.NavigateAndWaitForCompletion(htmlFile);

            WindowUtilities.DoEvents();

            try
            {
                dynamic doc = form.Browser.Document;
                markdown = doc.ParentWindow.htmltomarkdown(html);
            }
            catch (Exception ex)
            {
                mmApp.Log("Failed to convert Html to Markdown", ex);
                if (!noErrorUI)
                {
                    MessageBox.Show("Unable to convert Html to Markdown. Returning Html.", "Html to Markdown Conversion Failed.", MessageBoxButton.OK, MessageBoxImage.Warning);
                }
            }

            form.Close();
            form = null;

            return(markdown ?? html);
#endif
        }
        /// <summary>
        /// Converts an HTML string to Markdown.
        /// </summary>
        /// <remarks>
        /// This routine relies on a browser window
        /// and an HTML file that handles the actual
        /// parsing: Editor\HtmlToMarkdown.htm
        /// </remarks>
        /// <param name="html"></param>
        /// <returns></returns>
        public static string HtmlToMarkdown(string html, bool noErrorUI = false)
        {
            if (string.IsNullOrEmpty(html))
            {
                return("");
            }

            var config = new ReverseMarkdown.Config
            {
                GithubFlavored    = true,
                UnknownTags       = ReverseMarkdown.Config.UnknownTagsOption.PassThrough, // Include the unknown tag completely in the result (default as well)
                SmartHrefHandling = true                                                  // remove markdown output for links where appropriate
            };
            var    converter = new ReverseMarkdown.Converter(config);
            string markdown  = converter.Convert(html);

            return(markdown ?? html);
        }
Beispiel #9
0
        public void HtmlToMarkdownReverseMarkdownFromClipboardTest()
        {
            var html =
                @"<span style=""color: rgb(36, 41, 46); font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"">Markdown Monster is an easy to use and extensible<span> </span></span><strong style=""box-sizing: border-box; font-weight: 600; color: rgb(36, 41, 46); font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"">Markdown Editor</strong><span style=""color: rgb(36, 41, 46); font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"">,<span> </span></span><strong style=""box-sizing: border-box; font-weight: 600; color: rgb(36, 41, 46); font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"">Viewer</strong><span style=""color: rgb(36, 41, 46); font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;""><span> </span>and<span> </span></span><strong style=""box-sizing: border-box; font-weight: 600; color: rgb(36, 41, 46); font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"">Weblog Publisher</strong><span style=""color: rgb(36, 41, 46); font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;""><span> </span>for Windows. Our goal is to provide the best Markdown specific editor for Windows and make it as easy as possible to create Markdown documents. We provide a core editor and previewer, and a number of non-intrusive helpers to help embed content like images, links, tables, code and more into your documents with minimal effort.</span>";

            var config = new ReverseMarkdown.Config
            {
                GithubFlavored = true,
                UnknownTags    =
                    ReverseMarkdown.Config.UnknownTagsOption
                    .PassThrough,        // Include the unknown tag completely in the result (default as well)
                SmartHrefHandling = true // remove markdown output for links where appropriate
            };
            var    converter = new ReverseMarkdown.Converter(config);
            string markdown  = converter.Convert(html);

            Console.WriteLine(markdown);

            Assert.IsTrue(markdown.Contains("and **Weblog Publisher** for Windows"));
        }
Beispiel #10
0
        public MdConverter(List <string> popupResources)
        {
            _popupResources = popupResources;

            string unknownTagsConverter = "pass_through";
            bool   githubFlavored       = true;
            var    config = new ReverseMarkdown.Config(unknownTagsConverter, githubFlavored);

            _mdConverter = new ReverseMarkdown.Converter(config);
            _mdConverter.Unregister("span");
            _mdConverter.Register("span", new SpanEx(_mdConverter));

            _mdConverter.Unregister("p");
            _mdConverter.Register("p", new PEx(_mdConverter));

            _mdConverter.Unregister("table");
            _mdConverter.Register("table", new TableEx(_mdConverter));

            _mdConverter.Unregister("img");
            _mdConverter.Register("img", new ImgEx(_mdConverter));

            _mdConverter.Unregister("a");
            _mdConverter.Register("a", new AEx(_mdConverter));


            _mdConverter.Register("object", new ObjectEx(_mdConverter));

            _mdConverter.Unregister("td");
            _mdConverter.Unregister("th");
            _mdConverter.Register("td", new TdEx(_mdConverter));
            _mdConverter.Register("th", new TdEx(_mdConverter));


            if (HtmlNode.ElementsFlags.ContainsKey("li"))
            {
                HtmlNode.ElementsFlags.Remove("li");
            }
        }
Beispiel #11
0
        public void ReverseMarkdown_CodeSnippetFromBrowserHTML()
        {
            var html = ClipboardHelper.GetHtmlFromClipboard();


            var config = new ReverseMarkdown.Config
            {
                GithubFlavored = true,
                UnknownTags    =
                    ReverseMarkdown.Config.UnknownTagsOption
                    .PassThrough,        // Include the unknown tag completely in the result (default as well)
                SmartHrefHandling = true // remove markdown output for links where appropriate
            };

            var    converter = new ReverseMarkdown.Converter(config);
            string markdown  = converter.Convert(html);

            Console.WriteLine(html);

            Console.WriteLine("\n---\n");

            Console.WriteLine(markdown);
        }
		private string HtmlToMarkdown(string html)
		{
			var config = new ReverseMarkdown.Config("pass_through", true);
			var converter = new ReverseMarkdown.Converter(config);

			return converter.Convert(html);
		}