vexyl 1.4.0

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.
data/lib/vexyl/xml.rb ADDED
@@ -0,0 +1,570 @@
1
+ # SPDX-License-Identifier: Apache-2.0
2
+ # Copyright 2026 Naomi Persephone Amethyst <naomi@amethyst.name>
3
+
4
+ module Vexyl
5
+ # Reads well-formed XML 1.0 with namespaces, one token at a time, as SPEC section 8.3 defines it,
6
+ # and refuses everything else. A sanitizer's security rests on reading markup exactly one way, so
7
+ # nothing here is lenient.
8
+ #
9
+ # Positions are code point indices into the markup, once its line endings are normalized.
10
+ class Xml
11
+ XML_NAMESPACE = "http://www.w3.org/XML/1998/namespace"
12
+ XMLNS_NAMESPACE = "http://www.w3.org/2000/xmlns/"
13
+
14
+ # A name with its namespace worked out.
15
+ Name = Struct.new(:space, :local)
16
+
17
+ Attr = Struct.new(:name, :value)
18
+
19
+ # +kind+ is one of :start, :end, :text, :other and :directive.
20
+ Token = Struct.new(:kind, :name, :attrs, :text) do
21
+ def self.of(kind)
22
+ new(kind, nil, [].freeze, "")
23
+ end
24
+ end
25
+
26
+ # Malformed markup.
27
+ class XmlException < StandardError; end
28
+
29
+ # An open element, with what its namespace declarations replaced.
30
+ class Frame
31
+ attr_reader :raw, :undo
32
+ attr_accessor :name
33
+
34
+ def initialize(raw)
35
+ @raw = raw
36
+ @name = nil
37
+ @undo = []
38
+ end
39
+ end
40
+ private_constant :Frame
41
+
42
+ LT = "<".ord
43
+ AMP = "&".ord
44
+ RSQB = "]".ord
45
+ GT = ">".ord
46
+ SLASH = "/".ord
47
+ EQ = "=".ord
48
+ QUOT = '"'.ord
49
+ APOS = "'".ord
50
+ TAB = 0x09
51
+ LF = 0x0a
52
+ CR = 0x0d
53
+ SP = 0x20
54
+ COLON = ":".ord
55
+ private_constant :LT, :AMP, :RSQB, :GT, :SLASH, :EQ, :QUOT, :APOS, :TAB, :LF, :CR, :SP, :COLON
56
+
57
+ def initialize(markup)
58
+ markup = markup.encode(Encoding::UTF_8) unless markup.encoding == Encoding::UTF_8
59
+ raise XmlException, "the markup isn't valid UTF-8" unless markup.valid_encoding?
60
+
61
+ markup.each_codepoint do |r|
62
+ raise XmlException, format("U+%04X isn't an XML character", r) unless Xml.xml_char(r)
63
+ end
64
+ @src = Xml.normalize_newlines(markup).codepoints
65
+ @len = @src.length
66
+ @pos = 0
67
+ @stack = []
68
+ @ns = { "xml" => XML_NAMESPACE }
69
+ @pending = nil
70
+ @done_root = false
71
+ rescue EncodingError
72
+ raise XmlException, "the markup isn't valid UTF-8"
73
+ end
74
+
75
+ def self.xml_char(r)
76
+ r == 0x09 || r == 0x0a || r == 0x0d || (r >= 0x20 && r <= 0xd7ff) ||
77
+ (r >= 0xe000 && r <= 0xfffd) || (r >= 0x10000 && r <= 0x10ffff)
78
+ end
79
+
80
+ # Takes a code point.
81
+ def self.xml_space(c)
82
+ c == SP || c == TAB || c == LF || c == CR
83
+ end
84
+
85
+ def self.name_start(r)
86
+ r == COLON || r == 0x5f || (r >= 0x41 && r <= 0x5a) || (r >= 0x61 && r <= 0x7a) ||
87
+ (r >= 0xc0 && r <= 0xd6) || (r >= 0xd8 && r <= 0xf6) || (r >= 0xf8 && r <= 0x2ff) ||
88
+ (r >= 0x370 && r <= 0x37d) || (r >= 0x37f && r <= 0x1fff) || (r >= 0x200c && r <= 0x200d) ||
89
+ (r >= 0x2070 && r <= 0x218f) || (r >= 0x2c00 && r <= 0x2fef) || (r >= 0x3001 && r <= 0xd7ff) ||
90
+ (r >= 0xf900 && r <= 0xfdcf) || (r >= 0xfdf0 && r <= 0xfffd) || (r >= 0x10000 && r <= 0xeffff)
91
+ end
92
+
93
+ def self.name_char(r)
94
+ name_start(r) || r == 0x2d || r == 0x2e || (r >= 0x30 && r <= 0x39) || r == 0xb7 ||
95
+ (r >= 0x300 && r <= 0x36f) || (r >= 0x203f && r <= 0x2040)
96
+ end
97
+
98
+ # The next token, or nil once the document has ended properly.
99
+ def token
100
+ if @pending
101
+ t = @pending
102
+ @pending = nil
103
+ return t
104
+ end
105
+ if @pos >= @len
106
+ raise ended unless @stack.empty?
107
+
108
+ return nil
109
+ end
110
+ return text if @src[@pos] != LT
111
+ return end_tag if starts?("</")
112
+ return instruction if starts?("<?")
113
+ return comment if starts?("<!--")
114
+ return cdata if starts?("<![CDATA[")
115
+ if starts?("<!")
116
+ # A directive is always refused, so there's no need to find where it ends.
117
+ @pos = @len
118
+ return Token.of(:directive)
119
+ end
120
+ start_tag
121
+ end
122
+
123
+ # Turns CRLF and lone CR into LF.
124
+ def self.normalize_newlines(s)
125
+ return s unless s.include?("\r")
126
+
127
+ s.gsub("\r\n", "\n").tr("\r", "\n")
128
+ end
129
+
130
+ ESCAPES = {
131
+ "&" => "&amp;", "<" => "&lt;", ">" => "&gt;", '"' => "&#34;", "'" => "&#39;",
132
+ "\t" => "&#x9;", "\n" => "&#xA;", "\r" => "&#xD;"
133
+ }.freeze
134
+ ESCAPED = /[&<>"'\t\n\r]|[^\u0009\u000A\u000D -퟿-�\u{10000}-\u{10FFFF}]/
135
+ private_constant :ESCAPES, :ESCAPED
136
+
137
+ # Escapes text for an attribute's value or an element's text, as Go's xml.EscapeText does and
138
+ # SDK section 4.2 says, so that tabs and line endings in a value survive being read again.
139
+ # What isn't an XML character, or isn't valid UTF-8, becomes U+FFFD.
140
+ def self.escape(s)
141
+ unless s.encoding == Encoding::UTF_8
142
+ s = s.encode(Encoding::UTF_8, invalid: :replace, undef: :replace, replace: "�")
143
+ end
144
+ s = s.scrub("�") unless s.valid_encoding?
145
+ return s unless ESCAPED.match?(s)
146
+
147
+ s.gsub(ESCAPED) { |c| ESCAPES[c] || "�" }
148
+ end
149
+
150
+ private
151
+
152
+ def ended
153
+ XmlException.new("unexpected end of the markup")
154
+ end
155
+
156
+ # Whether the markup has +lit+ (ASCII) at +at+.
157
+ def starts?(lit, at = @pos)
158
+ return false if at + lit.length > @len
159
+
160
+ i = 0
161
+ lit.each_byte do |b|
162
+ return false if @src[at + i] != b
163
+
164
+ i += 1
165
+ end
166
+ true
167
+ end
168
+
169
+ # Where +lit+ (ASCII) next appears from +from+, or nil.
170
+ def index_of(lit, from)
171
+ first = lit.getbyte(0)
172
+ i = from
173
+ last = @len - lit.length
174
+ while i <= last
175
+ return i if @src[i] == first && starts?(lit, i)
176
+
177
+ i += 1
178
+ end
179
+ nil
180
+ end
181
+
182
+ def substring(from, to)
183
+ @src[from...to].pack("U*")
184
+ end
185
+
186
+ # Character data, which outside the root can only be whitespace.
187
+ def text
188
+ b = +""
189
+ while @pos < @len && @src[@pos] != LT
190
+ c = @src[@pos]
191
+ if c == AMP
192
+ raise XmlException, "a character reference can't appear outside the root" if @stack.empty?
193
+
194
+ b << reference
195
+ elsif c == RSQB && starts?("]]>")
196
+ raise XmlException, "]]> can't appear in text"
197
+ else
198
+ b << c
199
+ @pos += 1
200
+ end
201
+ end
202
+ Token.new(:text, nil, [].freeze, b)
203
+ end
204
+
205
+ def cdata
206
+ raise XmlException, "a CDATA section can't appear outside the root" if @stack.empty?
207
+
208
+ start = @pos + "<![CDATA[".length
209
+ stop = index_of("]]>", start)
210
+ raise ended if stop.nil?
211
+
212
+ @pos = stop + 3
213
+ Token.new(:text, nil, [].freeze, substring(start, stop))
214
+ end
215
+
216
+ def comment
217
+ start = @pos + "<!--".length
218
+ stop = index_of("--", start)
219
+ raise ended if stop.nil?
220
+ raise XmlException, "\"--\" can't appear in a comment" unless starts?("-->", stop)
221
+
222
+ @pos = stop + 3
223
+ Token.of(:other)
224
+ end
225
+
226
+ def instruction
227
+ begin_at = @pos
228
+ @pos += 2
229
+ target = begin
230
+ name
231
+ rescue XmlException
232
+ raise XmlException, "expected a target after <?"
233
+ end
234
+ stop = index_of("?>", @pos)
235
+ raise ended if stop.nil?
236
+
237
+ data = substring(@pos, stop)
238
+ if !data.empty? && !Xml.xml_space(data.ord)
239
+ raise XmlException, "a processing instruction's target needs a space after it"
240
+ end
241
+
242
+ @pos = stop + 2
243
+ if target.length == 3 && target.downcase(:ascii) == "xml"
244
+ if begin_at != 0 || target != "xml"
245
+ raise XmlException, "an XML declaration can only come first"
246
+ end
247
+
248
+ Xml.check_declaration(data)
249
+ elsif target.include?(":")
250
+ raise XmlException, "a processing instruction's target can't have a colon"
251
+ end
252
+ Token.of(:other)
253
+ end
254
+
255
+ class << self
256
+ # Reads version="1.0" encoding="UTF-8" standalone="yes", of which only the version is
257
+ # required, in that order.
258
+ def check_declaration(data)
259
+ pos = [0]
260
+ version = field(data, pos, "version")
261
+ raise bad if version.nil?
262
+ raise XmlException, "unsupported XML version \"#{version}\"; only 1.0 is" if version != "1.0"
263
+
264
+ encoding = field(data, pos, "encoding")
265
+ if !encoding.nil? && encoding.downcase(:ascii) != "utf-8"
266
+ raise XmlException, "unsupported encoding \"#{encoding}\"; only UTF-8 is"
267
+ end
268
+
269
+ standalone = field(data, pos, "standalone")
270
+ raise bad if !standalone.nil? && standalone != "yes" && standalone != "no"
271
+
272
+ data.each_char.drop(pos[0]).each do |c|
273
+ raise bad unless xml_space(c.ord)
274
+ end
275
+ end
276
+
277
+ private
278
+
279
+ def bad
280
+ XmlException.new("malformed XML declaration")
281
+ end
282
+
283
+ # One field of a declaration, or nil if the next isn't that one. +pos+ is a one-element
284
+ # Array holding a char index into +data+.
285
+ def field(data, pos, key)
286
+ chars = data.chars
287
+ n = chars.length
288
+ i = pos[0]
289
+ i += 1 while i < n && xml_space(chars[i].ord)
290
+ return nil if i == pos[0] || chars[i, key.length].join != key
291
+
292
+ i += key.length
293
+ i += 1 while i < n && xml_space(chars[i].ord)
294
+ raise bad if i >= n || chars[i] != "="
295
+
296
+ i += 1
297
+ i += 1 while i < n && xml_space(chars[i].ord)
298
+ raise bad if i >= n || (chars[i] != '"' && chars[i] != "'")
299
+
300
+ stop = data.index(chars[i], i + 1)
301
+ raise bad if stop.nil?
302
+
303
+ pos[0] = stop + 1
304
+ chars[(i + 1)...stop].join
305
+ end
306
+ end
307
+
308
+ # An XML 1.0 Name.
309
+ def name
310
+ start = @pos
311
+ while @pos < @len
312
+ r = @src[@pos]
313
+ break if @pos == start ? !Xml.name_start(r) : !Xml.name_char(r)
314
+
315
+ @pos += 1
316
+ end
317
+ raise XmlException, "expected a name" if @pos == start
318
+
319
+ substring(start, @pos)
320
+ end
321
+
322
+ def space
323
+ start = @pos
324
+ @pos += 1 while @pos < @len && Xml.xml_space(@src[@pos])
325
+ @pos > start
326
+ end
327
+
328
+ # Divides a name at its colon, of which it can have one, between two parts.
329
+ def split_qname(raw)
330
+ colon = raw.index(":")
331
+ return ["", raw] if colon.nil?
332
+
333
+ before = raw[0, colon]
334
+ after = raw[(colon + 1)..]
335
+ if before.empty? || after.empty? || after.include?(":") || !Xml.name_start(after.ord)
336
+ raise XmlException, "\"#{raw}\" isn't a valid qualified name"
337
+ end
338
+
339
+ [before, after]
340
+ end
341
+
342
+ def end_tag
343
+ @pos += 2
344
+ raw = begin
345
+ name
346
+ rescue XmlException
347
+ raise XmlException, "expected a name after </"
348
+ end
349
+ space
350
+ raise ended if @pos >= @len
351
+ raise XmlException, "unexpected characters in </#{raw}>" if @src[@pos] != GT
352
+
353
+ @pos += 1
354
+ raise XmlException, "</#{raw}> closes nothing" if @stack.empty?
355
+
356
+ top = @stack.last
357
+ raise XmlException, "<#{top.raw}> is closed by </#{raw}>" if top.raw != raw
358
+
359
+ close_element
360
+ end
361
+
362
+ def close_element
363
+ top = @stack.pop
364
+ top.undo.reverse_each do |prefix, old|
365
+ if old.nil?
366
+ @ns.delete(prefix)
367
+ else
368
+ @ns[prefix] = old
369
+ end
370
+ end
371
+ @done_root = true if @stack.empty?
372
+ Token.new(:end, top.name, [].freeze, "")
373
+ end
374
+
375
+ def start_tag
376
+ @pos += 1
377
+ raw = begin
378
+ name
379
+ rescue XmlException
380
+ raise XmlException, "expected a name after <"
381
+ end
382
+ raise XmlException, "markup can only have one root element" if @done_root
383
+
384
+ attrs = []
385
+ seen = {}
386
+ self_closing = false
387
+ loop do
388
+ spaced = space
389
+ raise ended if @pos >= @len
390
+
391
+ c = @src[@pos]
392
+ if c == GT
393
+ @pos += 1
394
+ break
395
+ end
396
+ if c == SLASH
397
+ raise XmlException, "expected /> in an element" unless starts?("/>")
398
+
399
+ @pos += 2
400
+ self_closing = true
401
+ break
402
+ end
403
+ raise XmlException, "<#{raw}> needs a space before each attribute" unless spaced
404
+
405
+ aname = begin
406
+ name
407
+ rescue XmlException
408
+ raise XmlException, "expected an attribute name in <#{raw}>"
409
+ end
410
+ raise XmlException, "attribute \"#{aname}\" is repeated" if seen.key?(aname)
411
+
412
+ seen[aname] = true
413
+ space
414
+ raise XmlException, "attribute \"#{aname}\" has no value" if @pos >= @len || @src[@pos] != EQ
415
+
416
+ @pos += 1
417
+ space
418
+ attrs << [aname, attr_value]
419
+ end
420
+ frame = Frame.new(raw)
421
+ # Declarations apply to the element they're on, so they're read first.
422
+ attrs.each { |a| declare(a[0], a[1], frame) }
423
+ q = split_qname(raw)
424
+ raise XmlException, "an element can't use the xmlns prefix" if q[0] == "xmlns"
425
+
426
+ space_uri = @ns.fetch(q[0], "")
427
+ raise XmlException, "prefix \"#{q[0]}\" isn't declared" if !q[0].empty? && space_uri.empty?
428
+
429
+ frame.name = Name.new(space_uri, q[1])
430
+ expanded = {}
431
+ out = []
432
+ attrs.each do |a|
433
+ next if a[0] == "xmlns" || a[0].start_with?("xmlns:")
434
+
435
+ aq = split_qname(a[0])
436
+ aspace = ""
437
+ unless aq[0].empty?
438
+ aspace = @ns.fetch(aq[0], "")
439
+ raise XmlException, "prefix \"#{aq[0]}\" isn't declared" if aspace.empty?
440
+ end
441
+ n = Name.new(aspace, aq[1])
442
+ raise XmlException, "attribute \"#{a[0]}\" is repeated" if expanded.key?(n)
443
+
444
+ expanded[n] = true
445
+ out << Attr.new(n, a[1])
446
+ end
447
+ @stack.push(frame)
448
+ tok = Token.new(:start, frame.name, out, "")
449
+ @pending = close_element if self_closing
450
+ tok
451
+ end
452
+
453
+ # Applies a namespace declaration, if an attribute is one.
454
+ def declare(name, value, frame)
455
+ prefix = ""
456
+ if name == "xmlns"
457
+ # The default namespace.
458
+ elsif name.start_with?("xmlns:")
459
+ prefix = name["xmlns:".length..]
460
+ split_qname(name)
461
+ raise XmlException, "the xmlns prefix can't be declared" if prefix == "xmlns"
462
+ raise XmlException, "prefix \"#{prefix}\" can't be undeclared" if value.empty?
463
+ else
464
+ return
465
+ end
466
+ if (prefix == "xml") != (value == XML_NAMESPACE) || value == XMLNS_NAMESPACE
467
+ raise XmlException, "namespace \"#{value}\" can't be bound to that prefix"
468
+ end
469
+
470
+ frame.undo << [prefix, @ns[prefix]]
471
+ @ns[prefix] = value
472
+ end
473
+
474
+ # A quoted value, with literal whitespace turned into spaces as XML says.
475
+ def attr_value
476
+ if @pos >= @len || (@src[@pos] != QUOT && @src[@pos] != APOS)
477
+ raise XmlException, "an attribute's value needs quotes"
478
+ end
479
+
480
+ quote = @src[@pos]
481
+ @pos += 1
482
+ b = +""
483
+ loop do
484
+ raise ended if @pos >= @len
485
+
486
+ c = @src[@pos]
487
+ if c == quote
488
+ @pos += 1
489
+ return b
490
+ end
491
+ case c
492
+ when LT
493
+ raise XmlException, "< can't appear in an attribute's value"
494
+ when AMP
495
+ b << reference
496
+ when TAB, LF, CR
497
+ b << " "
498
+ @pos += 1
499
+ else
500
+ b << c
501
+ @pos += 1
502
+ end
503
+ end
504
+ end
505
+
506
+ SEMI = ";".ord
507
+ NAMED = { "lt" => "<", "gt" => ">", "amp" => "&", "apos" => "'", "quot" => '"' }.freeze
508
+ private_constant :SEMI, :NAMED
509
+
510
+ # One of XML's five named entities, or a reference to a character.
511
+ def reference
512
+ stop = nil
513
+ i = @pos
514
+ while i < @len
515
+ if @src[i] == SEMI
516
+ stop = i
517
+ break
518
+ end
519
+ i += 1
520
+ end
521
+ raise XmlException, "& needs to begin a reference ending in ;" if stop.nil?
522
+
523
+ body = substring(@pos + 1, stop)
524
+ @pos = stop + 1
525
+ named = NAMED[body]
526
+ return named if named
527
+
528
+ if body.start_with?("#x")
529
+ digits = body[2..]
530
+ base = 16
531
+ elsif body.start_with?("#")
532
+ digits = body[1..]
533
+ base = 10
534
+ else
535
+ raise XmlException, "unknown entity &#{body};"
536
+ end
537
+ v = 0
538
+ ok = !digits.empty?
539
+ digits.each_char do |ch|
540
+ break unless ok
541
+
542
+ d = digit(ch, base)
543
+ if d.nil?
544
+ ok = false
545
+ else
546
+ v = v * base + d
547
+ ok = v <= 0x10ffff
548
+ end
549
+ end
550
+ raise XmlException, "invalid reference &#{body};" if !ok || !Xml.xml_char(v)
551
+
552
+ [v].pack("U")
553
+ end
554
+
555
+ # An ASCII digit's value in +base+ (10 or 16), or nil.
556
+ def digit(ch, base)
557
+ o = ch.ord
558
+ d = if o >= 0x30 && o <= 0x39
559
+ o - 0x30
560
+ elsif o >= 0x61 && o <= 0x66
561
+ o - 0x61 + 10
562
+ elsif o >= 0x41 && o <= 0x46
563
+ o - 0x41 + 10
564
+ end
565
+ return nil if d.nil? || d >= base
566
+
567
+ d
568
+ end
569
+ end
570
+ end
data/lib/vexyl.rb ADDED
@@ -0,0 +1,101 @@
1
+ # SPDX-License-Identifier: Apache-2.0
2
+ # Copyright 2026 Naomi Persephone Amethyst <naomi@amethyst.name>
3
+
4
+ require_relative "vexyl/version"
5
+ require_relative "vexyl/errors"
6
+ require_relative "vexyl/model"
7
+ require_relative "vexyl/kdl"
8
+ require_relative "vexyl/limits"
9
+ require_relative "vexyl/numbers"
10
+ require_relative "vexyl/xml"
11
+ require_relative "vexyl/sanitizer"
12
+ require_relative "vexyl/parser"
13
+ require_relative "vexyl/geometry"
14
+ require_relative "vexyl/shapes"
15
+ require_relative "vexyl/adapter"
16
+ require_relative "vexyl/element"
17
+ require_relative "vexyl/render_options"
18
+ require_relative "vexyl/render"
19
+ require_relative "vexyl/canonical"
20
+ require_relative "vexyl/json"
21
+ require_relative "vexyl/locate"
22
+ require_relative "vexyl/share_dictionary"
23
+ require_relative "vexyl/share"
24
+ require_relative "vexyl/flag"
25
+
26
+ # Reads, checks and renders Vexyl flag documents, and reads and writes share links: the
27
+ # operations of section 1 of the SDK. Nothing here reads the network or the filesystem, since a
28
+ # document is self-contained.
29
+ #
30
+ # problems = Vexyl.validate(source) # empty if the document is valid
31
+ # flag = Vexyl.parse(source) # raises Vexyl::ProblemsError holding every problem
32
+ # image = flag.svg(height: 1200)
33
+ module Vexyl
34
+ # The format version this library reads.
35
+ FORMAT_VERSION = 1
36
+
37
+ # The attribute a tagged render marks each drawn element with.
38
+ TAG_ATTRIBUTE = "data-vexyl-layer"
39
+
40
+ # Reads a document, given as a String of UTF-8, or of bytes that have to be UTF-8. Raises
41
+ # Vexyl::ProblemsError with every problem found, if it isn't valid.
42
+ def self.parse(document)
43
+ Flag.new(Parser.parse(text(document)))
44
+ end
45
+
46
+ # Every problem in a document, as Vexyl::Problem, or none if it's valid.
47
+ def self.validate(document)
48
+ parse(document)
49
+ []
50
+ rescue ProblemsError => e
51
+ e.problems
52
+ end
53
+
54
+ # Where each layer and other part of a document is, as Vexyl::Span, in document order. It
55
+ # reads only the document's syntax, so it works on one with other problems, and gives nothing
56
+ # if the document isn't KDL.
57
+ def self.locate(document)
58
+ Locate.locate(document.to_s).freeze
59
+ end
60
+
61
+ # The innermost of a document's parts holding a line, or nil if none does.
62
+ def self.layer_at(spans, line)
63
+ Locate.layer_at(spans, line)
64
+ end
65
+
66
+ # Writes a name as part of a path, where % is %25 and / is %2F.
67
+ def self.encode_path_name(name)
68
+ Locate.encode_name(name)
69
+ end
70
+
71
+ # Reads a name written in a path.
72
+ def self.decode_path_name(part)
73
+ Locate.decode_name(part)
74
+ end
75
+
76
+ # The path of the layer holding another, or "" for one at the top of its scope.
77
+ def self.parent_path(path)
78
+ Locate.parent_path(path)
79
+ end
80
+
81
+ # Writes a document as the part of a share link after its #. Raises Vexyl::Error if it's
82
+ # larger than a link can hold.
83
+ def self.share_encode(document)
84
+ Share.encode(document)
85
+ end
86
+
87
+ # Reads the document a share link holds, given the part after its #. Raises Vexyl::Error if
88
+ # the link is malformed, corrupt, too large, or of a version this library doesn't read.
89
+ def self.share_decode(link)
90
+ Share.decode(link)
91
+ end
92
+
93
+ def self.text(document)
94
+ raise TypeError, "a document is a String, not #{document.class}" unless document.is_a?(String)
95
+ return document if document.encoding == Encoding::UTF_8 && document.valid_encoding?
96
+ s = document.dup.force_encoding(Encoding::UTF_8)
97
+ raise ProblemsError, [Problem.new(0, 0, nil, "a document has to be UTF-8")] unless s.valid_encoding?
98
+ s
99
+ end
100
+ private_class_method :text
101
+ end