xmlscan 0.3.0prea → 0.3.0preb

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.0prea
1
+ 0.3.0preb
@@ -13,7 +13,9 @@ module XMLScan
13
13
  MY_METHODS = XMLScan::Visitor.instance_methods.to_a - SKIP
14
14
 
15
15
  def initialize(opts={}, mod=nil)
16
- (mod ? MY_METHODS - mod.instance_methods : MY_METHODS).each do |i|
16
+ raise "No module" unless mod
17
+ STDERR << "init Element Processer #{mod}\n"
18
+ (MY_METHODS - mod.instance_methods).each do |i|
17
19
  self.class.class_eval %{def #{i}(d, *a) d&&(@out << d) end}, __FILE__, __LINE__
18
20
  end
19
21
  self.class.send :include, mod
@@ -35,13 +37,73 @@ module XMLScan
35
37
  class XMLProcessor
36
38
  include ElementProcessor
37
39
 
38
- def self.process(file, opts={}, mod=nil)
39
- raise "Not readable #{file.inspect}" unless IO===( io =
40
- IO===file ? file : open(file) )
40
+ def self.process(io, opts={}, mod=nil)
41
+ mod ||= ElementProcessing
42
+ STDERR << "process #{io.inspect}, #{opts.inspect}\n"
43
+ io = case io
44
+ when String; open(io)
45
+ when IO, StringIO; io
46
+ else raise "bad type file input #{io.inspect}"
47
+ end
48
+
41
49
  visitor = new(opts, mod)
42
50
  visitor.parser.parse(io)
43
51
  visitor.pairs
44
52
  end
45
53
  end
46
54
 
55
+
56
+ module ElementProcessing
57
+ def on_chardata(s) @out << s end
58
+ def on_stag_end(name, s, h, *a)
59
+ if name.to_sym == @element
60
+ # starting a new context, first output our substitute string
61
+ key= h&&h[@key.to_s]||'*no-name*'
62
+ @tmpl = ":transclude|{{:name}}" # def: "{{:key}}"
63
+ STDERR << "templ #{@tmpl.inspect}\n"
64
+ #STDERR << "x> #{x.inspect}, #{h.inspect}, #{(!(/:\w[\w\d]*/ =~ x)) || h[$&[1..-1].to_s] }\n"
65
+ sub =
66
+ @tmpl.split('|').find {|x| !(/:\w[\w\d]*/ =~ x) ||
67
+ h[$&[1..-1].to_s] }.gsub(/:\w[\w\d]*/) {|m|
68
+ STDERR << "templ sub match #{m.inspect}, #{h[m[1..-1]]}\n"
69
+ h[m[1..-1]] }
70
+ #sub = h['transclude'] || "{{#{key}}}"
71
+ @out << sub
72
+ # then push the current context and initialize this one
73
+ @stack.push([@context, @out, *@ex])
74
+ @context = key; @out = []
75
+ @ex = @extras.map {|e| h[e]}
76
+ else @out << s end # pass through tags we aren't processing
77
+ end
78
+
79
+ def on_etag(name, s=nil)
80
+ if name.to_sym == @element
81
+ # output a card (name, content, type)
82
+ @pairs << [@context, @out, @stack[-1][0], *@ex]
83
+ # restore previous context from stack
84
+ last = @stack.pop
85
+ @context, @out, @ex = last.shift, last.shift, *last
86
+ else @out << s end
87
+ end
88
+
89
+ def on_stag_empty_end(name, s=nil, h={}, *a)
90
+ if name.to_sym == @element
91
+
92
+ key= h&&h[@key.to_s]||'*no-name*'
93
+ ex = @extras.map {|e| h[e]}
94
+ @pairs << [key, [], @context, *ex]
95
+ else @out << s end
96
+ end
97
+
98
+ attr_reader :pairs, :parser
99
+ end
100
+
101
+ end
102
+ ARGV.each do |a|
103
+ pairs = XMLScan::XMLProcessor.process(a, {:key=>:name, :element=>:card, :extras=>[:type]}, XMLScan::ElementProcessing)
104
+ STDOUT << "Result\n"
105
+ STDOUT << pairs.map do |p| n,o,c,t = p
106
+ "#{c&&c.size>0&&"#{c}::"||''}#{n}#{t&&"[#{t}]"}=>#{o*''}"
107
+ end * "\n"
108
+ STDOUT << "\nDone\n"
47
109
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: xmlscan
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0prea
4
+ version: 0.3.0preb
5
5
  prerelease: 5
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-02-16 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &9220620 !ruby/object:Gem::Requirement
16
+ requirement: &8986000 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 2.8.0
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *9220620
24
+ version_requirements: *8986000
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rdoc
27
- requirement: &9220120 !ruby/object:Gem::Requirement
27
+ requirement: &8985420 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '3.12'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *9220120
35
+ version_requirements: *8985420
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: bundler
38
- requirement: &9219620 !ruby/object:Gem::Requirement
38
+ requirement: &8984920 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: 1.0.0
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *9219620
46
+ version_requirements: *8984920
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: jeweler
49
- requirement: &9219060 !ruby/object:Gem::Requirement
49
+ requirement: &8984420 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ~>
@@ -54,7 +54,7 @@ dependencies:
54
54
  version: 1.8.3
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *9219060
57
+ version_requirements: *8984420
58
58
  description: The fastest XML parser written in 100% pure Ruby.
59
59
  email: gerryg@inbox.com
60
60
  executables: []
@@ -65,12 +65,10 @@ files:
65
65
  - ChangeLog
66
66
  - Gemfile
67
67
  - Gemfile.lock
68
- - README.processor
69
68
  - README.rdoc
70
69
  - Rakefile
71
70
  - THANKS
72
71
  - VERSION
73
- - install.rb
74
72
  - lib/xmlscan/htmlscan.rb
75
73
  - lib/xmlscan/namespace.rb
76
74
  - lib/xmlscan/parser.rb
@@ -79,8 +77,6 @@ files:
79
77
  - lib/xmlscan/version.rb
80
78
  - lib/xmlscan/visitor.rb
81
79
  - lib/xmlscan/xmlchar.rb
82
- - test.rb
83
- - xmlcard.rb
84
80
  homepage: http://github.com/GerryG/xmlformat/
85
81
  licenses:
86
82
  - MIT
@@ -100,7 +96,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
100
96
  version: '0'
101
97
  segments:
102
98
  - 0
103
- hash: -1617079850723236327
99
+ hash: 1414227114206674686
104
100
  required_rubygems_version: !ruby/object:Gem::Requirement
105
101
  none: false
106
102
  requirements:
data/README.processor DELETED
@@ -1,33 +0,0 @@
1
-
2
- processor = XMLScan::Processor.hash(:element=>:card, :key=>:name) do |k,h,s,|
3
- h[:transclude] || "{{#{h[:name]}}}"
4
- end
5
-
6
- test_cases [
7
- [ '<card name="foo" transclude="{{foo|titled}}">Some
8
- <card name="name">Name data</card> and &lt; &gt;
9
- <p>para data<b>bold</b>
10
- </p><br/>
11
- more<card
12
- name="+hello" attr="&quote;foo&quote;"> and <card name="+nested">nested twice data</card>
13
- </card>
14
- </card>
15
- ', {
16
- 'foo' => 'Some
17
- {{name}} and &lt; &gt;
18
- <p>para data<b>bold</b>
19
- </p><br/>
20
- more{{+hello}}
21
- ',
22
- 'name' => 'Name data',
23
- 'foo+hello' => ' and {{+nested}}
24
- ',
25
- 'foo+hello+nested' => 'nested twice data' } ],
26
- ]
27
-
28
- test_casts.each { |p|
29
- assert processor.call(p[0]) == p[1]
30
- }
31
-
32
-
33
-
data/install.rb DELETED
@@ -1,41 +0,0 @@
1
- #!/usr/bin/ruby
2
- #
3
- # install.rb
4
- #
5
- # $Id: install.rb,v 1.2 2002/12/26 21:09:38 katsu Exp $
6
-
7
- require 'rbconfig'
8
- require 'ftools'
9
- require 'find'
10
- require 'getoptlong'
11
-
12
- DEFAULT_DESTDIR = Config::CONFIG['sitelibdir'] || Config::CONFIG['sitedir']
13
- SRCDIR = File.dirname(__FILE__)
14
-
15
-
16
- def install_rb(from, to)
17
- from = SRCDIR + '/' + from
18
- Find.find(from) { |src|
19
- next unless File.file? src
20
- next unless /\.rb\z/ =~ src
21
- dst = src.sub(/\A#{Regexp.escape(from)}/, to)
22
- File.makedirs File.dirname(dst), true
23
- File.install src, dst, 0644, true
24
- }
25
- end
26
-
27
-
28
- destdir = DEFAULT_DESTDIR
29
- begin
30
- GetoptLong.new([ "-d", "--destdir", GetoptLong::REQUIRED_ARGUMENT ]
31
- ).each_option { |opt, arg|
32
- case opt
33
- when '-d' then
34
- destdir = arg
35
- end
36
- }
37
- rescue
38
- exit 2
39
- end
40
-
41
- install_rb "lib", destdir
data/test.rb DELETED
@@ -1,7 +0,0 @@
1
- #!/usr/bin/ruby
2
- $-w = true
3
- $LOAD_PATH.unshift 'lib'
4
- $LOAD_PATH.unshift 'tests'
5
- Dir.chdir File.dirname($0)
6
- require 'testall'
7
- load 'runtest.rb'
data/xmlcard.rb DELETED
@@ -1,48 +0,0 @@
1
- # encoding: UTF-8
2
-
3
- require 'xmlscan/processor'
4
-
5
- # need to make these into supplied blocks somehome
6
- module CustomProcessing
7
- def on_chardata(s) @out << s end
8
- def on_stag_end(name, s, h, *a)
9
- if name.to_sym == @element
10
- # starting a new context, first output our substitute string
11
- key= h&&h[@key.to_s]||'*no-name*'
12
- sub = h['transclude'] || "{{#{key}}}"
13
- @out << sub
14
- # then push the current context and initialize this one
15
- @stack.push([@context, @out, *@ex])
16
- @context = key; @out = []
17
- @ex = @extras.map {|e| h[e]}
18
- else @out << s end # pass through tags we aren't processing
19
- end
20
-
21
- def on_etag(name, s=nil)
22
- if name.to_sym == @element
23
- # output a card (name, content, type)
24
- @pairs << [@context, @out, @stack[-1][0], *@ex]
25
- # restore previous context from stack
26
- last = @stack.pop
27
- @context, @out, @ex = last.shift, last.shift, *last
28
- else @out << s end
29
- end
30
-
31
- def on_stag_empty_end(name, s=nil, h={}, *a)
32
- if name.to_sym == @element
33
- # I don't think we have this case, but it is simple to add later
34
- STDERR << "empty card ???: #{name}, #{s}, #{h.inspect}\n"
35
- else @out << s end
36
- end
37
-
38
- attr_reader :pairs, :parser
39
- end
40
-
41
- ARGV.each do |a|
42
- pairs = XMLScan::XMLProcessor.process(a, {:key=>:name, :element=>:card, :extras=>[:type]}, CustomProcessing)
43
- STDOUT << "Result\n"
44
- STDOUT << pairs.map do |p| n,o,c,t = p
45
- "#{c&&c.size>0&&"#{c}::"||''}#{n}#{t&&"[#{t}]"}=>#{o*''}"
46
- end * "\n"
47
- STDOUT << "\nDone\n"
48
- end