class JekyllFile

Public Class Methods

new(path, data, file_type, file_name = nil) click to toggle source
# 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

Public Instance Methods

getFileName() click to toggle source
# File lib/code/JekyllFile.rb, line 121
def getFileName
    return @file_name
end
getPageLayout() click to toggle source
# 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
parseFile() click to toggle source
# 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) click to toggle source
# 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
savePageFile() click to toggle source
# 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