eim_xml 0.0.3 → 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,135 +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_ < Simple_; end
35
-
36
- class HEAD < Simple_; end
37
- class META < Simple_; end
38
- class LINK < Simple_; end
39
- class STYLE < PreserveSpace_; end
40
- class SCRIPT < PreserveSpace_; end
41
- class TITLE < Simple_; end
42
- class BODY < Simple_; end
43
- class PRE < PreserveSpace_; end
44
- class FORM < Simple_
45
- def initialize(attributes={})
46
- if attributes
47
- if s = attributes.delete(:session)
48
- name = attributes.delete(:session_name) || "token"
49
- require "digest/sha1"
50
- token = s[name] ||= Digest::SHA1.hexdigest("#{$$}#{Time.now}#{rand}")
51
- end
52
- end
53
- super
54
- add(HIDDEN.new(:name=>name, :value=>token)) if token
55
- end
56
- end
57
- class H1 < PreserveSpace_; end
58
- class H2 < PreserveSpace_; end
59
- class H3 < PreserveSpace_; end
60
- class H4 < PreserveSpace_; end
61
- class H5 < PreserveSpace_; end
62
- class H6 < PreserveSpace_; end
63
- class P < PreserveSpace_; end
64
- class A < PreserveSpace_; end
65
- class EM < PreserveSpace_; end
66
- class STRONG < PreserveSpace_; end
67
- class DIV < Simple_; end
68
- class SPAN < PreserveSpace_; end
69
- class UL < Simple_; end
70
- class OL < Simple_; end
71
- class LI < PreserveSpace_; end
72
- class DL < Simple_; end
73
- class DT < PreserveSpace_; end
74
- class DD < PreserveSpace_; end
75
- class TABLE < Simple_; end
76
- class CAPTION < PreserveSpace_; end
77
- class TR < Simple_; end
78
- class TH < PreserveSpace_; end
79
- class TD < PreserveSpace_; end
80
- class BR < Simple_; end
81
- class HR < Simple_; end
82
-
83
- module Hn
84
- def self.new(level, attr={}, &proc)
85
- raise ArgumentError unless 1<=level && level<=6
86
- klass = EimXML::XHTML.const_get("H#{level}")
87
- klass.new(attr, &proc)
88
- end
89
- end
90
-
91
- class TEXTAREA < PreserveSpace_; end
92
-
93
- class INPUT < Base_
94
- def initialize(opt={})
95
- super(:input, opt)
96
- end
97
- end
98
-
99
- class BUTTON < Base_
100
- def initialize(opt={})
101
- super(:button, opt)
102
- end
103
- end
104
-
105
- class SUBMIT < BUTTON
106
- def initialize(opt={})
107
- super(opt.merge(:type=>:submit))
108
- end
109
- end
110
-
111
- class HIDDEN < INPUT
112
- def initialize(opt={})
113
- super(opt.merge(:type=>:hidden))
114
- end
115
- end
116
-
117
- class TEXT < INPUT
118
- def initialize(opt={})
119
- super(opt.merge(:type=>:text))
120
- end
121
- end
122
-
123
- class PASSWORD < INPUT
124
- def initialize(opt={})
125
- super(opt.merge(:type=>:password))
126
- end
127
- end
128
-
129
- PRESERVE_SPACES = [PreserveSpace_]
130
- class Formatter < EimXML::Formatter
131
- def self.write(element, opt={})
132
- EimXML::Formatter.write(element, opt.merge(:preservers=>PRESERVE_SPACES))
133
- end
134
- 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
135
157
  end