~jpgleeson/piaine

a05b108902f18eb3dd37a8ef2b0ac0680ee7a512 — jpgleeson 17 days ago 86dbf8b
Tag generation.

Tags are now generated at runtime if the -tags flag is passed to the program. Only post type pages are included in tags, and an index for each tag is generated and resides in the /tags folder. If the index file is generated with tags. It will look for a !tagCloud tag in the template and the tags will be inserted there, one per line.
3 files changed, 162 insertions(+), 30 deletions(-)

M PageConsumer.cs
M Parser.cs
M Program.cs
M PageConsumer.cs => PageConsumer.cs +1 -1
@@ 33,7 33,7 @@ namespace piaine
                                tagsInSplit.Add(splitTagString[i].Trim());
                                Console.WriteLine(splitTagString[i].Trim());
                            }
                            TagVariable tV = new TagVariable(splitString[0], tagsInSplit);
                            TagCollectionVariable tV = new TagCollectionVariable(splitString[0], tagsInSplit);
                            tV.name = tV.name.Trim();
                            tV.name = tV.name.Remove(0, 1);
                            variablesInPage.Add(tV);

M Parser.cs => Parser.cs +87 -0
@@ 82,6 82,93 @@ namespace piaine
            return outputStrings;
        }

        public List<string> writeVariablesInSource(string source, List<Post> posts, List<string> tags)
        {
            outputStrings.Clear();

            foreach (Token t in tokens)
            {
                if (t.type == TokenType.Unscoped)
                {
                    outputStrings.Add(t.literal.ToString());
                }
                else if (t.type == TokenType.Variable)
                {
                    int counter = 0;

                    if (t.literal.ToString() == "post")
                    {
                        foreach (Post p in posts)
                        {
                            String outputLink = "<div>{0}  -  <a href='{1}'>{2}</a></div>";

                            outputLink = outputLink.Replace("{0}", p.date.ToShortDateString());
                            outputLink = outputLink.Replace("{1}", p.path);
                            outputLink = outputLink.Replace("{2}", p.name);

                            counter++;
                            outputStrings.Add(outputLink);
                        }
                    }
                    else if (t.literal.ToString() == "tagCloud")
                    {
                        foreach (string tag in tags)
                        {
                            String outputLink = "<div>-<a href='{1}'>{2}</a></div>";

                            outputLink = outputLink.Replace("{1}", "./tags/" + tag + ".html");
                            outputLink = outputLink.Replace("{2}", tag);

                            counter++;
                            outputStrings.Add(outputLink);
                        }
                    }
                    //outputStrings.Add(t.literal.ToString());
                }
            }

            return outputStrings;
        }

        public List<string> writeVariablesInSource(string source, List<Post> posts, string title)
        {
            outputStrings.Clear();

            foreach (Token t in tokens)
            {
                if (t.type == TokenType.Unscoped)
                {
                    outputStrings.Add(t.literal.ToString());
                }
                else if (t.type == TokenType.Variable)
                {
                    int counter = 0;

                    if (t.literal.ToString() == "post")
                    {
                        foreach (Post p in posts)
                        {
                            String outputLink = "<div>{0}  -  <a href='{1}'>{2}</a></div>";

                            outputLink = outputLink.Replace("{0}", p.date.ToShortDateString());
                            outputLink = outputLink.Replace("{1}", "../" + p.path);
                            outputLink = outputLink.Replace("{2}", p.name);

                            counter++;
                            outputStrings.Add(outputLink);
                        }
                    }
                    else if (t.literal.ToString() == "title")
                    {
                        counter++;
                        outputStrings.Add(title);
                    }
                }
            }

            return outputStrings;
        }

        public List<string> writeAtomFeed(string source, List<Post> posts)
        {
            outputStrings.Clear();

M Program.cs => Program.cs +74 -29
@@ 11,7 11,7 @@ namespace piaine
            string inputString = readTemplateFile("post.html");
            List<string> outputStrings = new List<string>();
            List<string> inputLines = new List<string>();
            List<string> tags = new List<string>();
            bool buildTagIndexes = false;

            Scanner scanner = new Scanner(inputString);



@@ 24,6 24,14 @@ namespace piaine
            //For some reason parser needs to be used outside of the foreach scope for the source files. I've no idea why, but this works right now.
            Parser parser = new Parser(scanner.scanTokens());

            foreach (string argument in args)
            {
                if (argument == "-tags")
                {
                    buildTagIndexes = true;
                }
            }

            int i = 0;

            foreach(string s in sourceDirectory)


@@ 36,6 44,7 @@ namespace piaine
                Post post = new Post();
                post.path = Path.GetRelativePath("output", "output/posts/" + Path.GetFileNameWithoutExtension(s).Replace(' ', '_') + ".html");
                post.date = DateTime.Today;
                post.tags = new List<string>();

                inputLines.Clear();
                string[] strings = File.ReadAllLines(s);


@@ 50,10 59,6 @@ namespace piaine
                post.date = pageConsumer.getPageDate();
                post.tags = pageConsumer.getPageTags();

                foreach (string tag in post.tags)
                {
                    tags.Add(tag);
                }

                if (pageConsumer.getPageTemplate() != null)
                {


@@ 72,7 77,13 @@ namespace piaine

                if (post.tags != null)
                {
                    tags.AddRange(post.tags);
                    foreach (string tag in post.tags)
                    {
                        if (!tags.Contains(tag))
                        {
                            tags.Add(tag);
                        }
                    }
                }

                outputStrings = parser.writeVariablesInSource(inputString, pageConsumer.variablesInPage);


@@ 104,7 115,17 @@ namespace piaine
                i++;
            }

            buildIndexFile(posts);
            

            if (buildTagIndexes)
            {
                buildTagFiles(posts, tags);
                buildIndexFile(posts, tags);
            }
            else
            {
                buildIndexFile(posts);
            }

            buildAtomFile(posts);



@@ 122,7 143,7 @@ namespace piaine
            Console.ReadKey();
        }

        static void buildIndexFile(List<Post> posts)
        static void buildIndexFile(List<Post> posts, List<string> tagCloud = null)
        {
            string inputString = readTemplateFile("index.html");
            Scanner scanner = new Scanner(inputString);


@@ 145,7 166,14 @@ namespace piaine
            }

            //Make an index here.
            outputStrings = parser.writeVariablesInSource(inputString, justPosts);
            if (tagCloud != null)
            {
                outputStrings = parser.writeVariablesInSource(inputString, justPosts, tagCloud);
            }
            else
            {
                outputStrings = parser.writeVariablesInSource(inputString, justPosts);
            }

            StreamWriter indexWriter = new StreamWriter(indexFile);



@@ 193,41 221,58 @@ namespace piaine
            Console.WriteLine("Atom feed written.");
        }

        static void buildTagFiles(List<Post> posts)
        static void buildTagFiles(List<Post> posts, List<string> tags)
        {
            string inputString = readTemplateFile("index.html");
            string inputString = readTemplateFile("tagIndex.html");
            Scanner scanner = new Scanner(inputString);
            Parser parser = new Parser(scanner.scanTokens());
            List<string> outputStrings = new List<string>();
            var indexFile = File.Create("output/index.html");

            posts.Sort((x, y) => x.date.CompareTo(y.date));
            foreach (string tag in tags)
            {
                Console.WriteLine("{0} index being built.", tag);

            posts.Reverse();
                string filePath = "output/tags/" + tag + ".html";
                if (!Directory.Exists("output/tags/"))
                {
                    Directory.CreateDirectory("output/tags/");
                }
                var tagIndex = File.Create(filePath);

            List<Post> justPosts = new List<Post>();
                List<Post> taggedPosts = new List<Post>();

            foreach (Post p in posts)
            {
                if (p.typeOfPage == pageType.post)
                posts.Sort((x, y) => x.date.CompareTo(y.date));

                posts.Reverse();

                foreach (Post p in posts)
                {
                    justPosts.Add(p);
                    if (p.typeOfPage == pageType.post)
                    {
                        if (p.tags != null)
                        {
                            if (p.tags.Contains(tag))
                            {
                                Console.WriteLine(p.name);
                                taggedPosts.Add(p);
                            }
                        }
                    }
                }
            }

            //Make an index here.
            outputStrings = parser.writeVariablesInSource(inputString, justPosts);
                outputStrings = parser.writeVariablesInSource(inputString, taggedPosts, tag);

            StreamWriter indexWriter = new StreamWriter(indexFile);
                StreamWriter tagIndexWriter = new StreamWriter(tagIndex);

            foreach (string st in outputStrings)
            {
                indexWriter.WriteLine(st);
            }
                foreach (string st in outputStrings)
                {
                    tagIndexWriter.WriteLine(st);
                }

            indexWriter.Flush();
                tagIndexWriter.Flush();
            }

            Console.WriteLine("Index written.");
            Console.WriteLine("{0} tag indices written.", tags.Count.ToString());
        }
    }
}