~jpgleeson/piaine

ref: 783f7bd6a62f4243b94c3ecd1cde88c05b917ad7 piaine/Program.cs -rw-r--r-- 5.8 KiB
783f7bd6jack gleeson Add post tag read to processing 5 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
using System;
using System.Collections.Generic;
using System.IO;

namespace piaine
{
    class Program
    {
        static void Main(string[] args)
        {
            string inputString = readTemplateFile("post.html");
            List<string> outputStrings = new List<string>();
            List<string> inputLines = new List<string>();

            Scanner scanner = new Scanner(inputString);

            List<string> sourceDirectory = new List<string>(Directory.GetFiles(Directory.GetCurrentDirectory() + "/posts/"));

            StreamWriter[] files = new StreamWriter[sourceDirectory.Count];
            List<Post> posts = new List<Post>();

            //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());

            int i = 0;

            foreach(string s in sourceDirectory)
            {
                //Strip each one to get just the local path in the posts folder i.e. "test.txt"
                //From each of these a html file will be created with the same name in a folder.
                //These html files will then be used to build the index
                outputStrings.Clear();

                Post post = new Post();
                post.path = Path.GetRelativePath("output", "output/posts/" + Path.GetFileNameWithoutExtension(s).Replace(' ', '_') + ".html");
                post.date = DateTime.Today;

                inputLines.Clear();
                string[] strings = File.ReadAllLines(s);
                foreach (string st in strings)
                {
                    inputLines.Add(st);
                }

                PageConsumer pageConsumer = new PageConsumer(inputLines);

                post.name = pageConsumer.getPageTitle();
                post.date = pageConsumer.getPageDate();
                post.tags = pageConsumer.getPageTags();

                if (pageConsumer.getPageTemplate() != null)
                {
                    inputString = readTemplateFile(pageConsumer.getPageTemplate());
                    scanner.refreshSource(inputString);
                    parser.refreshTokens(scanner.scanTokens());
                    post.typeOfPage = pageType.staticpage;
                }
                else
                {
                    inputString = readTemplateFile("post.html");
                    scanner.refreshSource(inputString);
                    parser.refreshTokens(scanner.scanTokens());
                    post.typeOfPage = pageType.post;
                }



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

                if (post.typeOfPage == pageType.post)
                {
                    var outputFile = File.Create("output/posts/" + Path.GetFileNameWithoutExtension(s).Replace(' ', '_') + ".html");
                    files[i] = new StreamWriter(outputFile);
                }
                else
                {
                    var outputFile = File.Create("output/" + Path.GetFileNameWithoutExtension(s).Replace(' ', '_') + ".html");
                    files[i] = new StreamWriter(outputFile);
                }
                
                

                foreach (string st in outputStrings)
                {
                    files[i].WriteLine(st);
                }

                files[i].Flush();

                Console.WriteLine("{0} written.", Path.GetFileNameWithoutExtension(s).Replace(' ', '_'));

                posts.Add(post);

                i++;
            }

            buildIndexFile(posts);

            buildAtomFile(posts);

            Console.WriteLine("Files generated. Press any key to exit.");

            Console.ReadKey();
        }

        static void buildIndexFile(List<Post> posts)
        {
            string inputString = readTemplateFile("index.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));

            posts.Reverse();

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

            foreach (Post p in posts)
            {
                if (p.typeOfPage == pageType.post)
                {
                    justPosts.Add(p);
                }
            }

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

            StreamWriter indexWriter = new StreamWriter(indexFile);

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

            indexWriter.Flush();

            Console.WriteLine("Index written.");
        }

        static string readTemplateFile(string path)
        {
            string holderString = File.ReadAllText(path);

            return holderString;
        }

        static void buildAtomFile(List<Post> posts)
        {
            string inputString = readTemplateFile("atom.xml");
            Scanner scanner = new Scanner(inputString);
            Parser parser = new Parser(scanner.scanTokens());
            List<string> outputStrings = new List<string>();
            var atomFile = File.Create("output/feed.atom.xml");

            posts.Sort((x, y) => x.date.CompareTo(y.date));

            posts.Reverse();

            //Make an index here.
            outputStrings = parser.writeAtomFeed(inputString, posts);

            StreamWriter atomWriter = new StreamWriter(atomFile);

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

            atomWriter.Flush();

            Console.WriteLine("Atom feed written.");
        }
    }
}