1
0
Fork 0
mirror of https://github.com/middleman/middleman.git synced 2022-11-09 12:20:27 -05:00

make page_classes prefix configurable

This commit is contained in:
Thomas Reynolds 2013-10-29 09:33:27 -07:00
parent 1e57eb5c1b
commit 9a2c1533e3
2 changed files with 19 additions and 3 deletions

View file

@ -14,4 +14,19 @@ Feature: Built-in page_classes view helper
Scenario: Viewing a tier-2 path
Given the Server is running at "page-classes-app"
When I go to "/sub1/sub2/page-classes.html"
Then I should see "sub1 sub1_sub2 sub1_sub2_page-classes"
Then I should see "sub1 sub1_sub2 sub1_sub2_page-classes"
Scenario: The page starts with a number
Given the Server is running at "page-classes-app"
When I go to "/1-starts-with-numeric.html"
Then I should see "x1-starts-with-numeric"
Scenario: The folder starts with a number
Given the Server is running at "page-classes-app"
When I go to "/1-folder/1-inside-with-numeric.html"
Then I should see "x1-folder x1-folder_1-inside-with-numeric"
Scenario: Custom prefix for numeric
Given the Server is running at "page-classes-app"
When I go to "/2-starts-with-numeric-custom.html"
Then I should see "haaaaay2-starts-with-numeric-custom"

View file

@ -132,7 +132,7 @@ class Middleman::CoreExtensions::DefaultHelpers < ::Middleman::Extension
# Generate body css classes based on the current path
#
# @return [String]
def page_classes
def page_classes(options={})
path = current_path.dup
path << index_file if path.end_with?('/')
path = ::Middleman::Util.strip_leading_slash(path)
@ -141,12 +141,13 @@ class Middleman::CoreExtensions::DefaultHelpers < ::Middleman::Extension
parts = path.split('.').first.split('/')
parts.each_with_index { |path, i| classes << parts.first(i+1).join('_') }
prefix = options[:numeric_prefix] || "x"
classes.map do |c|
# Replace weird class name characters
c = c.gsub(/[^a-zA-Z0-9\-_]/, '-')
# Class names can't start with a digit
c = "x#{c}" if c =~ /\A\d/
c = "#{prefix}#{c}" if c =~ /\A\d/
c
end.join(' ')
end