Module: ODT2HTML::AnalyzeStyles

Included in:
Base
Defined in:
lib/odt2html-nsi/analyze_styles.rb

Instance Method Summary collapse

Instance Method Details

#analyze_styles_xmlObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/odt2html-nsi/analyze_styles.rb', line 4

def analyze_styles_xml

  #
  # Get the namespaces from the root element; populate the
  # dynamic instance variable names and the namespace hash from them.
  #
  get_namespaces

  create_dispatch_table

  # handle default styles; attach to the body
  @doc.root.elements.each(
    "#{@office_ns}:styles/#{@style_ns}:default-style")  do |el|
    if (el.attribute("#{@style_ns}:family").value == "paragraph") then
      process_style( "body",
        el.elements["#{@style_ns}:paragraph-properties"])
      process_style( "body",
        el.elements["#{@style_ns}:text-properties"])
    end
  end

  @doc.root.elements.each(
    "#{@office_ns}:styles/#{@style_ns}:style") do |el|
    process_style_style( el )
  end

  @doc.root.elements.each(
    "#{@office_ns}:styles/#{@text_ns}:list-style") do |el|
    process_text_list_style( el )
  end

end

#create_dispatch_tableObject

Create the @style_dispatch hash by substituting the @valid_style array entries with their appropriate prefix



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/odt2html-nsi/analyze_styles.rb', line 41

def create_dispatch_table
  i = 0;
  while (i < @valid_style.length) do
    style_name = @valid_style[i].sub(/^([^:]+)/) { |pfx|
      @nshash[pfx]
    }
    if (@valid_style[i].index("*") != nil) then
      style_name = style_name.sub(/.$/, "" )
      @style_dispatch[style_name] = @valid_style[i+1]
      i+=1
    else
      @style_dispatch[style_name] = "process_normal_style_attr"
    end
    i+=1
  end
end

#process_column_width(selector, property, value) ⇒ Object

style:column-width becomes width



109
110
111
# File 'lib/odt2html-nsi/analyze_styles.rb', line 109

def process_column_width( selector, property, value )
  process_normal_style_attr( selector, "width", value )
end

#process_font_name(selector, property, value) ⇒ Object

The font-name attribute changes to font-family in CSS



94
95
96
# File 'lib/odt2html-nsi/analyze_styles.rb', line 94

def process_font_name( selector, property, value )
  process_normal_style_attr(selector, "font-family", value)
end

#process_normal_style_attr(selector, property, value) ⇒ Object

If the style hasn’t been registered yet, create a new array with the style property and value.

If the style has been registered, and the property name is a duplicate, supplant the old property value with the new one.

If the style has been registered, and the property is a new one, push the property and value onto the array.



142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/odt2html-nsi/analyze_styles.rb', line 142

def process_normal_style_attr( selector, property, value )
  if (@style_info[selector] == nil) then
    @style_info[selector] = DeclarationBlock.new( )
    @style_info[selector].push Declaration.new(property, value)
  else
    found = @style_info[selector].find { |obj|
      obj.property == property }
    if (found != nil) then
      found.value = value
    else
      @style_info[selector].push Declaration.new(property, value)
    end
  end
end

#process_style(class_name, style_element) ⇒ Object

Handle a <style:foo-properties> element



61
62
63
64
65
66
67
68
69
70
# File 'lib/odt2html-nsi/analyze_styles.rb', line 61

def process_style( class_name, style_element )
  if (style_element != nil) then
    style_element.attributes.each_attribute do |attr|
      if (@style_dispatch.has_key?(attr.expanded_name)) then
        self.send( @style_dispatch[attr.expanded_name], class_name,
          attr.name, attr.value )
      end
    end
  end
end

#process_style_style(element) ⇒ Object

Handle a <style:style> element



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/odt2html-nsi/analyze_styles.rb', line 75

def process_style_style( element )
  style_name = element.attribute("#{@style_ns}:name").value.gsub(/\./, "_");
  parent_name = element.attribute("#{@style_ns}:parent-style-name");
  if (parent_name) then
    parent_name = parent_name.value.gsub(/\./,"_")
    if (@style_info[parent_name]) then
      @style_info[style_name] = DeclarationBlock.new(
        @style_info[parent_name] )
    end
  elsif (@style_info[style_name] == nil) then
    @style_info[style_name] = DeclarationBlock.new( )
  end

  element.elements.each do |child|
    process_style( style_name, child )
  end
end

#process_style_text_position(selector, property, value) ⇒ Object

The style:text-position attribute gives whitespace-separated distance above or below baseline and a scaling factor as percentages. If the distance is not 0%, then we have to process as sup/sub; otherwise, don’t touch.



124
125
126
127
128
129
130
# File 'lib/odt2html-nsi/analyze_styles.rb', line 124

def process_style_text_position( selector, property, value )
  data = value.split(' ')
  if (data[0] != "0%") then
    process_normal_style_attr( selector, "vertical-align", data[0] )
    process_normal_style_attr( selector, "font-size", data[1] )
  end
end

#process_text_align(selector, property, value) ⇒ Object

text-align:end becomes text-align:right and text-align:start becomes text-align:left in CSS.



101
102
103
104
105
# File 'lib/odt2html-nsi/analyze_styles.rb', line 101

def process_text_align( selector, property, value )
  value = "right" if (value == "end")
  value = "left" if (value == "start")
  process_normal_style_attr( selector, property, value )
end

#process_underline_style(selector, property, value) ⇒ Object

style:text-underline-style becomes text-decoration



114
115
116
117
# File 'lib/odt2html-nsi/analyze_styles.rb', line 114

def process_underline_style( selector, property, value )
  process_normal_style_attr( selector, "text-decoration",
    (value == "none") ? "none" : "underline" )
end

#style_to_s(selector) ⇒ Object



157
158
159
160
# File 'lib/odt2html-nsi/analyze_styles.rb', line 157

def style_to_s( selector )
  str = "." + selector + @style_info[selector].to_s
  return str
end