Class: ODDB::Html::View::Chapter

Inherits:
HtmlGrid::Component
  • Object
show all
Defined in:
lib/oddb/html/view/document.rb

Constant Summary collapse

STYLES =
{
  "b"   => [["font-weight", "bold"]],
  "i"   => [["font-style", "italic"]],
  "sub" => [["vertical-align", "sub"], ["font-size", "smaller"]],
  "sup" => [["vertical-align", "sup"], ["font-size", "smaller"]],
  "u"   => [["text-decoration", "underline"]],
}

Instance Method Summary collapse

Instance Method Details

#_formatted_paragraph(context, paragraph) ⇒ Object



64
65
66
67
68
69
70
71
# File 'lib/oddb/html/view/document.rb', line 64

def _formatted_paragraph(context, paragraph)
  return '' unless paragraph
  paragraph.formats.inject('') { |memo, format|
    memo << formatted_string(context, format.values) { 
      paragraph[format.range].gsub(/\n/, context.br)
    }
  }
end

#formatted_paragraph(context, paragraph) ⇒ Object



58
59
60
61
62
63
# File 'lib/oddb/html/view/document.rb', line 58

def formatted_paragraph(context, paragraph)
  return '' unless paragraph
  context.p {
    _formatted_paragraph(context, paragraph)
  }
end

#formatted_string(context, stack, &block) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/oddb/html/view/document.rb', line 72

def formatted_string(context, stack, &block)
  if(stack.empty?)
    block.call
  else
    styles = stack.inject({}) { |memo, fmt| 
      STYLES[fmt].each { |pair| memo.store(*pair) } 
      memo
    }
    args = { :style => styles.collect { |pair| pair.join(":") }.join(";") }
    context.span(args) { block.call }
  end
end

#formatted_table(context, table) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/oddb/html/view/document.rb', line 84

def formatted_table(context, table)
  context.table {
    memo = ''
    table.each_normalized { |row|
      memo << context.tr {
        row.inject('') { |tr, cell| 
          args = {}
          if(cell && (align = cell.align))
            args.store :style, "text-align: #{align}"
          end
          tr << context.td(args) { _formatted_paragraph(context, cell) }
        }
      }
    }
    memo
  }
end

#to_html(context) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/oddb/html/view/document.rb', line 46

def to_html(context)
  @model.paragraphs.inject('') { |memo, paragraph|
    memo << case paragraph
            when Text::Picture
              context.img(:src => paragraph.path, :alt => paragraph.filename)
            when Text::Table
              formatted_table(context, paragraph)
            else
              formatted_paragraph(context, paragraph)
            end
  }
end