class RichText::HTML

@todo Work in progress

Constants

ConfigError

Attributes

config[R]

Public Class Methods

new(config) click to toggle source
# File lib/rich-text/html.rb, line 15
def initialize(config)
  @default_block_tag = config.html_default_block_tag
  @block_tags = config.html_block_tags
  @inline_tags = config.html_inline_tags
end
render(delta, options) click to toggle source
# File lib/rich-text/html.rb, line 11
def self.render(delta, options)
  new(RichText.config).render(delta)
end

Public Instance Methods

render(delta) click to toggle source
# File lib/rich-text/html.rb, line 21
def render(delta)
  raise TypeError.new("cannot convert retain or delete ops to html") unless delta.insert_only?
  html = delta.each_line.inject('') do |html, line|
    html << render_line(line)
  end
  normalize(html)
end

Private Instance Methods

apply_tag(tag, content, value) click to toggle source
# File lib/rich-text/html.rb, line 53
def apply_tag(tag, content, value)
  if tag.respond_to?(:call)
    tag.call(content, value)
  elsif tag
    "<#{tag}>#{content}</#{tag}>"
  end
end
apply_tags(tags, text, attributes) click to toggle source
# File lib/rich-text/html.rb, line 47
def apply_tags(tags, text, attributes)
  attributes.inject(text) do |content, (key, value)|
    apply_tag(tags[key], content, value)
  end
end
normalize(html) click to toggle source
# File lib/rich-text/html.rb, line 61
def normalize(html)
  # merge sibling tags
  # standardize nesting order
end
render_line(delta) click to toggle source
# File lib/rich-text/html.rb, line 31
def render_line(delta)
  # TODO: handle a delta without a trailing "\n"
  line = ''
  delta.slice(0, delta.length - 1).each_op do |op|
    line << apply_tags(@inline_tags, op.value, op.attributes)
  end
  delta.slice(delta.length - 1, 1).each_op do |op|
    if op.attributes?
      line = apply_tags(@block_tags, line, op.attributes)
    else
      line = apply_tag(@default_block_tag, line, true)
    end
  end
  line
end