swordfish 0.0.4 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- ZTEyODI1ZjU2OTMyODY3ZDM0NjM3OGY3ZjcyNTU1YTIwMjk1NDQ0ZA==
4
+ NDJiOGRjNGUyYjg3OWRjZWYwNTJhNWI5YTQ0NTgyMWUxMTdlMWJiOQ==
5
5
  data.tar.gz: !binary |-
6
- YTIxMTkzNWMzNmYwNTY3YTNmOTg2YzE1MTYyZDUxYjk3YTNkZGU1MQ==
6
+ ZGJkYTgxMDUzZGRiZTU3M2I1MTEwMjJhNGE1NzZmMzlhNGNhZTFlOQ==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- ZmQ2YTk3YTcxNzFmNjNhMTg4Yjk0OGE1ZmI0ZjVmMWM5M2U1NmM1NDA3Y2Y0
10
- MzUzNjI4NGQ1Y2IwOTgyNGI3MmM3M2M2OGM3OGIwMzAyY2VmZjE3NWMyNmJl
11
- N2M0MjQ1OGQzZDZhMjI1M2RmN2Y4MTE0MjYyOGZjZWQ4MzJiZWY=
9
+ YmY4YzdlODI5NGUwYzFjOTliZmJmZmRlZjc4Njc4YzlhMmQ3MGZiYjA5NGZj
10
+ NmE4YjdlNTYxNmM1MzM1MWFmMjUxOTE2MDhlMGI1NTFkZTdlMWI5ZTQwOTA1
11
+ MjIyZGI1NzY4YzcyNGU1NWI3MDllNTIwOGZkNmU0OWE4OTc5OGU=
12
12
  data.tar.gz: !binary |-
13
- YjY5NDlhZmQ0N2I0NGYzNjA0OGY4YzQ5MjY1YmI1MDM4OTIzODYxNDBjODIy
14
- OGE3MjdjZTU4YmNkNmVmZjk1NDAxYjA2MmVkMzAzNzViY2ExYzg3MjU5ZjU0
15
- ZGE1NTY5ZWQ1MTI2ZWU5MjJjMTI5Y2ZmYzM1NTI5YWU3YWJmNjM=
13
+ MjgzM2YxMTMzNDlkMDJlN2JhNWI4NzM4M2U4Y2IyOGI0MjkyOGY1MDEzZjNk
14
+ MjYzNGYzNzBlYzY2YzdhZGExM2M4MDMzMDBmYjYxYTRlMzNlMWQ4MmY0YWIy
15
+ MTllYzM2MDQwNGZiMjEwMjdmZjZhNjMxNGFlMDNhNWMzN2UwMjg=
@@ -9,7 +9,6 @@ module Swordfish
9
9
 
10
10
  attr_reader :swordfish_doc # The Swordfish::Document corresponding to the parsed document
11
11
  attr_reader :docx_archive # The source archive
12
- attr_reader :namespaces # A hash of XML namespaces used in this doc
13
12
 
14
13
  # Parse a document and return a Swordfish::Document object
15
14
  def self.open(filepath)
@@ -48,7 +47,6 @@ module Swordfish
48
47
  # Parse the document structure XML
49
48
  def parse(document_xml)
50
49
  @xml = Nokogiri::XML(document_xml)
51
- @namespaces = @xml.collect_namespaces
52
50
 
53
51
  # Iterate over each element node and dispatch it to the appropriate parser
54
52
  @xml.xpath('//w:body').children.each do |node|
@@ -170,10 +168,10 @@ module Swordfish
170
168
  text.content = run_xml.xpath('./w:t')[0].content
171
169
  get_styles_for_node(text, run_xml.xpath('./w:rPr')[0])
172
170
  texts << text
173
- elsif @namespaces['xmlns:pic'] && run_xml.xpath('.//pic:pic', :pic => @namespaces['xmlns:pic']).length > 0
171
+ elsif run_xml.xpath('.//*[name()="pic:pic"]').length > 0
174
172
  # An image run
175
173
  image = Swordfish::Node::Image.new
176
- relationship_id = run_xml.xpath('.//pic:pic/pic:blipFill/a:blip', :pic => @namespaces['xmlns:pic'], :a => @namespaces['xmlns:a'])[0]['r:embed'] rescue nil
174
+ relationship_id = run_xml.xpath('.//*[name()="pic:pic"]/*[name()="pic:blipFill"]/*[name()="a:blip"]')[0]['r:embed'] rescue nil
177
175
  if relationship_id
178
176
  image.original_name = @relationships[relationship_id].split('/').last
179
177
  @swordfish_doc.images[image.original_name] = read_image(image.original_name)
@@ -190,6 +188,17 @@ module Swordfish
190
188
  texts << link
191
189
  end
192
190
  end
191
+ # Clean up runs by merging them if they have identical styles
192
+ to_delete = []
193
+ texts.each_with_index do |text, idx|
194
+ if idx > 0
195
+ if text.is_a?(Swordfish::Node::Text) && texts[idx-1].is_a?(Swordfish::Node::Text) && text.style == texts[idx-1].style
196
+ texts[idx-1].content += text.content
197
+ to_delete << text
198
+ end
199
+ end
200
+ end
201
+ texts.reject! {|t| to_delete.include?(t) }
193
202
  texts
194
203
  end
195
204
 
@@ -2,6 +2,8 @@
2
2
 
3
3
  module Swordfish
4
4
  class Stylesheet
5
+
6
+ attr_reader :styles
5
7
 
6
8
  # Define all supported values here
7
9
  SUPPORTED_STYLES = [
@@ -22,6 +24,11 @@ module Swordfish
22
24
  styles = [styles] unless styles.is_a?(Array)
23
25
  @styles |= styles.select{|s| SUPPORTED_STYLES.include?(s)}
24
26
  end
27
+
28
+ # Test stylesheets for equality (same styles)
29
+ def ==(other)
30
+ @styles.sort == other.styles.sort
31
+ end
25
32
 
26
33
  # For each supported style, define a boolean method to check its presence
27
34
  # (i.e., :bold?, :italic?, etc.)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: swordfish
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Martin Posthumus
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-02 00:00:00.000000000 Z
11
+ date: 2014-06-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler