Module: Mint::Helpers

Defined in:
lib/mint/helpers.rb

Class Method Summary collapse

Class Method Details

.create_temp_file!(basename, extension = nil, &block) ⇒ Object



150
151
152
153
154
155
156
157
# File 'lib/mint/helpers.rb', line 150

def self.create_temp_file!(basename, extension=nil, &block)
  tmp_args = basename && extension ? [basename, extension] : basename
  tempfile = Tempfile.new(tmp_args)
  block.call(tempfile)
  tempfile.flush
  tempfile.close
  tempfile.path
end

.generate_temp_file!(file) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
# File 'lib/mint/helpers.rb', line 159

def self.generate_temp_file!(file)
  basename  = File.basename file
  extension = File.extname file
  content   = File.read file

  tempfile = Tempfile.new([basename, extension])
  tempfile << content
  tempfile.flush
  tempfile.close
  tempfile.path
end

.hashify(list1, list2) ⇒ Object



106
107
108
# File 'lib/mint/helpers.rb', line 106

def self.hashify(list1, list2)
  Hash[*list1.zip(list2).flatten]
end

.listify(list) ⇒ Object



74
75
76
77
78
79
80
# File 'lib/mint/helpers.rb', line 74

def self.listify(list)
  if list.length > 2
    list[0..-2].join(", ") + " & " + list.last
  else
    list.join(" & ")
  end
end

.normalize_path(to_directory, from_directory) ⇒ Pathname

Returns the relative path to to_directory from from_directory. If to_directory and from_directory have no parents in common besides /, returns the absolute directory of to_directory. Assumes no symlinks.



119
120
121
122
123
124
125
# File 'lib/mint/helpers.rb', line 119

def self.normalize_path(to_directory, from_directory)
  to_path, from_path = [to_directory, from_directory].map {|d| pathize d }
  to_root, from_root = [to_path, from_path].map {|p| p.each_filename.first }
  to_root == from_root ?
    to_path.relative_path_from(from_path) :
    to_path
end

.pathize(str_or_path) ⇒ Pathname

Transforms a String or Pathname into a fully expanded Pathname.



46
47
48
49
50
51
52
53
# File 'lib/mint/helpers.rb', line 46

def self.pathize(str_or_path)
  case str_or_path
  when String
    Pathname.new str_or_path
  when Pathname
    str_or_path
  end.expand_path
end

.slugize(obj) ⇒ String

Transforms a String into a URL-ready slug. Properly handles ampersands, non-alphanumeric characters, extra hyphens and spaces.



26
27
28
29
30
31
32
# File 'lib/mint/helpers.rb', line 26

def self.slugize(obj)
  obj.to_s.downcase.
    gsub(/&/, "and").
    gsub(/[\s-]+/, "-").
    gsub(/[^a-z0-9-]/, "").
    gsub(/[-]+/, "-")
end

.standardize(metadata, opts = {}) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/mint/helpers.rb', line 82

def self.standardize(, opts={})
  table = opts[:table] || {}
  .reduce({}) do |hash, (key,value)|
    if table[key] && table[key].length == 2
      standard_key, standard_type = table[key]
      standard_value =
        case standard_type
        when :array
          [*value]
        when :string
          value
        else
          # If key/type were not in table
          value
        end

      hash[standard_key] = standard_value
    else
      hash[key] = value
    end
    hash
  end
end

.symbolize(obj) ⇒ Symbol

Transforms a potentially hyphenated String into a symbol name.



38
39
40
# File 'lib/mint/helpers.rb', line 38

def self.symbolize(obj)
  slugize(obj).gsub(/-/, "_").to_sym
end

.symbolize_keys(map, opts = {}) ⇒ Hash

Recursively transforms all keys in a Hash into Symbols.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/mint/helpers.rb', line 59

def self.symbolize_keys(map, opts={})
  transform = lambda {|x| opts[:downcase] ? x.downcase : x }

  map.reduce(Hash.new) do |syms,(k,v)|
    syms[transform[k].to_sym] =
      case v
      when Hash
        self.symbolize_keys(v, opts)
      else
        v
      end
    syms
  end
end

Transforms markdown links from .md extensions to .html for digital gardens



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/mint/helpers.rb', line 175

def self.transform_markdown_links(text)
  # Transform relative markdown links like [text](path/file.md) to [text](path/file.html)
  text.gsub(/(\[([^\]]*)\]\()([^)]*\.md)(\))/) do |match|
    link_start = $1
    link_text = $2
    link_url = $3
    link_end = $4
    
    # Only transform relative links (not absolute URLs)
    if link_url !~ /^https?:\/\//
      new_url = link_url.gsub(/\.md$/, '.html')
      "#{link_start}#{new_url}#{link_end}"
    else
      match
    end
  end
end

.underscore(obj, opts = {}) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/mint/helpers.rb', line 8

def self.underscore(obj, opts={})
  namespaces = obj.to_s.split("::").map do |namespace|
    if opts[:ignore_prefix]
      namespace[0..1].downcase + namespace[2..-1]
    else
      namespace
    end
  end

  string = opts[:namespaces] ? namespaces.join("::") : namespaces.last
  string.underscore
end

.update_yaml!(file, opts = {}) ⇒ void

This method returns an undefined value.

Reads Yaml options from file. Updates values with new_opts. Writes merged data back to the same file, overwriting previous data.



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/mint/helpers.rb', line 133

def self.update_yaml!(file, opts={})
  curr_opts = if File.exist?(file)
                begin
                  YAML.load_file(file) || {}
                rescue Psych::SyntaxError, StandardError
                  # Handle corrupted YAML gracefully by treating it as empty
                  {}
                end
              else
                {}
              end

  File.open file, "w" do |f|
    YAML.dump(curr_opts.merge(opts), f)
  end
end