Class: Uniword::Mhtml::WordCss
- Inherits:
-
Object
- Object
- Uniword::Mhtml::WordCss
- Defined in:
- lib/uniword/mhtml/word_css.rb
Overview
Handles Word CSS stylesheet generation.
Responsibility: Provide Word-compatible CSS styles for MHTML documents. This includes default Word styles and custom style generation.
Class Method Summary collapse
-
.basic_css ⇒ String
Get basic fallback CSS.
-
.build_list_rule(instance) ⇒ String?
Build a CSS rule for list numbering.
-
.build_page_rule(section, section_name) ⇒ String?
Build @page CSS rule for a section.
-
.build_section_div_rule(section_name) ⇒ String
Build div.Section CSS rule.
-
.build_style_rule(style) ⇒ String?
Build a CSS rule for a style.
-
.default_css ⇒ String
Get the default Word CSS stylesheet.
-
.generate_list_css(numbering_config) ⇒ String
Generate CSS for list numbering.
-
.generate_section_css(sections) ⇒ String
Generate CSS for document sections with page layout.
-
.generate_style_css(styles_config) ⇒ String
Generate CSS for custom styles.
Class Method Details
.basic_css ⇒ String
Get basic fallback CSS.
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/uniword/mhtml/word_css.rb', line 161 def self.basic_css <<~CSS /* Basic Word styles */ p.MsoNormal, li.MsoNormal, div.MsoNormal { margin: 0; margin-bottom: .0001pt; font-size: 11pt; font-family: "Calibri", sans-serif; } h1 { margin-top: 12pt; margin-bottom: 0; page-break-after: avoid; font-size: 16pt; font-family: "Calibri Light", sans-serif; font-weight: normal; } h2 { margin-top: 10pt; margin-bottom: 0; page-break-after: avoid; font-size: 13pt; font-family: "Calibri Light", sans-serif; font-weight: normal; } h3 { margin-top: 10pt; margin-bottom: 0; page-break-after: avoid; font-size: 12pt; font-family: "Calibri Light", sans-serif; font-weight: normal; } table.MsoNormalTable { font-size: 11pt; font-family: "Calibri", sans-serif; } a:link, span.MsoHyperlink { color: blue; text-decoration: underline; } CSS end |
.build_list_rule(instance) ⇒ String?
Build a CSS rule for list numbering.
151 152 153 154 155 156 |
# File 'lib/uniword/mhtml/word_css.rb', line 151 def self.build_list_rule(instance) return nil unless instance # Generate @list rule for Word "@list l#{instance.num_id} {\n mso-list-id: #{instance.num_id};\n}" end |
.build_page_rule(section, section_name) ⇒ String?
Build @page CSS rule for a section.
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/uniword/mhtml/word_css.rb', line 80 def self.build_page_rule(section, section_name) properties = section.respond_to?(:properties) ? section.properties : nil return nil unless properties rules = [] # Page size width = properties.respond_to?(:page_width) && properties.page_width height = properties.respond_to?(:page_height) && properties.page_height if width && height # Convert from twips to inches (1 inch = 1440 twips) w_in = CssNumberFormatter.twips_to_in(width, precision: 1) h_in = CssNumberFormatter.twips_to_in(height, precision: 1) rules << "size: #{w_in} #{h_in}" else # Default letter size rules << "size: 8.5in 11in" end # Margins rules << "margin-top: #{CssNumberFormatter.twips_to_in(properties.margin_top)}" if properties.respond_to?(:margin_top) && properties.margin_top rules << "margin-bottom: #{CssNumberFormatter.twips_to_in(properties.margin_bottom)}" if properties.respond_to?(:margin_bottom) && properties.margin_bottom rules << "margin-left: #{CssNumberFormatter.twips_to_in(properties.margin_left)}" if properties.respond_to?(:margin_left) && properties.margin_left rules << "margin-right: #{CssNumberFormatter.twips_to_in(properties.margin_right)}" if properties.respond_to?(:margin_right) && properties.margin_right # Header/Footer margins rules << "mso-header-margin: #{CssNumberFormatter.twips_to_in(properties.header_margin)}" if properties.respond_to?(:header_margin) && properties.header_margin rules << "mso-footer-margin: #{CssNumberFormatter.twips_to_in(properties.)}" if properties.respond_to?(:footer_margin) && properties. "@page #{section_name} {\n #{rules.join(";\n ")};\n}" end |
.build_section_div_rule(section_name) ⇒ String
Build div.Section CSS rule.
116 117 118 |
# File 'lib/uniword/mhtml/word_css.rb', line 116 def self.build_section_div_rule(section_name) "div.Word#{section_name} {\n page: #{section_name};\n}" end |
.build_style_rule(style) ⇒ String?
Build a CSS rule for a style.
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/uniword/mhtml/word_css.rb', line 124 def self.build_style_rule(style) return nil unless style properties = [] # Font properties properties << "font-family: '#{style.font}'" if style.respond_to?(:font) && style.font if style.respond_to?(:font_size) && style.font_size properties << "font-size: #{CssNumberFormatter.format(style.font_size, 'pt', precision: 1)}" end properties << "font-weight: bold" if style.respond_to?(:bold) && style.bold properties << "font-style: italic" if style.respond_to?(:italic) && style.italic # Paragraph properties properties << "text-align: #{style.alignment}" if style.respond_to?(:alignment) && style.alignment return nil if properties.empty? selector = ".#{style.style_id}" "#{selector} {\n #{properties.join(";\n ")};\n}" end |
.default_css ⇒ String
Get the default Word CSS stylesheet.
19 20 21 22 23 24 25 26 27 |
# File 'lib/uniword/mhtml/word_css.rb', line 19 def self.default_css css_path = File.join(__dir__, "wordstyle.css") if File.exist?(css_path) File.read(css_path, encoding: "UTF-8") else # Fallback to basic CSS if file doesn't exist basic_css end end |
.generate_list_css(numbering_config) ⇒ String
Generate CSS for list numbering.
47 48 49 50 51 52 53 54 55 |
# File 'lib/uniword/mhtml/word_css.rb', line 47 def self.generate_list_css(numbering_config) return "" unless numbering_config css_rules = numbering_config.instances.map do |instance| build_list_rule(instance) end css_rules.compact.join("\n") end |
.generate_section_css(sections) ⇒ String
Generate CSS for document sections with page layout.
61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/uniword/mhtml/word_css.rb', line 61 def self.generate_section_css(sections) return "" unless sections && !sections.empty? css_rules = [] sections.each_with_index do |section, index| section_name = "Section#{index + 1}" css_rules << build_page_rule(section, section_name) css_rules << build_section_div_rule(section_name) end css_rules.compact.join("\n") end |
.generate_style_css(styles_config) ⇒ String
Generate CSS for custom styles.
33 34 35 36 37 38 39 40 41 |
# File 'lib/uniword/mhtml/word_css.rb', line 33 def self.generate_style_css(styles_config) return "" unless styles_config css_rules = styles_config.styles.map do |style| build_style_rule(style) end css_rules.compact.join("\n") end |