コード例 #1
0
        private async Task <Chapter> ProcessChapterAsync(Book book, string chapterPath, int chapterNumber, List <Language> languages, string outContentPath)
        {
            // read chapter content
            var baseContentPath = Path.Combine(chapterPath, "content.md");
            var contentPath     = Path.Combine(chapterPath, $"content.{book.Language.Code}.md");
            var hasTranslations = File.Exists(contentPath);

            if (!hasTranslations)
            {
                contentPath = baseContentPath;
            }
            var content        = File.ReadAllText(contentPath);
            var chapterContent = Markdown.Parse(content, markdownPipeline);
            var metadata       = ParseMetadata(chapterContent);

            // if this has a translation, then load the tags from the base
            ChapterMetadata?baseMetadata;

            if (hasTranslations)
            {
                var baseContent        = File.ReadAllText(baseContentPath);
                var baseChapterContent = Markdown.Parse(baseContent, markdownPipeline);

                baseMetadata = ParseMetadata(baseChapterContent);
            }
            else
            {
                baseMetadata = metadata;
            }

            // copy some properties
            metadata.HideInContents = baseMetadata.HideInContents;

            // translate tags
            if (baseMetadata.Tags != null)
            {
                metadata.Tags = new ChapterMetadataTags
                {
                    Testament = GetTranslation(book.Books, baseMetadata.Tags.Testament),
                    Books     = GetTranslation(book.Books, baseMetadata.Tags.Books)?.ToList(),
                    People    = GetTranslation(book.People, baseMetadata.Tags.People)?.ToList(),
                };
            }

            // create chapter
            var chapter = new Chapter
            {
                Number         = chapterNumber,
                Metadata       = metadata,
                Pages          = ParsePages(chapterContent),
                ContentPath    = contentPath,
                HasTranslation = hasTranslations,
            };

            if (string.IsNullOrEmpty(chapter.Metadata.Tags?.Testament))
            {
                Console.WriteLine($"WARNING: Missing 'Testament' tag for {contentPath}!");
            }
            if (!(chapter.Metadata.Tags?.Books?.Count > 0))
            {
                Console.WriteLine($"WARNING: Missing 'Books' tag for {contentPath}!");
            }

            var model = new ChapterPageModel
            {
                Book         = book,
                Chapter      = chapter,
                AllLanguages = languages,
            };

            // generate html files
            {
                var outPath = Path.Combine(outContentPath, chapter.Number.ToString());
                var dest    = await ProcessTemplate("chapter.cshtml", outPath, model);

                if (verbose)
                {
                    Console.WriteLine($"Saved '{book.Language.Name}' chapter {chapterNumber} to '{dest}'.");
                }
            }

            return(chapter);
        }