andruhovski icon

MD-to-HTML tool

andruhovski | PRO | 05/31/20 10:44:56 PM UTC | 0 ⭐ | 1836 👁️ | Never ⏰ | []
C# |

6.62 KB

|

None

|

0 👍

/

0 👎

using System.Linq;
using Aspose.Html;
using Newtonsoft.Json;
 
namespace Aspose.DocMaker
{
    class Program
    {
        private const string Path = @"D:\Github\Aspose.PDF.Docs";
        private const string Theme = @"D:\Github\Aspose.PDF.Docs\template\demo01";
        private static readonly License License = new License();
        static void Main()
        {
            License.SetLicense(@"C:\Keys\Aspose.Total.Product.Family.lic");
            var inputPath = System.IO.Path.Combine(Path, "input");
            var outputPath = System.IO.Path.Combine(Path, "output");
            ScanFolders(inputPath);
            RenderStaticPages(inputPath, outputPath);
        }
 
        private static void RenderStaticPages(string inputPath, string outputPath)
        {
            InputConfig inputConfig;
            if (System.IO.Directory.Exists(outputPath))
                System.IO.Directory.CreateDirectory(outputPath);
            var inputConfigFile = System.IO.Path.Combine(inputPath, "index.json");
            if (System.IO.File.Exists(inputConfigFile))
            {
                var json = System.IO.File.ReadAllText(inputConfigFile);
                inputConfig = JsonConvert.DeserializeObject<InputConfig>(json);
            }
            else
            {
                inputConfig = new InputConfig();
            }
            var indexTemplate = System.IO.Path.Combine(Theme, "index.html");
            var outputIndexFile = System.IO.Path.Combine(outputPath, "index.html");
            var document = new HTMLDocument(indexTemplate);
            var menuPlaceholder = document.GetElementById("aspose-menu-placeholder");
 
            foreach (var folder in inputConfig.Folders.OrderBy(f => f.Order))
            {
                if (document.GetElementById("aspose-menu-item") is HTMLTemplateElement menuItemTemplate)
                {
                    var menuNode = menuItemTemplate.Content.CloneNode(true) as Html.Dom.DocumentFragment;
 
                    var titleElements = menuNode?.QuerySelectorAll(".aspose-menu-item-title").Cast<HTMLElement>();
                    if (titleElements != null)
                        foreach (var element in titleElements)
                        {
                            element.InnerHTML = folder.Title;
                        }
 
                    var anchorElements = menuNode?.QuerySelectorAll(".aspose-menu-item-href").Cast<HTMLAnchorElement>();
                    if (anchorElements != null)
                        foreach (var element in anchorElements)
                        {
                            element.Href =$"{folder.Name}/index.html";
                        }
                    menuPlaceholder.AppendChild(menuNode);
                }
 
                RenderStaticPages(
                    System.IO.Path.Combine(inputPath, folder.Name),
                    System.IO.Path.Combine(outputPath, folder.Name));
            }
            
 
            var pageTemplateFileName = System.IO.Path.Combine(Theme, "page.html");
            var pageDocument = new Aspose.Html.HTMLDocument(pageTemplateFileName);
            var contentPlaceholder = pageDocument.GetElementById("aspose-content-placeholder");
 
            foreach (var page in inputConfig.Pages)
            {
                var markdownFileName = System.IO.Path.Combine(inputPath, page.Name + ".md");
                var pageFileName = System.IO.Path.Combine(outputPath, page.Name + ".html");
                var markdownContent = Aspose.Html.Converters.Converter.ConvertMarkdown(markdownFileName);
                pageDocument.Title = page.Title;
                contentPlaceholder.InnerHTML = markdownContent.GetElementsByTagName("body")[0].InnerHTML;
                var tables = pageDocument.GetElementsByTagName("table");
                foreach (var element in tables)
                {
                    element.ClassName = "table table-bordered";
                }
                pageDocument.Save(pageFileName);
                pageDocument.Title = string.Empty;
                if (document.GetElementById("aspose-menu-item") is HTMLTemplateElement menuItemTemplate)
                {
                    var menuNode = menuItemTemplate.Content.CloneNode(true) as Aspose.Html.Dom.DocumentFragment;
 
                    var titleElements = menuNode?.QuerySelectorAll(".aspose-menu-item-title").Cast<HTMLElement>();
                    if (titleElements != null)
                        foreach (var element in titleElements)
                        {
                            element.InnerHTML = page.Title;
                        }
 
                    var anchorElements = menuNode?.QuerySelectorAll(".aspose-menu-item-href").Cast<HTMLAnchorElement>();
                    if (anchorElements != null)
                        foreach (var element in anchorElements)
                        {
                            element.Href = $"{page.Name}.html";
                        }
                    menuPlaceholder.AppendChild(menuNode);
                }
            }
 
 
            document.Save(outputIndexFile);
        }
 
 
        private static void ScanFolders(string inputPath)
        {
            var inputConfigFile = System.IO.Path.Combine(inputPath, "index.json");
 
            InputConfig inputConfig;
 
            if (System.IO.File.Exists(inputConfigFile))
            {
                var json = System.IO.File.ReadAllText(inputConfigFile);
                inputConfig = JsonConvert.DeserializeObject<InputConfig>(json);
            }
            else
            {
                inputConfig = new InputConfig();
            }
 
            foreach (var folder in System.IO.Directory.GetDirectories(inputPath))
            {
                ScanFolders(folder);
                var folderName = System.IO.Path.GetFileName(folder.TrimEnd(System.IO.Path.DirectorySeparatorChar));
                if (!inputConfig.Folders.Any(f => f.Name.Equals(folderName)))
                    inputConfig.Folders.Add(new Folder() { Name = folderName, Order = 1000, Title = folderName });
 
            }
 
            foreach (var file in System.IO.Directory.GetFiles(inputPath, "*.md"))
            {
                var pageFileName = System.IO.Path.GetFileNameWithoutExtension(file);
                if (!inputConfig.Pages.Any(f =>f.Name.StartsWith(pageFileName)))
                    inputConfig.Pages.Add(new Page() { Name = pageFileName, Order = 1000, Title = pageFileName });
            }
            System.IO.File.WriteAllText(inputConfigFile, JsonConvert.SerializeObject(inputConfig, Formatting.Indented));
 
        }
    }
}

Comments