~jpgleeson/piaine

ref: 68ae65db03422f9f5cee4659371a2e3b6e471b8a piaine/PageConsumer.cs -rw-r--r-- 3.5 KiB
68ae65db — jpgleeson Tags 4 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
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(':');
                        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;
        }
    }
}