clarion-builder 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.
@@ -0,0 +1,63 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ #--
4
+ # Copyright 2004 by Jim Weirich (jim@weirichhouse.org).
5
+ # All rights reserved.
6
+
7
+ # Permission is granted for use, copying, modification, distribution,
8
+ # and distribution of modified versions of this work as long as the
9
+ # above copyright notice is included.
10
+ #++
11
+
12
+ require 'builder/xmlmarkup'
13
+
14
+ module Builder
15
+
16
+ # Create a series of SAX-like XML events (e.g. start_tag, end_tag)
17
+ # from the markup code. XmlEvent objects are used in a way similar
18
+ # to XmlMarkup objects, except that a series of events are generated
19
+ # and passed to a handler rather than generating character-based
20
+ # markup.
21
+ #
22
+ # Usage:
23
+ # xe = Builder::XmlEvents.new(hander)
24
+ # xe.title("HI") # Sends start_tag/end_tag/text messages to the handler.
25
+ #
26
+ # Indentation may also be selected by providing value for the
27
+ # indentation size and initial indentation level.
28
+ #
29
+ # xe = Builder::XmlEvents.new(handler, indent_size, initial_indent_level)
30
+ #
31
+ # == XML Event Handler
32
+ #
33
+ # The handler object must expect the following events.
34
+ #
35
+ # [<tt>start_tag(tag, attrs)</tt>]
36
+ # Announces that a new tag has been found. +tag+ is the name of
37
+ # the tag and +attrs+ is a hash of attributes for the tag.
38
+ #
39
+ # [<tt>end_tag(tag)</tt>]
40
+ # Announces that an end tag for +tag+ has been found.
41
+ #
42
+ # [<tt>text(text)</tt>]
43
+ # Announces that a string of characters (+text+) has been found.
44
+ # A series of characters may be broken up into more than one
45
+ # +text+ call, so the client cannot assume that a single
46
+ # callback contains all the text data.
47
+ #
48
+ class XmlEvents < XmlMarkup
49
+ def text!(text)
50
+ @target.text(text)
51
+ end
52
+
53
+ def _start_tag(sym, attrs, end_too=false)
54
+ @target.start_tag(sym, attrs)
55
+ _end_tag(sym) if end_too
56
+ end
57
+
58
+ def _end_tag(sym)
59
+ @target.end_tag(sym)
60
+ end
61
+ end
62
+
63
+ end
@@ -0,0 +1,341 @@
1
+ #!/usr/bin/env ruby
2
+ #--
3
+ # Copyright 2004, 2005 by Jim Weirich (jim@weirichhouse.org).
4
+ # All rights reserved.
5
+
6
+ # Permission is granted for use, copying, modification, distribution,
7
+ # and distribution of modified versions of this work as long as the
8
+ # above copyright notice is included.
9
+ #++
10
+
11
+ # Provide a flexible and easy to use Builder for creating XML markup.
12
+ # See XmlBuilder for usage details.
13
+
14
+ require 'builder/xmlbase'
15
+
16
+ module Builder
17
+
18
+ # Create XML markup easily. All (well, almost all) methods sent to
19
+ # an XmlMarkup object will be translated to the equivalent XML
20
+ # markup. Any method with a block will be treated as an XML markup
21
+ # tag with nested markup in the block.
22
+ #
23
+ # Examples will demonstrate this easier than words. In the
24
+ # following, +xm+ is an +XmlMarkup+ object.
25
+ #
26
+ # xm.em("emphasized") # => <em>emphasized</em>
27
+ # xm.em { xm.b("emp & bold") } # => <em><b>emph &amp; bold</b></em>
28
+ # xm.a("A Link", "href"=>"http://onestepback.org")
29
+ # # => <a href="http://onestepback.org">A Link</a>
30
+ # xm.div { xm.br } # => <div><br/></div>
31
+ # xm.target("name"=>"compile", "option"=>"fast")
32
+ # # => <target option="fast" name="compile"\>
33
+ # # NOTE: order of attributes is not specified.
34
+ #
35
+ # xm.instruct! # <?xml version="1.0" encoding="UTF-8"?>
36
+ # xm.html { # <html>
37
+ # xm.head { # <head>
38
+ # xm.title("History") # <title>History</title>
39
+ # } # </head>
40
+ # xm.body { # <body>
41
+ # xm.comment! "HI" # <!-- HI -->
42
+ # xm.h1("Header") # <h1>Header</h1>
43
+ # xm.p("paragraph") # <p>paragraph</p>
44
+ # } # </body>
45
+ # } # </html>
46
+ #
47
+ # == Notes:
48
+ #
49
+ # * The order that attributes are inserted in markup tags is
50
+ # undefined.
51
+ #
52
+ # * Sometimes you wish to insert text without enclosing tags. Use
53
+ # the <tt>text!</tt> method to accomplish this.
54
+ #
55
+ # Example:
56
+ #
57
+ # xm.div { # <div>
58
+ # xm.text! "line"; xm.br # line<br/>
59
+ # xm.text! "another line"; xmbr # another line<br/>
60
+ # } # </div>
61
+ #
62
+ # * The special XML characters <, >, and & are converted to &lt;,
63
+ # &gt; and &amp; automatically. Use the <tt><<</tt> operation to
64
+ # insert text without modification.
65
+ #
66
+ # * Sometimes tags use special characters not allowed in ruby
67
+ # identifiers. Use the <tt>tag!</tt> method to handle these
68
+ # cases.
69
+ #
70
+ # Example:
71
+ #
72
+ # xml.tag!("SOAP:Envelope") { ... }
73
+ #
74
+ # will produce ...
75
+ #
76
+ # <SOAP:Envelope> ... </SOAP:Envelope>"
77
+ #
78
+ # <tt>tag!</tt> will also take text and attribute arguments (after
79
+ # the tag name) like normal markup methods. (But see the next
80
+ # bullet item for a better way to handle XML namespaces).
81
+ #
82
+ # * Direct support for XML namespaces is now available. If the
83
+ # first argument to a tag call is a symbol, it will be joined to
84
+ # the tag to produce a namespace:tag combination. It is easier to
85
+ # show this than describe it.
86
+ #
87
+ # xml.SOAP :Envelope do ... end
88
+ #
89
+ # Just put a space before the colon in a namespace to produce the
90
+ # right form for builder (e.g. "<tt>SOAP:Envelope</tt>" =>
91
+ # "<tt>xml.SOAP :Envelope</tt>")
92
+ #
93
+ # * XmlMarkup builds the markup in any object (called a _target_)
94
+ # that accepts the <tt><<</tt> method. If no target is given,
95
+ # then XmlMarkup defaults to a string target.
96
+ #
97
+ # Examples:
98
+ #
99
+ # xm = Builder::XmlMarkup.new
100
+ # result = xm.title("yada")
101
+ # # result is a string containing the markup.
102
+ #
103
+ # buffer = ""
104
+ # xm = Builder::XmlMarkup.new(buffer)
105
+ # # The markup is appended to buffer (using <<)
106
+ #
107
+ # xm = Builder::XmlMarkup.new(STDOUT)
108
+ # # The markup is written to STDOUT (using <<)
109
+ #
110
+ # xm = Builder::XmlMarkup.new
111
+ # x2 = Builder::XmlMarkup.new(:target=>xm)
112
+ # # Markup written to +x2+ will be send to +xm+.
113
+ #
114
+ # * Indentation is enabled by providing the number of spaces to
115
+ # indent for each level as a second argument to XmlBuilder.new.
116
+ # Initial indentation may be specified using a third parameter.
117
+ #
118
+ # Example:
119
+ #
120
+ # xm = Builder.new(:indent=>2)
121
+ # # xm will produce nicely formatted and indented XML.
122
+ #
123
+ # xm = Builder.new(:indent=>2, :margin=>4)
124
+ # # xm will produce nicely formatted and indented XML with 2
125
+ # # spaces per indent and an over all indentation level of 4.
126
+ #
127
+ # builder = Builder::XmlMarkup.new(:target=>$stdout, :indent=>2)
128
+ # builder.name { |b| b.first("Jim"); b.last("Weirich) }
129
+ # # prints:
130
+ # # <name>
131
+ # # <first>Jim</first>
132
+ # # <last>Weirich</last>
133
+ # # </name>
134
+ #
135
+ # * The instance_eval implementation which forces self to refer to
136
+ # the message receiver as self is now obsolete. We now use normal
137
+ # block calls to execute the markup block. This means that all
138
+ # markup methods must now be explicitly send to the xml builder.
139
+ # For instance, instead of
140
+ #
141
+ # xml.div { strong("text") }
142
+ #
143
+ # you need to write:
144
+ #
145
+ # xml.div { xml.strong("text") }
146
+ #
147
+ # Although more verbose, the subtle change in semantics within the
148
+ # block was found to be prone to error. To make this change a
149
+ # little less cumbersome, the markup block now gets the markup
150
+ # object sent as an argument, allowing you to use a shorter alias
151
+ # within the block.
152
+ #
153
+ # For example:
154
+ #
155
+ # xml_builder = Builder::XmlMarkup.new
156
+ # xml_builder.div { |xml|
157
+ # xml.stong("text")
158
+ # }
159
+ #
160
+ class XmlMarkup < XmlBase
161
+
162
+ # Create an XML markup builder. Parameters are specified by an
163
+ # option hash.
164
+ #
165
+ # :target => <em>target_object</em>::
166
+ # Object receiving the markup. +target_object+ must respond to
167
+ # the <tt><<(<em>a_string</em>)</tt> operator and return
168
+ # itself. The default target is a plain string target.
169
+ #
170
+ # :indent => <em>indentation</em>::
171
+ # Number of spaces used for indentation. The default is no
172
+ # indentation and no line breaks.
173
+ #
174
+ # :margin => <em>initial_indentation_level</em>::
175
+ # Amount of initial indentation (specified in levels, not
176
+ # spaces).
177
+ #
178
+ # :quote => <em>:single</em>::
179
+ # Use single quotes for attributes rather than double quotes.
180
+ #
181
+ # :escape_attrs => <em>OBSOLETE</em>::
182
+ # The :escape_attrs option is no longer supported by builder
183
+ # (and will be quietly ignored). String attribute values are
184
+ # now automatically escaped. If you need unescaped attribute
185
+ # values (perhaps you are using entities in the attribute
186
+ # values), then give the value as a Symbol. This allows much
187
+ # finer control over escaping attribute values.
188
+ #
189
+ def initialize(options={}, **kwargs)
190
+ options = options.merge(kwargs) unless kwargs.empty?
191
+ indent = options[:indent] || 0
192
+ margin = options[:margin] || 0
193
+ @quote = (options[:quote] == :single) ? "'" : '"'
194
+ @explicit_nil_handling = options[:explicit_nil_handling]
195
+ super(indent, margin)
196
+ @target = options[:target] || ""
197
+ end
198
+
199
+ # Return the target of the builder.
200
+ def target!
201
+ @target
202
+ end
203
+
204
+ def comment!(comment_text)
205
+ _ensure_no_block ::Kernel::block_given?
206
+ _special("<!-- ", " -->", comment_text, nil)
207
+ end
208
+
209
+ # Insert an XML declaration into the XML markup.
210
+ #
211
+ # For example:
212
+ #
213
+ # xml.declare! :ELEMENT, :blah, "yada"
214
+ # # => <!ELEMENT blah "yada">
215
+ def declare!(inst, *args, &block)
216
+ _indent
217
+ @target << "<!#{inst}"
218
+ args.each do |arg|
219
+ case arg
220
+ when ::String
221
+ @target << %{ "#{arg}"} # " WART
222
+ when ::Symbol
223
+ @target << " #{arg}"
224
+ end
225
+ end
226
+ if ::Kernel::block_given?
227
+ @target << " ["
228
+ _newline
229
+ _nested_structures(block)
230
+ @target << "]"
231
+ end
232
+ @target << ">"
233
+ _newline
234
+ end
235
+
236
+ # Insert a processing instruction into the XML markup. E.g.
237
+ #
238
+ # For example:
239
+ #
240
+ # xml.instruct!
241
+ # #=> <?xml version="1.0" encoding="UTF-8"?>
242
+ # xml.instruct! :aaa, :bbb=>"ccc"
243
+ # #=> <?aaa bbb="ccc"?>
244
+ #
245
+ # Note: If the encoding is setup to "UTF-8" and the value of
246
+ # $KCODE is "UTF8", then builder will emit UTF-8 encoded strings
247
+ # rather than the entity encoding normally used.
248
+ def instruct!(directive_tag=:xml, attrs={}, **kwargs)
249
+ _ensure_no_block ::Kernel::block_given?
250
+ attrs = attrs.merge(kwargs) unless kwargs.empty?
251
+ if directive_tag == :xml
252
+ a = { :version=>"1.0", :encoding=>"UTF-8" }
253
+ attrs = a.merge attrs
254
+ @encoding = attrs[:encoding].downcase
255
+ end
256
+ _special(
257
+ "<?#{directive_tag}",
258
+ "?>",
259
+ nil,
260
+ attrs,
261
+ [:version, :encoding, :standalone])
262
+ end
263
+
264
+ # Insert a CDATA section into the XML markup.
265
+ #
266
+ # For example:
267
+ #
268
+ # xml.cdata!("text to be included in cdata")
269
+ # #=> <![CDATA[text to be included in cdata]]>
270
+ #
271
+ def cdata!(text)
272
+ _ensure_no_block ::Kernel::block_given?
273
+ _special("<![CDATA[", "]]>", text.gsub(']]>', ']]]]><![CDATA[>'), nil)
274
+ end
275
+
276
+ private
277
+
278
+ # NOTE: All private methods of a builder object are prefixed when
279
+ # a "_" character to avoid possible conflict with XML tag names.
280
+
281
+ # Insert text directly in to the builder's target.
282
+ def _text(text)
283
+ @target << text
284
+ end
285
+
286
+ # Insert special instruction.
287
+ def _special(open, close, data=nil, attrs=nil, order=[])
288
+ _indent
289
+ @target << open
290
+ @target << data if data
291
+ _insert_attributes(attrs, order) if attrs
292
+ @target << close
293
+ _newline
294
+ end
295
+
296
+ # Start an XML tag. If <tt>end_too</tt> is true, then the start
297
+ # tag is also the end tag (e.g. <br/>
298
+ def _start_tag(sym, attrs, end_too=false)
299
+ @target << "<#{sym}"
300
+ _insert_attributes(attrs)
301
+ @target << "/" if end_too
302
+ @target << ">"
303
+ end
304
+
305
+ # Insert an ending tag.
306
+ def _end_tag(sym)
307
+ @target << "</#{sym}>"
308
+ end
309
+
310
+ # Insert the attributes (given in the hash).
311
+ def _insert_attributes(attrs, order=[])
312
+ return if attrs.nil?
313
+ order.each do |k|
314
+ v = attrs[k]
315
+ @target << %{ #{k}=#{@quote}#{_attr_value(v)}#{@quote}} if v
316
+ end
317
+ attrs.each do |k, v|
318
+ @target << %{ #{k}=#{@quote}#{_attr_value(v)}#{@quote}} unless order.member?(k) # " WART
319
+ end
320
+ end
321
+
322
+ def _attr_value(value)
323
+ case value
324
+ when ::Symbol
325
+ value.to_s
326
+ else
327
+ _escape_attribute(value.to_s)
328
+ end
329
+ end
330
+
331
+ def _ensure_no_block(got_block)
332
+ if got_block
333
+ ::Kernel::raise IllegalBlockError.new(
334
+ "Blocks are not allowed on XML instructions"
335
+ )
336
+ end
337
+ end
338
+
339
+ end
340
+
341
+ end
data/lib/builder.rb ADDED
@@ -0,0 +1,13 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ #--
4
+ # Copyright 2004 by Jim Weirich (jim@weirichhouse.org).
5
+ # All rights reserved.
6
+
7
+ # Permission is granted for use, copying, modification, distribution,
8
+ # and distribution of modified versions of this work as long as the
9
+ # above copyright notice is included.
10
+ #++
11
+
12
+ require 'builder/xmlmarkup'
13
+ require 'builder/xmlevents'
@@ -0,0 +1,26 @@
1
+ # Optional publish task for Rake
2
+ #
3
+ # NOTE (keeper): the rake/contrib publishers this task relied on were
4
+ # removed from modern rake, and RubyForge itself is long gone. The
5
+ # task is skipped when the contrib libraries are unavailable so that
6
+ # plain `rake` (test suite) keeps working.
7
+
8
+ begin
9
+ require 'rake/contrib/sshpublisher'
10
+ require 'rake/contrib/rubyforgepublisher'
11
+ rescue LoadError
12
+ # Publish task unavailable on this rake; skip it.
13
+ else
14
+ publisher = Rake::CompositePublisher.new
15
+ publisher.add Rake::RubyForgePublisher.new('builder', 'jimweirich')
16
+ publisher.add Rake::SshFilePublisher.new(
17
+ 'linode',
18
+ 'htdocs/software/builder',
19
+ '.',
20
+ 'builder.blurb')
21
+
22
+ desc "Publish the Documentation to RubyForge."
23
+ task :publish => [:rdoc] do
24
+ publisher.upload
25
+ end
26
+ end
data/rakelib/tags.rake ADDED
@@ -0,0 +1,68 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ module Tags
4
+ extend Rake::DSL if defined?(Rake::DSL)
5
+
6
+ PROG = ENV['TAGS'] || 'ctags'
7
+
8
+ RAKEFILES = FileList['Rakefile', '**/*.rake']
9
+
10
+ FILES = FileList['**/*.rb', '**/*.js'] + RAKEFILES
11
+ FILES.exclude('pkg', 'dist')
12
+
13
+ PROJECT_DIR = ['.']
14
+
15
+ # NOTE (keeper): guard the rvm probe so plain `rake` works without rvm;
16
+ # File.exists? was removed in Ruby 3.2 (use File.exist?).
17
+ RVM_GEMDIR = begin
18
+ File.join(`rvm gemdir`.strip, "gems")
19
+ rescue Errno::ENOENT
20
+ nil
21
+ end
22
+ SYSTEM_DIRS = (RVM_GEMDIR && File.exist?(RVM_GEMDIR)) ? RVM_GEMDIR : []
23
+
24
+ module_function
25
+
26
+ # Convert key_word to --key-word.
27
+ def keyword(key)
28
+ k = key.to_s.gsub(/_/, '-')
29
+ (k.length == 1) ? "-#{k}" : "--#{k}"
30
+ end
31
+
32
+ # Run ctags command
33
+ def run(*args)
34
+ opts = {
35
+ :e => true,
36
+ :totals => true,
37
+ :recurse => true,
38
+ }
39
+ opts = opts.merge(args.pop) if args.last.is_a?(Hash)
40
+ command_args = opts.map { |k, v|
41
+ (v == true) ? keyword(k) : "#{keyword(k)}=#{v}"
42
+ }.join(" ")
43
+ sh %{#{Tags::PROG} #{command_args} #{args.join(' ')}}
44
+ end
45
+ end
46
+
47
+ namespace "tags" do
48
+ desc "Generate an Emacs TAGS file"
49
+ task :emacs, [:all] => Tags::FILES do |t, args|
50
+ puts "Making Emacs TAGS file"
51
+ verbose(true) do
52
+ Tags.run(Tags::PROJECT_DIR)
53
+ Tags.run(Tags::RAKEFILES,
54
+ :language_force => "ruby",
55
+ :append => true)
56
+ if args.all
57
+ Tags::SYSTEM_DIRS.each do |dir|
58
+ Tags.run(dir,
59
+ :language_force => "ruby",
60
+ :append => true)
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
66
+
67
+ desc "Generate the TAGS file"
68
+ task :tags, [:all] => ["tags:emacs"]
@@ -0,0 +1,41 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: iso-8859-1
3
+
4
+ #--
5
+ # Portions copyright 2004 by Jim Weirich (jim@weirichhouse.org).
6
+ # Portions copyright 2005 by Sam Ruby (rubys@intertwingly.net).
7
+ # All rights reserved.
8
+
9
+ # Permission is granted for use, copying, modification, distribution,
10
+ # and distribution of modified versions of this work as long as the
11
+ # above copyright notice is included.
12
+ #++
13
+
14
+ require 'builder/xmlmarkup'
15
+ require 'benchmark'
16
+
17
+ text = "This is a test of the new xml markup. I�t�rn�ti�n�liz�ti�n\n" * 10000
18
+
19
+ include Benchmark # we need the CAPTION and FMTSTR constants
20
+ include Builder
21
+ n = 50
22
+ Benchmark.benchmark do |bm|
23
+ tf = bm.report("base") {
24
+ n.times do
25
+ x = XmlMarkup.new
26
+ x.text(text)
27
+ x.target!
28
+ end
29
+ }
30
+ def XmlMarkup._escape(text)
31
+ text.to_xs
32
+ end
33
+ tf = bm.report("to_xs") {
34
+ n.times do
35
+ x = XmlMarkup.new
36
+ x.text(text)
37
+ x.target!
38
+ end
39
+ }
40
+ end
41
+
data/test/preload.rb ADDED
@@ -0,0 +1,39 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ #--
4
+ # Portions copyright 2004 by Jim Weirich (jim@weirichhouse.org).
5
+ # Portions copyright 2005 by Sam Ruby (rubys@intertwingly.net).
6
+ # All rights reserved.
7
+
8
+ # Permission is granted for use, copying, modification, distribution,
9
+ # and distribution of modified versions of this work as long as the
10
+ # above copyright notice is included.
11
+ #++
12
+
13
+ # We are defining method_added in Kernel and Object so that when
14
+ # BlankSlate overrides them later, we can verify that it correctly
15
+ # calls the older hooks.
16
+
17
+ module Kernel
18
+ class << self
19
+ attr_reader :k_added_names
20
+ alias_method :preload_method_added, :method_added
21
+ def method_added(name)
22
+ preload_method_added(name)
23
+ @k_added_names ||= []
24
+ @k_added_names << name
25
+ end
26
+ end
27
+ end
28
+
29
+ class Object
30
+ class << self
31
+ attr_reader :o_added_names
32
+ alias_method :preload_method_added, :method_added
33
+ def method_added(name)
34
+ preload_method_added(name)
35
+ @o_added_names ||= []
36
+ @o_added_names << name
37
+ end
38
+ end
39
+ end