~jpgleeson/piaine

ref: a05b108902f18eb3dd37a8ef2b0ac0680ee7a512 piaine/PageConsumer.cs -rw-r--r-- 4.5 KiB
a05b1089 — jpgleeson Tag generation. 10 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
using System;
using System.Collections.Generic;
using System.Text;

namespace piaine
{
    public class PageConsumer
    {
        public List<Variable> variablesInPage;
        bool body = false;

        public PageConsumer(List<string> inputLines)
        {
            variablesInPage = new List<Variable>();
            int currentLine = 0;
            TagParser tagparser;

            foreach (string s in inputLines)
            {
                if (s.Length > 0)
                {
                    if (s[0] == '-')
                    {
                        string[] splitString = s.Split(':');
                        
                        if (splitString[0] == "tags")
                        {
                            string[] splitTagString = splitString[1].Split(',');
                            List<string> tagsInSplit = new List<string>();

                            for (int i = 0; i < splitTagString.Length; i++)
                            {
                                tagsInSplit.Add(splitTagString[i].Trim());
                                Console.WriteLine(splitTagString[i].Trim());
                            }
                            TagCollectionVariable tV = new TagCollectionVariable(splitString[0], tagsInSplit);
                            tV.name = tV.name.Trim();
                            tV.name = tV.name.Remove(0, 1);
                            variablesInPage.Add(tV);
                        }
                        else
                        {
                            Variable v = new Variable(splitString[0], splitString[1]);
                            v.name = v.name.Trim();
                            v.name = v.name.Remove(0, 1);
                            v.literal = v.literal.Trim();
                            variablesInPage.Add(v);

                            if (v.name == "body")
                            {
                                for (int i = currentLine + 1; i < inputLines.Count; i++)
                                {
                                     v.literal += "\n";
                                     v.literal += inputLines[i];
                                }
                                break;
                            }
                        }
                        
                        
                        currentLine++;
                    }
                }
            }

            foreach (Variable v in variablesInPage)
            {
                if (v.name == "body")
                {
                    //use markdown parser to output html that will be saved as the literal.
                    textParser textParser = new textParser();
                    v.literal = textParser.parseString(v.literal);
                }
                else if (v.name == "tags")
                {
                    TagCollectionVariable tagList;
                    tagList = new TagCollectionVariable(v.name, new List<string>());
                    tagparser = new TagParser();
                    tagList.literalList = tagparser.parseString(v.literal);
                }
            }
        }

        public string getPageTitle()
        {
            foreach (Variable v in variablesInPage)
            {
                if (v.name == "title")
                {
                    return v.literal;
                }
            }

            return null;
        }

        public string getPageTemplate()
        {
            foreach(Variable v in variablesInPage)
            {
                if (v.name == "template")
                {
                    return v.literal;
                }
            }

            return null;
        }

        public DateTime getPageDate()
        {
            foreach (Variable v in variablesInPage)
            {
                if (v.name == "date")
                {
                    DateTime returnDate = DateTime.Parse(v.literal);
                    return returnDate;
                }
            }

            return DateTime.Today;
        }

        public List<string> getPageTags()
        {
            TagCollectionVariable tagList = new TagCollectionVariable();
            foreach (Variable v in variablesInPage)
            {
                if (v.name == "tags")
                {
                    tagList = new TagCollectionVariable(v.name, new List<string>());
                    TagParser tagparser = new TagParser();
                    tagList.literalList = tagparser.parseString(v.literal);
                }
            }
            return tagList.literalList;
        }
    }
}