class ClientPages::MarkdownContent::ContentBuilder

Attributes

html[RW]

Public Class Methods

new(html) click to toggle source
# File lib/client_pages/markdown_content/content_builder.rb, line 6
def initialize(html)
  @html = html
end

Public Instance Methods

modify(tag, options={}) click to toggle source
# File lib/client_pages/markdown_content/content_builder.rb, line 10
def modify(tag, options={})
  html_lines do |line|
    conditions(tag, line, options[:if]) do
      match(tag, line, :open) { |open_tag| open_tag.gsub(/>/, "#{attributes(options)}>") }
    end
  end
end
replace(tag, new_tag, options={}) click to toggle source
# File lib/client_pages/markdown_content/content_builder.rb, line 27
def replace(tag, new_tag, options={})
  html_lines do |line|
    conditions(tag, line, options[:if]) do
      line = match(tag, line, :open) { |open_tag| "<#{new_tag.to_s + attributes(options)}>" }
      line = match(tag, line, :close) { |closing_tag| "</#{new_tag}>" }
    end
  end
end
wrap(tag, wrapper, options={}) click to toggle source
# File lib/client_pages/markdown_content/content_builder.rb, line 18
def wrap(tag, wrapper, options={})
  html_lines do |line|
    conditions(tag, line, options[:if]) do
      line = match(tag, line, :open) { |rest| "<#{wrapper.to_s + attributes(options)}>" + rest }
      line = match(tag, line, :close) { |rest| rest + "</#{wrapper}>" }
    end
  end
end

Private Instance Methods

attributes(options) click to toggle source
# File lib/client_pages/markdown_content/content_builder.rb, line 59
def attributes(options)
  options.fetch(:with, {}).map { |k, v| " #{k}=\"#{v}\"" }.join
end
conditions(tag, line, condition) { || ... } click to toggle source
# File lib/client_pages/markdown_content/content_builder.rb, line 42
def conditions(tag, line, condition)
  return yield if condition.nil?
  if condition.respond_to?(:call) && Matcher.new(tag, line).instance_exec(&condition)
    yield
  elsif condition.is_a?(Hash) && Matcher.new(tag, line).match_all?(condition)
    yield
  elsif condition == true
    yield
  else
    line
  end
end
html_lines() { |line| ... } click to toggle source
# File lib/client_pages/markdown_content/content_builder.rb, line 55
def html_lines(&block)
  self.html = html.lines.map { |line| yield line }.join
end
match(tag, line, state) { |match| ... } click to toggle source
# File lib/client_pages/markdown_content/content_builder.rb, line 38
def match(tag, line, state)
  Matcher.new(tag, line).find(state) { |match| yield match }
end