/// <summary> /// /// mm htmltomarkdown [inputfile] [outputFile] -open /// </summary> /// <param name="inputFile"></param> /// <param name="outputFile"></param> /// <param name="openOutputFile"></param> public void HtmlToMarkdown() { Processor.ConsoleHeader(); string inputFile = Arguments.InputFile; string outputFile = Arguments.OutputFile; if (string.IsNullOrEmpty(inputFile) || !File.Exists(inputFile)) { var fd = new OpenFileDialog { DefaultExt = ".html", Filter = "HTML files (*.html, *.htm)|*.html;*.htm|" + "All files (*.*)|*.*", CheckFileExists = true, RestoreDirectory = true, Title = "Open HTML File", InitialDirectory = Environment.CurrentDirectory }; var res = fd.ShowDialog(); if (res == null) { return; } inputFile = fd.FileName; } if (string.IsNullOrEmpty(outputFile)) { var fd = new SaveFileDialog { DefaultExt = ".md", Filter = "Markdown files (*.md,*.markdown,*.mdcrypt)|*.md;*.markdown;*.mdcrypt|" + "All files (*.*)|*.*", CheckFileExists = false, RestoreDirectory = true, Title = "Save as Markdown File", InitialDirectory = Path.GetDirectoryName(inputFile), FileName = Path.ChangeExtension(Path.GetFileName(inputFile), "md") }; var res = fd.ShowDialog(); if (res == null) { return; } outputFile = fd.FileName; } string md; try { var html = File.ReadAllText(inputFile); md = MarkdownUtilities.HtmlToMarkdown(html, true); } catch { ColorConsole.WriteError("Failed: Couldn't read input file."); Processor.ConsoleFooter(); return; } if (!string.IsNullOrEmpty(outputFile)) { try { File.WriteAllText(outputFile, md); } catch { ColorConsole.WriteError("Failed: Couldn't write output file."); Processor.ConsoleFooter(); return; } if (Arguments.OpenOutputFile) { ShellUtils.ExecuteProcess("markdownmonster.exe", $"'{outputFile}'"); } ColorConsole.WriteSuccess($"Created Markdown file: {outputFile}"); Processor.ConsoleFooter(); } }
public void CreateDownloadedPostOnDisk(Post post, string weblogName) { string filename = SafeFilename(post.Title); var folder = Path.Combine(WeblogAddinConfiguration.Current.PostsFolder, "Downloaded", weblogName, filename); if (!Directory.Exists(folder)) { Directory.CreateDirectory(folder); } var outputFile = Path.Combine(folder, StringUtils.ToCamelCase(filename) + ".md"); bool isMarkdown = false; string body = post.Body; string featuredImage = null; if (post.CustomFields != null) { var cf = post.CustomFields.FirstOrDefault(custf => custf.ID == "mt_markdown"); if (cf != null) { body = cf.Value; isMarkdown = true; } cf = post.CustomFields.FirstOrDefault(custf => custf.ID == "wp_post_thumbnail"); if (cf != null) { featuredImage = cf.Value; } } if (!isMarkdown) { if (!string.IsNullOrEmpty(post.mt_text_more)) { // Wordpress ReadMore syntax - SERIOUSLY??? if (string.IsNullOrEmpty(post.mt_excerpt)) { post.mt_excerpt = HtmlUtils.StripHtml(post.Body); } body = MarkdownUtilities.HtmlToMarkdown(body) + "\n\n<!--more-->\n\n" + MarkdownUtilities.HtmlToMarkdown(post.mt_text_more); } else { body = MarkdownUtilities.HtmlToMarkdown(body); } } string categories = null; if (post.Categories != null && post.Categories.Length > 0) { categories = string.Join(",", post.Categories); } // Create the new post by creating a file with title preset string newPostMarkdown = NewWeblogPost(new WeblogPostMetadata() { Title = post.Title, MarkdownBody = body, Categories = categories, Keywords = post.mt_keywords, Abstract = post.mt_excerpt, PostId = post.PostID.ToString(), WeblogName = weblogName, FeaturedImageUrl = featuredImage }); File.WriteAllText(outputFile, newPostMarkdown); mmApp.Configuration.LastFolder = Path.GetDirectoryName(outputFile); if (isMarkdown) { string html = post.Body; string path = mmApp.Configuration.LastFolder; // do this synchronously so images show up :-< ShowStatus("Downloading post images..."); SaveMarkdownImages(html, path); ShowStatus("Post download complete.", 5000); //new Action<string,string>(SaveImages).BeginInvoke(html,path,null, null); } Model.Window.OpenTab(outputFile); }
public void CreateDownloadedPostOnDisk(Post post, string weblogName) { string filename = FileUtils.SafeFilename(post.Title); var folder = Path.Combine(WeblogAddinConfiguration.Current.PostsFolder, "Downloaded", weblogName, filename); if (!Directory.Exists(folder)) { Directory.CreateDirectory(folder); } var outputFile = Path.Combine(folder, StringUtils.ToCamelCase(filename) + ".md"); bool isMarkdown = false; string body = post.Body; string featuredImage = null; if (post.CustomFields != null) { var cf = post.CustomFields.FirstOrDefault(custf => custf.Id == "mt_markdown"); if (cf != null) { body = cf.Value; isMarkdown = true; } cf = post.CustomFields.FirstOrDefault(custf => custf.Id == "wp_post_thumbnail"); if (cf != null) { featuredImage = cf.Value; } } if (!isMarkdown) { if (!string.IsNullOrEmpty(post.mt_text_more)) { // Wordpress ReadMore syntax - SERIOUSLY??? if (string.IsNullOrEmpty(post.mt_excerpt)) { post.mt_excerpt = HtmlUtils.StripHtml(post.Body); } body = MarkdownUtilities.HtmlToMarkdown(body) + $"{mmApp.NewLine}{mmApp.NewLine}<!--more-->{mmApp.NewLine}{mmApp.NewLine}" + MarkdownUtilities.HtmlToMarkdown(post.mt_text_more); } else { body = MarkdownUtilities.HtmlToMarkdown(body); } } string categories = null; if (post.Categories != null && post.Categories.Length > 0) { categories = string.Join(",", post.Categories); } // Create the new post by creating a file with title preset var meta = new WeblogPostMetadata() { Title = post.Title, MarkdownBody = body, Categories = categories, Keywords = post.mt_keywords, Abstract = post.mt_excerpt, PostId = post.PostId.ToString(), WeblogName = weblogName, FeaturedImageUrl = featuredImage, PostDate = post.DateCreated, PostStatus = post.PostStatus, Permalink = post.Permalink }; string newPostMarkdown = NewWeblogPost(meta); try { File.WriteAllText(outputFile, newPostMarkdown); } catch (Exception ex) { MessageBox.Show($@"Couldn't write new file at: {outputFile} {ex.Message} ", "Weblog Entry File not created", MessageBoxButton.OK, MessageBoxImage.Warning); return; } mmApp.Configuration.LastFolder = Path.GetDirectoryName(outputFile); if (isMarkdown) { string html = post.Body; string path = mmApp.Configuration.LastFolder; // do this synchronously so images show up :-< ShowStatus("Downloading post images...", mmApp.Configuration.StatusMessageTimeout); SaveMarkdownImages(html, path); ShowStatus("Post download complete.", mmApp.Configuration.StatusMessageTimeout); //new Action<string,string>(SaveImages).BeginInvoke(html,path,null, null); } Model.Window.OpenTab(outputFile); Model.Window.ShowFolderBrowser(folder: Path.GetDirectoryName(outputFile)); }