xml-mixup 0.1.6 → 0.1.8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b913c511d5bba6785d8db148163e165cdefc95e812a1e50be2c0b340c004b8d8
4
- data.tar.gz: 14000a5d897c303d6afda38d9a133a5b51f25d3f3fe05bd7e515c29bb4a95fed
3
+ metadata.gz: 8d600f3895e5048db0b914aa5252b480486bf0c45591a3eb73e4ddcef410b5d8
4
+ data.tar.gz: '088830420d03127e46e1d5d9e23dc8e6e9ab9bf9caad6ad88f9906ad8928fabe'
5
5
  SHA512:
6
- metadata.gz: 0b195ce5cc4194bb50eaf6d6b45f63455d510cb866849907bc6a76eeea61062f1369271f43ede0744f74663fcdd92febf36ce88b55673cd3f27c10a3fe8439f2
7
- data.tar.gz: 4a2ff48f0abf03e28951a0f9431e9799c791d3838c2316e8f78a0007b281cafcae1fa893f63f6c892f0a8d73fa4f1c9bcbfbbf0fc03f4b17da5cf3073733d36e
6
+ metadata.gz: ac41a0326ac73bff9b2fb89f8ead181e0cc308195d64d0b29ad76315e8c8687dfd570eb546572212fb8ab2f6fd08d6c559a0a1069982b18cadd4eb4c45c56384
7
+ data.tar.gz: 8fea64d8a5079e1f7e6e4e592f4517499c5026281ae9c62806f91e4e8aa2fdaf6fce26bfd739dabbe6d003f10ab7df30873f63698f9e1f4a86d6d1f9ad0a3f73
data/.gitignore CHANGED
@@ -13,3 +13,4 @@ Gemfile.lock
13
13
 
14
14
  # rspec failure tracking
15
15
  .rspec_status
16
+ xml-mixup*.gem
@@ -1,5 +1,5 @@
1
1
  module XML
2
2
  module Mixup
3
- VERSION = "0.1.6"
3
+ VERSION = "0.1.8"
4
4
  end
5
5
  end
data/lib/xml/mixup.rb CHANGED
@@ -304,9 +304,13 @@ module XML::Mixup
304
304
  p, _ = /^(?:([^:]+):)?(.+)$/.match(k).captures
305
305
  ns[p] ||= nil
306
306
  end
307
+
307
308
  # also do the tag prefix but only if there is a local name
308
309
  ns[prefix] ||= nil if local
309
310
 
311
+ # unconditionally remove ns['xml'], we never want it in there
312
+ ns.delete 'xml'
313
+
310
314
  # pseudo is a stand-in for non-parent adjacent nodes
311
315
  pseudo = nodes[:pseudo] || nodes[:parent]
312
316
 
@@ -324,7 +328,7 @@ module XML::Mixup
324
328
 
325
329
  # there should be no nil namespace declarations now
326
330
  if ns.has_value? nil
327
- raise 'INTERNAL ERROR: nil namespace declaration'
331
+ raise "INTERNAL ERROR: nil namespace declaration: #{ns}"
328
332
  end
329
333
 
330
334
  # generate the node
@@ -373,7 +377,8 @@ module XML::Mixup
373
377
  #
374
378
  # @param vocab [#to_s] the contents of the root node's +vocab=+.
375
379
  #
376
- # @param lang [#to_s] the contents of +lang=+ and when applicable, +xml:lang+.
380
+ # @param lang [#to_s] the contents of +lang=+ and when applicable,
381
+ # +xml:lang+.
377
382
  #
378
383
  # @param title [#to_s, #to_a, Hash] the contents of the +<title>+
379
384
  # tag. When given as an array-like object, all elements after the
@@ -381,9 +386,11 @@ module XML::Mixup
381
386
  # the +property=+ attribute. When given as a {Hash}, it will be
382
387
  # coerced into a snippet of spec that produces the appropriate tag.
383
388
  #
384
- # @param link [#to_a, Hash] A spec describing one or more +<link/>+ elements.
389
+ # @param link [#to_a, Hash] A spec describing one or more +<link/>+
390
+ # elements.
385
391
  #
386
- # @param meta [#to_a, Hash] A spec describing one or more +<meta/>+ elements.
392
+ # @param meta [#to_a, Hash] A spec describing one or more +<meta/>+
393
+ # elements.
387
394
  #
388
395
  # @param style [#to_a, Hash] A spec describing one or more
389
396
  # +<style/>+ elements.
@@ -391,12 +398,16 @@ module XML::Mixup
391
398
  # @param script [#to_a, Hash] A spec describing one or more
392
399
  # +<script/>+ elements.
393
400
  #
401
+ # @param extra [#to_a, Hash] A spec describing any extra elements
402
+ # inside +<head>+ that aren't in the previous categories.
403
+ #
394
404
  # @param attr [Hash] A spec containing attributes for the +<body>+.
395
405
  #
396
406
  # @param content [Hash, Array, Nokogiri::XML::Node, ...] A spec which
397
407
  # will be attached underneath the +<body>+.
398
408
  #
399
- # @param head [Hash] A spec which overrides the entire +<head>+.
409
+ # @param head [Hash, Array] A Hash spec which overrides the entire
410
+ # +<head>+ element, or otherwise an array of its children.
400
411
  #
401
412
  # @param body [Hash] A spec which overrides the entire +<body>+.
402
413
  #
@@ -418,7 +429,7 @@ module XML::Mixup
418
429
 
419
430
  def xhtml_stub doc: nil, base: nil, ns: {}, prefix: {}, vocab: nil,
420
431
  lang: nil, title: nil, link: [], meta: [], style: [], script: [],
421
- head: {}, body: {}, attr: {}, content: [],
432
+ extra: [], head: {}, body: {}, attr: {}, content: [],
422
433
  transform: nil, dtd: true, xmlns: true, args: []
423
434
 
424
435
  spec = []
@@ -426,8 +437,7 @@ module XML::Mixup
426
437
  # add xslt stylesheet
427
438
  if transform
428
439
  spec << (transform.is_a?(Hash) ? transform :
429
- { nil => ['#pi', 'xml-stylesheet'],
430
- type: 'text/xsl', href: transform.to_s })
440
+ { '#pi' => 'xml-stylesheet', type: 'text/xsl', href: transform })
431
441
  end
432
442
 
433
443
  # add doctype declaration
@@ -436,19 +446,47 @@ module XML::Mixup
436
446
  spec << { nil => %w{#dtd html} + ps }
437
447
  end
438
448
 
449
+ # normalize title
450
+
451
+ if title.nil? or (title.respond_to?(:empty?) and title.empty?)
452
+ title = { '#title' => '' } # add an empty string for closing tag
453
+ elsif title.is_a? Hash
454
+ # nothing
455
+ elsif title.respond_to? :to_a
456
+ title = title.to_a
457
+ text = title.shift
458
+ props = title
459
+ title = { '#title' => text }
460
+ title[:property] = props unless props.empty?
461
+ else
462
+ title = { '#title' => title }
463
+ end
464
+
465
+ # normalize base
466
+
467
+ if base and not base.is_a? Hash
468
+ base = { nil => :base, href: base }
469
+ end
470
+
471
+ # TODO normalize link, meta, style, script elements
472
+
439
473
  # construct document tree
440
474
 
441
475
  head ||= {}
442
- if head.respond_to?(:empty) and head.empty?
443
- head[nil] = [:head, title, base, link, meta, style, script]
476
+ if head.is_a? Hash and head.empty?
477
+ head[nil] = [:head, title, base, link, meta, style, script, extra]
478
+ elsif head.is_a? Array
479
+ # children of unmarked head element
480
+ head = { nil => [:head] + head }
444
481
  end
445
482
 
446
483
  body ||= {}
447
- if body.respond_to?(:empty?) and body.empty?
484
+ if body.is_a? Hash and body.empty?
448
485
  body[nil] = [:body, content]
486
+ body = attr.merge(body) if attr and attr.is_a?(Hash)
449
487
  end
450
488
 
451
- root = { nil => [:html, [head, body]] }
489
+ root = { nil => [:html, head, body] }
452
490
  root[:vocab] = vocab if vocab
453
491
  root[:lang] = lang if lang
454
492
 
@@ -458,31 +496,52 @@ module XML::Mixup
458
496
 
459
497
  # namespaced language attribute
460
498
  root['xml:lang'] = lang if lang
499
+
500
+ if !prefix and xmlns.is_a? Hash
501
+ x = prefix.transform_keys { |k| "xmlns:#{k}" }
502
+ root = x.merge(root)
503
+ end
461
504
  end
462
505
 
463
506
  # deal with prefixes distinct from namespaces
464
- if prefix
507
+ prefix ||= {}
508
+ if prefix.respond_to? :to_h
509
+ prefix = prefix.to_h
510
+ unless prefix.empty?
511
+ # this will get automatically smushed into the right shape
512
+ root[:prefix] = prefix
513
+
514
+ if xmlns
515
+ x = prefix.transform_keys { |k| "xmlns:#{k}" }
516
+ root = x.merge(root)
517
+ end
518
+ end
465
519
  end
466
520
 
467
521
  # add the document structure to the spec
468
522
  spec << root
469
523
 
470
524
  # as usual this will return the last innermost node
471
- markup spec: spec, doc: doc
525
+ markup spec: spec, doc: doc, args: args
472
526
  end
473
527
 
474
528
  private
475
529
 
476
530
  def element tag, doc: nil, ns: {}, attr: {}, args: []
477
531
  raise 'Document node must be present' unless doc
478
- prefix = local = nil
532
+ pfx = nil
479
533
  if tag.respond_to? :to_a
480
- prefix, local = tag
481
- tag = tag[0..1].join ':'
534
+ pfx = tag[0]
535
+ tag = tag.to_a[0..1].join ':'
536
+ elsif m = tag.match(/([^:]+):/)
537
+ pfx = m[1]
482
538
  end
539
+
483
540
  elem = doc.create_element tag.to_s
484
- ns.sort.each do |p, u|
485
- elem.add_namespace((p.nil? ? p : p.to_s), u.to_s)
541
+ elem.default_namespace = ns[pfx] if ns[pfx]
542
+
543
+ ns.keys.sort { |a, b| a.to_s <=> b.to_s }.each do |p|
544
+ elem.add_namespace((p.nil? ? p : p.to_s), ns[p].to_s)
486
545
  end
487
546
  attr.sort.each do |k, v|
488
547
  elem[k.to_s] = flatten(v, args)
@@ -491,7 +550,7 @@ module XML::Mixup
491
550
  elem
492
551
  end
493
552
 
494
- ATOMS = [String, Symbol, Numeric, NilClass, FalseClass, TrueClass]
553
+ ATOMS = [String, Symbol, Numeric, NilClass, FalseClass, TrueClass].freeze
495
554
 
496
555
  # yo dawg
497
556
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: xml-mixup
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.1.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dorian Taylor
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-02-04 00:00:00.000000000 Z
11
+ date: 2019-04-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler