Class: JekyllFile

Inherits:
Object
  • Object
show all
Defined in:
lib/code/JekyllFile.rb

Instance Method Summary collapse

Constructor Details

#initialize(path, data, file_type, file_name = nil) ⇒ JekyllFile

Returns a new instance of JekyllFile.



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
# File 'lib/code/JekyllFile.rb', line 17

def initialize(path, data, file_type, file_name = nil)
    @content_keys = ['content']
    @ignore_keys = ['layout']
    
    @file_data = data;
    @file_type = file_type
    
    if file_name.nil?
        case @file_type
            when 'page'
                @file_name = data['url'] + '.md';
            else
                @file_name = data['slug'] + '.md';
        end
    else
        @file_name = file_name + '.md';
    end

    @base_path = path
    @file_path = path + '/'  + @file_name;
    
    @yaml_parsed = {}
    if Pathname.new(@file_path).file?
       parseFile
    else
        if 'page' == @file_type
            # disabled permalinks for pages
            #@yaml_parsed = {
            #    'permalink' => '/' + @file_data['url'] + '/'
            #    }

            #@content_section = '{% include pages/' + @file_data['url'] + '.md %}'

            content_file = @file_data['url']
            if content_file.include? "/"
                content_file = content_file.gsub(/\//, "_")
            end

            @content_section = '{% capture ' + content_file + ' %}{% include pages/' + content_file + '.md %}{% endcapture %}
{{ ' + content_file + ' | markdownify }}'
        end
    end
    
    # set layout
    if @file_data.key?('layout') || !@yaml_parsed.key?('layout')
        @yaml_parsed['layout'] = getPageLayout
    end
    
    # do some file type specific stuff
    case @file_type
        when 'page'
            # set page title
            @yaml_parsed['title'] = @file_data['name']
            @yaml_parsed['url'] = @file_data['url']
            if @file_data['image']
                @yaml_parsed['image'] = 'images/pages/' + @file_data['image']
            else
                @yaml_parsed.delete("image") 
            end
            
            @yaml_parsed['sitemap'] = ('1' == data['ignore_sitemap']) ? false : true
            @yaml_parsed['parent_id'] = @file_data['parent_id']
            @yaml_parsed['page_id'] = @file_data['id']
            @yaml_parsed['meta_title'] = (@file_data['meta_title']) ? @file_data['meta_title'] : @file_data['name']
            @yaml_parsed['meta_description'] = @file_data['meta_description']
        else
            # add file data
            @file_data.each do |key, value|
                if !@content_keys.include?(key) &&! @ignore_keys.include?(key)
                    @yaml_parsed[key] = value
                end
            end
        
            # add content
            @content_keys.each do |key|
            if @file_data.key?(key)
                if @file_data[key].is_a? String
                    @content_section = @file_data[key]
                end
            end
        end
    end
end

Instance Method Details

#getFileNameObject



121
122
123
# File 'lib/code/JekyllFile.rb', line 121

def getFileName
    return @file_name
end

#getPageLayoutObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/code/JekyllFile.rb', line 101

def getPageLayout
    if @file_data['layout']
        layout = File.basename(@file_data['layout'],File.extname(@file_data['layout']))
    else
        case @file_type
            when 'page'
                if 'index' == @file_data['url']
                    layout = 'home'
                else
                    layout = 'page'
                end
            else
                layout = 'post'
        end
    end
    #puts "Layout:" + layout
    
    return layout
end

#parseFileObject



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
# File 'lib/code/JekyllFile.rb', line 125

def parseFile
    file_contents = ZeroFetcher.readFile(@file_path)

    file_contents.gsub!(/\r\n?/, "\n")
    
    area = false
    @yaml_section = ''
    @content_section = ''
    
    file_contents.each_line do |line|
        sline = line
        
        if '---' == sline.strip
            case area
                when false
                    area = 'yaml'
                when 'yaml'
                    area = 'content'
            end
        else
            case area
                when 'yaml'
                    @yaml_section += line
                when 'content'
                    @content_section += line
            end
        end
    end
    
    @yaml_parsed = YAML.load(@yaml_section)
end

#saveContentFile(file_path, file_name, contents) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
# File 'lib/code/JekyllFile.rb', line 161

def saveContentFile(file_path, file_name, contents)
    if file_name.include? "/"
        file_name = file_name.gsub(/\//, "_")
    end

    #puts "Create Content File" + file_path + '/' + file_name

    ZeroFetcher.writeFile(file_path + '/' + file_name, contents)

    return file_path + '/' + file_name
end

#savePageFileObject



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/code/JekyllFile.rb', line 173

def savePageFile
    #file_contents = '---A'+"\n"
    #file_contents += YAML.generate(@yaml_parsed)+"\n"
    file_contents = @yaml_parsed.to_yaml
    file_contents += '---'+"\n"
    
    if @content_section.is_a? String
        file_contents += @content_section
    end

    # create folders
    if 'page' == @file_type
        if @file_name.include? "/"
            parsed = @file_name.split("/")

            folders = parsed.first parsed.size - 1

            cur_path = @base_path

            folders.each do |folder|
                cur_path += '/' + folder
                #puts "Create Dir" + cur_path
                FileUtils::mkdir_p cur_path
            end
        end
    end

    File.write(@file_path, file_contents)
end