|
| 1 | +require 'docx/helpers' |
| 2 | +require 'docx/elements' |
| 3 | +require 'docx/elements/style/converters' |
| 4 | +require 'docx/elements/style/validators' |
| 5 | + |
| 6 | +module Docx |
| 7 | + module Elements |
| 8 | + class Style |
| 9 | + include Docx::SimpleInspect |
| 10 | + |
| 11 | + def self.attributes |
| 12 | + @@attributes |
| 13 | + end |
| 14 | + |
| 15 | + def self.attribute(name, *selectors, converter: Converters::DefaultValueConverter, validator: Validators::DefaultValidator) |
| 16 | + define_method(name) do |
| 17 | + selectors |
| 18 | + .lazy |
| 19 | + .filter_map { |node_xpath| node.at_xpath(node_xpath)&.value } |
| 20 | + .map { |value| converter.decode(value) } |
| 21 | + .first |
| 22 | + end |
| 23 | + |
| 24 | + define_method("#{name}=") do |value| |
| 25 | + validator.validate(value) || raise(Errors::StyleInvalidPropertyValue, "Invalid value for #{name}: #{value}") |
| 26 | + |
| 27 | + selectors.map do |attribute_xpath| |
| 28 | + encoded_value = converter.encode(value).to_s |
| 29 | + if (existing_attribute = node.at_xpath(attribute_xpath)) |
| 30 | + existing_attribute.value = encoded_value |
| 31 | + next value |
| 32 | + end |
| 33 | + |
| 34 | + node_xpath, attribute = attribute_xpath.split("/@") |
| 35 | + |
| 36 | + created_node = |
| 37 | + node_xpath |
| 38 | + .split("/") |
| 39 | + .reduce(node) do |parent_node, child_xpath| |
| 40 | + # find the child node |
| 41 | + parent_node.at_xpath(child_xpath) || |
| 42 | + # or create the child node |
| 43 | + Nokogiri::XML::Node.new(child_xpath, parent_node).tap { |created_child_node| parent_node << created_child_node } |
| 44 | + end |
| 45 | + |
| 46 | + created_node.set_attribute(attribute, encoded_value) |
| 47 | + end |
| 48 | + .first |
| 49 | + end |
| 50 | + end |
| 51 | + |
| 52 | + def self.create(configuration, attributes = {}) |
| 53 | + node = Nokogiri::XML::Node.new("w:style", configuration.styles_parent_node) |
| 54 | + configuration.styles_parent_node.add_child(node) |
| 55 | + |
| 56 | + Elements::Style.new(configuration, node, **attributes) |
| 57 | + end |
| 58 | + |
| 59 | + def initialize(configuration, node, **attributes) |
| 60 | + @configuration = configuration |
| 61 | + @node = node |
| 62 | + |
| 63 | + attributes.each do |name, value| |
| 64 | + self.send("#{name}=", value) |
| 65 | + end |
| 66 | + end |
| 67 | + |
| 68 | + attr_accessor :node |
| 69 | + |
| 70 | + attribute :id, "./@w:styleId" |
| 71 | + attribute :name, "./w:name/@w:val", "./w:next/@w:val" |
| 72 | + attribute :type, ".//@w:type" |
| 73 | + attribute :keep_next, "./w:pPr/w:keepNext/@w:val", converter: Converters::BooleanConverter |
| 74 | + attribute :keep_lines, "./w:pPr/w:keepLines/@w:val", converter: Converters::BooleanConverter |
| 75 | + attribute :page_break_before, "./w:pPr/w:pageBreakBefore/@w:val", converter: Converters::BooleanConverter |
| 76 | + attribute :widow_control, "./w:pPr/w:widowControl/@w:val", converter: Converters::BooleanConverter |
| 77 | + attribute :shading_style, "./w:pPr/w:shd/@w:val", "./w:rPr/w:shd/@w:val" |
| 78 | + attribute :shading_color, "./w:pPr/w:shd/@w:color", "./w:rPr/w:shd/@w:color", validator: Validators::ColorValidator |
| 79 | + attribute :shading_fill, "./w:pPr/w:shd/@w:fill", "./w:rPr/w:shd/@w:fill" |
| 80 | + attribute :suppress_auto_hyphens, "./w:pPr/w:suppressAutoHyphens/@w:val", converter: Converters::BooleanConverter |
| 81 | + attribute :bidirectional_text, "./w:pPr/w:bidi/@w:val", converter: Converters::BooleanConverter |
| 82 | + attribute :spacing_before, "./w:pPr/w:spacing/@w:before" |
| 83 | + attribute :spacing_after, "./w:pPr/w:spacing/@w:after" |
| 84 | + attribute :line_spacing, "./w:pPr/w:spacing/@w:line" |
| 85 | + attribute :line_rule, "./w:pPr/w:spacing/@w:lineRule" |
| 86 | + attribute :indent_left, "./w:pPr/w:ind/@w:left" |
| 87 | + attribute :indent_right, "./w:pPr/w:ind/@w:right" |
| 88 | + attribute :indent_first_line, "./w:pPr/w:ind/@w:firstLine" |
| 89 | + attribute :align, "./w:pPr/w:jc/@w:val" |
| 90 | + attribute :font, "./w:rPr/w:rFonts/@w:ascii", "./w:rPr/w:rFonts/@w:cs", "./w:rPr/w:rFonts/@w:hAnsi", "./w:rPr/w:rFonts/@w:eastAsia" # setting :font, will set all other fonts |
| 91 | + attribute :font_ascii, "./w:rPr/w:rFonts/@w:ascii" |
| 92 | + attribute :font_cs, "./w:rPr/w:rFonts/@w:cs" |
| 93 | + attribute :font_hAnsi, "./w:rPr/w:rFonts/@w:hAnsi" |
| 94 | + attribute :font_eastAsia, "./w:rPr/w:rFonts/@w:eastAsia" |
| 95 | + attribute :bold, "./w:rPr/w:b/@w:val", "./w:rPr/w:bCs/@w:val", converter: Converters::BooleanConverter |
| 96 | + attribute :italic, "./w:rPr/w:i/@w:val", "./w:rPr/w:iCs/@w:val", converter: Converters::BooleanConverter |
| 97 | + attribute :caps, "./w:rPr/w:caps/@w:val", converter: Converters::BooleanConverter |
| 98 | + attribute :small_caps, "./w:rPr/w:smallCaps/@w:val", converter: Converters::BooleanConverter |
| 99 | + attribute :strike, "./w:rPr/w:strike/@w:val", converter: Converters::BooleanConverter |
| 100 | + attribute :double_strike, "./w:rPr/w:dstrike/@w:val", converter: Converters::BooleanConverter |
| 101 | + attribute :outline, "./w:rPr/w:outline/@w:val", converter: Converters::BooleanConverter |
| 102 | + attribute :outline_level, "./w:pPr/w:outlineLvl/@w:val" |
| 103 | + attribute :font_color, "./w:rPr/w:color/@w:val", validator: Validators::ColorValidator |
| 104 | + attribute :font_size, "./w:rPr/w:sz/@w:val", "./w:rPr/w:szCs/@w:val", converter: Converters::FontSizeConverter |
| 105 | + attribute :font_size_cs, "./w:rPr/w:szCs/@w:val", converter: Converters::FontSizeConverter |
| 106 | + attribute :underline_style, "./w:rPr/w:u/@w:val" |
| 107 | + attribute :underline_color, "./w:rPr/w:u /@w:color", validator: Validators::ColorValidator |
| 108 | + attribute :spacing, "./w:rPr/w:spacing/@w:val" |
| 109 | + attribute :kerning, "./w:rPr/w:kern/@w:val" |
| 110 | + attribute :position, "./w:rPr/w:position/@w:val" |
| 111 | + attribute :text_fill_color, "./w:rPr/w14:textFill/w14:solidFill/w14:srgbClr/@w14:val", validator: Validators::ColorValidator |
| 112 | + attribute :vertical_alignment, "./w:rPr/w:vertAlign/@w:val" |
| 113 | + attribute :lang, "./w:rPr/w:lang/@w:val" |
| 114 | + |
| 115 | + def to_xml |
| 116 | + node.to_xml |
| 117 | + end |
| 118 | + |
| 119 | + def remove |
| 120 | + node.remove |
| 121 | + @configuration.styles.delete(self) |
| 122 | + end |
| 123 | + end |
| 124 | + end |
| 125 | +end |
0 commit comments