Module: Middleman::KSS::Helpers

Defined in:
lib/middleman-kss/extension.rb

Constant Summary collapse

DEFAULT_STYLEGUIDE_BLOCK_FILE =
'_styleguide_block.html.erb'

Instance Method Summary collapse

Instance Method Details

#get_styleguideObject



79
80
81
82
83
84
85
86
87
# File 'lib/middleman-kss/extension.rb', line 79

def get_styleguide
  # Parse the KSS style guide once per request (because it probably changes every request)
  unless request.has_key?(:styleguide)
    extension_options = ::Middleman::KSS.options
    request[:styleguide] = ::Kss::Parser.new(File.join(self.source_dir, extension_options[:kss_dir]))
  end

  return request[:styleguide]
end

#kss_h(text) ⇒ Object

Simple HTML escape helper



69
70
71
# File 'lib/middleman-kss/extension.rb', line 69

def kss_h(text)
  Rack::Utils.escape_html(text)
end

#kss_markdown(input) ⇒ Object

Markdown in KSS



74
75
76
77
# File 'lib/middleman-kss/extension.rb', line 74

def kss_markdown(input)
  markdown = ::Redcarpet::Markdown.new(::Redcarpet::Render::HTML, :autolink => true, :space_after_headers => true)
  markdown.render(input)
end

#styleblock(tile, options = {}) ⇒ String

Renders a styleblock with or without styleguide information.

Parameters:

  • tile (String)

    Name of the style tile file to render.

  • options (Hash) (defaults to: {})

    Options for rendering.

Options Hash (options):

  • :section (String)

    KSS section number (e.g. “1.1”) for fetching the styleguide information.

Returns:

  • (String)

    Generated HTML.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/middleman-kss/extension.rb', line 44

def styleblock(tile, options = {})
  tile_file = "_#{tile}.html.erb"
  tile_path = File.join(self.source_dir, "styleblocks", tile_file)
  tile_template = ::Tilt.new(tile_path)

  @block_html = tile_template.render(self)
  @styleguide = self.get_styleguide

  if options.has_key?(:section)
    @section = @styleguide.section(options[:section])
    raise "Section must have a description. Section #{options[:section]} does not have one or section does not exist." if @section.description.blank?
  end

  if @section
    # Render the styleguide block
    styleguide_block_path = File.join(File.dirname(__FILE__), DEFAULT_STYLEGUIDE_BLOCK_FILE)
    template = ::Tilt.new(styleguide_block_path)
    return template.render(self)
  else
    # Render just the HTML without the $modifier_class thingies
    return @block_html.gsub('$modifier_class', '').gsub(' class=""', '').prepend('<div class="styleguide-styleblock">') << '</div>'
  end
end