Skip to content

Instantly share code, notes, and snippets.

@IQAndreas
Created October 19, 2013 22:51
Show Gist options
  • Save IQAndreas/7062536 to your computer and use it in GitHub Desktop.
Save IQAndreas/7062536 to your computer and use it in GitHub Desktop.
Slug function where there IS a trailing slash (note the difference between `contact.html` and `contact/index.html`)
# 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 (always)
# URL: /contact/index.html SLUG: /contact/ # <-- Here is the major difference between this and the other version
# URL: /index.html SLUG: /
# URL: index.html SLUG: (empty string)
# 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
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