Sha256: aad342dc2a21d2dd19696a42af2868865184dbda11f3502229f9d229f55339dd

Contents?: true

Size: 1.94 KB

Versions: 3

Compression:

Stored size: 1.94 KB

Contents

module MediaWiki
  class << self

    # Extract base name.  If there are no subpages, return page name.
    #
    # Examples:
    # get_base_name("Namespace:Foo/Bar/Baz") -> "Namespace:Foo"
    # get_base_name("Namespace:Foo") -> "Namespace:Foo"
    #
    # [title] Page name string in Wiki format 
    def get_base_name(title)
      title.split('/').first if title
    end

    # Extract path leading up to subpage.  If title does not contain a subpage, returns nil.
    #
    # Examples:
    # get_path_to_subpage("Namespace:Foo/Bar/Baz") -> "Namespace:Foo/Bar"
    # get_path_to_subpage("Namespace:Foo") -> nil
    #
    # [title] Page name string in Wiki format 
    def get_path_to_subpage(title)
      return nil unless title and title.include? '/'
      parts = title.split(/\/([^\/]*)$/).first
    end

    # Extract subpage name.  If there is no hierarchy above, return page name.
    #
    # Examples:
    # get_subpage("Namespace:Foo/Bar/Baz") -> "Baz"
    # get_subpage("Namespace:Foo") -> "Namespace:Foo"
    #
    # [title] Page name string in Wiki format 
    def get_subpage(title)
      title.split('/').last if title
    end

    # Convert URL-ized page name ("getting_there_%26_away") into Wiki display format page name ("getting there & away").
    # Also strips out any illegal characters (#<>[]|{}, cf. http://meta.wikimedia.org/wiki/Help:Page_name#Restrictions).
    #
    # [wiki] Page name string in URL
    def uri_to_wiki(uri)
      CGI.unescape(uri).tr('_', ' ').tr('#<>[]|{}', '') if uri
    end
    
    # Convert a Wiki page name ("getting there & away") to URI-safe format ("getting_there_%26_away"),
    # taking care not to mangle slashes or colons
    # [wiki] Page name string in Wiki format
    def wiki_to_uri(wiki)
      wiki.to_s.split('/').map {|chunk| CGI.escape(chunk.tr(' ', '_')) }.join('/').gsub('%3A', ':') if wiki
    end

    # Return current version of MediaWiki::Gateway
    def version
      MediaWiki::VERSION
    end
  end
  
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
mediawiki-gateway-0.3.4 lib/media_wiki/utils.rb
mediawiki-gateway-0.3.3 lib/media_wiki/utils.rb
mediawiki-gateway-0.3.2 lib/media_wiki/utils.rb