lib/vectory/svg.rb in vectory-0.7.3 vs lib/vectory/svg.rb in vectory-0.7.4

- old
+ new

@@ -23,12 +23,14 @@ return Vectory::Datauri.new(uri).to_vector if %r{^data:}.match?(uri) from_path(uri) end - def content - @document&.to_xml || @content + def initialize(content = nil, initial_path = nil) + super + + self.content = content end def to_emf convert_with_inkscape("--export-type=emf", Emf) end @@ -39,83 +41,19 @@ def to_ps convert_with_inkscape("--export-type=ps", Ps) end - def namespace(suffix, links, xpath_to_remove) - remap_links(links) - suffix_ids(suffix) - remove_xpath(xpath_to_remove) - end - - def remap_links(map) - document.xpath(".//m:a", "m" => SVG_NS).each do |a| - href_attrs = ["xlink:href", "href"] - href_attrs.each do |p| - a[p] and x = map[File.expand_path(a[p])] and a[p] = x - end - end - - self - end - - def suffix_ids(suffix) - ids = collect_ids - return if ids.empty? - - update_ids_attrs(ids, suffix) - update_ids_css(ids, suffix) - - self - end - - def remove_xpath(xpath) - document.xpath(xpath).remove - - self - end - private def content=(content) - if @document - @document = Nokogiri::XML(content) - else - @content = content + # non-root node inserts the xml tag which breaks markup when placed in + # another xml document + document = Nokogiri::XML(content).root + unless document + raise ParsingError, "Could not parse '#{content&.slice(0, 30)}'" end - end - def document - @document ||= begin - doc = Nokogiri::XML(@content) - @content = nil - doc - end - end - - def collect_ids - document.xpath("./@id | .//@id").map(&:value) - end - - def update_ids_attrs(ids, suffix) - document.xpath(". | .//*[@*]").each do |a| - a.attribute_nodes.each do |x| - ids.include?(x.value) and x.value += sprintf("_%09d", suffix) - end - end - end - - def update_ids_css(ids, suffix) - document.xpath("//m:style", "m" => SVG_NS).each do |s| - c = s.children.to_xml - ids.each do |i| - c = c.gsub(%r[##{i}\b], - sprintf("#%<id>s_%<suffix>09d", id: i, suffix: suffix)) - .gsub(%r(\[id\s*=\s*['"]?#{i}['"]?\]), - sprintf("[id='%<id>s_%<suffix>09d']", id: i, suffix: suffix)) - end - - s.children = c - end + @content = document.to_xml end end end