coradoc 1.1.4 → 1.1.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,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e528645f0bdb38f707239ed1862dc74944dc7eba4149cf9f1243458e98591edb
4
- data.tar.gz: 268acd80823a507d3a86e83bfeaef55a8145819822aac9903f0bc96cee29d55c
3
+ metadata.gz: 9931a29512bb9c4e3b590dfc1b3e60ae1dc949da9b4c401e1395a14011c55843
4
+ data.tar.gz: 5990bc988013a86964fd4554f5f094e30a83086b5ae47bada16e83a7a31d596b
5
5
  SHA512:
6
- metadata.gz: 624f718300d0877f0d610a3fbf953b324d5bfd6a73c68de30db69ddc75b45e1b396eeb5bdfdcfc7d0ecac2d50c8301d7011f118a2d23d4ac02d11c2e54e92e6a
7
- data.tar.gz: 849b5851b2ca0b37313d8f7c743defbc168f698284c694c0a467536dbffd9cc10f5ec263fd2c28e1f287a926c6acbe3756c8753f6f3ae303dc61fc9678981ff1
6
+ metadata.gz: a20be2069a6dd16193c6d53c1f08731a3f6732378bd26acac9af98c223c7e801d257c9c0d2055155ed82534d78979fb29fc926d1d896ed07f71d7dffdf002eb7
7
+ data.tar.gz: c4d5552c3b9862849a995bf77c46b413bf2864569a0d75ef00ee3b071182ec25e3c672105841b9944b85d8f690e19f83efca49a069449954dc727f946fdb4681
@@ -15,26 +15,83 @@ module Coradoc
15
15
  @line_break = options.fetch(:line_break, "\n")
16
16
  end
17
17
 
18
+ def inline?(elem)
19
+ case elem
20
+ when Inline::HardLineBreak
21
+ :hardbreak
22
+ when ->(i){ i.class.name.to_s.include? "::Inline::" }
23
+ true
24
+ when String, TextElement, Image::InlineImage
25
+ true
26
+ else
27
+ false
28
+ end
29
+ end
30
+
18
31
  def to_adoc
19
32
  anchor = @anchor.nil? ? "" : " #{@anchor.to_adoc.to_s} "
20
33
  # text = Coradoc::Generator.gen_adoc(@content)
21
- content = Array(@content).map do |subitem|
22
- next if subitem.is_a? Inline::HardLineBreak
34
+ content = Array(@content).flatten.compact
35
+ out = ""
36
+ prev_inline = :init
37
+
38
+ # Collapse meaningless <DIV>s
39
+ while content.map(&:class) == [Section] && content.first.safe_to_collapse?
40
+ content = Array(content.first.contents)
41
+ end
23
42
 
43
+ content.each_with_index do |subitem, idx|
24
44
  subcontent = Coradoc::Generator.gen_adoc(subitem)
45
+
46
+ inline = inline?(subitem)
47
+ next_inline = idx+1 == content.length ? :end : inline?(content[idx+1])
48
+
25
49
  # Only try to postprocess elements that are text,
26
50
  # otherwise we could strip markup.
27
- if Coradoc.a_single?(subitem, Coradoc::Element::TextElement)
28
- subcontent = Coradoc.strip_unicode(subcontent)
51
+ if subitem.is_a? Coradoc::Element::TextElement
52
+ if [:hardbreak, :init, false].include?(prev_inline)
53
+ subcontent = Coradoc.strip_unicode(subcontent, only: :begin)
54
+ end
55
+ if [:hardbreak, :end, false].include?(next_inline)
56
+ subcontent = Coradoc.strip_unicode(subcontent, only: :end)
57
+ end
29
58
  end
30
- subcontent
31
- end.compact.join("\n+\n")
59
+
60
+ case inline
61
+ when true
62
+ if prev_inline == false
63
+ out += "\n+\n" + subcontent
64
+ else
65
+ out += subcontent
66
+ end
67
+ when false
68
+ case prev_inline
69
+ when :hardbreak
70
+ out += subcontent.strip
71
+ when :init
72
+ out += "{empty}\n+\n" + subcontent.strip
73
+ else
74
+ out += "\n+\n" + subcontent.strip
75
+ end
76
+ when :hardbreak
77
+ if %i[hardbreak init].include? prev_inline
78
+ # can't have two hard breaks in a row; can't start with a hard break
79
+ else
80
+ out += "\n+\n"
81
+ end
82
+ end
83
+
84
+ prev_inline = inline
85
+ end
86
+ out += "{empty}" if prev_inline == :hardbreak
87
+ out = "{empty}" if out.empty?
88
+
32
89
  # attach = Coradoc::Generator.gen_adoc(@attached)
33
90
  attach = @attached.map do |elem|
34
91
  "+\n" + Coradoc::Generator.gen_adoc(elem)
35
92
  end.join
36
93
  nest = Coradoc::Generator.gen_adoc(@nested)
37
- out = " #{anchor}#{content}#{@line_break}"
94
+ out = " #{anchor}#{out}#{@line_break}"
38
95
  out + attach + nest
39
96
  end
40
97
  end
data/lib/coradoc/util.rb CHANGED
@@ -1,6 +1,8 @@
1
1
  module Coradoc
2
- def self.strip_unicode(str)
3
- str.gsub(/\A[[:space:]]+|[[:space:]]+\z/, "")
2
+ def self.strip_unicode(str, only: nil)
3
+ str = str.gsub(/\A[[:space:]]+/, "") unless only == :end
4
+ str = str.gsub(/[[:space:]]+\z/, "") unless only == :begin
5
+ str
4
6
  end
5
7
 
6
8
  def self.a_single?(obj, klass)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Coradoc
4
- VERSION = "1.1.4"
4
+ VERSION = "1.1.5"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: coradoc
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.4
4
+ version: 1.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ribose Inc.
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2024-12-06 00:00:00.000000000 Z
12
+ date: 2024-12-14 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: marcel