Module: Scribo::Utility
- Defined in:
- lib/scribo/utility.rb
Constant Summary collapse
- ADDITIONAL_EXTENSIONS =
{ 'text/css' => %w[scss sass], 'text/html' => %w[md markdown mkd slim], 'application/javascript' => %w[es6 babel jsx js] }.freeze
- KNOWN_TEXT_FILES =
%w[Gemfile].freeze
- KNOWN_TEXT_EXTENSIONS =
%w[scss sass less slim es6 babel jsx json link].freeze
- FILTER_FOR_EXTENSION =
{ 'scss' => 'scss', 'sass' => 'sass', 'md' => 'markdown', 'markdown' => 'markdown', 'mkd' => 'markdown', 'slim' => 'slim', 'es6' => 'babel', 'babel' => 'babel', 'jsx' => 'babel' }.freeze
- OUTPUT_CONTENT_TYPE_FOR_EXTENSION =
{ 'scss' => 'text/css', 'sass' => 'text/css', 'md' => 'text/html', 'markdown' => 'text/html', 'mkd' => 'text/html', 'slim' => 'text/html', 'es6' => 'application/javascript', 'babel' => 'application/javascript', 'jsx' => 'application/javascript', 'js' => 'application/javascript' }.freeze
Class Method Summary collapse
- .file_name(path) ⇒ Object
- .filter_for_path(path) ⇒ Object
- .kind_for_content_type(content_type) ⇒ Object
- .kind_for_path(path) ⇒ Object
- .output_content_type(content) ⇒ Object
- .switch_extension(path, new_extension = '') ⇒ Object
- .variations_for_path(path) ⇒ Object
- .yaml_safe_parse(text) ⇒ Object
Class Method Details
.file_name(path) ⇒ Object
46 47 48 |
# File 'lib/scribo/utility.rb', line 46 def file_name(path) File.basename(path, File.extname(path)) end |
.filter_for_path(path) ⇒ Object
69 70 71 |
# File 'lib/scribo/utility.rb', line 69 def filter_for_path(path) FILTER_FOR_EXTENSION[File.extname(path.to_s)[1..-1].to_s] end |
.kind_for_content_type(content_type) ⇒ Object
56 57 58 |
# File 'lib/scribo/utility.rb', line 56 def kind_for_content_type(content_type) MIME::Types[content_type].any? { |t| t.media_type == 'text' } ? 'text' : 'asset' end |
.kind_for_path(path) ⇒ Object
60 61 62 63 64 65 66 67 |
# File 'lib/scribo/utility.rb', line 60 def kind_for_path(path) if KNOWN_TEXT_EXTENSIONS.include?(File.extname(path)[1..-1].to_s) || KNOWN_TEXT_FILES.include?(File.basename(path)) 'text' else MIME::Types.type_for(path).any? { |t| t.media_type == 'text' } ? 'text' : 'asset' end end |
.output_content_type(content) ⇒ Object
90 91 92 |
# File 'lib/scribo/utility.rb', line 90 def output_content_type(content) OUTPUT_CONTENT_TYPE_FOR_EXTENSION[File.extname(content.path)[1..-1].to_s] || content.content_type end |
.switch_extension(path, new_extension = '') ⇒ Object
50 51 52 53 54 |
# File 'lib/scribo/utility.rb', line 50 def switch_extension(path, new_extension = '') new_extension = '.' + new_extension if new_extension.present? && !new_extension.start_with?('.') ext = File.extname(path) path.gsub(/#{ext}$/, new_extension) end |
.variations_for_path(path) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/scribo/utility.rb', line 73 def variations_for_path(path) path = path.to_s result = [] MIME::Types.type_for(path).each do |mime_type| result += mime_type.extensions if mime_type.extensions result += ADDITIONAL_EXTENSIONS[mime_type.content_type] || [] end result += [File.extname(path).gsub(/^\./, '')] dir = File.dirname(path) ext = File.extname(path) base = File.basename(path, ext) variations = result.compact.uniq.map do |e| (dir.end_with?('/') ? dir : dir + '/') + base + '.' + e end variations + [(dir.end_with?('/') ? dir : dir + '/') + base] end |
.yaml_safe_parse(text) ⇒ Object
41 42 43 44 |
# File 'lib/scribo/utility.rb', line 41 def yaml_safe_parse(text) permitted_classes = [Date, Time] YAML.safe_load(text, permitted_classes: permitted_classes, aliases: true) end |