Skip to content

Instantly share code, notes, and snippets.

@IQAndreas
Created October 19, 2013 22:48
Show Gist options
  • Save IQAndreas/7062501 to your computer and use it in GitHub Desktop.
Save IQAndreas/7062501 to your computer and use it in GitHub Desktop.
Slug function where there is no trailing slash (note that `contact.html` and `contact/index.html` return the same value)
# Sample output:
# URL: /contact.html SLUG: /contact
# URL: /contact.htm SLUG: /contact
# URL: contact.html SLUG: contact # The output only starts with a forward-slash if the input does...
# URL: /contact/index.html SLUG: /contact
# URL: /index.html SLUG: /
# URL: index.html SLUG: / # ... except here.
# URL: /dummy/.html SLUG: /dummy # Just don't use stupid page names like this
# URL: /.html SLUG: / # Again, I'm just too lazy to account for stupid cases
# URL: /funny/images/cats.html SLUG: /funny/images/cats
def slug
# If `html?` remove trailing '.html' (or trailing '.htm')
# If `index?` remove trailing '/index.html'
# Done with regex instead of calling those two functions
# The last non-regex part is to make sure `/index.html` resolves as `/` and not an empty string
self.fixed_octopress_url.sub( /(\/)?(index)?\.html?$/ , "" ) || "/"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment