From 9a2c1533e3f182698ee0ccb44516636cb413eb63 Mon Sep 17 00:00:00 2001 From: Thomas Reynolds Date: Tue, 29 Oct 2013 09:33:27 -0700 Subject: [PATCH] make page_classes prefix configurable --- .../features/helpers_page_classes.feature | 17 ++++++++++++++++- .../core_extensions/default_helpers.rb | 5 +++-- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/middleman-core/features/helpers_page_classes.feature b/middleman-core/features/helpers_page_classes.feature index b556d6e9..3003f4f8 100644 --- a/middleman-core/features/helpers_page_classes.feature +++ b/middleman-core/features/helpers_page_classes.feature @@ -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" \ No newline at end of file + 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" \ No newline at end of file diff --git a/middleman-core/lib/middleman-more/core_extensions/default_helpers.rb b/middleman-core/lib/middleman-more/core_extensions/default_helpers.rb index 62a5b0bf..39121d84 100644 --- a/middleman-core/lib/middleman-more/core_extensions/default_helpers.rb +++ b/middleman-core/lib/middleman-more/core_extensions/default_helpers.rb @@ -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