Ejemplo n.º 1
0
        /// <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);
        }
Ejemplo n.º 2
0
        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);
        }