Class: ActiveMigration::Converters::RtfToHtml

Inherits:
Object
  • Object
show all
Defined in:
lib/active_migration/converters/rtf_to_html.rb

Instance Method Summary collapse

Constructor Details

#initializeRtfToHtml

Returns a new instance of RtfToHtml.



9
10
11
12
# File 'lib/active_migration/converters/rtf_to_html.rb', line 9

def initialize()
  @prefix = ''
  @suffix = ''
end

Instance Method Details

#add(open, close = open) ⇒ Object



14
15
16
17
# File 'lib/active_migration/converters/rtf_to_html.rb', line 14

def add(open, close = open)
  @prefix << "<#{open}>"
  @suffix = "</#{close}>#{@suffix}"
end

#format(str, section) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/active_migration/converters/rtf_to_html.rb', line 19

def format(str, section)
  @prefix = ''
  @suffix = ''

  mods = section[:modifiers]

  if mods[:paragraph]
    if section[:text].empty?
      str << "<p></p>\n"
    else
      add('p')
    end

  elsif mods[:tab]
    str << "&nbsp;&nbsp;&nbsp;&nbsp;"
    return
  elsif mods[:newline]
    str << "<br />\n"
    return
  elsif mods[:rquote]
    str << "&rsquo;"
    return
  elsif mods[:lquote]
    str << "&lsquo;"
    return
  elsif mods[:ldblquote]
    str << "&ldquo;"
    return
  elsif mods[:rdblquote]
    str << "&rdquo;"
    return
  elsif mods[:emdash]
    str << "&mdash;"
    return
  elsif mods[:endash]
    str << "&ndash;"
    return
  elsif mods[:nbsp]
    str << "&nbsp;"
    return
  end
  return if section[:text].empty?

  add('b') if mods[:bold]
  add('i') if mods[:italic]
  add('u') if mods[:underline]
  add('sup') if mods[:superscript]
  add('sub') if mods[:subscript]
  add('del') if mods[:strikethrough]

  style = ''
  style << "font-variant: small-caps;" if mods[:smallcaps]
  style << "font-size: #{mods[:font_size]}pt;" if mods[:font_size]
  style << "font-family: \"#{mods[:font].name}\";" if mods[:font]
  if mods[:foreground_colour] && !mods[:foreground_colour].use_default?
    colour = mods[:foreground_colour]
    style << "color: rgb(#{colour.red},#{colour.green},#{colour.blue});"
  end
  if mods[:background_colour] && !mods[:background_colour].use_default?
    colour = mods[:background_colour]
    style << "background-color: rgb(#{colour.red},#{colour.green},#{colour.blue});"
  end

  add("span style='#{style}'", 'span') unless style.empty?

  str << @prefix + section[:text].force_encoding('UTF-8') + @suffix
end

#parse(contents) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/active_migration/converters/rtf_to_html.rb', line 91

def parse(contents)
  doc = RubyRTF::Parser.new.parse(contents)
  
  str = ''
  
  doc.sections.each do |section|
    mods = section[:modifiers]

    if mods[:table]
      str << "<table width=\"100%\">\n"
      mods[:table].rows.each do |row|
        str << "<tr>\n"
        row.cells.each do |cell|
          str << "<td width=\"#{cell.width}%\">\n"
          cell.sections.each do |sect|
            format(str, sect)
          end
          str << "</td>\n"
        end
        str << "</tr>\n"
      end
      str << "</table>\n"
      next
    end

    format(str, section)
  end

  str
end

#parse_from_file(file) ⇒ Object



87
88
89
# File 'lib/active_migration/converters/rtf_to_html.rb', line 87

def parse_from_file(file)
  parse(File.open(ARGV[0]).read)
end