rowx 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/rowx.rb +74 -0
  2. metadata +77 -0
data/lib/rowx.rb ADDED
@@ -0,0 +1,74 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # file: rowx.rb
4
+
5
+ require 'line-tree'
6
+ require 'rexle'
7
+
8
+ class Array
9
+ def collate(pattern=nil)
10
+ a = self.inject([[]]) do |r,x|
11
+ if block_given? then
12
+ yield(x) ? r << [x] : r[-1] << x
13
+ else
14
+ x =~ pattern ? r << [x] : r[-1] << x
15
+ end # block
16
+ r
17
+ end
18
+ a.shift if a.first.empty?
19
+ a
20
+ end
21
+ end
22
+
23
+ class RowX
24
+
25
+ attr_reader :to_a, :to_xml
26
+
27
+ def initialize(txt)
28
+
29
+ a = LineTree.new(txt).to_a
30
+
31
+ keyfield = a[0][0][/\w+/]; i = 0
32
+
33
+ while a.select {|x| x[0][/#{keyfield}/]}.length <= 1 and i < a.length
34
+ i += 1
35
+ keyfield = a[i][0][/\w+/]
36
+ end
37
+
38
+ keyfield = a[0][0][/\w+/] if i == a.length - 1
39
+ records = a[i..-1].collate { |x| x.first =~ /^#{keyfield }/ }
40
+ summary = scan_a a.slice!(0,i)
41
+
42
+ summary[0] = 'summary'
43
+ @to_a = ['root', '', {}] + [summary] + scan_records(records)
44
+ @to_xml = Rexle.new(@to_a).xml pretty: true
45
+
46
+ end
47
+
48
+ private
49
+
50
+ def scan_a(row)
51
+
52
+ a = row.map do |field|
53
+
54
+ s = field.is_a?(Array) ? field[0] : field
55
+ value, name = s.split(':',2).reverse
56
+ name ||= 'description'
57
+
58
+ children = scan_a(field[1..-1]) if field[-1] .is_a?(Array)
59
+ value = value.to_s.strip
60
+ value = ['![', value, {}] if value[/[<>]/]
61
+
62
+ result = [name, value, {}]
63
+ result << children if children
64
+ result
65
+ end
66
+
67
+ ['item', '', {}] + a
68
+ end
69
+
70
+ def scan_records(row)
71
+ row.map {|x| scan_a x }
72
+ end
73
+
74
+ end
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rowx
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.0
6
+ platform: ruby
7
+ authors: []
8
+
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-04-22 00:00:00 +01:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: line-tree
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: "0"
25
+ type: :runtime
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: rexle
29
+ prerelease: false
30
+ requirement: &id002 !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: "0"
36
+ type: :runtime
37
+ version_requirements: *id002
38
+ description: Generates XML from rows of labelled, nested, and plain text
39
+ email:
40
+ executables: []
41
+
42
+ extensions: []
43
+
44
+ extra_rdoc_files: []
45
+
46
+ files:
47
+ - lib/rowx.rb
48
+ has_rdoc: true
49
+ homepage:
50
+ licenses: []
51
+
52
+ post_install_message:
53
+ rdoc_options: []
54
+
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: "0"
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: "0"
69
+ requirements: []
70
+
71
+ rubyforge_project:
72
+ rubygems_version: 1.5.2
73
+ signing_key:
74
+ specification_version: 3
75
+ summary: rowx
76
+ test_files: []
77
+