Module: Woo::StyleguideHelper

Included in:
StyleguideController
Defined in:
app/helpers/woo/styleguide_helper.rb

Constant Summary collapse

BASE_PATH =
"app/views/styleguide"

Instance Method Summary collapse

Instance Method Details

#current_page(folder, page) ⇒ Object



13
14
15
16
# File 'app/helpers/woo/styleguide_helper.rb', line 13

def current_page(folder, page)
  filepath = Dir.glob("#{BASE_PATH}/#{folder}/#{page}.html*").first
  page_hash(filepath)
end

#folder_files(folder_name) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'app/helpers/woo/styleguide_helper.rb', line 56

def folder_files(folder_name)
  prefix = (folder_name == 'ui_elements') ? '#' : "#{folder_name}/"

  files = Dir.glob("#{BASE_PATH}/#{folder_name}/*.html*")
  files.map do |filepath|
    name = File.basename(filepath)
               .gsub(/^\_|.html.*/, '')

    route = "/styleguide/#{prefix}#{name}"

    {:name => name, :route => route}
  end
end

#folders_namesObject



49
50
51
52
53
54
# File 'app/helpers/woo/styleguide_helper.rb', line 49

def folders_names
  Dir.glob("#{BASE_PATH}/*").map do |filepath|
    name = File.basename(filepath)
    name if File.directory?(filepath) && name != 'shared'
  end.compact
end

#load_notes(filepath) ⇒ Object



22
23
24
25
26
# File 'app/helpers/woo/styleguide_helper.rb', line 22

def load_notes(filepath)
  haml_frontmatter = /^-{1,3}$\n(?<notes_contents>.*)^-{1,3}$\n/m
  match = haml_frontmatter.match(File.read(filepath))
  match[:notes_contents] if match
end


42
43
44
45
46
47
# File 'app/helpers/woo/styleguide_helper.rb', line 42

def navigation_hash
  folders = folders_names
  folders.each_with_object({}) do |filepath, collection|
    collection[filepath] = folder_files(filepath)
  end
end

#page_hash(filepath) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/helpers/woo/styleguide_helper.rb', line 28

def page_hash(filepath)
  lang     = filepath.match(/haml$/) ? 'haml' : 'markup'
  name     = File.basename(filepath)
                 .gsub(/^\_|.html.*/, '')

  {
    :name       => name,
    :filepath   => filepath,
    :contents   => File.read(filepath).sub(/^-{1,3}$\n.*^-{1,3}$\n/m, ''),
    :notes      => load_notes(filepath),
    :lang       => lang
  }
end

#render_haml_string(contents) ⇒ Object



18
19
20
# File 'app/helpers/woo/styleguide_helper.rb', line 18

def render_haml_string(contents)
  Haml::Engine.new(contents).render
end

#ui_elementsObject



6
7
8
9
10
11
# File 'app/helpers/woo/styleguide_helper.rb', line 6

def ui_elements
  ui_elements_partials = Dir.glob("#{BASE_PATH}/ui_elements/_*.html*")
  ui_elements_partials.map do |filepath|
    page_hash(filepath)
  end
end