module DocXify module Element class Paragraph < Base attr_accessor :text, :font, :size, :color, :background, :align, :inline_styling, :tab_stops_cm, :hanging_cm def initialize(text, options = {}) super() @document = options[:document] @text = text @font = options[:font] || @document&.font || "Times New Roman" @size = options[:size] || @document&.size || 14 @color = options[:color] || @document&.color || "#000000" @highlight = options[:highlight] || false @background = options[:background] if options[:background] @background ||= @document&.background if @document&.background @align = options[:align] || :left @inline_styling = options.key?(:inline_styling) ? options[:inline_styling] : true @tab_stops_cm = options[:tab_stops_cm] || [] @hanging_cm = options[:hanging_cm] || 0 end def to_s(_container = nil) nodes = if @inline_styling parse_simple_html(@text) else [@text.gsub("<", "<").gsub(">", ">")] end xml = "" xml << "" xml << "" if @align != :left if tab_stops_cm.any? xml << "" tab_stops_cm.each do |stop| xml << "" end xml << "" end if @hanging_cm&.positive? xml << "" end xml << "" nodes.each do |node| if node.is_a?(Hash) && node[:tag] == "a" xml << "" end xml << "" xml << <<~XML #{"" if @highlight} #{"" if node.is_a?(Hash) && node[:tag] == "b"} #{"" if node.is_a?(Hash) && node[:tag] == "i"} #{"" if node.is_a?(Hash) && (node[:tag] == "u" || node[:tag] == "a")} #{"" if node.is_a?(Hash) && node[:tag] == "a"} XML content = node.is_a?(Hash) ? node[:content] : node content = content.gsub("{CHECKBOX_EMPTY}", "☐").gsub("{CHECKBOX_CHECKED}", "☒") xml << "#{content}" xml << "" if node.is_a?(Hash) && node[:tag] == "a" xml << "" end end xml << "" end def ref_for_href(href) relation = nil @document.relationships.select { |r| r.is_a?(DocXify::Element::WebAddress) }.each do |r| if r.target == href relation = r break end end if relation.nil? relation = DocXify::Element::WebAddress.new(href) @document.relationships << relation end relation.reference end end end end