public Config(string articlesPath, string templatesPath, string stylesPath, string outputPath, ArticleHeadInfo defaultArticleInfo) { ArticlesPath = articlesPath; TemplatesPath = templatesPath; StylesPath = stylesPath; OutputPath = outputPath; DefaultArticleInfo = defaultArticleInfo; }
public static ArticleHeadInfo Parse(string article, ArticleHeadInfo fallback) { string content = File.ReadAllText(article); Match info = Regex.Match(content, @"{[^}]+}"); if (info.Success) { try { return(JsonConvert.DeserializeObject <ArticleHeadInfo>(info.Value)); } catch (Exception e) { Console.WriteLine($"Error while parsing '{article}':", ConsoleColor.Red); Console.WriteLine(e.Message, ConsoleColor.Red); // ignored, return fallback } } Console.WriteLine($"Using DefaultArticleInfo for article '{article}'", ConsoleColor.Yellow); return(fallback); }
public static ArticleData Parse(string article, ArticleHeadInfo articleHeadInfo, bool debug = false) { string content = File.ReadAllText(article); ArticleData ad = new ArticleData(articleHeadInfo); MarkdownDocument document = new MarkdownDocument(); document.Parse(content); List <MarkdownBlock> elements = new List <MarkdownBlock>(); List <ChapterData> chapters = new List <ChapterData>(); ChapterData chapter = new ChapterData(); List <MarkdownBlock> chapterElements = new List <MarkdownBlock>(); void AddChapter() { if (chapterElements.Count > 0) { chapter.Elements = chapterElements.ToArray(); chapters.Add(chapter); } } void NewChapter(HeaderBlock header) { // Add old AddChapter(); // New chapter chapter = new ChapterData { Title = header.ToString().Trim() }; chapterElements = new List <MarkdownBlock>(); } for (int i = 0; i < document.Blocks.Count; i++) { MarkdownBlock block = document.Blocks[i]; if (debug) { Console.Write($"{block.Type}: ", ConsoleColor.Cyan); } if (!IsCommentParagraph(block)) { elements.Add(block); } if (block is HeaderBlock header) { if (header.HeaderLevel <= 2) { if (header.HeaderLevel == 1 && ad.TITLE == null) { ad.TITLE = header.ToString().Trim(); } NewChapter(header); continue; } } if (!IsCommentParagraph(block)) { chapterElements.Add(block); } if (block is ParagraphBlock paragraph) { if (debug) { Console.WriteLine(); } for (int j = 0; j < paragraph.Inlines.Count; j++) { MarkdownInline inline = paragraph.Inlines[j]; if (debug) { Console.Write($" {inline.Type}: ", ConsoleColor.DarkCyan); } if (debug) { Console.WriteLine(inline.ToString()); } if (inline.Type != MarkdownInlineType.Comment) { } } if (debug) { Console.WriteLine("\n" + block.ToHtml()); } } else { if (debug) { Console.WriteLine(block + "\n" + block.ToHtml()); } } } AddChapter(); ad.CHAPTERS = chapters.ToArray(); ad.ELEMENTS = elements.ToArray(); return(ad); }
/// <summary> /// Generates the file structure from config file /// </summary> /// <param name="projectDirectory">Project directory</param> /// <param name="config">Configuration</param> public static void Compile(string projectDirectory, Config config, bool debug = false) { string articlesPath = Path.Combine(projectDirectory, config.ArticlesPath); string templatesPath = Path.Combine(projectDirectory, config.TemplatesPath); string stylesPath = Path.Combine(projectDirectory, config.StylesPath); string outputPath = Path.Combine(projectDirectory, config.OutputPath); if (!Directory.Exists(articlesPath)) { Console.WriteLine("Articles directory could not be found!", ConsoleColor.Red); } else if (!Directory.Exists(templatesPath)) { Console.WriteLine("Templates directory could not be found!", ConsoleColor.Red); } else if (!Directory.Exists(stylesPath)) { Console.WriteLine("Styles directory could not be found!", ConsoleColor.Red); } else { if (!Directory.Exists(outputPath)) { Directory.CreateDirectory(outputPath); } #region Compile Articles void CompileArticleDirectory(string directoryPath, string relativePath) { // Compile all articles using the templates. string[] articles = Directory.GetFiles(directoryPath); foreach (var article in articles) { ArticleHeadInfo ahi = ArticleHeadInfoParser.Parse(article, config.DefaultArticleInfo); string fileOutput = Path.Combine(outputPath, Path.Combine(relativePath, Path.GetFileNameWithoutExtension(article))) + ".html"; Console.WriteLine($"Compiling '{article}' to '{fileOutput}' using template '{ahi.TemplateFileName()}'...", ConsoleColor.Yellow); string template = String.Empty; if (File.Exists(Path.Combine(templatesPath, ahi.Template))) { template = Path.Combine(templatesPath, ahi.Template); } else if (File.Exists(Path.Combine(templatesPath, ahi.TemplateFileName()))) { template = Path.Combine(templatesPath, ahi.TemplateFileName()); } else { Console.WriteLine($"Template '{ahi.TemplateFileName()}' not found in '{templatesPath}'! Please enter a valid filepath.", ConsoleColor.Red); continue; } ArticleData ad = ArticleParser.Parse(article, ahi, debug); TemplateAssembler.InitializeInterpreter(ad); string result = TemplateAssembler.AssembleFile(ad, template); File.WriteAllText(fileOutput, result); } string[] parts = Directory.GetDirectories(directoryPath); foreach (string part in parts) { CompileArticleDirectory(Path.Combine(directoryPath, part), part); } } CompileArticleDirectory(articlesPath, string.Empty); #endregion #region Move Styles #endregion } }