-
Notifications
You must be signed in to change notification settings - Fork 39
/
list.rb
219 lines (173 loc) · 4.85 KB
/
list.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
module RTF
class ListTable
def initialize
@templates = []
end
def new_template
@templates.push ListTemplate.new(next_template_id)
@templates.last
end
def to_rtf(indent=0)
return '' if @templates.empty?
prefix = indent > 0 ? ' ' * indent : ''
# List table
text = "#{prefix}{\\*\\listtable"
@templates.each {|tpl| text << tpl.to_rtf}
text << "}"
# List override table, a Cargo Cult.
text << "#{prefix}{\\*\\listoverridetable"
@templates.each do |tpl|
text << "{\\listoverride\\listid#{tpl.id}\\listoverridecount0\\ls#{tpl.id}}"
end
text << "}\n"
end
protected
def next_template_id
@templates.size + 1
end
end
class ListMarker
def initialize(name, codepoint=nil)
@name = name
@codepoint = codepoint
end
def bullet?
!@codepoint.nil?
end
def type
bullet? ? :bullet : :decimal
end
def number_type
# 23: bullet, 0: arabic
# applies to the \levelnfcN macro
#
bullet? ? 23 : 0
end
def name
name = "\\{#@name\\}"
name << '.' unless bullet?
name
end
def template_format
# The first char is the string size, the next ones are
# either placeholders (\'0X) or actual characters to
# include in the format. In the bullet case, \uc0 is
# used to get rid of the multibyte translation: we want
# an Unicode character.
#
# In the decimal case, we have a fixed format, with a
# dot following the actual number.
#
if bullet?
"\\'01\\uc0\\u#@codepoint"
else
"\\'02\\'00. "
end
end
def text_format(n=nil)
text =
if bullet?
"\\uc0\\u#@codepoint"
else
"#{n}."
end
"\t#{text}\t"
end
end
class ListTemplate
attr_reader :id
Markers = {
:disc => ListMarker.new('disc', 0x2022),
:hyphen => ListMarker.new('hyphen', 0x2043),
:decimal => ListMarker.new('decimal' )
}
def initialize(id)
@levels = []
@id = id
end
def level_for(level, kind = :bullets)
@levels[level-1] ||= begin
# Only disc for now: we'll add support
# for more customization options later
marker = Markers[kind == :bullets ? :disc : :decimal]
ListLevel.new(self, marker, level)
end
end
def to_rtf(indent=0)
prefix = indent > 0 ? ' ' * indent : ''
text = "#{prefix}{\\list\\listtemplate#{id}\\listhybrid"
@levels.each {|lvl| text << lvl.to_rtf}
text << "{\\listname;}\\listid#{id}}\n"
text
end
end
class ListLevel
ValidLevels = (1..9)
LevelTabs = [
220, 720, 1133, 1700, 2267,
2834, 3401, 3968, 4535, 5102,
5669, 6236, 6803
].freeze
ResetTabs = [560].concat(LevelTabs[2..-1]).freeze
attr_reader :level, :marker
def initialize(template, marker, level)
unless marker.kind_of? ListMarker
RTFError.fire("Invalid marker #{marker.inspect}")
end
unless ValidLevels.include? level
RTFError.fire("Invalid list level: #{level}")
end
@template = template
@level = level
@marker = marker
end
def type
@marker.type
end
def reset_tabs
ResetTabs
end
def tabs
@tabs ||= begin
tabs = LevelTabs.dup # Kernel#tap would be prettier here
(@level - 1).times do
# Reverse-engineered while looking at Textedit.app
# generated output: they already made sure that it
# would look good on every RTF editor :-p
#
a, = tabs.shift(3)
a,b = a + 720, a + 1220
tabs.shift while tabs.first < b
tabs.unshift a, b
end
tabs
end
end
def id
@id ||= @template.id * 10 + level
end
def indent
@indent ||= level * 720
end
def to_rtf(indent=0)
prefix = indent > 0 ? ' ' * indent : ''
text = "#{prefix}{\\listlevel\\levelstartat1"
# Marker type. The first declaration is for Backward Compatibility (BC).
nfc = @marker.number_type
text << "\\levelnfc#{nfc}\\levelnfcn#{nfc}"
# Justification, currently only left justified (0). First decl for BC.
text << '\leveljc0\leveljcn0'
# Character that follows the level text, currently only TAB.
text << '\levelfollow0'
# BC: Minimum distance from the left & right edges.
text << '\levelindent0\levelspace360'
# Marker name
text << "{\\*\\levelmarker #{@marker.name}}"
# Marker text format
text << "{\\leveltext\\leveltemplateid#{id}#{@marker.template_format};}"
text << '{\levelnumbers;}'
# The actual spacing
text << "\\fi-360\\li#{self.indent}\\lin#{self.indent}}\n"
end
end
end