awestruct 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/awestruct/engine.rb +47 -11
- data/lib/awestruct/extensions/atomizer.rb +21 -0
- data/lib/awestruct/extensions/intense_debate.rb +26 -0
- data/lib/awestruct/extensions/paginator.rb +99 -0
- data/lib/awestruct/extensions/posts.rb +3 -2
- data/lib/awestruct/extensions/template.atom.haml +29 -0
- data/lib/awestruct/marukuable.rb +1 -1
- data/lib/awestruct/renderable_file.rb +7 -5
- data/lib/awestruct/util/default_inflections.rb +45 -0
- data/lib/awestruct/util/inflector.rb +242 -0
- metadata +9 -3
data/lib/awestruct/engine.rb
CHANGED
@@ -16,6 +16,11 @@ require 'awestruct/haml_helpers'
|
|
16
16
|
require 'awestruct/extensions/pipeline'
|
17
17
|
require 'awestruct/extensions/posts'
|
18
18
|
require 'awestruct/extensions/indexifier'
|
19
|
+
require 'awestruct/extensions/paginator'
|
20
|
+
require 'awestruct/extensions/atomizer'
|
21
|
+
|
22
|
+
require 'awestruct/util/inflector'
|
23
|
+
require 'awestruct/util/default_inflections'
|
19
24
|
|
20
25
|
module Awestruct
|
21
26
|
|
@@ -29,6 +34,7 @@ module Awestruct
|
|
29
34
|
@dir = dir
|
30
35
|
@config = config
|
31
36
|
@site = Site.new( @dir )
|
37
|
+
@site.engine = self
|
32
38
|
@site.tmp_dir = File.join( dir, '_tmp' )
|
33
39
|
FileUtils.mkdir_p( @site.tmp_dir )
|
34
40
|
FileUtils.mkdir_p( @site.output_dir )
|
@@ -45,6 +51,44 @@ module Awestruct
|
|
45
51
|
generate_files(force)
|
46
52
|
end
|
47
53
|
|
54
|
+
def find_and_load_site_page(simple_path)
|
55
|
+
path_glob = File.join( dir, simple_path + '.*' )
|
56
|
+
candidates = Dir[ path_glob ]
|
57
|
+
return nil if candidates.empty?
|
58
|
+
throw Exception.new( "too many choices for #{simple_path}" ) if candidates.size != 1
|
59
|
+
dir_pathname = Pathname.new( dir )
|
60
|
+
path_name = Pathname.new( candidates[0] )
|
61
|
+
relative_path = path_name.relative_path_from( dir_pathname ).to_s
|
62
|
+
load_page( candidates[0], relative_path )
|
63
|
+
end
|
64
|
+
|
65
|
+
def load_site_page(relative_path)
|
66
|
+
load_page( File.join( dir, relative_path ) )
|
67
|
+
end
|
68
|
+
|
69
|
+
def load_page(path, relative_path=nil)
|
70
|
+
page = nil
|
71
|
+
if ( relative_path.nil? )
|
72
|
+
#dir_pathname = Pathname.new( dir )
|
73
|
+
#path_name = Pathname.new( path )
|
74
|
+
#relative_path = path_name.relative_path_from( dir_pathname ).to_s
|
75
|
+
end
|
76
|
+
|
77
|
+
fixed_relative_path = ( relative_path.nil? ? nil : File.join( '', relative_path ) )
|
78
|
+
|
79
|
+
if ( path =~ /\.haml$/ )
|
80
|
+
page = HamlFile.new( site, path, fixed_relative_path )
|
81
|
+
elsif ( path =~ /\.md$/ )
|
82
|
+
page = MarukuFile.new( site, path, fixed_relative_path )
|
83
|
+
elsif ( path =~ /\.sass$/ )
|
84
|
+
page = SassFile.new( site, path, fixed_relative_path )
|
85
|
+
elsif ( File.file?( path ) )
|
86
|
+
page = VerbatimFile.new( site, path, fixed_relative_path )
|
87
|
+
end
|
88
|
+
page
|
89
|
+
end
|
90
|
+
|
91
|
+
|
48
92
|
private
|
49
93
|
|
50
94
|
def set_urls
|
@@ -134,7 +178,8 @@ module Awestruct
|
|
134
178
|
layout_pathname = Pathname.new( layout_path )
|
135
179
|
relative_path = layout_pathname.relative_path_from( dir_pathname ).to_s
|
136
180
|
name = File.basename( layout_path, '.haml' )
|
137
|
-
site.layouts[ name ] = HamlFile.new( site, layout_path, relative_path )
|
181
|
+
#site.layouts[ name ] = HamlFile.new( site, layout_path, relative_path )
|
182
|
+
site.layouts[ name ] = load_page( layout_path, relative_path )
|
138
183
|
end
|
139
184
|
end
|
140
185
|
|
@@ -172,16 +217,7 @@ module Awestruct
|
|
172
217
|
unless ( site.has_page?( path ) )
|
173
218
|
file_pathname = Pathname.new( path )
|
174
219
|
relative_path = file_pathname.relative_path_from( dir_pathname ).to_s
|
175
|
-
page =
|
176
|
-
if ( path =~ /\.haml$/ )
|
177
|
-
page = HamlFile.new( site, path, File.join( '', relative_path ) )
|
178
|
-
elsif ( path =~ /\.md$/ )
|
179
|
-
page = MarukuFile.new( site, path, File.join( '', relative_path ) )
|
180
|
-
elsif ( path =~ /\.sass$/ )
|
181
|
-
page = SassFile.new( site, path, File.join( '', relative_path ) )
|
182
|
-
elsif ( File.file?( path ) )
|
183
|
-
page = VerbatimFile.new( site, path, File.join( '', relative_path ) )
|
184
|
-
end
|
220
|
+
page = load_page( path, relative_path )
|
185
221
|
if ( page )
|
186
222
|
site.pages << page
|
187
223
|
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module Awestruct
|
2
|
+
module Extensions
|
3
|
+
class Atomizer
|
4
|
+
def initialize(entries_name, output_path)
|
5
|
+
@entries_name = entries_name
|
6
|
+
@output_path = output_path
|
7
|
+
end
|
8
|
+
|
9
|
+
def execute(site)
|
10
|
+
entries = site.send( @entries_name )
|
11
|
+
input_page = File.join( File.dirname(__FILE__), 'template.atom.haml' )
|
12
|
+
page = site.engine.load_page( input_page )
|
13
|
+
page.output_path = @output_path
|
14
|
+
page.entries = entries
|
15
|
+
page.title = site.title || site.base_url
|
16
|
+
site.pages << page
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
|
2
|
+
#module Awestruct
|
3
|
+
#module Extension
|
4
|
+
#class IntenseDebate
|
5
|
+
#
|
6
|
+
#def execute(site)
|
7
|
+
#end
|
8
|
+
#
|
9
|
+
#end
|
10
|
+
#end
|
11
|
+
#end
|
12
|
+
|
13
|
+
module Haml::Helpers
|
14
|
+
|
15
|
+
def intense_debate_comments()
|
16
|
+
html = %Q(<script>\n)
|
17
|
+
html += %Q( var idcomments_acct='#{site.intense_debate_acct}';\n)
|
18
|
+
html += %Q( var idcomments_post_id;\n)
|
19
|
+
html += %Q( var idcomments_post_url;\n)
|
20
|
+
html += %Q(</script>\n)
|
21
|
+
html += %Q(<span id="IDCommentsPostTitle" style="display:none"></span>\n)
|
22
|
+
html += %Q(<script type='text/javascript' src='http://www.intensedebate.com/js/genericCommentWrapperV2.js'></script>\n)
|
23
|
+
html
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
@@ -0,0 +1,99 @@
|
|
1
|
+
|
2
|
+
|
3
|
+
module Awestruct
|
4
|
+
module Extensions
|
5
|
+
class Paginator
|
6
|
+
|
7
|
+
module Paginated
|
8
|
+
attr_accessor :window
|
9
|
+
attr_accessor :next_page
|
10
|
+
attr_accessor :previous_page
|
11
|
+
attr_accessor :current_page
|
12
|
+
attr_accessor :current_page_index
|
13
|
+
attr_accessor :pages
|
14
|
+
|
15
|
+
def links
|
16
|
+
html = %Q(<div class="pagination-links">)
|
17
|
+
unless ( previous_page.nil? )
|
18
|
+
html += %Q(<a href="#{previous_page.url}" class="previous-link">Previous</a> )
|
19
|
+
end
|
20
|
+
first_skip = false
|
21
|
+
second_skip = false
|
22
|
+
pages.each_with_index do |page, i|
|
23
|
+
if ( i == current_page_index )
|
24
|
+
html += %Q(<span class="current-page">#{i+1}</span> )
|
25
|
+
elsif ( i <= window )
|
26
|
+
html += %Q(<a href="#{page.url}" class="page-link">#{i+1}</a> )
|
27
|
+
elsif ( ( i > window ) && ( i < ( current_page_index - window ) ) && ! first_skip )
|
28
|
+
html += %Q(<span class="skip">...</span>)
|
29
|
+
first_skip = true
|
30
|
+
elsif ( ( i > ( current_page_index + window ) ) && ( i < ( ( pages.size - window ) - 1 ) ) && ! second_skip )
|
31
|
+
html += %Q(<span class="skip">...</span>)
|
32
|
+
second_skip = true
|
33
|
+
elsif ( ( i >= ( current_page_index - window ) ) && ( i <= ( current_page_index + window ) ) )
|
34
|
+
html += %Q(<a href="#{page.url}" class="page-link">#{i+1}</a> )
|
35
|
+
elsif ( i >= ( ( pages.size - window ) - 1 ) )
|
36
|
+
html += %Q(<a href="#{page.url}" class="page-link">#{i+1}</a> )
|
37
|
+
end
|
38
|
+
end
|
39
|
+
unless ( next_page.nil? )
|
40
|
+
html += %Q(<a href="#{next_page.url}" class="next-link">Next</a> )
|
41
|
+
end
|
42
|
+
html += %Q(</div>)
|
43
|
+
html
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
|
48
|
+
def initialize(prop_name, input_path, opts={})
|
49
|
+
@prop_name = prop_name
|
50
|
+
@input_path = input_path
|
51
|
+
@per_page = opts[:per_page] || 20
|
52
|
+
@window_size = opts[:window_size] || 2
|
53
|
+
end
|
54
|
+
|
55
|
+
def execute(site)
|
56
|
+
removal_path = nil
|
57
|
+
all = site.send( @prop_name )
|
58
|
+
i = 1
|
59
|
+
paginated_pages = []
|
60
|
+
all.each_slice( @per_page ) do |slice|
|
61
|
+
page = site.engine.find_and_load_site_page( @input_path )
|
62
|
+
removal_path ||= page.output_path
|
63
|
+
slice.extend( Paginated )
|
64
|
+
page.send( "#{@prop_name}=", slice )
|
65
|
+
if ( i == 1 )
|
66
|
+
page.output_path = File.join( File.dirname( @input_path ), File.basename( @input_path ) + ".html" )
|
67
|
+
else
|
68
|
+
page.output_path = File.join( File.dirname( @input_path ), "page#{i}.html" )
|
69
|
+
end
|
70
|
+
page.paginate_generated = true
|
71
|
+
site.pages << page
|
72
|
+
paginated_pages << page
|
73
|
+
i = i + 1
|
74
|
+
end
|
75
|
+
|
76
|
+
site.pages.reject!{|page|
|
77
|
+
( ! page.paginate_generated && ( page.output_path == removal_path ) )
|
78
|
+
}
|
79
|
+
|
80
|
+
prev_page = nil
|
81
|
+
paginated_pages.each_with_index do |page,i|
|
82
|
+
page.posts.current_page = page
|
83
|
+
page.posts.current_page_index = i
|
84
|
+
page.posts.pages = paginated_pages
|
85
|
+
page.posts.window = 1
|
86
|
+
|
87
|
+
if ( prev_page != nil )
|
88
|
+
prev_page.send( @prop_name ).next_page = page
|
89
|
+
page.send( @prop_name ).previous_page = prev_page
|
90
|
+
end
|
91
|
+
|
92
|
+
prev_page = page
|
93
|
+
end
|
94
|
+
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
end
|
99
|
+
end
|
@@ -3,8 +3,9 @@ module Awestruct
|
|
3
3
|
module Extensions
|
4
4
|
class Posts
|
5
5
|
|
6
|
-
def initialize(path_prefix)
|
6
|
+
def initialize(path_prefix, assign_to=:posts)
|
7
7
|
@path_prefix = path_prefix
|
8
|
+
@assign_to = assign_to
|
8
9
|
end
|
9
10
|
|
10
11
|
def execute(site)
|
@@ -39,7 +40,7 @@ module Awestruct
|
|
39
40
|
last = e
|
40
41
|
end
|
41
42
|
|
42
|
-
site.
|
43
|
+
site.send( "#{@assign_to}=", posts )
|
43
44
|
end
|
44
45
|
end
|
45
46
|
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
---
|
2
|
+
---
|
3
|
+
!!! XML
|
4
|
+
|
5
|
+
%feed{ 'xml:base'=>site.base_url, 'xml:lang'=>'en-US', :xmlns=>'http://www.w3.org/2005/Atom' }
|
6
|
+
%id #{site.base_url}
|
7
|
+
%title #{page.title}
|
8
|
+
- unless page.entries.empty?
|
9
|
+
%updated= page.entries.first.date.xmlschema
|
10
|
+
%link{:rel=>"alternate", :type=>"text/html", :href=>site.base_url}
|
11
|
+
%link{:rel=>"self", :type=>"application/atom+xml", :href=>"#{site.base_url}#{page.url}" }
|
12
|
+
- for entry in page.entries
|
13
|
+
%entry
|
14
|
+
%id #{site.base_url}#{entry.url}
|
15
|
+
%title= escape_once( entry.title )
|
16
|
+
%updated= entry.date.xmlschema
|
17
|
+
%link{:rel=>"alternate", :type=>"text/html", :href=>"#{site.base_url}#{entry.url}" }
|
18
|
+
%author
|
19
|
+
- if ( defined?( entry.author.name ) )
|
20
|
+
%name= entry.author.name
|
21
|
+
- if ( entry.author.email )
|
22
|
+
%name= entry.author.email
|
23
|
+
- else
|
24
|
+
%name= entry.author
|
25
|
+
%summary
|
26
|
+
= html_to_text( entry.content )
|
27
|
+
%content{:type=>'html'}
|
28
|
+
= escape_once( fully_qualify_urls( site.base_url, entry.content ) )
|
29
|
+
|
data/lib/awestruct/marukuable.rb
CHANGED
@@ -8,11 +8,13 @@ module Awestruct
|
|
8
8
|
super( site )
|
9
9
|
self.source_path = source_path
|
10
10
|
self.relative_source_path = relative_source_path
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
11
|
+
unless ( relative_source_path.nil? )
|
12
|
+
dir_name = File.dirname( relative_source_path )
|
13
|
+
if ( dir_name == '.' )
|
14
|
+
self.output_path = output_filename
|
15
|
+
else
|
16
|
+
self.output_path = File.join( dir_name, output_filename )
|
17
|
+
end
|
16
18
|
end
|
17
19
|
end
|
18
20
|
|
@@ -0,0 +1,45 @@
|
|
1
|
+
# Proc that is instance evaled to create the default inflections for both the
|
2
|
+
# model inflector and the inflector extension.
|
3
|
+
unless defined?( DEFAULT_INFLECTIONS_PROC )
|
4
|
+
DEFAULT_INFLECTIONS_PROC = proc do
|
5
|
+
plural(/$/, 's')
|
6
|
+
plural(/s$/i, 's')
|
7
|
+
plural(/(alias|(?:stat|octop|vir|b)us)$/i, '\1es')
|
8
|
+
plural(/(buffal|tomat)o$/i, '\1oes')
|
9
|
+
plural(/([ti])um$/i, '\1a')
|
10
|
+
plural(/sis$/i, 'ses')
|
11
|
+
plural(/(?:([^f])fe|([lr])f)$/i, '\1\2ves')
|
12
|
+
plural(/(hive)$/i, '\1s')
|
13
|
+
plural(/([^aeiouy]|qu)y$/i, '\1ies')
|
14
|
+
plural(/(x|ch|ss|sh)$/i, '\1es')
|
15
|
+
plural(/(matr|vert|ind)ix|ex$/i, '\1ices')
|
16
|
+
plural(/([m|l])ouse$/i, '\1ice')
|
17
|
+
|
18
|
+
singular(/s$/i, '')
|
19
|
+
singular(/([ti])a$/i, '\1um')
|
20
|
+
singular(/(analy|ba|cri|diagno|parenthe|progno|synop|the)ses$/i, '\1sis')
|
21
|
+
singular(/([^f])ves$/i, '\1fe')
|
22
|
+
singular(/([h|t]ive)s$/i, '\1')
|
23
|
+
singular(/([lr])ves$/i, '\1f')
|
24
|
+
singular(/([^aeiouy]|qu)ies$/i, '\1y')
|
25
|
+
singular(/(m)ovies$/i, '\1ovie')
|
26
|
+
singular(/(x|ch|ss|sh)es$/i, '\1')
|
27
|
+
singular(/([m|l])ice$/i, '\1ouse')
|
28
|
+
singular(/buses$/i, 'bus')
|
29
|
+
singular(/oes$/i, 'o')
|
30
|
+
singular(/shoes$/i, 'shoe')
|
31
|
+
singular(/(alias|(?:stat|octop|vir|b)us)es$/i, '\1')
|
32
|
+
singular(/(vert|ind)ices$/i, '\1ex')
|
33
|
+
singular(/matrices$/i, 'matrix')
|
34
|
+
|
35
|
+
irregular('person', 'people')
|
36
|
+
irregular('man', 'men')
|
37
|
+
irregular('child', 'children')
|
38
|
+
irregular('sex', 'sexes')
|
39
|
+
irregular('move', 'moves')
|
40
|
+
irregular('quiz', 'quizzes')
|
41
|
+
irregular('testis', 'testes')
|
42
|
+
|
43
|
+
uncountable(%w(equipment information rice money species series fish sheep news))
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,242 @@
|
|
1
|
+
# The inflector extension adds inflection instance methods to String, which allows the easy transformation of
|
2
|
+
# words from singular to plural, class names to table names, modularized class
|
3
|
+
# names to ones without, and class names to foreign keys. It exists for
|
4
|
+
# backwards compatibility to legacy Sequel code.
|
5
|
+
|
6
|
+
class String
|
7
|
+
# This module acts as a singleton returned/yielded by String.inflections,
|
8
|
+
# which is used to override or specify additional inflection rules. Examples:
|
9
|
+
#
|
10
|
+
# String.inflections do |inflect|
|
11
|
+
# inflect.plural /^(ox)$/i, '\1\2en'
|
12
|
+
# inflect.singular /^(ox)en/i, '\1'
|
13
|
+
#
|
14
|
+
# inflect.irregular 'octopus', 'octopi'
|
15
|
+
#
|
16
|
+
# inflect.uncountable "equipment"
|
17
|
+
# end
|
18
|
+
#
|
19
|
+
# New rules are added at the top. So in the example above, the irregular rule for octopus will now be the first of the
|
20
|
+
# pluralization and singularization rules that is runs. This guarantees that your rules run before any of the rules that may
|
21
|
+
# already have been loaded.
|
22
|
+
module Inflections
|
23
|
+
@plurals, @singulars, @uncountables = [], [], []
|
24
|
+
|
25
|
+
class << self
|
26
|
+
# Array of 2 element arrays, first containing a regex, and the second containing a substitution pattern, used for plurization.
|
27
|
+
attr_reader :plurals
|
28
|
+
|
29
|
+
# Array of 2 element arrays, first containing a regex, and the second containing a substitution pattern, used for singularization.
|
30
|
+
attr_reader :singulars
|
31
|
+
|
32
|
+
# Array of strings for words were the singular form is the same as the plural form
|
33
|
+
attr_reader :uncountables
|
34
|
+
end
|
35
|
+
|
36
|
+
# Clears the loaded inflections within a given scope (default is :all). Give the scope as a symbol of the inflection type,
|
37
|
+
# the options are: :plurals, :singulars, :uncountables
|
38
|
+
#
|
39
|
+
# Examples:
|
40
|
+
# clear :all
|
41
|
+
# clear :plurals
|
42
|
+
def self.clear(scope = :all)
|
43
|
+
case scope
|
44
|
+
when :all
|
45
|
+
@plurals, @singulars, @uncountables = [], [], []
|
46
|
+
else
|
47
|
+
instance_variable_set("@#{scope}", [])
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
# Specifies a new irregular that applies to both pluralization and singularization at the same time. This can only be used
|
52
|
+
# for strings, not regular expressions. You simply pass the irregular in singular and plural form.
|
53
|
+
#
|
54
|
+
# Examples:
|
55
|
+
# irregular 'octopus', 'octopi'
|
56
|
+
# irregular 'person', 'people'
|
57
|
+
def self.irregular(singular, plural)
|
58
|
+
plural(Regexp.new("(#{singular[0,1]})#{singular[1..-1]}$", "i"), '\1' + plural[1..-1])
|
59
|
+
singular(Regexp.new("(#{plural[0,1]})#{plural[1..-1]}$", "i"), '\1' + singular[1..-1])
|
60
|
+
end
|
61
|
+
|
62
|
+
# Specifies a new pluralization rule and its replacement. The rule can either be a string or a regular expression.
|
63
|
+
# The replacement should always be a string that may include references to the matched data from the rule.
|
64
|
+
#
|
65
|
+
# Example:
|
66
|
+
# plural(/(x|ch|ss|sh)$/i, '\1es')
|
67
|
+
def self.plural(rule, replacement)
|
68
|
+
@plurals.insert(0, [rule, replacement])
|
69
|
+
end
|
70
|
+
|
71
|
+
# Specifies a new singularization rule and its replacement. The rule can either be a string or a regular expression.
|
72
|
+
# The replacement should always be a string that may include references to the matched data from the rule.
|
73
|
+
#
|
74
|
+
# Example:
|
75
|
+
# singular(/([^aeiouy]|qu)ies$/i, '\1y')
|
76
|
+
def self.singular(rule, replacement)
|
77
|
+
@singulars.insert(0, [rule, replacement])
|
78
|
+
end
|
79
|
+
|
80
|
+
# Add uncountable words that shouldn't be attempted inflected.
|
81
|
+
#
|
82
|
+
# Examples:
|
83
|
+
# uncountable "money"
|
84
|
+
# uncountable "money", "information"
|
85
|
+
# uncountable %w( money information rice )
|
86
|
+
def self.uncountable(*words)
|
87
|
+
(@uncountables << words).flatten!
|
88
|
+
end
|
89
|
+
|
90
|
+
load( File.join( File.dirname(__FILE__), 'default_inflections.rb' ) )
|
91
|
+
instance_eval(&DEFAULT_INFLECTIONS_PROC)
|
92
|
+
end
|
93
|
+
|
94
|
+
# Yield the Inflections module if a block is given, and return
|
95
|
+
# the Inflections module.
|
96
|
+
def self.inflections
|
97
|
+
yield Inflections if block_given?
|
98
|
+
Inflections
|
99
|
+
end
|
100
|
+
|
101
|
+
# By default, camelize converts the string to UpperCamelCase. If the argument to camelize
|
102
|
+
# is set to :lower then camelize produces lowerCamelCase.
|
103
|
+
#
|
104
|
+
# camelize will also convert '/' to '::' which is useful for converting paths to namespaces
|
105
|
+
#
|
106
|
+
# Examples
|
107
|
+
# "active_record".camelize #=> "ActiveRecord"
|
108
|
+
# "active_record".camelize(:lower) #=> "activeRecord"
|
109
|
+
# "active_record/errors".camelize #=> "ActiveRecord::Errors"
|
110
|
+
# "active_record/errors".camelize(:lower) #=> "activeRecord::Errors"
|
111
|
+
def camelize(first_letter_in_uppercase = :upper)
|
112
|
+
s = gsub(/\/(.?)/){|x| "::#{x[-1..-1].upcase unless x == '/'}"}.gsub(/(^|_)(.)/){|x| x[-1..-1].upcase}
|
113
|
+
s[0...1] = s[0...1].downcase unless first_letter_in_uppercase == :upper
|
114
|
+
s
|
115
|
+
end
|
116
|
+
alias_method :camelcase, :camelize
|
117
|
+
|
118
|
+
# Singularizes and camelizes the string. Also strips out all characters preceding
|
119
|
+
# and including a period (".").
|
120
|
+
#
|
121
|
+
# Examples
|
122
|
+
# "egg_and_hams".classify #=> "EggAndHam"
|
123
|
+
# "post".classify #=> "Post"
|
124
|
+
# "schema.post".classify #=> "Post"
|
125
|
+
def classify
|
126
|
+
sub(/.*\./, '').singularize.camelize
|
127
|
+
end
|
128
|
+
|
129
|
+
# Constantize tries to find a declared constant with the name specified
|
130
|
+
# in the string. It raises a NameError when the name is not in CamelCase
|
131
|
+
# or is not initialized.
|
132
|
+
#
|
133
|
+
# Examples
|
134
|
+
# "Module".constantize #=> Module
|
135
|
+
# "Class".constantize #=> Class
|
136
|
+
def constantize
|
137
|
+
raise(NameError, "#{inspect} is not a valid constant name!") unless m = /\A(?:::)?([A-Z]\w*(?:::[A-Z]\w*)*)\z/.match(self)
|
138
|
+
Object.module_eval("::#{m[1]}", __FILE__, __LINE__)
|
139
|
+
end
|
140
|
+
|
141
|
+
# Replaces underscores with dashes in the string.
|
142
|
+
#
|
143
|
+
# Example
|
144
|
+
# "puni_puni".dasherize #=> "puni-puni"
|
145
|
+
def dasherize
|
146
|
+
gsub(/_/, '-')
|
147
|
+
end
|
148
|
+
|
149
|
+
# Removes the module part from the expression in the string
|
150
|
+
#
|
151
|
+
# Examples
|
152
|
+
# "ActiveRecord::CoreExtensions::String::Inflections".demodulize #=> "Inflections"
|
153
|
+
# "Inflections".demodulize #=> "Inflections"
|
154
|
+
def demodulize
|
155
|
+
gsub(/^.*::/, '')
|
156
|
+
end
|
157
|
+
|
158
|
+
# Creates a foreign key name from a class name.
|
159
|
+
# +use_underscore+ sets whether the method should put '_' between the name and 'id'.
|
160
|
+
#
|
161
|
+
# Examples
|
162
|
+
# "Message".foreign_key #=> "message_id"
|
163
|
+
# "Message".foreign_key(false) #=> "messageid"
|
164
|
+
# "Admin::Post".foreign_key #=> "post_id"
|
165
|
+
def foreign_key(use_underscore = true)
|
166
|
+
"#{demodulize.underscore}#{'_' if use_underscore}id"
|
167
|
+
end
|
168
|
+
|
169
|
+
# Capitalizes the first word and turns underscores into spaces and strips _id.
|
170
|
+
# Like titleize, this is meant for creating pretty output.
|
171
|
+
#
|
172
|
+
# Examples
|
173
|
+
# "employee_salary" #=> "Employee salary"
|
174
|
+
# "author_id" #=> "Author"
|
175
|
+
def humanize
|
176
|
+
gsub(/_id$/, "").gsub(/_/, " ").capitalize
|
177
|
+
end
|
178
|
+
|
179
|
+
# Returns the plural form of the word in the string.
|
180
|
+
#
|
181
|
+
# Examples
|
182
|
+
# "post".pluralize #=> "posts"
|
183
|
+
# "octopus".pluralize #=> "octopi"
|
184
|
+
# "sheep".pluralize #=> "sheep"
|
185
|
+
# "words".pluralize #=> "words"
|
186
|
+
# "the blue mailman".pluralize #=> "the blue mailmen"
|
187
|
+
# "CamelOctopus".pluralize #=> "CamelOctopi"
|
188
|
+
def pluralize
|
189
|
+
result = dup
|
190
|
+
Inflections.plurals.each{|(rule, replacement)| break if result.gsub!(rule, replacement)} unless Inflections.uncountables.include?(downcase)
|
191
|
+
result
|
192
|
+
end
|
193
|
+
|
194
|
+
# The reverse of pluralize, returns the singular form of a word in a string.
|
195
|
+
#
|
196
|
+
# Examples
|
197
|
+
# "posts".singularize #=> "post"
|
198
|
+
# "octopi".singularize #=> "octopus"
|
199
|
+
# "sheep".singluarize #=> "sheep"
|
200
|
+
# "word".singluarize #=> "word"
|
201
|
+
# "the blue mailmen".singularize #=> "the blue mailman"
|
202
|
+
# "CamelOctopi".singularize #=> "CamelOctopus"
|
203
|
+
def singularize
|
204
|
+
result = dup
|
205
|
+
Inflections.singulars.each{|(rule, replacement)| break if result.gsub!(rule, replacement)} unless Inflections.uncountables.include?(downcase)
|
206
|
+
result
|
207
|
+
end
|
208
|
+
|
209
|
+
# Underscores and pluralizes the string.
|
210
|
+
#
|
211
|
+
# Examples
|
212
|
+
# "RawScaledScorer".tableize #=> "raw_scaled_scorers"
|
213
|
+
# "egg_and_ham".tableize #=> "egg_and_hams"
|
214
|
+
# "fancyCategory".tableize #=> "fancy_categories"
|
215
|
+
def tableize
|
216
|
+
underscore.pluralize
|
217
|
+
end
|
218
|
+
|
219
|
+
# Capitalizes all the words and replaces some characters in the string to create
|
220
|
+
# a nicer looking title. Titleize is meant for creating pretty output.
|
221
|
+
#
|
222
|
+
# titleize is also aliased as as titlecase
|
223
|
+
#
|
224
|
+
# Examples
|
225
|
+
# "man from the boondocks".titleize #=> "Man From The Boondocks"
|
226
|
+
# "x-men: the last stand".titleize #=> "X Men: The Last Stand"
|
227
|
+
def titleize
|
228
|
+
underscore.humanize.gsub(/\b([a-z])/){|x| x[-1..-1].upcase}
|
229
|
+
end
|
230
|
+
alias_method :titlecase, :titleize
|
231
|
+
|
232
|
+
# The reverse of camelize. Makes an underscored form from the expression in the string.
|
233
|
+
# Also changes '::' to '/' to convert namespaces to paths.
|
234
|
+
#
|
235
|
+
# Examples
|
236
|
+
# "ActiveRecord".underscore #=> "active_record"
|
237
|
+
# "ActiveRecord::Errors".underscore #=> active_record/errors
|
238
|
+
def underscore
|
239
|
+
gsub(/::/, '/').gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
|
240
|
+
gsub(/([a-z\d])([A-Z])/,'\1_\2').tr("-", "_").downcase
|
241
|
+
end
|
242
|
+
end
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: 0.0.
|
8
|
+
- 4
|
9
|
+
version: 0.0.4
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Bob McWhirter
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-02-
|
17
|
+
date: 2010-02-28 00:00:00 -05:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -93,7 +93,10 @@ files:
|
|
93
93
|
- lib/awestruct/commands/server.rb
|
94
94
|
- lib/awestruct/config.rb
|
95
95
|
- lib/awestruct/engine.rb
|
96
|
+
- lib/awestruct/extensions/atomizer.rb
|
96
97
|
- lib/awestruct/extensions/indexifier.rb
|
98
|
+
- lib/awestruct/extensions/intense_debate.rb
|
99
|
+
- lib/awestruct/extensions/paginator.rb
|
97
100
|
- lib/awestruct/extensions/pipeline.rb
|
98
101
|
- lib/awestruct/extensions/posts.rb
|
99
102
|
- lib/awestruct/front_matter_file.rb
|
@@ -107,10 +110,13 @@ files:
|
|
107
110
|
- lib/awestruct/sass_file.rb
|
108
111
|
- lib/awestruct/sassable.rb
|
109
112
|
- lib/awestruct/site.rb
|
113
|
+
- lib/awestruct/util/default_inflections.rb
|
114
|
+
- lib/awestruct/util/inflector.rb
|
110
115
|
- lib/awestruct/verbatim_file.rb
|
111
116
|
- lib/awestruct.rb
|
112
117
|
- lib/awestruct/commands/base_index.html.haml
|
113
118
|
- lib/awestruct/commands/base_layout.html.haml
|
119
|
+
- lib/awestruct/extensions/template.atom.haml
|
114
120
|
has_rdoc: true
|
115
121
|
homepage:
|
116
122
|
licenses: []
|