class Jekyll::LiquidRenderer

Public Instance Methods

parse(content) click to toggle source
# File lib/jekyll-pug/pug-renderer.rb, line 44
def parse(content)
    if @filename =~ /\.pug$/
        filename_regex = /[a-zA-Z1-9\s\~\-\.\_\%\#\&\*\{\}\:\?\+\|\<\>\"\']+.\w+$/

        $jekyll_pug_curFile = @filename.match(filename_regex)
        announce("Processing " + @filename)                    

        # Creating Cache Variables
        cache_dir = ".pug-cache/"
        cached_file = cache_dir + @filename
        cached_file_dir = cached_file.sub(filename_regex, '')

        # Creating cache directory neccesary (if needed)
        FileUtils.mkdir_p(cached_file_dir) unless ::File.exists?(cached_file_dir)

        # Loop through Pug includes to determine if any had been modified
        jp("Checking the Pug includes of " + @filename)
        pugIncludeChange = false
        includes_in_file = content.scan(/^\s+include\s[a-zA-Z1-9\/\_\-\.]+/)
        for i in includes_in_file do
            # Remove spaces/tabs in front of code
            include_file = i.sub(/^\s+/, '')
            # Remove include statement to be left with filename
            include_file = include_file.sub(/include\s/, '')
            # If no extension provided, add one
            if include_file.scan(/\.\w+/).length == 0
                include_file = include_file + ".pug"
            end
            jp("  Checking the include " + include_file)
            # Make the include file into an exact path into the user's project
            include_file = $JEKYLLPUG_PROJECT_INCLUDES + "/" + include_file
            # Create the cached location of the include file and its path
            include_cache_file = ".pug-cache/" + include_file.sub(/#{$JEKYLLPUG_PROJECT_SOURCE}/, '')
            include_cache_file_dir = include_cache_file.sub(filename_regex, '')
            # Make a cache folder for this include if not already created
            FileUtils.mkdir_p(include_cache_file_dir) unless ::File.exists?(include_cache_file_dir)

            # Read the file of the include
            include_content = ::File.read(include_file)

            # If cached version of include exists
            if ::File.file?(include_cache_file)
                jp("    Cached file of include exists. Checking if modified...")
                cached_include_content = ::File.read(include_cache_file)
                if include_content == cached_include_content
                    jp("    The include is identical to the cache. Not recompiling")
                else
                    jp("    There has been a change in an include")
                    pugIncludeChange = true
                end
            else
                jp("    Cached file of include does not exist. Creating cache file for include...")
                pugIncludeChange = true
                create_cache(include_content, include_cache_file)
            end
        end

        # If cached pug file exists
        if ::File.file?(cached_file)
            jp("Cached file of " + @filename + " exists! Attempting to use it...")
            cached_file_content = ::File.read(cached_file)

            # Check if Pug includes have changed.
            # If Pug includes changed, we must recompile

            # If files are identical
            if content == cached_file_content and not pugIncludeChange
                jp("Cached file of " + @filename + " is identical and includes haven't changed. Using Cached file")
                # If there is a cached HTML file availible
                cached_html_filename = cached_file + ".html"
                if ::File.file?(cached_html_filename)
                    content = ::File.read(cached_html_filename)
                else
                    jp("The HTML cached file of " + @filename + " does not exist. Can't use cache file right now. Creating it instead.")
                    content = create_cache_and_compile(content, cached_file)
                end
            # If not identical (There has been a change)
            else
                jp("There has been a change since last cache. Re-Caching " + @filename + "...")
                content = create_cache_and_compile(content, cached_file)
            end
        else
            jp("No cached file for " + @filename + " availible. Creating one...")
            content = create_cache_and_compile(content, cached_file)
        end

    end
    if content.lines.first =~ /^$/
    content = content.sub(/^$\n/, "")
    end     
    measure_time do
        @template = Liquid::Template.parse(content, :line_numbers => true)
    end
end