htmlcom 0.3.0 → 0.3.3

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: 41f76e94774f2bfc83083ea47a7a47272f13dcb9c813100f3f5c688114faef6f
4
- data.tar.gz: 1eec290b80a32ebfc3f976d0c35f2a2ffccee516da6c71aed20f110bc9fd412a
3
+ metadata.gz: 6272c236431ec9708a8805b6e28237e9faf86eac4d957941e8ec9ce74fc657ec
4
+ data.tar.gz: 6d878d46b83d2cfb3f2b4a91f0de32d360b3ca8a0e843859513ab5b501f1a7d1
5
5
  SHA512:
6
- metadata.gz: d431b683fd27995074a06a0ccb2726dc0dc8af57eae06514c7e57402c781c257f2a2f8e1210f8939afdff4a6c23013840ca2a42e0b0053eb9ae37f4a8adc95de
7
- data.tar.gz: d63d7b19dae7e63d24d931a64ebebc2bfa904f1632ec30a7a07734e8325edb9a1db4441bcc7acce17826ecd02de1208282405a8c94894bcc464e0fe4231be5c4
6
+ metadata.gz: be0c30b00af02d00386ee6c36191e7955e02a0154002c6835bd5c5d739460124775d01bb24515a1f81e135ea75ad9e591b76dc5941857368e1ccb4914d9e6b2e
7
+ data.tar.gz: f7adcb761bacb26fba80d79f6602b7d18b4c591f35aaa9feb0fec76b50669b13a03828fcdb2f9052ffaab56ea013dc1e8e39c46585da6389790df0eb602073fc
checksums.yaml.gz.sig CHANGED
Binary file
data/lib/htmlcom.rb CHANGED
@@ -22,6 +22,7 @@ require 'rexle-builder'
22
22
  # InputElement # used by the HTML input element
23
23
  # Dropdown # an HTML element
24
24
  # Textarea # an HTML element
25
+ # FormBuilder
25
26
 
26
27
  module HtmlCom
27
28
 
@@ -332,20 +333,36 @@ EOF
332
333
 
333
334
  class Element
334
335
 
335
- def initialize(h)
336
- @doc = build(h)
336
+ def initialize(obj)
337
+
338
+ @doc = build(obj)
339
+ puts '@doc.xml : ' + @doc.xml.inspect
340
+ if @id then
341
+ elem = @doc.root.element(@tag + '/' + @htmltag)
342
+ puts 'elem: ' +elem.inspect
343
+ elem.attributes[:name] = @id
344
+ elem.attributes[:id] = @id
345
+ end
346
+
347
+ end
348
+
349
+ def html_element()
350
+ @doc.root.element(@tag + '/' + @htmltag)
337
351
  end
338
352
 
339
353
  def to_doc()
340
354
  @doc
341
355
  end
342
356
 
343
- def build(h)
357
+ def to_html()
358
+ html_element.xml pretty: true
359
+ end
344
360
 
345
- puts 'h: ' + h.inspect
346
- doc = Rexle.new(RexleBuilder.new(h).to_a)
347
- puts doc.xml pretty: true
348
- doc
361
+ private
362
+
363
+ def build(rawobj)
364
+ obj = rawobj.is_a?(Hash) ? RexleBuilder.new(rawobj).to_a : rawobj
365
+ Rexle.new(obj)
349
366
  end
350
367
 
351
368
  end
@@ -364,12 +381,6 @@ EOF
364
381
  super({@tag.to_sym => rawobj})
365
382
  end
366
383
 
367
- if @id then
368
- elem = @doc.root.element(@tag + '/' + @htmltag)
369
- elem.attributes[:name] = @id
370
- elem.attributes[:id] = @id
371
- end
372
-
373
384
  end
374
385
 
375
386
  end
@@ -388,11 +399,15 @@ EOF
388
399
  list = options.map {|option| {option: option}}
389
400
  super({_select: list})
390
401
 
402
+ values = options.map(&:downcase)
403
+ @doc.root.xpath('dropdown/select/option').each.with_index do |e,i|
404
+ e.attributes[:value] = values[i]
405
+ end
406
+
391
407
  elsif options.is_a? Hash then
392
408
 
393
409
  list = options.values.map {|option| {option: option}}
394
410
  values = options.keys
395
- puts 'list: ' + list.inspect
396
411
  super({_select: list})
397
412
 
398
413
  @doc.root.xpath('dropdown/select/option').each.with_index do |e,i|
@@ -418,4 +433,127 @@ EOF
418
433
 
419
434
  end
420
435
 
436
+
437
+ class Text < InputElement
438
+
439
+ def initialize(value='', text: value, label: nil, id: nil)
440
+
441
+ @tag = 'text'
442
+ @htmltag = 'input'
443
+ @id = id
444
+ @label = label
445
+ super( [@htmltag, {type: 'text', value: text}])
446
+
447
+ end
448
+
449
+ end
450
+
451
+
452
+ class Hidden < InputElement
453
+
454
+ def initialize(value='', text: value, id: nil, label: nil)
455
+
456
+ @tag = 'hidden'
457
+ @htmltag = 'input'
458
+ @id = id
459
+ super( [@htmltag, {type: 'hidden', value: text}])
460
+
461
+ end
462
+
463
+ end
464
+
465
+ class Form < Element
466
+
467
+ def initialize(inputs=nil, id: nil, method: :get, action: '')
468
+
469
+ @tag = 'form'
470
+ @htmltag = 'form'
471
+ @id = id
472
+
473
+ super([:root, {}, [@tag,{}, [@htmltag, {}]]])
474
+ form = @doc.root.element(@tag + '/' + @htmltag)
475
+ form.add inputs if inputs
476
+ form.attributes[:method] = method.to_s
477
+ form.attributes[:action] = action
478
+
479
+ end
480
+
481
+ def add_submit()
482
+ submit = Rexle.new(['input', {type: 'submit', value: 'Submit'}])
483
+ html_element().add submit
484
+ end
485
+
486
+ end
487
+
488
+
489
+ # e.g. inputs = {txt: [:textarea], audiotype: [:dropdown, 'gsm'], voice: [:dropdown, 'Stuart']}
490
+ # options = {audiotype: ['gsm', 'wav'], voice: %w(Stuart Kirsty Andrew)}
491
+ #
492
+ # fb = HtmlCom::FormBuilder.new(inputs: inputs, options: options)
493
+ # puts fb.to_html
494
+
495
+ class FormBuilder
496
+
497
+ def initialize(inputs: {}, options: {}, id: 'form1', method: :get, action: '', debug: false)
498
+
499
+ @debug = debug
500
+
501
+ h = inputs.map do |key, value|
502
+
503
+ if debug then
504
+ puts 'id: ' + key.inspect
505
+ puts 'klass: ' + value.first.inspect
506
+ end
507
+
508
+ [key, value]
509
+ end.to_h
510
+
511
+ options.each do |key, value|
512
+ h[key] << value
513
+ end
514
+
515
+ klass = {
516
+ dropdown: HtmlCom::Dropdown,
517
+ textarea: HtmlCom::Textarea,
518
+ text: HtmlCom::Text,
519
+ hidden: HtmlCom::Hidden
520
+ }
521
+
522
+ @form = HtmlCom::Form.new(id: id, method: method, action: action)
523
+
524
+ h.each do |key, value|
525
+
526
+ id = key
527
+ puts 'value: ' + value.inspect
528
+ type, content = value
529
+ action = case type
530
+ when :dropdown
531
+ content = value.last
532
+ 'Select'
533
+ else
534
+ 'Enter'
535
+ end
536
+
537
+ obj = klass[type].new content, id: id, label: action + ' ' + id.to_s
538
+
539
+ obj.to_doc.root.element(type.to_s).elements.each do |e|
540
+ @form.html_element.add e
541
+ end
542
+
543
+ end
544
+
545
+ @form.add_submit
546
+
547
+ end
548
+
549
+ def to_doc()
550
+ @form.to_doc
551
+ end
552
+
553
+ def to_html()
554
+ @form.to_html
555
+ end
556
+
557
+ end
558
+
421
559
  end
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: htmlcom
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Robertson
@@ -36,7 +36,7 @@ cert_chain:
36
36
  JNdZs/7/k4vQlgqYQ3HnoA6GgIanmCgNkediqllxv/epLyIkW2DSJGcYXM8QjSEk
37
37
  +4RQ8UHDCOWHpFPlT5Pgc0wPc7KyXosRYHg=
38
38
  -----END CERTIFICATE-----
39
- date: 2022-07-05 00:00:00.000000000 Z
39
+ date: 2022-07-18 00:00:00.000000000 Z
40
40
  dependencies:
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: jsmenubuilder
metadata.gz.sig CHANGED
Binary file