Exemplo n.º 1
0
 private void applyArticleTemplates(List <ArticleDto> articles, List <TemplateDto> templates)
 {
     foreach (ArticleDto article in articles)
     {
         if (article.Attributes.ContainsKey(TEMPLATE_ATTR_KEY))
         {
             if (article.Attributes[TEMPLATE_ATTR_KEY].Count > 0)
             {
                 string      articleTemplateName = article.Attributes[TEMPLATE_ATTR_KEY][0];
                 TemplateDto template            = templates.FirstOrDefault(x => x.Name.ToLower() == articleTemplateName.ToLower());
                 if (template != null)
                 {
                     article.Template        = template;
                     article.TemplateContent = template.Content;
                     foreach (var templateAttr in template.Attributes)
                     {
                         if (!article.Attributes.ContainsKey(templateAttr.Key))
                         {
                             article.Attributes.Add(templateAttr.Key, templateAttr.Value);
                         }
                     }
                 }
                 else
                 {
                     Logger.LogWarning("Article " + article.SourceFileRelativePath + " refers to non-existent template " + articleTemplateName + ". ");
                 }
             }
         }
     }
 }
Exemplo n.º 2
0
        /// <summary>
        /// Top level function of the site compilation/conversion process
        /// </summary>
        public bool ConvertArticles()
        {
            List <ArticleDto>                     articles = new List <ArticleDto>();
            IList <ITemplateProcessor>            templateProcessors;
            List <TemplateDto>                    templates = new List <TemplateDto>();
            List <IVirtualArticleGeneratorLoader> virtualArticleGenLoaders = new List <IVirtualArticleGeneratorLoader>();
            List <IVirtualArticleGenerator>       articleGenerators        = new List <IVirtualArticleGenerator>();

            virtualArticleGenLoaders.Add(new XslMarkdownArticleGeneratorLoader(validAttributes));
            virtualArticleGenLoaders.Add(new XslArticleGeneratorLoader(validAttributes));


            string[] articlePaths;

            templateProcessors = new List <ITemplateProcessor>();
            templateProcessors.Add(new MarkdownTemplateProcessor(validAttributes));


            if (validAttributes == null)
            {
                return(false);
            }

            //Load articles
            //At this point in time, we only have one type of article reader...
            IArticleProcessor articleProcessor = new MarkdownArticleProcessor(validAttributes);

            articlePaths = Directory.GetFiles(paths.ArticlesRootDir, "*" + articleProcessor.PrimaryFileExtension);
            foreach (string path in articlePaths)
            {
                string     relativePath = path.Substring(paths.ArticlesRootDir.Length);
                ArticleDto art          = articleProcessor.ParseArticleRawText(File.ReadAllText(path), relativePath);
                if (art != null)
                {
                    articles.Add(art);
                }
            }


            //Load templates
            foreach (ITemplateProcessor tp in templateProcessors)
            {
                string[] templatePaths = Directory.GetFiles(paths.TemplatesRootDir, "*" + tp.PrimaryFileExtension);
                foreach (string path in templatePaths)
                {
                    string      relativePath = path.Substring(paths.ArticlesRootDir.Length);
                    TemplateDto template     = tp.ParseTemplateRawText(File.ReadAllText(path), relativePath, Path.GetFileNameWithoutExtension(relativePath));
                    if (template != null)
                    {
                        template.TemplateProcessor = tp;
                        templates.Add(template);
                    }
                }
            }

            //Load virtual article generators
            if (!string.IsNullOrEmpty(paths.VirtualArticlesRootDir))
            {
                foreach (IVirtualArticleGeneratorLoader loader in virtualArticleGenLoaders)
                {
                    string[] generatorPaths = Directory.GetFiles(paths.VirtualArticlesRootDir, "*" + loader.PrimaryFileExtension);
                    foreach (string path in generatorPaths)
                    {
                        if (path.ToLower().EndsWith(loader.PrimaryFileExtension.ToLower()))
                        {
                            IVirtualArticleGenerator gen = loader.ParseGeneratorFromFile(path);
                            if (gen != null)
                            {
                                articleGenerators.Add(gen);
                            }
                        }
                    }
                }
            }

            applyOutputPaths(articles);

            createVirtualArticles(articles, articleGenerators);

            applyArticleTemplates(articles, templates);

            applyOutputPaths(articles);

            applyMacros(articles);

            transformArticles(articles);

            outputArticles(articles);

            return(true);
        }