languages 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9d12556cf95cd74fbd61a95960484102707915f7
4
+ data.tar.gz: 46f2359172a824bffd38971b54ef09c2885d9229
5
+ SHA512:
6
+ metadata.gz: 90c454846e3e7dc10e37e150448145a0877c455140965600642a01d3c6894595a5c9bf20c9d56fcf6ddfb2690ee629811cc20dc59d3dec39f41eb4fbdbff30b0
7
+ data.tar.gz: b1c331b1e8cf829cad2d0bb04ec9c7ad9716767207c48a5f135175e04a7be4c2a05b7fe95f9a396da5251989a921201ae07a0e12096bd70dc97b47263dbc2c40
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in languages.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Andrew Nesbitt
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,35 @@
1
+ # Languages
2
+
3
+ Just the language names and colors extracted from [github-lingust](https://github.com/github/linguist), avoiding the heavy dependencies like `charlock_holmes` which doesn't install well on heroku.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'languages'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install languages
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ Languages::Language['ruby'] #=> <Languages::Language name=Ruby color=#701516>
25
+
26
+ Languages::Language.all #=> [#<Languages::Language name=ActionScript color=#e3491a>, ..]
27
+ ```
28
+
29
+ ## Contributing
30
+
31
+ 1. Fork it ( https://github.com/andrew/languages/fork )
32
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
33
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
34
+ 4. Push to the branch (`git push origin my-new-feature`)
35
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/languages.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'languages/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "languages"
8
+ spec.version = Languages::VERSION
9
+ spec.authors = ["Andrew Nesbitt"]
10
+ spec.email = ["andrewnez@gmail.com"]
11
+ spec.summary = "Github languages and colors"
12
+ spec.description = "Just the language names and colors from github-lingust"
13
+ spec.homepage = "https://github.com/andrew/languages"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ end
@@ -0,0 +1,3 @@
1
+ module Languages
2
+ VERSION = "1.0.0"
3
+ end
data/lib/languages.rb ADDED
@@ -0,0 +1,409 @@
1
+ require 'escape_utils'
2
+ require 'yaml'
3
+
4
+ module Languages
5
+ # Language names that are recognizable by GitHub. Defined languages
6
+ # can be highlighted, searched and listed under the Top Languages page.
7
+ #
8
+ # Languages are defined in `lib/languages.yml`.
9
+ class Language
10
+ @languages = []
11
+ @index = {}
12
+ @name_index = {}
13
+ @alias_index = {}
14
+
15
+ # Valid Languages types
16
+ TYPES = [:data, :markup, :programming, :prose]
17
+
18
+ # Detect languages by a specific type
19
+ #
20
+ # type - A symbol that exists within TYPES
21
+ #
22
+ # Returns an array
23
+ def self.by_type(type)
24
+ all.select { |h| h.type == type }
25
+ end
26
+
27
+ # Internal: Create a new Language object
28
+ #
29
+ # attributes - A hash of attributes
30
+ #
31
+ # Returns a Language object
32
+ def self.create(attributes = {})
33
+ language = new(attributes)
34
+
35
+ @languages << language
36
+
37
+ # All Language names should be unique. Raise if there is a duplicate.
38
+ if @name_index.key?(language.name)
39
+ raise ArgumentError, "Duplicate language name: #{language.name}"
40
+ end
41
+
42
+ # Language name index
43
+ @index[language.name.downcase] = @name_index[language.name.downcase] = language
44
+
45
+ language.aliases.each do |name|
46
+ # All Language aliases should be unique. Raise if there is a duplicate.
47
+ if @alias_index.key?(name)
48
+ raise ArgumentError, "Duplicate alias: #{name}"
49
+ end
50
+
51
+ @index[name.downcase] = @alias_index[name.downcase] = language
52
+ end
53
+
54
+ language.extensions.each do |extension|
55
+ if extension !~ /^\./
56
+ raise ArgumentError, "Extension is missing a '.': #{extension.inspect}"
57
+ end
58
+
59
+ @extension_index[extension] << language
60
+ end
61
+
62
+ language.interpreters.each do |interpreter|
63
+ @interpreter_index[interpreter] << language
64
+ end
65
+
66
+ language.filenames.each do |filename|
67
+ @filename_index[filename] << language
68
+ end
69
+
70
+ language
71
+ end
72
+
73
+ # Public: Get all Languages
74
+ #
75
+ # Returns an Array of Languages
76
+ def self.all
77
+ @languages
78
+ end
79
+
80
+ # Public: Look up Language by its proper name.
81
+ #
82
+ # name - The String name of the Language
83
+ #
84
+ # Examples
85
+ #
86
+ # Language.find_by_name('Ruby')
87
+ # # => #<Language name="Ruby">
88
+ #
89
+ # Returns the Language or nil if none was found.
90
+ def self.find_by_name(name)
91
+ name && @name_index[name.downcase]
92
+ end
93
+
94
+ # Public: Look up Language by one of its aliases.
95
+ #
96
+ # name - A String alias of the Language
97
+ #
98
+ # Examples
99
+ #
100
+ # Language.find_by_alias('cpp')
101
+ # # => #<Language name="C++">
102
+ #
103
+ # Returns the Language or nil if none was found.
104
+ def self.find_by_alias(name)
105
+ name && @alias_index[name.downcase]
106
+ end
107
+
108
+ # Public: Look up Language by its name.
109
+ #
110
+ # name - The String name of the Language
111
+ #
112
+ # Examples
113
+ #
114
+ # Language['Ruby']
115
+ # # => #<Language name="Ruby">
116
+ #
117
+ # Language['ruby']
118
+ # # => #<Language name="Ruby">
119
+ #
120
+ # Returns the Language or nil if none was found.
121
+ def self.[](name)
122
+ name && @index[name.downcase]
123
+ end
124
+
125
+ # Public: A List of popular languages
126
+ #
127
+ # Popular languages are sorted to the top of language chooser
128
+ # dropdowns.
129
+ #
130
+ # This list is configured in "popular.yml".
131
+ #
132
+ # Returns an Array of Languages.
133
+ def self.popular
134
+ @popular ||= all.select(&:popular?).sort_by { |lang| lang.name.downcase }
135
+ end
136
+
137
+ # Public: A List of non-popular languages
138
+ #
139
+ # Unpopular languages appear below popular ones in language
140
+ # chooser dropdowns.
141
+ #
142
+ # This list is created from all the languages not listed in "popular.yml".
143
+ #
144
+ # Returns an Array of Languages.
145
+ def self.unpopular
146
+ @unpopular ||= all.select(&:unpopular?).sort_by { |lang| lang.name.downcase }
147
+ end
148
+
149
+ # Public: A List of languages with assigned colors.
150
+ #
151
+ # Returns an Array of Languages.
152
+ def self.colors
153
+ @colors ||= all.select(&:color).sort_by { |lang| lang.name.downcase }
154
+ end
155
+
156
+ # Internal: Initialize a new Language
157
+ #
158
+ # attributes - A hash of attributes
159
+ def initialize(attributes = {})
160
+ # @name is required
161
+ @name = attributes[:name] || raise(ArgumentError, "missing name")
162
+
163
+ # Set type
164
+ @type = attributes[:type] ? attributes[:type].to_sym : nil
165
+ if @type && !TYPES.include?(@type)
166
+ raise ArgumentError, "invalid type: #{@type}"
167
+ end
168
+
169
+ @color = attributes[:color]
170
+
171
+ # Set aliases
172
+ @aliases = [default_alias_name] + (attributes[:aliases] || [])
173
+
174
+ # Load the TextMate scope name or try to guess one
175
+ @tm_scope = attributes[:tm_scope] || begin
176
+ context = case @type
177
+ when :data, :markup, :prose
178
+ 'text'
179
+ when :programming, nil
180
+ 'source'
181
+ end
182
+ "#{context}.#{@name.downcase}"
183
+ end
184
+
185
+ @ace_mode = attributes[:ace_mode]
186
+ @wrap = attributes[:wrap] || false
187
+
188
+ # Set legacy search term
189
+ @search_term = attributes[:search_term] || default_alias_name
190
+
191
+ # Set extensions or default to [].
192
+ @extensions = attributes[:extensions] || []
193
+ @interpreters = attributes[:interpreters] || []
194
+ @filenames = attributes[:filenames] || []
195
+
196
+ # Set popular, and searchable flags
197
+ @popular = attributes.key?(:popular) ? attributes[:popular] : false
198
+ @searchable = attributes.key?(:searchable) ? attributes[:searchable] : true
199
+
200
+ # If group name is set, save the name so we can lazy load it later
201
+ if attributes[:group_name]
202
+ @group = nil
203
+ @group_name = attributes[:group_name]
204
+
205
+ # Otherwise we can set it to self now
206
+ else
207
+ @group = self
208
+ end
209
+ end
210
+
211
+ # Public: Get proper name
212
+ #
213
+ # Examples
214
+ #
215
+ # # => "Ruby"
216
+ # # => "Python"
217
+ # # => "Perl"
218
+ #
219
+ # Returns the name String
220
+ attr_reader :name
221
+
222
+ # Public: Get type.
223
+ #
224
+ # Returns a type Symbol or nil.
225
+ attr_reader :type
226
+
227
+ # Public: Get color.
228
+ #
229
+ # Returns a hex color String.
230
+ attr_reader :color
231
+
232
+ # Public: Get aliases
233
+ #
234
+ # Examples
235
+ #
236
+ # Language['C++'].aliases
237
+ # # => ["cpp"]
238
+ #
239
+ # Returns an Array of String names
240
+ attr_reader :aliases
241
+
242
+ # Deprecated: Get code search term
243
+ #
244
+ # Examples
245
+ #
246
+ # # => "ruby"
247
+ # # => "python"
248
+ # # => "perl"
249
+ #
250
+ # Returns the name String
251
+ attr_reader :search_term
252
+
253
+ # Public: Get the name of a TextMate-compatible scope
254
+ #
255
+ # Returns the scope
256
+ attr_reader :tm_scope
257
+
258
+ # Public: Get Ace mode
259
+ #
260
+ # Examples
261
+ #
262
+ # # => "text"
263
+ # # => "javascript"
264
+ # # => "c_cpp"
265
+ #
266
+ # Returns a String name or nil
267
+ attr_reader :ace_mode
268
+
269
+ # Public: Should language lines be wrapped
270
+ #
271
+ # Returns true or false
272
+ attr_reader :wrap
273
+
274
+ # Public: Get extensions
275
+ #
276
+ # Examples
277
+ #
278
+ # # => ['.rb', '.rake', ...]
279
+ #
280
+ # Returns the extensions Array
281
+ attr_reader :extensions
282
+
283
+ # Public: Get interpreters
284
+ #
285
+ # Examples
286
+ #
287
+ # # => ['awk', 'gawk', 'mawk' ...]
288
+ #
289
+ # Returns the interpreters Array
290
+ attr_reader :interpreters
291
+
292
+ # Public: Get filenames
293
+ #
294
+ # Examples
295
+ #
296
+ # # => ['Rakefile', ...]
297
+ #
298
+ # Returns the extensions Array
299
+ attr_reader :filenames
300
+
301
+ # Deprecated: Get primary extension
302
+ #
303
+ # Defaults to the first extension but can be overridden
304
+ # in the languages.yml.
305
+ #
306
+ # The primary extension can not be nil. Tests should verify this.
307
+ #
308
+ # This method is only used by app/helpers/gists_helper.rb for creating
309
+ # the language dropdown. It really should be using `name` instead.
310
+ # Would like to drop primary extension.
311
+ #
312
+ # Returns the extension String.
313
+ def primary_extension
314
+ extensions.first
315
+ end
316
+
317
+ # Public: Get URL escaped name.
318
+ #
319
+ # Examples
320
+ #
321
+ # "C%23"
322
+ # "C%2B%2B"
323
+ # "Common%20Lisp"
324
+ #
325
+ # Returns the escaped String.
326
+ def escaped_name
327
+ EscapeUtils.escape_url(name).gsub('+', '%20')
328
+ end
329
+
330
+ # Internal: Get default alias name
331
+ #
332
+ # Returns the alias name String
333
+ def default_alias_name
334
+ name.downcase.gsub(/\s/, '-')
335
+ end
336
+
337
+ # Public: Get Language group
338
+ #
339
+ # Returns a Language
340
+ def group
341
+ @group ||= Language.find_by_name(@group_name)
342
+ end
343
+
344
+ # Public: Is it popular?
345
+ #
346
+ # Returns true or false
347
+ def popular?
348
+ @popular
349
+ end
350
+
351
+ # Public: Is it not popular?
352
+ #
353
+ # Returns true or false
354
+ def unpopular?
355
+ !popular?
356
+ end
357
+
358
+ # Public: Is it searchable?
359
+ #
360
+ # Unsearchable languages won't by indexed by solr and won't show
361
+ # up in the code search dropdown.
362
+ #
363
+ # Returns true or false
364
+ def searchable?
365
+ @searchable
366
+ end
367
+
368
+ # Public: Return name as String representation
369
+ def to_s
370
+ name
371
+ end
372
+
373
+ def ==(other)
374
+ eql?(other)
375
+ end
376
+
377
+ def eql?(other)
378
+ equal?(other)
379
+ end
380
+
381
+ def hash
382
+ name.hash
383
+ end
384
+
385
+ def inspect
386
+ "#<#{self.class} name=#{name} color=#{color}>"
387
+ end
388
+ end
389
+
390
+ popular = YAML.load_file(File.expand_path("../popular.yml", __FILE__))
391
+
392
+ languages = YAML.load_file(File.expand_path("../languages.yml", __FILE__))
393
+
394
+ languages.each do |name, options|
395
+ Language.create(
396
+ :name => name,
397
+ :color => options['color'],
398
+ :type => options['type'],
399
+ :aliases => options['aliases'],
400
+ :tm_scope => options['tm_scope'],
401
+ :ace_mode => options['ace_mode'],
402
+ :wrap => options['wrap'],
403
+ :group_name => options['group'],
404
+ :searchable => options.fetch('searchable', true),
405
+ :search_term => options['search_term'],
406
+ :popular => popular.include?(name)
407
+ )
408
+ end
409
+ end