Exemple #1
0
        private static IList <ArticleDto> getArticles()
        {
            List <ArticleDto>        articles = new List <ArticleDto>();
            MarkdownArticleProcessor ar       = new MarkdownArticleProcessor(getAttributeSet());
            ArticleDto a = ar.ParseArticleRawText(
                @"---
Title:Xbox Controller Repair
Date:2014-08-02
Category:Electronics
Tags:Gaming,Electronics
---
![:youtube 800 600](https://www.youtube.com/embed/Di5AT4MI6BY)", "Some\\File.md");

            articles.Add(a);

            a = ar.ParseArticleRawText(
                @"---
Title:Desk
Date:2014-12-03
Category:Woodwork
Tags:Carpentry,Woodwork
---
Chisels...", "Some\\Desk.cd");
            articles.Add(a);
            return(articles);
        }
Exemple #2
0
        public void EscapedArticleMacrosTest()
        {
            MarkdownArticleProcessor ar = new MarkdownArticleProcessor(getAttributeSet());
            ArticleDto a = ar.ParseArticleRawText(
                @"---
Title:Xbox Controller Repair
Date:2014-08-02
Category:Electronics
Tags:Gaming,Electronics
---
##The internals
![:youtube 800 600](https://www.youtube.com/embed/Di5AT4MI6BY)
\![:noMatchMacro parm1]", "Some\\File.md");

            MacroInvocation mi  = ar.LocateMacrosInContent(a)[0];
            MarkdownMacro   mdm = new MarkdownMacro("youtube", "<iframe width=\"%p1%\" height=\"%p2%\" src=\"%v1%\" frameborder=\"0\"></iframe>");

            ArticleDto[] allArticles = new ArticleDto[1];
            allArticles[0] = a;

            IList <MacroInvocation> miList = MarkdownUtil.LocateMarkdownMacros(a.Content);

            Assert.AreEqual(1, miList.Count);
            Assert.AreEqual("youtube", miList[0].MacroName);
        }
Exemple #3
0
        public void ExpandArticleMacrosTest()
        {
            MarkdownArticleProcessor ar = new MarkdownArticleProcessor(getAttributeSet());
            ArticleDto a = ar.ParseArticleRawText(
                @"---
Title:Xbox Controller Repair
Date:2014-08-02
Category:Electronics
Tags:Gaming,Electronics
---
##The internals
![:youtube 800 600](https://www.youtube.com/embed/Di5AT4MI6BY)
![:youtube 512 400](http://www.youtube.com/embed/Di5AT4MI6BY)", "Some\\File.md");

            MacroInvocation mi  = ar.LocateMacrosInContent(a)[0];
            MarkdownMacro   mdm = new MarkdownMacro("youtube", "<iframe width=\"%p1%\" height=\"%p2%\" src=\"%v1%\" frameborder=\"0\"></iframe>");

            ArticleDto[] allArticles = new ArticleDto[1];
            allArticles[0] = a;
            string finalText = mdm.Expand(mi.Parameters, mi.Values, a, allArticles);

            Assert.AreEqual(
                "<iframe width=\"800\" height=\"600\" src=\"https://www.youtube.com/embed/Di5AT4MI6BY\" frameborder=\"0\"></iframe>",
                finalText.Trim());
        }
Exemple #4
0
        public void LocateMacro()
        {
            MarkdownArticleProcessor ar = new MarkdownArticleProcessor(getAttributeSet());
            ArticleDto a = ar.ParseArticleRawText(
                @"---
Title:Xbox Controller Repair
Date:2014-08-02
Category:Electronics
Tags:Gaming,Electronics
---
##The internals
![:youtube 800 600](https://www.youtube.com/embed/Di5AT4MI6BY)
![:youtube 512 400](http://www.youtube.com/embed/Di5AT4MI6BY)
![My image caption](imgs/someImageNotAMacro.jpg", "Some\\File.md");

            MacroInvocation mi  = ar.LocateMacrosInContent(a)[0];
            MarkdownMacro   mdm = new MarkdownMacro("youtube", "<iframe width=\"%p1%\" height=\"%p2%\" src=\"%v1%\" frameborder=\"0\"></iframe>");

            ArticleDto[]            allArticles = new ArticleDto[1];
            IList <MacroInvocation> mis         = ar.LocateMacrosInContent(a);

            Assert.AreEqual(2, mis.Count);
            Assert.AreEqual(17, mis[0].StartingCharIndex);
            Assert.AreEqual(79, mis[0].EndingCharIndex);
        }
Exemple #5
0
        public void LoadArticleBadDate()
        {
            string input =
                @"---
Title:MyArticle
Date:XXXXXX
---
##The internals
Some article content...
";
            MarkdownArticleProcessor ar = new MarkdownArticleProcessor(getAttributeSet());
            ArticleDto output           = ar.ParseArticleRawText(input, "TestArticle");

            Assert.AreEqual(1, output.Attributes.Count);
            Assert.AreEqual("MyArticle", output.Attributes["Title"][0]);
        }
Exemple #6
0
        public void LoadArticlePositive()
        {
            string input =
                @"---
Title:MyArticle
Date:2015-01-02
---
##The internals
Some article content...
";
            MarkdownArticleProcessor ar = new MarkdownArticleProcessor(getAttributeSet());
            ArticleDto output           = ar.ParseArticleRawText(input, "TestArticle");

            Assert.AreEqual(2, output.Attributes.Count);
            Assert.AreEqual("MyArticle", output.Attributes["Title"][0]);
            Assert.AreEqual("2015-01-02", output.Attributes["Date"][0]);
            Assert.AreEqual("##The internals\r\nSome article content...\r\n\r\n", output.Content);
        }
Exemple #7
0
        /// <summary>
        /// Generates Markdown articles using an XSL transformation that returns Article nodes containing a Content node, which in turns contains markdown text.
        /// </summary>
        /// <param name="concreteArticles">The articles loaded from disk.</param>
        /// <returns></returns>
        public IEnumerable <ArticleDto> GenerateArticles(IList <ArticleDto> concreteArticles)
        {
            MarkdownArticleProcessor artProce          = new MarkdownArticleProcessor(validAttributes);
            List <ArticleDto>        generatedArticles = new List <ArticleDto>();

            try
            {
                XmlDocument outputDoc = new XmlDocument();
                XmlDocument inputDoc  = BuildArticleTree(concreteArticles, groupingCriteria.ToArray(), null);
                using (XmlWriter writer = outputDoc.CreateNavigator().AppendChild())
                {
                    transform.Transform(inputDoc, writer);
                }

                XmlNodeList articles = outputDoc.SelectNodes("//Article");

                int index = 1;
                foreach (XmlNode articleNode in articles)
                {
                    XmlNode contentNode = articleNode.SelectSingleNode("Content");
                    if (contentNode != null)
                    {
                        string content = contentNode.InnerText;
                        try
                        {
                            generatedArticles.Add(artProce.ParseArticleRawText(content, "Virtual~" + index));
                        }
                        catch (Exception ex)
                        {
                            Logger.LogError("Unable to parse virtual Markdown article: " + ex.Message);
                            Logger.LogDebug(content);
                        }
                    }
                    index++;
                }
            }
            catch (XmlException ex)
            {
                Logger.LogError("Error on virtual article generator " + Name + ": " + ex.Message);
            }

            return(generatedArticles);
        }
        /// <summary>
        /// Generates Markdown articles using an XSL transformation that returns Article nodes containing a Content node, which in turns contains markdown text.
        /// </summary>
        /// <param name="concreteArticles">The articles loaded from disk.</param>
        /// <returns></returns>
        public IEnumerable <ArticleDto> GenerateArticles(IList <ArticleDto> concreteArticles)
        {
            MarkdownArticleProcessor artProce          = new MarkdownArticleProcessor(validAttributes);
            List <ArticleDto>        generatedArticles = new List <ArticleDto>();

            try
            {
                XmlDocument outputDoc = new XmlDocument();
                XmlDocument inputDoc  = BuildArticleTree(concreteArticles, groupingCriteria.ToArray(), null);
                using (XmlWriter writer = outputDoc.CreateNavigator().AppendChild())
                {
                    transform.Transform(inputDoc, writer);
                }

                XmlNodeList articles = outputDoc.SelectNodes("//Article");

                int index = 1;
                foreach (XmlNode articleNode in articles)
                {
                    XmlNode contentNode = articleNode.SelectSingleNode("Content");
                    XmlNode pathNode    = articleNode.SelectSingleNode("Path");
                    XmlNode titleNode   = articleNode.SelectSingleNode("Title");
                    if (contentNode != null && pathNode != null)
                    {
                        string content = contentNode.InnerText;
                        try
                        {
                            string path;
                            path = pathNode.InnerText;

                            ArticleDto art = new ArticleDto(pathNode.InnerText, new PureTextArticleProcessor());
                            art.Attributes.Add("Title", new List <string>());

                            if (titleNode != null)
                            {
                                art.Attributes["Title"].Add(titleNode.InnerText);
                            }
                            else
                            {
                                art.Attributes["Title"].Add("");
                            }

                            art.SourceFileRelativePath = path;
                            art.OutputPath             = path;
                            art.Attributes.Add("Path", new List <string>());
                            art.Attributes["Path"].Add(path);

                            art.Content = content;

                            generatedArticles.Add(art);
                        }
                        catch (Exception ex)
                        {
                            Logger.LogError("Unable to parse virtual Markdown article: " + ex.Message);
                            Logger.LogDebug(content);
                        }
                    }
                    else
                    {
                        Logger.LogWarning("Either no Path or no Content node specified for article " + index + " in " + Name);
                    }
                    index++;
                }
            }
            catch (XmlException ex)
            {
                Logger.LogError("Error on virtual article generator " + Name + ": " + ex.Message);
            }

            return(generatedArticles);
        }
Exemple #9
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);
        }