private void Button_Click(object sender, RoutedEventArgs e) { var name = (sender as Control).Name; if (sender == ButtonCancel) { Cancelled = true; Close(); } else if (sender == ButtonPasteHtml) { CreateTableFromClipboardHtml(); } else if (sender == ButtonImportCsv) { var form = new TableEditorCsvImport(); form.Owner = this; form.ShowDialog(); if (form.IsCancelled) { return; } var parser = new TableParserHtml(); bool deleteCsvFile = false; string csvFile = form.CsvFilename; if (form.ImportFromClipboard) { string csvText = ClipboardHelper.GetText(); csvFile = Path.GetTempFileName(); csvFile = Path.ChangeExtension(csvFile, "csv"); File.WriteAllText(csvFile, csvText); deleteCsvFile = true; } var data = parser.ParseCsvFileToData(csvFile, form.CsvSeparator); if (data == null || data.Headers.Count < 1 && data.Rows.Count < 1) { AppModel.Window.ShowStatusError($"Couldn\'t open file {csvFile} or the file is empty."); return; } TableData = data; RenderTable(); if (deleteCsvFile) { File.Delete(csvFile); } } }
private void ParseHtmlAndRender(string markdownOrHtmlTable) { var parser = new TableParserHtml(); var data = parser.ParseMarkdownToData(markdownOrHtmlTable); if (data == null || data.Headers.Count < 1 && data.Rows.Count < 1) { AppModel.Window.ShowStatusError("No HTML Table to process found..."); return; } TableData = data; RenderTable(); }
/// <summary> /// Re-Formats a Markdown table to nicely formatted output (size permitting) /// </summary> /// <param name="tableMarkdown"></param> /// <returns>formatted markdown, if it can't be formatted original is returned</returns> public string FormatMarkdownTable(string tableMarkdown) { var parser = new TableParserHtml(); var type = parser.DetectTableType(tableMarkdown); if (type == MarkdownTableType.None) { return(null); } var tableData = ParseMarkdownToData(tableMarkdown); if (tableData == null) { return(tableMarkdown); } string output = null; switch (type) { case MarkdownTableType.Pipe: output = parser.ToPipeTableMarkdown(tableData); break; case MarkdownTableType.Grid: output = parser.ToGridTableMarkdown(tableData); break; case MarkdownTableType.Html: output = parser.ToTableHtml(tableData); break; } return(output); }
public void RefreshPreview(bool dontReloadData = false, TableLocation loc = null) { if (!IsPreviewActive) { return; } if (!dontReloadData) { TableData = Interop.GetJsonTableData(); } this.PreviewTableLocation = loc; var parser = new TableParserHtml(); parser.TableData = TableData; string markdown = null; if (TableMode == "Grid Table") { markdown = parser.ToGridTableMarkdown(); } else if (TableMode == "Html Table") { markdown = parser.ToTableHtml(); } else { markdown = parser.ToPipeTableMarkdown(); } #if DEBUG var file = Path.Combine("c:\\projects\\MarkdownMonster\\MarkdownMonster", "PreviewThemes", "TableEditor.html"); var outputFile = file.Replace("TableEditor.html", "_TableEditorPreview.html"); var url = outputFile; #else var file = Path.Combine(App.InitialStartDirectory, "PreviewThemes", "TableEditor.html"); var outputFile = file.Replace("TableEditor.html", "_TableEditorPreview.html"); var url = outputFile; #endif var doc = new MarkdownDocument(); var html = doc.RenderHtml(markdown); try { string template = File.ReadAllText(file); template = template.Replace("{{Theme}}", mmApp.Configuration.PreviewTheme); template = template.Replace("{{Content}}", html); string body = StringUtils.ExtractString(template, "<body>", "</body>", returnDelimiters: true); // replace the entire body with just the HTML and remove scripts template = template.Replace(body, "<body>\n" + html + "\n</body>"); File.WriteAllText(outputFile, template); } catch { // if this fails use the template shipped } WebBrowserPreview.Navigate(url); }