module Middleman
  module Blog
    module Drafts
      # A module that adds draft-article-specific methods to Resources.
      # A {DraftArticle} can be retrieved via {Blog::Drafts::Helpers#current_article} or
      # methods on {Blog::Drafts::Data} (accessible through BlogData#drafts).
      # @see http://rdoc.info/github/middleman/middleman/Middleman/Sitemap/Resource Middleman::Sitemap::Resource
      module DraftArticle
        # The "slug" of the draft article that shows up in its URL.
        # @return [String]
        def slug
          @_slug ||= path_part("title")
        end

        # Retrieve a section of the source path
        # @param [String] The part of the path, e.g. "title"
        # @return [String]
        def path_part(part)
          @_path_parts ||= blog_data.drafts.source_template.extract(path)
          @_path_parts[part.to_s]
        end

        # Returns current date as we can't guess when the article will be
        # published
        # We need this in place or the layout used for blog posts might blow up
        #
        # @return [TimeWithZone]
        def date
          Time.now.in_time_zone
        end

        # Extends resource data adding the date field
        #
        # @return [Thor::CoreExt::HashWithIndifferentAccess]
        def data
          super.dup.merge(date: date)
        end
      end
    end
  end
end

module Middleman
  module Blog
    module Drafts
      # A module that adds draft-article-specific methods to Resources.
      # A {DraftArticle} can be retrieved via {Blog::Drafts::Helpers#current_article} or
      # methods on {Blog::Drafts::Data} (accessible through BlogData#drafts).
      # @see http://rdoc.info/github/middleman/middleman/Middleman/Sitemap/Resource Middleman::Sitemap::Resource
      module DraftArticle
        # The "slug" of the draft article that shows up in its URL.
        # @return [String]
        def slug
          @_slug ||= path_part("title")
        end

        # Retrieve a section of the source path
        # @param [String] The part of the path, e.g. "title"
        # @return [String]
        def path_part(part)
          normalized_path = Addressable::URI.parse(path).normalize.to_s
          @_path_parts ||= blog_data.drafts.source_template.extract(normalized_path)
          @_path_parts[part.to_s]
        end

        # Returns current date as we can't guess when the article will be
        # published
        # We need this in place or the layout used for blog posts might blow up
        #
        # @return [TimeWithZone]
        def date
          Time.now.in_time_zone
        end

        # Extends resource data adding the date field
        #
        # @return [Thor::CoreExt::HashWithIndifferentAccess]
        def data
          super.dup.merge(date: date)
        end
      end
    end
  end
end

Fix failure on handling the path containing non-uri chars
Fix failure on handling the path containing non-uri chars

In `DraftArticle#path_part`, `Addressable::Template#extract`
returns `nil` when `path` has characters not allowed for
URI (RFC3986) so that `DraftArticle#slug` fails for undefined `[]`.
Normalizing (percent-encoding) `path` string before `extract`
prevents its failure.

Characters outside the allowed set are Greek, Cyrillic or Japanese for example.
see http://www.ietf.org/rfc/rfc3986.txt
