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"]],
  "symbol"=> [["font-family", "symbol, 'Standard Symbols L'"]],
  "u"     => [["text-decoration", "underline"]],
}

Instance Method Summary collapse

Instance Method Details

#_formatted_paragraph(context, paragraph) ⇒ Object



69
70
71
72
73
74
75
76
# File 'lib/oddb/html/view/document.rb', line 69

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



59
60
61
62
63
64
65
66
67
68
# File 'lib/oddb/html/view/document.rb', line 59

def formatted_paragraph(context, paragraph)
  return '' unless paragraph
  args = {}
  if paragraph.respond_to?(:preformatted?) && paragraph.preformatted?
    args.store 'style', 'white-space:pre;font-family:fixed-width,Courier'
  end
  context.p(args) {
    _formatted_paragraph(context, paragraph)
  }
end

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



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/oddb/html/view/document.rb', line 77

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

#formatted_table(context, table) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/oddb/html/view/document.rb', line 93

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



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

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