Esempio n. 1
0
        public ObservableCollection <HeaderItem> CreateDocumentOutline(string md)
        {
            if (string.IsNullOrEmpty(md))
            {
                return(null);
            }

            // create a full pipeline to strip out front matter/yaml
            var parser  = new MarkdownParserMarkdig();
            var builder = parser.CreatePipelineBuilder();
            var syntax  = Markdig.Markdown.Parse(md, builder.Build());

            var list = new ObservableCollection <HeaderItem>();

            foreach (var item in syntax)
            {
                var line = item.Line;

                if (item is HeadingBlock)
                {
                    var heading = item as HeadingBlock;
                    var inline  = heading?.Inline?.FirstChild as LiteralInline;
                    if (inline == null)
                    {
                        continue;
                    }

                    var content = inline.Content.ToString();
                    if (content.Contains("\n"))
                    {
                        continue;
                    }

                    if (heading.Level > AppModel.Configuration.MaxDocumentOutlineLevel)
                    {
                        continue;
                    }

                    var headerItem = new HeaderItem()
                    {
                        Text   = $"{content}",
                        Level  = heading.Level,
                        Line   = line,
                        LinkId = LinkHelper.UrilizeAsGfm(content.TrimEnd())
                    };

                    list.Add(headerItem);
                }
            }



            return(list);
        }
Esempio n. 2
0
        public ObservableCollection <HeaderItem> CreateDocumentOutline(string md)
        {
            if (string.IsNullOrEmpty(md))
            {
                return(null);
            }

            // create a full pipeline to strip out front matter/yaml
            var parser  = new MarkdownParserMarkdig();
            var builder = parser.CreatePipelineBuilder();
            var syntax  = Markdig.Markdown.Parse(md, builder.Build());

            var list = new ObservableCollection <HeaderItem>();

            foreach (var item in syntax)
            {
                var line = item.Line;

                if (item is HeadingBlock)
                {
                    var heading = item as HeadingBlock;
                    var inline  = heading?.Inline?.FirstChild;
                    if (inline == null)
                    {
                        continue;
                    }

                    StringBuilder sb = new StringBuilder();
                    foreach (var inl in heading.Inline)
                    {
                        if (inl is HtmlEntityInline)
                        {
                            var htmlEntity = inl as HtmlEntityInline;
                            sb.Append(WebUtility.HtmlDecode(htmlEntity.Transcoded.ToString()));
                        }
                        else if (inl is CodeInline)
                        {
                            var codeInline = inl as CodeInline;
                            sb.Append(WebUtility.HtmlDecode(codeInline.Content));
                        }
                        else if (inl is LinkInline)
                        {
                            var link       = inl as LinkInline;
                            var textInline = link?.FirstOrDefault(l => l is LiteralInline)?.ToString();
                            if (textInline == null)
                            {
                                textInline = "-- non-text header --";
                            }

                            sb.Append(WebUtility.HtmlDecode(textInline));
                        }
                        else
                        {
                            sb.Append(inl.ToString());
                        }
                    }
                    var content = sb.ToString();

                    if (content.Contains("\n"))
                    {
                        continue;
                    }

                    if (heading.Level > AppModel.Configuration.MaxDocumentOutlineLevel)
                    {
                        continue;
                    }

                    var headerItem = new HeaderItem()
                    {
                        Text   = content,
                        Level  = heading.Level,
                        Line   = line,
                        LinkId = LinkHelper.UrilizeAsGfm(content.TrimEnd())
                    };

                    list.Add(headerItem);
                }
            }



            return(list);
        }
        /// <summary>
        ///
        /// mm markdowntohtml <inputfile> <outputFile> <rendermode> -open
        /// </summary>
        /// <param name="inputFile"></param>
        /// <param name="outputFile"></param>
        /// <param name="openOutputFile"></param>
        /// <param name="fileMode">html,packagedhtml,zip</param>
        public void MarkdownToHtml()
        {
            Processor.ConsoleHeader();

            string inputFile  = Arguments.InputFile;
            string outputFile = Arguments.OutputFile;

            if (string.IsNullOrEmpty(inputFile) || !File.Exists(inputFile))
            {
                var fd = new OpenFileDialog
                {
                    DefaultExt = ".md",
                    Filter     = "Markdown files (*.md,*.markdown)|*.md;*.markdown|" +
                                 "All files (*.*)|*.*",
                    CheckFileExists  = true,
                    RestoreDirectory = true,
                    Title            = "Open Markdown File",
                    InitialDirectory = Environment.CurrentDirectory
                };
                var res = fd.ShowDialog();
                if (res == null)
                {
                    return;
                }
                inputFile = fd.FileName;
            }

            if (string.IsNullOrEmpty(outputFile))
            {
                var fd = new SaveFileDialog
                {
                    DefaultExt = ".html",
                    Filter     = "HTML files (*.html,*.htm)|*.html;*.htm|" +
                                 "All files (*.*)|*.*",
                    CheckFileExists  = false,
                    RestoreDirectory = true,
                    Title            = "Save as HTML File",
                    InitialDirectory = Path.GetDirectoryName(inputFile),
                    FileName         = Path.ChangeExtension(Path.GetFileName(inputFile), "html")
                };
                var res = fd.ShowDialog();
                if (res == null)
                {
                    return;
                }
                outputFile = fd.FileName;
            }

            string html;
            var    doc = new MarkdownDocument();

            if (!string.IsNullOrEmpty(Arguments.Theme))
            {
                mmApp.Configuration.PreviewTheme = Arguments.Theme;
            }

            try
            {
                if (!doc.Load(inputFile))
                {
                    throw new AccessViolationException();
                }
            }
            catch
            {
                ColorConsole.WriteError("Failed: Couldn't read input file.");
                Processor.ConsoleFooter();
                return;
            }

            try
            {
                string renderMode = Arguments.HtmlRenderMode?.ToLower();

                if (renderMode == "fragment" || renderMode == "raw")
                {
                    try
                    {
                        var parser = new MarkdownParserMarkdig();
                        html = parser.Parse(doc.CurrentText);

                        File.WriteAllText(outputFile, html, new UTF8Encoding(false));
                    }
                    catch
                    {
                        ColorConsole.WriteError("Failed: Couldn't convert Markdown document or generate output file.");
                        Processor.ConsoleFooter();
                        return;
                    }
                }
                else
                {
                    if (doc.RenderHtmlToFile(filename: outputFile) == null)
                    {
                        throw new AccessViolationException();
                    }
                }

                if (renderMode == "packagedhtml")
                {
                    var    packager   = new Westwind.HtmlPackager.HtmlPackager();
                    string outputHtml = packager.PackageHtml(outputFile);
                    try
                    {
                        File.WriteAllText(outputFile, outputHtml);
                    }
                    catch
                    {
                        ColorConsole.WriteError("Failed: Couldn't write output file.");
                        Processor.ConsoleFooter();
                        return;
                    }
                }
                else if (renderMode == "htmlfiles")
                {
                    var packager = new Westwind.HtmlPackager.HtmlPackager();
                    if (!packager.PackageHtmlToFolder(outputFile, outputFile))
                    {
                        ColorConsole.WriteLine("Failed: Create output folder.", ConsoleColor.Red);
                    }
                }
                else if (renderMode == "zip")
                {
                    var packager = new Westwind.HtmlPackager.HtmlPackager();
                    if (!packager.PackageHtmlToZipFile(Path.ChangeExtension(outputFile, "html"), Path.ChangeExtension(outputFile, "zip")))
                    {
                        ColorConsole.WriteError("Failed: Couldn't create Packaged HTML Zip files.");
                    }

                    try
                    {
                        File.Delete(Path.ChangeExtension(outputFile, "html"));
                    }catch {}
                }
            }

            catch
            {
                ColorConsole.WriteError("Failed: Couldn't write output file.");
                Processor.ConsoleFooter();
                return;
            }

            if (Arguments.OpenOutputFile)
            {
                ShellUtils.GoUrl($"{outputFile}");
            }

            ColorConsole.WriteSuccess($"Created file: {outputFile}");
            ColorConsole.WriteSuccess($" Output Mode: {Arguments.HtmlRenderMode}");

            Processor.ConsoleFooter();
        }