eim_xml 0.0.4 → 1.0.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/eim_xml/xhtml.rb CHANGED
@@ -1,153 +1,157 @@
1
- require "eim_xml"
2
- require "eim_xml/formatter"
3
-
4
- module EimXML::XHTML
5
- module DocType
6
- XHTML_MATHML = %[<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 plus MathML 2.0//EN" "http://www.w3.org/TR/MathML2/dtd/xhtml-math11-f.dtd">]
7
- end
8
-
9
- class Base_ < EimXML::Element
10
- end
11
-
12
- class HTML < Base_
13
- attr_accessor :prefix
14
- module NameSpace
15
- XHTML = "http://www.w3.org/1999/xhtml"
16
- end
17
-
18
- def initialize(attributes={})
19
- super(:html, attributes)
20
- end
21
-
22
- def write_to(out="")
23
- out << @prefix << "\n" if @prefix
24
- super
25
- end
26
- end
27
-
28
- class Simple_ < Base_
29
- def initialize(attributes={})
30
- super(self.class.name[/.*::(.*)/, 1].downcase.to_sym, attributes)
31
- end
32
- end
33
-
34
- class PreserveSpace_ < Base_
35
- def initialize(name={}, attributes={})
36
- if name.is_a?(Hash)
37
- super(self.class.name[/.*::(.*)/, 1].downcase.to_sym, name)
38
- else
39
- super(name, attributes)
40
- end
41
- end
42
- end
43
-
44
- class HEAD < Simple_; end
45
- class META < Simple_; end
46
- class LINK < Simple_; end
47
- class IMG < Simple_; end
48
- class STYLE < PreserveSpace_; end
49
- class SCRIPT < PreserveSpace_; end
50
- class TITLE < Simple_; end
51
- class BODY < Simple_; end
52
- class PRE < PreserveSpace_; end
53
- class FORM < Simple_
54
- def initialize(attributes={})
55
- if attributes
56
- if s = attributes.delete(:session)
57
- name = attributes.delete(:session_name) || "token"
58
- require "digest/sha1"
59
- token = s[name] ||= Digest::SHA1.hexdigest("#{$$}#{Time.now}#{rand}")
60
- end
61
- end
62
- super
63
- add(HIDDEN.new(:name=>name, :value=>token)) if token
64
- end
65
- end
66
- class H1 < PreserveSpace_; end
67
- class H2 < PreserveSpace_; end
68
- class H3 < PreserveSpace_; end
69
- class H4 < PreserveSpace_; end
70
- class H5 < PreserveSpace_; end
71
- class H6 < PreserveSpace_; end
72
- class P < PreserveSpace_; end
73
- class A < PreserveSpace_; end
74
- class EM < PreserveSpace_; end
75
- class STRONG < PreserveSpace_; end
76
- class DIV < Simple_; end
77
- class SPAN < PreserveSpace_; end
78
- class UL < Simple_; end
79
- class OL < Simple_; end
80
- class LI < PreserveSpace_; end
81
- class DL < Simple_; end
82
- class DT < PreserveSpace_; end
83
- class DD < PreserveSpace_; end
84
- class TABLE < Simple_; end
85
- class CAPTION < PreserveSpace_; end
86
- class TR < Simple_; end
87
- class TH < PreserveSpace_; end
88
- class TD < PreserveSpace_; end
89
- class BR < Simple_; end
90
- class HR < Simple_; end
91
- class SELECT < Simple_; end
92
- class OPTION < Simple_; end
93
-
94
-
95
- module Hn
96
- def self.new(level, attr={}, &proc)
97
- raise ArgumentError unless 1<=level && level<=6
98
- klass = EimXML::XHTML.const_get("H#{level}")
99
- klass.new(attr, &proc)
100
- end
101
- end
102
-
103
- class TEXTAREA < PreserveSpace_; end
104
-
105
- class INPUT < Base_
106
- def initialize(opt={})
107
- super(:input, opt)
108
- end
109
- end
110
-
111
- class BUTTON < PreserveSpace_
112
- def initialize(opt={})
113
- super(:button, opt)
114
- end
115
- end
116
-
117
- class SUBMIT < BUTTON
118
- def initialize(opt={})
119
- super(opt.merge(:type=>:submit))
120
- end
121
- end
122
-
123
- class HIDDEN < INPUT
124
- def initialize(opt={})
125
- super(opt.merge(:type=>:hidden))
126
- end
127
- end
128
-
129
- class TEXT < INPUT
130
- def initialize(opt={})
131
- super(opt.merge(:type=>:text))
132
- end
133
- end
134
-
135
- class PASSWORD < INPUT
136
- def initialize(opt={})
137
- super(opt.merge(:type=>:password))
138
- end
139
- end
140
-
141
- class FILE < INPUT
142
- def initialize(opt={})
143
- super(opt.merge(:type=>:file))
144
- end
145
- end
146
-
147
- PRESERVE_SPACES = [PreserveSpace_]
148
- class Formatter < EimXML::Formatter
149
- def self.write(element, opt={})
150
- EimXML::Formatter.write(element, opt.merge(:preservers=>PRESERVE_SPACES))
151
- end
152
- end
1
+ require 'English'
2
+ require 'eim_xml'
3
+ require 'eim_xml/formatter'
4
+
5
+ module EimXML
6
+ module XHTML
7
+ module DocType
8
+ XHTML_MATHML = %(<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 plus MathML 2.0//EN" "http://www.w3.org/Math/DTD/mathml2/xhtml-math11-f.dtd">)
9
+ end
10
+
11
+ class Base < EimXML::Element
12
+ end
13
+
14
+ class HTML < Base
15
+ attr_accessor :prefix
16
+
17
+ module NameSpace
18
+ XHTML = 'http://www.w3.org/1999/xhtml'
19
+ end
20
+
21
+ def initialize(attributes = {})
22
+ super(:html, attributes)
23
+ end
24
+
25
+ def write_to(out = '')
26
+ out << @prefix << "\n" if @prefix
27
+ super
28
+ end
29
+ end
30
+
31
+ class Simple < Base
32
+ def initialize(attributes = {})
33
+ super(self.class.name[/.*::(.*)/, 1].downcase.to_sym, attributes)
34
+ end
35
+ end
36
+
37
+ class PreserveSpace < Base
38
+ def initialize(name = {}, attributes = {})
39
+ if name.is_a?(Hash)
40
+ super(self.class.name[/.*::(.*)/, 1].downcase.to_sym, name)
41
+ else
42
+ super
43
+ end
44
+ end
45
+ end
46
+
47
+ class HEAD < Simple; end
48
+ class META < Simple; end
49
+ class LINK < Simple; end
50
+ class IMG < Simple; end
51
+ class STYLE < PreserveSpace; end
52
+ class SCRIPT < PreserveSpace; end
53
+ class TITLE < Simple; end
54
+ class BODY < Simple; end
55
+ class PRE < PreserveSpace; end
56
+
57
+ class FORM < Simple
58
+ def initialize(attributes = {})
59
+ if attributes && (s = attributes.delete(:session))
60
+ name = attributes.delete(:session_name) || 'token'
61
+ require 'digest/sha1'
62
+ token = s[name] ||= Digest::SHA1.hexdigest("#{$PID}#{Time.now}#{rand}")
63
+ end
64
+ super
65
+ add(HIDDEN.new(name:, value: token)) if token
66
+ end
67
+ end
68
+
69
+ class H1 < PreserveSpace; end
70
+ class H2 < PreserveSpace; end
71
+ class H3 < PreserveSpace; end
72
+ class H4 < PreserveSpace; end
73
+ class H5 < PreserveSpace; end
74
+ class H6 < PreserveSpace; end
75
+ class P < PreserveSpace; end
76
+ class A < PreserveSpace; end
77
+ class EM < PreserveSpace; end
78
+ class STRONG < PreserveSpace; end
79
+ class DIV < Simple; end
80
+ class SPAN < PreserveSpace; end
81
+ class UL < Simple; end
82
+ class OL < Simple; end
83
+ class LI < PreserveSpace; end
84
+ class DL < Simple; end
85
+ class DT < PreserveSpace; end
86
+ class DD < PreserveSpace; end
87
+ class TABLE < Simple; end
88
+ class CAPTION < PreserveSpace; end
89
+ class TR < Simple; end
90
+ class TH < PreserveSpace; end
91
+ class TD < PreserveSpace; end
92
+ class BR < Simple; end
93
+ class HR < Simple; end
94
+ class SELECT < Simple; end
95
+ class OPTION < Simple; end
96
+
97
+ module Hn
98
+ def self.new(level, attr = {}, &)
99
+ raise ArgumentError unless 1 <= level && level <= 6
100
+
101
+ klass = EimXML::XHTML.const_get("H#{level}")
102
+ klass.new(attr, &)
103
+ end
104
+ end
105
+
106
+ class TEXTAREA < PreserveSpace; end
107
+
108
+ class INPUT < Base
109
+ def initialize(opt = {})
110
+ super(:input, opt)
111
+ end
112
+ end
113
+
114
+ class BUTTON < PreserveSpace
115
+ def initialize(opt = {})
116
+ super(:button, opt)
117
+ end
118
+ end
119
+
120
+ class SUBMIT < BUTTON
121
+ def initialize(opt = {})
122
+ super(opt.merge(type: :submit))
123
+ end
124
+ end
125
+
126
+ class HIDDEN < INPUT
127
+ def initialize(opt = {})
128
+ super(opt.merge(type: :hidden))
129
+ end
130
+ end
131
+
132
+ class TEXT < INPUT
133
+ def initialize(opt = {})
134
+ super(opt.merge(type: :text))
135
+ end
136
+ end
137
+
138
+ class PASSWORD < INPUT
139
+ def initialize(opt = {})
140
+ super(opt.merge(type: :password))
141
+ end
142
+ end
143
+
144
+ class FILE < INPUT
145
+ def initialize(opt = {})
146
+ super(opt.merge(type: :file))
147
+ end
148
+ end
149
+
150
+ PRESERVE_SPACES = [PreserveSpace]
151
+ class Formatter < EimXML::Formatter
152
+ def self.write(element, opt = {})
153
+ EimXML::Formatter.write(element, **opt.merge(preservers: PRESERVE_SPACES))
154
+ end
155
+ end
156
+ end
153
157
  end