xrb 0.10.1 → 0.11.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 83ba083dbccd5e6f56df768104a6dc63860cc1dd0bf1aa52ce2928a881a3dd96
4
- data.tar.gz: 2922616d417867c3fc7790fe871fec4846605fb1a8656354ac8c5ccab1470abb
3
+ metadata.gz: d6f3f374f9c7841a9aabbe4a4f30a598e8567d9357d0fef4113770b318fc76ed
4
+ data.tar.gz: 16aff80df195168a9a2ec179c79d8219d18eaebb344e94e6fe668ea814b4e02e
5
5
  SHA512:
6
- metadata.gz: e191c69bf8043e5e195cdb798da3f23582cf391992c9a19f56dd96313c3783ded8adda3aef1151fa79b922cb9f388d3e3906af0f6781c028c8bccba3615f1645
7
- data.tar.gz: 6c7ae41f9ac7da195976263054f112f57cb89e6cbd689006998b042ac23346b21ce6f38fc451cc0911d1b2a4134bacfa5c51051b52c2e90891a9e691a91ac12e
6
+ metadata.gz: 90ba6bdffb495cd7356eb8818dd8649b2fcffddd903e756b4724cda48420fbea6ce9e6fa8a49ff8b3f084cca00783322d1ffd814e3cd00bcf93e07389e63385e
7
+ data.tar.gz: 52057eebbfee6c4b10c0258f2199c895f7a960f17bdae3892793a8e99647364d32868d2c7f91a5c6a6596db3d1d028fa0b2ed0f100a0af64db1c8aca524e753d
checksums.yaml.gz.sig CHANGED
Binary file
data/bake/xrb/entities.rb CHANGED
@@ -6,9 +6,9 @@
6
6
  # Fetch the HTML5 entities from w3.org and update the local cache.
7
7
  # @parameter force [Boolean] Whether to force regenerate the local cache.
8
8
  def fetch_entities(force: false)
9
- require 'json'
10
- require 'async'
11
- require 'async/http/internet'
9
+ require "json"
10
+ require "async"
11
+ require "async/http/internet"
12
12
 
13
13
  internet = Async::HTTP::Internet.new
14
14
  entites_json_path = self.entites_json_path
@@ -21,17 +21,17 @@ def fetch_entities(force: false)
21
21
  end
22
22
  end
23
23
 
24
- return JSON.parse(File.read(entites_json_path)).delete_if{|string, _| !string.end_with? ';'}
24
+ return JSON.parse(File.read(entites_json_path)).delete_if{|string, _| !string.end_with? ";"}
25
25
  end
26
26
 
27
27
  # Consume the HTML5 entites and generate parsers to escape them.
28
28
  # @parameter wet [Boolean] Whether to write updated files.
29
29
  def update_entities(wet: false)
30
- require 'xrb/template'
30
+ require "xrb/template"
31
31
 
32
32
  paths = {
33
33
  # 'ext/xrb/entities.rl' => 'ext/xrb/entities.xrb',
34
- 'lib/xrb/entities.rb' => 'lib/xrb/entities.xrb',
34
+ "lib/xrb/entities.rb" => "lib/xrb/entities.xrb",
35
35
  }
36
36
 
37
37
  entities = self.fetch_entities
data/ext/extconf.rb CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  return if RUBY_DESCRIPTION =~ /jruby/
7
7
 
8
- require 'mkmf'
8
+ require "mkmf"
9
9
 
10
10
  $CFLAGS << " -O3 -Wall -Wno-unknown-pragmas -std=c99"
11
11
 
@@ -17,4 +17,4 @@ $VPATH << "$(srcdir)/xrb"
17
17
 
18
18
  create_header
19
19
 
20
- create_makefile('XRB_Extension')
20
+ create_makefile("XRB_Extension")
data/lib/xrb/buffer.rb CHANGED
@@ -4,40 +4,68 @@
4
4
  # Copyright, 2016-2024, by Samuel Williams.
5
5
 
6
6
  module XRB
7
+ # Represents a string buffer with a given path.
7
8
  class Buffer
8
- def initialize(string, path: '<string>')
9
+ # Create a new buffer from a string.
10
+ # @parameter string [String] the string to use as the buffer.
11
+ # @parameter path [String] the path to use for the buffer.
12
+ def initialize(string, path: "<string>")
9
13
  @string = string
10
14
  @path = path
11
15
  end
12
16
 
17
+ # @attribute [String] the path name of the buffer.
13
18
  attr :path
14
19
 
20
+ def freeze
21
+ return self if frozen?
22
+
23
+ @string.freeze
24
+ @path.freeze
25
+
26
+ super
27
+ end
28
+
29
+ # @returns [Encoding] the encoding of the buffer.
15
30
  def encoding
16
31
  @string.encoding
17
32
  end
18
33
 
34
+ # @returns [String] the content of the buffer.
19
35
  def read
20
36
  @string
21
37
  end
22
38
 
39
+ # Create a buffer from the specified file.
40
+ # @parameter path [String] the path to load the file from.
41
+ # @returns [Buffer] a buffer with the contents of the specified path.
23
42
  def self.load_file(path)
24
43
  FileBuffer.new(path).freeze
25
44
  end
26
45
 
46
+ # Create a buffer from the specified string.
47
+ # @parameter string [String] the string to load into the buffer.
48
+ # @returns [Buffer] a buffer with the contents of the specified string.
27
49
  def self.load(string)
28
50
  Buffer.new(string).freeze
29
51
  end
30
52
 
53
+ # @returns [Buffer] itself.
31
54
  def to_buffer
32
55
  self
33
56
  end
34
57
  end
35
58
 
59
+ # Represents a file buffer with a given path.
36
60
  class FileBuffer
61
+ # Create a new file buffer from a file path.
62
+ # @parameter path [String] the path to the file.
37
63
  def initialize(path)
38
64
  @path = path
65
+ @cache = nil
39
66
  end
40
67
 
68
+ # Freeze the buffer, caching the contents of the file.
41
69
  def freeze
42
70
  return self if frozen?
43
71
 
@@ -46,27 +74,38 @@ module XRB
46
74
  super
47
75
  end
48
76
 
77
+ # @attribute [String] the path name of the buffer.
49
78
  attr :path
50
79
 
80
+ # @returns [Encoding] the encoding of the buffer.
51
81
  def encoding
52
82
  read.encoding
53
83
  end
54
84
 
85
+ # Read the contents of the file into the buffer.
86
+ # @returns [String] the content of the buffer.
55
87
  def read
56
88
  @cache ||= File.read(@path).freeze
57
89
  end
58
90
 
91
+ # @returns [Buffer] a buffer with the contents of the file.
59
92
  def to_buffer
60
93
  Buffer.new(self.read, @path)
61
94
  end
62
95
  end
63
96
 
97
+ # Represents an IO buffer with a given path.
64
98
  class IOBuffer
99
+ # Create a new IO buffer from an IO object.
100
+ # @parameter io [IO] the IO object to use as the buffer.
101
+ # @parameter path [String] the path to use for the buffer.
65
102
  def initialize(io, path: io.inspect)
66
103
  @io = io
67
104
  @path = path
105
+ @cache = nil
68
106
  end
69
107
 
108
+ # Freeze the buffer, caching the contents of the IO object.
70
109
  def freeze
71
110
  return self if frozen?
72
111
 
@@ -75,21 +114,31 @@ module XRB
75
114
  super
76
115
  end
77
116
 
117
+ # @attribute [String] the path name of the buffer.
78
118
  attr :path
79
119
 
120
+ # @returns [Encoding] the encoding of the buffer.
80
121
  def encoding
81
122
  read.encoding
82
123
  end
83
124
 
125
+ # Read the contents of the IO object into the buffer.
84
126
  def read
85
- @cache ||= @io.read.freeze
127
+ unless @cache
128
+ @cache = @io.read.freeze
129
+ @io.close
130
+ end
131
+
132
+ return @cache
86
133
  end
87
134
 
135
+ # @returns [Buffer] a buffer with the contents of the IO object.
88
136
  def to_buffer
89
137
  Buffer.new(self.read, path: @path)
90
138
  end
91
139
  end
92
140
 
141
+ # Convert the given value to a buffer.
93
142
  def self.Buffer(value)
94
143
  case value
95
144
  when String
data/lib/xrb/builder.rb CHANGED
@@ -3,20 +3,24 @@
3
3
  # Released under the MIT License.
4
4
  # Copyright, 2012-2024, by Samuel Williams.
5
5
 
6
- require_relative 'markup'
7
- require_relative 'tag'
6
+ require_relative "markup"
7
+ require_relative "tag"
8
8
 
9
9
  module XRB
10
10
  # Build markup quickly and efficiently.
11
11
  class Builder
12
12
  INDENT = "\t"
13
13
 
14
+ # Represents a lazy fragment of markup that can be generated on demand.
14
15
  class Fragment
16
+ # Initialize the fragment with a block that will be called to generate the markup.
17
+ # @parameter block [Proc] the block that will be called to generate the markup.
15
18
  def initialize(block)
16
19
  @block = block
17
20
  @builder = nil
18
21
  end
19
22
 
23
+ # Append the markup to the given output.
20
24
  def append_markup(output)
21
25
  builder = Builder.new(output)
22
26
 
@@ -25,19 +29,25 @@ module XRB
25
29
  return builder.output
26
30
  end
27
31
 
32
+ # Build the markup using the given builder.
28
33
  def build_markup(builder)
29
34
  @block.call(builder)
30
35
  end
31
36
 
37
+ # Convert the fragment to a string.
38
+ # @returns [MarkupString] the markup generated by the fragment.
32
39
  def to_s
33
40
  self.append_markup(nil)
34
41
  end
35
42
 
43
+ # Compare the fragment to another object as a string. Primarily useful for testing.
36
44
  def == other
37
45
  # This is a bit of a hack... but is required for existing specs to pass:
38
46
  self.to_s == other.to_s
39
47
  end
40
48
 
49
+ # Assuming the fragment is generated in the context of a template being rendered, append the fragment to the output buffer.
50
+ # @parameter block [Proc] the block which is within the scope of the template being rendered.
41
51
  def >> block
42
52
  if block
43
53
  Template.buffer(block.binding) << self
@@ -84,12 +94,15 @@ module XRB
84
94
  @children = [0]
85
95
  end
86
96
 
97
+ # Append the markup to the given output.
87
98
  def build_markup(builder)
88
99
  builder.append(@output)
89
100
  end
90
101
 
102
+ # @attribute [Object] the output buffer.
91
103
  attr :output
92
104
 
105
+ # @returns [Encoding] the encoding of the output.
93
106
  def encoding
94
107
  @output.encoding
95
108
  end
@@ -109,20 +122,21 @@ module XRB
109
122
  if @indent
110
123
  INDENT * (@level.size - 1)
111
124
  else
112
- ''
125
+ ""
113
126
  end
114
127
  end
115
128
 
116
- def doctype(attributes = 'html')
129
+ # Append a doctype declaration to the output.
130
+ def doctype(attributes = "html")
117
131
  @output << "<!doctype #{attributes}>\n"
118
132
  end
119
133
 
120
- # Begin a block tag.
134
+ # Append a tag to the output.
121
135
  def tag(name, attributes = {}, &block)
122
136
  full_tag(name, attributes, @indent, @indent, &block)
123
137
  end
124
138
 
125
- # Begin an inline tag.
139
+ # Append a tag to the output without indentation or whitespace.
126
140
  def inline_tag(name, attributes = {}, &block)
127
141
  original_indent = @indent
128
142
 
@@ -145,6 +159,7 @@ module XRB
145
159
  @indent = original_indent
146
160
  end
147
161
 
162
+ # Append text to the output, escaping it if necessary.
148
163
  def text(content)
149
164
  return unless content
150
165
 
@@ -164,6 +179,7 @@ module XRB
164
179
  @output << content
165
180
  end
166
181
 
182
+ # Append content to the output.
167
183
  def <<(content)
168
184
  content&.build_markup(self)
169
185
  end
data/lib/xrb/error.rb CHANGED
@@ -3,7 +3,7 @@
3
3
  # Released under the MIT License.
4
4
  # Copyright, 2016-2024, by Samuel Williams.
5
5
 
6
- require_relative 'buffer'
6
+ require_relative "buffer"
7
7
 
8
8
  module XRB
9
9
  class Error < StandardError
@@ -1,14 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # line 1 "lib/xrb/fallback/markup.rl"
4
3
  # Released under the MIT License.
5
4
  # Copyright, 2016-2024, by Samuel Williams.
6
5
 
7
-
8
6
  # line 190 "lib/xrb/fallback/markup.rl"
9
7
 
10
8
 
11
- require_relative '../error'
9
+ require_relative "../error"
12
10
 
13
11
  module XRB
14
12
  module Fallback
@@ -1,14 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # line 1 "lib/xrb/fallback/query.rl"
4
3
  # Released under the MIT License.
5
- # Copyright, 2016-2024, by Samuel Williams.
6
-
4
+ # Copyright, 2020-2024, by Samuel Williams.
7
5
 
8
6
  # line 57 "lib/xrb/fallback/query.rl"
9
7
 
10
8
 
11
- require_relative '../error'
9
+ require_relative "../error"
12
10
 
13
11
  module XRB
14
12
  module Fallback
@@ -1,14 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # line 1 "lib/xrb/fallback/template.rl"
4
3
  # Released under the MIT License.
5
4
  # Copyright, 2016-2024, by Samuel Williams.
6
5
 
7
-
8
6
  # line 80 "lib/xrb/fallback/template.rl"
9
7
 
10
8
 
11
- require_relative '../error'
9
+ require_relative "../error"
12
10
 
13
11
  module XRB
14
12
  module Fallback
data/lib/xrb/markup.rb CHANGED
@@ -3,7 +3,7 @@
3
3
  # Released under the MIT License.
4
4
  # Copyright, 2016-2024, by Samuel Williams.
5
5
 
6
- require 'cgi'
6
+ require "cgi"
7
7
 
8
8
  module XRB
9
9
  # A wrapper which indicates that `value` can be appended to the output buffer without any changes.
data/lib/xrb/native.rb CHANGED
@@ -3,13 +3,13 @@
3
3
  # Released under the MIT License.
4
4
  # Copyright, 2016-2024, by Samuel Williams.
5
5
 
6
- require_relative 'error'
6
+ require_relative "error"
7
7
 
8
8
  # Methods on the following classes may be replaced by native implementations:
9
- require_relative 'tag'
9
+ require_relative "tag"
10
10
 
11
11
  begin
12
- require 'XRB_Extension'
12
+ require "XRB_Extension"
13
13
  rescue LoadError => error
14
14
  warn "Could not load native parsers: #{error}"
15
- end unless ENV['XRB_PREFER_FALLBACK']
15
+ end unless ENV["XRB_PREFER_FALLBACK"]
data/lib/xrb/parsers.rb CHANGED
@@ -3,14 +3,14 @@
3
3
  # Released under the MIT License.
4
4
  # Copyright, 2012-2024, by Samuel Williams.
5
5
 
6
- require_relative 'native'
6
+ require_relative "native"
7
7
 
8
8
  if defined? XRB::Native
9
9
  XRB::Parsers = XRB::Native
10
10
  else
11
- require_relative 'fallback/markup'
12
- require_relative 'fallback/template'
13
- require_relative 'fallback/query'
11
+ require_relative "fallback/markup"
12
+ require_relative "fallback/template"
13
+ require_relative "fallback/query"
14
14
 
15
15
  XRB::Parsers = XRB::Fallback
16
16
  end
data/lib/xrb/query.rb CHANGED
@@ -3,10 +3,10 @@
3
3
  # Released under the MIT License.
4
4
  # Copyright, 2020-2024, by Samuel Williams.
5
5
 
6
- require_relative 'buffer'
7
- require_relative 'parsers'
6
+ require_relative "buffer"
7
+ require_relative "parsers"
8
8
 
9
- require 'uri'
9
+ require "uri"
10
10
 
11
11
  module XRB
12
12
  module Query
data/lib/xrb/reference.rb CHANGED
@@ -3,7 +3,7 @@
3
3
  # Released under the MIT License.
4
4
  # Copyright, 2020-2024, by Samuel Williams.
5
5
 
6
- require_relative 'query'
6
+ require_relative "query"
7
7
 
8
8
  module XRB
9
9
  class Reference
@@ -26,11 +26,11 @@ module XRB
26
26
  buffer << escape_path(@path)
27
27
 
28
28
  unless @query.empty?
29
- buffer << '?' << query_string
29
+ buffer << "?" << query_string
30
30
  end
31
31
 
32
32
  if @fragment
33
- buffer << '#' << escape(@fragment)
33
+ buffer << "#" << escape(@fragment)
34
34
  end
35
35
 
36
36
  return buffer
@@ -55,7 +55,7 @@ module XRB
55
55
  def escape_path(path)
56
56
  encoding = path.encoding
57
57
  path.b.gsub(NON_PCHAR) do |m|
58
- '%' + m.unpack('H2' * m.bytesize).join('%').upcase
58
+ "%" + m.unpack("H2" * m.bytesize).join("%").upcase
59
59
  end.force_encoding(encoding)
60
60
  end
61
61
 
@@ -63,7 +63,7 @@ module XRB
63
63
  def escape(string)
64
64
  encoding = string.encoding
65
65
  string.b.gsub(/([^a-zA-Z0-9_.\-]+)/) do |m|
66
- '%' + m.unpack('H2' * m.bytesize).join('%').upcase
66
+ "%" + m.unpack("H2" * m.bytesize).join("%").upcase
67
67
  end.force_encoding(encoding)
68
68
  end
69
69
 
@@ -80,7 +80,7 @@ module XRB
80
80
  when Hash
81
81
  value.map { |k, v|
82
82
  build_nested_query(v, prefix ? "#{prefix}[#{escape(k.to_s)}]" : escape(k.to_s))
83
- }.reject(&:empty?).join('&')
83
+ }.reject(&:empty?).join("&")
84
84
  when nil
85
85
  prefix
86
86
  else
@@ -91,9 +91,9 @@ module XRB
91
91
  end
92
92
 
93
93
  # Generate a URI from a path and user parameters. The path may contain a `#fragment` or `?query=parameters`.
94
- def self.Reference(path = '', **parameters)
95
- base, fragment = path.split('#', 2)
96
- path, query_string = base.split('?', 2)
94
+ def self.Reference(path = "", **parameters)
95
+ base, fragment = path.split("#", 2)
96
+ path, query_string = base.split("?", 2)
97
97
 
98
98
  if query_string
99
99
  query = Query.parse(Buffer.new(query_string))
data/lib/xrb/tag.rb CHANGED
@@ -3,13 +3,13 @@
3
3
  # Released under the MIT License.
4
4
  # Copyright, 2017-2024, by Samuel Williams.
5
5
 
6
- require_relative 'markup'
6
+ require_relative "markup"
7
7
 
8
8
  module XRB
9
9
  # This represents an individual SGML tag, e.g. <a>, </a> or <a />, with attributes. Attribute values must be escaped.
10
10
  Tag = Struct.new(:name, :closed, :attributes) do
11
11
  def self.split(qualified_name)
12
- if i = qualified_name.index(':')
12
+ if i = qualified_name.index(":")
13
13
  return qualified_name.slice(0...i), qualified_name.slice(i+1..-1)
14
14
  else
15
15
  return nil, qualified_name
@@ -49,19 +49,19 @@ module XRB
49
49
  end
50
50
 
51
51
  def write_opening_tag(buffer)
52
- buffer << '<' << name
52
+ buffer << "<" << name
53
53
 
54
54
  self.class.append_attributes(buffer, attributes, nil)
55
55
 
56
56
  if self_closed?
57
- buffer << '/>'
57
+ buffer << "/>"
58
58
  else
59
- buffer << '>'
59
+ buffer << ">"
60
60
  end
61
61
  end
62
62
 
63
63
  def write_closing_tag(buffer)
64
- buffer << '</' << name << '>'
64
+ buffer << "</" << name << ">"
65
65
  end
66
66
 
67
67
  def write(buffer, content = nil)
@@ -77,18 +77,18 @@ module XRB
77
77
  end
78
78
 
79
79
  def self.append_tag(buffer, name, attributes, content)
80
- buffer << '<' << name.to_s
80
+ buffer << "<" << name.to_s
81
81
 
82
82
  self.append_attributes(buffer, attributes, nil)
83
83
 
84
84
  if !content
85
- buffer << '/>'
85
+ buffer << "/>"
86
86
  else
87
- buffer << '>'
87
+ buffer << ">"
88
88
  unless content == true
89
89
  content.append_markup(buffer)
90
90
  end
91
- buffer << '</' << name.to_s << '>'
91
+ buffer << "</" << name.to_s << ">"
92
92
  end
93
93
 
94
94
  return nil
@@ -111,9 +111,9 @@ module XRB
111
111
  self.append_attributes(buffer, [attribute], attribute_key)
112
112
  end
113
113
  when TrueClass
114
- buffer << ' ' << attribute_key.to_s
114
+ buffer << " " << attribute_key.to_s
115
115
  else
116
- buffer << ' ' << attribute_key.to_s << '="'
116
+ buffer << " " << attribute_key.to_s << '="'
117
117
  value.append_markup(buffer)
118
118
  buffer << '"'
119
119
  end
data/lib/xrb/template.rb CHANGED
@@ -3,10 +3,10 @@
3
3
  # Released under the MIT License.
4
4
  # Copyright, 2012-2024, by Samuel Williams.
5
5
 
6
- require_relative 'parsers'
7
- require_relative 'markup'
8
- require_relative 'buffer'
9
- require_relative 'builder'
6
+ require_relative "parsers"
7
+ require_relative "markup"
8
+ require_relative "buffer"
9
+ require_relative "builder"
10
10
 
11
11
  module XRB
12
12
  # The output variable that will be used in templates:
@@ -41,7 +41,7 @@ module XRB
41
41
  return output.to_str
42
42
  end
43
43
 
44
- # Returns the buffer used for capturing output.
44
+ # @returns [Object] the buffer used for capturing output.
45
45
  def self.buffer(binding)
46
46
  binding.local_variable_get(OUT)
47
47
  end
@@ -54,6 +54,7 @@ module XRB
54
54
  attr :code
55
55
 
56
56
  # Output raw text to the template.
57
+ # Raw text should be escaped, e.g. it can't contain tags or other special characters.
57
58
  def text(text)
58
59
  # We have to use an approach which preserves newlines and tabs as raw characters.
59
60
  text = text.gsub("'", "\\\\'")
@@ -62,7 +63,7 @@ module XRB
62
63
 
63
64
  # Output a ruby expression (or part of).
64
65
  def instruction(text, postfix = nil)
65
- @code << text << (postfix || ';')
66
+ @code << text << (postfix || ";")
66
67
  end
67
68
 
68
69
  # Output a string interpolation.
@@ -71,15 +72,19 @@ module XRB
71
72
  end
72
73
  end
73
74
 
75
+ # Load a template from a file.
74
76
  def self.load_file(path, **options)
75
77
  self.new(FileBuffer.new(path), **options).freeze
76
78
  end
77
79
 
80
+ # Load a template from a string.
78
81
  def self.load(string, *arguments, **options)
79
82
  self.new(Buffer.new(string), **options).freeze
80
83
  end
81
84
 
82
- # @param binding [Binding] The binding in which the template is compiled. e.g. `TOPLEVEL_BINDING`.
85
+ # Initialize a new template.
86
+ # @parameter buffer [Buffer] The buffer containing the template.
87
+ # @parameter binding [Binding] The binding in which the template is compiled. e.g. `TOPLEVEL_BINDING`.
83
88
  def initialize(buffer, binding: BINDING)
84
89
  @buffer = buffer
85
90
  @binding = binding
@@ -95,14 +100,23 @@ module XRB
95
100
  super
96
101
  end
97
102
 
103
+ # The compiled code for the template.
104
+ # @returns [String] the compiled Ruby code.
98
105
  def code
99
106
  @code ||= compile!
100
107
  end
101
108
 
109
+ # The compiled template as a proc.
110
+ # @parameter scope [Object] The scope in which the template will be compiled.
111
+ # @returns [Proc] a proc that can be called with an output object.
102
112
  def compiled(scope = @binding.dup)
103
113
  @compiled ||= eval("\# frozen_string_literal: true\nproc{|#{OUT}|;#{code}}", scope, @buffer.path, 0).freeze
104
114
  end
105
115
 
116
+ # Renders the template to a string.
117
+ #
118
+ # @parameter scope [Object] The scope in which the template will be evaluated.
119
+ # @parameter output [String | Nil] The output string to append to.
106
120
  def to_string(scope = Object.new, output = nil)
107
121
  builder = Builder.new(output, encoding: code.encoding)
108
122
 
@@ -111,13 +125,29 @@ module XRB
111
125
  return builder.output
112
126
  end
113
127
 
128
+ # Renders the template to a buffer.
114
129
  def to_buffer(scope)
115
130
  Buffer.new(to_string(scope), path: @buffer.path)
116
131
  end
117
132
 
133
+ # Convert the template to a proc that can be called with an output object. The proc renders the template using `to_string` and writes the output to the given output object. The output should implement `<<` and `close_write(error = nil)` methods.
134
+ #
135
+ # @parameter scope [Object] The scope in which the template will be evaluated.
136
+ # @returns [Proc] a proc that can be called with an output object.
118
137
  def to_proc(scope = @binding.dup)
119
138
  proc do |output|
120
139
  to_string(scope, output)
140
+ rescue Exception => error
141
+ # I find this a bit ugly but we only use this code path on errors, so it's not performance critical.
142
+ if output.method(:close_write).arity != 0
143
+ # Inform the output that an error occured and the output was not generated correctly.
144
+ output.close_write(error)
145
+ output = nil
146
+ end
147
+
148
+ raise
149
+ ensure
150
+ output&.close_write
121
151
  end
122
152
  end
123
153
 
data/lib/xrb/uri.rb CHANGED
@@ -27,15 +27,15 @@ module XRB
27
27
 
28
28
  def append(buffer)
29
29
  if @query_string
30
- buffer << escape_path(@path) << '?' << query_string
31
- buffer << '&' << query_parameters if @parameters
30
+ buffer << escape_path(@path) << "?" << query_string
31
+ buffer << "&" << query_parameters if @parameters
32
32
  else
33
33
  buffer << escape_path(@path)
34
- buffer << '?' << query_parameters if @parameters
34
+ buffer << "?" << query_parameters if @parameters
35
35
  end
36
36
 
37
37
  if @fragment
38
- buffer << '#' << escape(@fragment)
38
+ buffer << "#" << escape(@fragment)
39
39
  end
40
40
 
41
41
  return buffer
@@ -55,7 +55,7 @@ module XRB
55
55
  def escape_path(path)
56
56
  encoding = path.encoding
57
57
  path.b.gsub(NON_PCHAR) do |m|
58
- '%' + m.unpack('H2' * m.bytesize).join('%').upcase
58
+ "%" + m.unpack("H2" * m.bytesize).join("%").upcase
59
59
  end.force_encoding(encoding)
60
60
  end
61
61
 
@@ -63,7 +63,7 @@ module XRB
63
63
  def escape(string)
64
64
  encoding = string.encoding
65
65
  string.b.gsub(/([^a-zA-Z0-9_.\-]+)/) do |m|
66
- '%' + m.unpack('H2' * m.bytesize).join('%').upcase
66
+ "%" + m.unpack("H2" * m.bytesize).join("%").upcase
67
67
  end.force_encoding(encoding)
68
68
  end
69
69
 
@@ -80,7 +80,7 @@ module XRB
80
80
  when Hash
81
81
  value.map { |k, v|
82
82
  build_nested_query(v, prefix ? "#{prefix}[#{escape(k.to_s)}]" : escape(k.to_s))
83
- }.reject(&:empty?).join('&')
83
+ }.reject(&:empty?).join("&")
84
84
  when nil
85
85
  prefix
86
86
  else
@@ -91,9 +91,9 @@ module XRB
91
91
  end
92
92
 
93
93
  # Generate a URI from a path and user parameters. The path may contain a `#fragment` or `?query=parameters`.
94
- def self.URI(path = '', parameters = nil)
95
- base, fragment = path.split('#', 2)
96
- path, query_string = base.split('?', 2)
94
+ def self.URI(path = "", parameters = nil)
95
+ base, fragment = path.split("#", 2)
96
+ path, query_string = base.split("?", 2)
97
97
 
98
98
  URI.new(path, query_string, fragment, parameters)
99
99
  end
data/lib/xrb/version.rb CHANGED
@@ -4,5 +4,5 @@
4
4
  # Copyright, 2012-2024, by Samuel Williams.
5
5
 
6
6
  module XRB
7
- VERSION = "0.10.1"
7
+ VERSION = "0.11.0"
8
8
  end
data/lib/xrb.rb CHANGED
@@ -3,9 +3,9 @@
3
3
  # Released under the MIT License.
4
4
  # Copyright, 2012-2024, by Samuel Williams.
5
5
 
6
- require_relative 'xrb/version'
7
- require_relative 'xrb/native'
8
- require_relative 'xrb/builder'
9
- require_relative 'xrb/template'
6
+ require_relative "xrb/version"
7
+ require_relative "xrb/native"
8
+ require_relative "xrb/builder"
9
+ require_relative "xrb/template"
10
10
 
11
- require_relative 'xrb/reference'
11
+ require_relative "xrb/reference"
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: xrb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.1
4
+ version: 0.11.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Williams
@@ -40,7 +40,7 @@ cert_chain:
40
40
  Q2K9NVun/S785AP05vKkXZEFYxqG6EW012U4oLcFl5MySFajYXRYbuUpH6AY+HP8
41
41
  voD0MPg1DssDLKwXyt1eKD/+Fq0bFWhwVM/1XiAXL7lyYUyOq24KHgQ2Csg=
42
42
  -----END CERTIFICATE-----
43
- date: 2024-10-27 00:00:00.000000000 Z
43
+ date: 2024-10-30 00:00:00.000000000 Z
44
44
  dependencies: []
45
45
  description:
46
46
  email:
@@ -51,16 +51,7 @@ extra_rdoc_files: []
51
51
  files:
52
52
  - bake/xrb/entities.rb
53
53
  - bake/xrb/parsers.rb
54
- - ext/Makefile
55
- - ext/XRB_Extension.bundle
56
- - ext/escape.o
57
- - ext/extconf.h
58
54
  - ext/extconf.rb
59
- - ext/markup.o
60
- - ext/query.o
61
- - ext/tag.o
62
- - ext/template.o
63
- - ext/xrb.o
64
55
  - ext/xrb/escape.c
65
56
  - ext/xrb/escape.h
66
57
  - ext/xrb/markup.c
metadata.gz.sig CHANGED
Binary file
data/ext/Makefile DELETED
@@ -1,270 +0,0 @@
1
-
2
- SHELL = /bin/sh
3
-
4
- # V=0 quiet, V=1 verbose. other values don't work.
5
- V = 0
6
- V0 = $(V:0=)
7
- Q1 = $(V:1=)
8
- Q = $(Q1:0=@)
9
- ECHO1 = $(V:1=@ :)
10
- ECHO = $(ECHO1:0=@ echo)
11
- NULLCMD = :
12
-
13
- #### Start of system configuration section. ####
14
-
15
- srcdir = .
16
- topdir = /Users/samuel/.rubies/ruby-3.3.4/include/ruby-3.3.0
17
- hdrdir = $(topdir)
18
- arch_hdrdir = /Users/samuel/.rubies/ruby-3.3.4/include/ruby-3.3.0/arm64-darwin24
19
- PATH_SEPARATOR = :
20
- VPATH = $(srcdir):$(arch_hdrdir)/ruby:$(hdrdir)/ruby:$(srcdir)/xrb
21
- prefix = $(DESTDIR)/Users/samuel/.rubies/ruby-3.3.4
22
- rubysitearchprefix = $(rubylibprefix)/$(sitearch)
23
- rubyarchprefix = $(rubylibprefix)/$(arch)
24
- rubylibprefix = $(libdir)/$(RUBY_BASE_NAME)
25
- exec_prefix = $(prefix)
26
- vendorarchhdrdir = $(vendorhdrdir)/$(sitearch)
27
- sitearchhdrdir = $(sitehdrdir)/$(sitearch)
28
- rubyarchhdrdir = $(rubyhdrdir)/$(arch)
29
- vendorhdrdir = $(rubyhdrdir)/vendor_ruby
30
- sitehdrdir = $(rubyhdrdir)/site_ruby
31
- rubyhdrdir = $(includedir)/$(RUBY_VERSION_NAME)
32
- vendorarchdir = $(vendorlibdir)/$(sitearch)
33
- vendorlibdir = $(vendordir)/$(ruby_version)
34
- vendordir = $(rubylibprefix)/vendor_ruby
35
- sitearchdir = $(sitelibdir)/$(sitearch)
36
- sitelibdir = $(sitedir)/$(ruby_version)
37
- sitedir = $(rubylibprefix)/site_ruby
38
- rubyarchdir = $(rubylibdir)/$(arch)
39
- rubylibdir = $(rubylibprefix)/$(ruby_version)
40
- sitearchincludedir = $(includedir)/$(sitearch)
41
- archincludedir = $(includedir)/$(arch)
42
- sitearchlibdir = $(libdir)/$(sitearch)
43
- archlibdir = $(libdir)/$(arch)
44
- ridir = $(datarootdir)/$(RI_BASE_NAME)
45
- mandir = $(datarootdir)/man
46
- localedir = $(datarootdir)/locale
47
- libdir = $(exec_prefix)/lib
48
- psdir = $(docdir)
49
- pdfdir = $(docdir)
50
- dvidir = $(docdir)
51
- htmldir = $(docdir)
52
- infodir = $(datarootdir)/info
53
- docdir = $(datarootdir)/doc/$(PACKAGE)
54
- oldincludedir = $(DESTDIR)/usr/include
55
- includedir = $(SDKROOT)$(prefix)/include
56
- runstatedir = $(localstatedir)/run
57
- localstatedir = $(prefix)/var
58
- sharedstatedir = $(prefix)/com
59
- sysconfdir = $(prefix)/etc
60
- datadir = $(datarootdir)
61
- datarootdir = $(prefix)/share
62
- libexecdir = $(exec_prefix)/libexec
63
- sbindir = $(exec_prefix)/sbin
64
- bindir = $(exec_prefix)/bin
65
- archdir = $(rubyarchdir)
66
-
67
-
68
- CC_WRAPPER =
69
- CC = clang
70
- CXX = clang++
71
- LIBRUBY = $(LIBRUBY_A)
72
- LIBRUBY_A = lib$(RUBY_SO_NAME)-static.a
73
- LIBRUBYARG_SHARED =
74
- LIBRUBYARG_STATIC = -l$(RUBY_SO_NAME)-static -framework CoreFoundation $(MAINLIBS)
75
- empty =
76
- OUTFLAG = -o $(empty)
77
- COUTFLAG = -o $(empty)
78
- CSRCFLAG = $(empty)
79
-
80
- RUBY_EXTCONF_H = extconf.h
81
- cflags = -fdeclspec $(optflags) $(debugflags) $(warnflags)
82
- cxxflags =
83
- optflags = -O3 -fno-fast-math
84
- debugflags = -ggdb3
85
- warnflags = -Wall -Wextra -Wextra-tokens -Wdeprecated-declarations -Wdivision-by-zero -Wdiv-by-zero -Wimplicit-function-declaration -Wimplicit-int -Wpointer-arith -Wshorten-64-to-32 -Wwrite-strings -Wold-style-definition -Wmissing-noreturn -Wno-cast-function-type -Wno-constant-logical-operand -Wno-long-long -Wno-missing-field-initializers -Wno-overlength-strings -Wno-parentheses-equality -Wno-self-assign -Wno-tautological-compare -Wno-unused-parameter -Wno-unused-value -Wunused-variable -Wmisleading-indentation -Wundef
86
- cppflags =
87
- CCDLFLAGS = -fno-common
88
- CFLAGS = $(CCDLFLAGS) $(cflags) -pipe -O3 -Wall -Wno-unknown-pragmas -std=c99 $(ARCH_FLAG)
89
- INCFLAGS = -I. -I$(arch_hdrdir) -I$(hdrdir)/ruby/backward -I$(hdrdir) -I$(srcdir)
90
- DEFS =
91
- CPPFLAGS = -DRUBY_EXTCONF_H=\"$(RUBY_EXTCONF_H)\" -I/opt/local/include -D_XOPEN_SOURCE -D_DARWIN_C_SOURCE -D_DARWIN_UNLIMITED_SELECT -D_REENTRANT $(DEFS) $(cppflags)
92
- CXXFLAGS = $(CCDLFLAGS) -fdeclspec $(ARCH_FLAG)
93
- ldflags = -L. -fstack-protector-strong -L/opt/local/lib
94
- dldflags = -L/opt/local/lib -Wl,-undefined,dynamic_lookup -bundle_loader '$(BUILTRUBY)'
95
- ARCH_FLAG = -arch arm64
96
- DLDFLAGS = $(ldflags) $(dldflags) $(ARCH_FLAG)
97
- LDSHARED = $(CC) -dynamic -bundle
98
- LDSHAREDXX = $(CXX) -dynamic -bundle
99
- AR = ar
100
- EXEEXT =
101
-
102
- RUBY_INSTALL_NAME = $(RUBY_BASE_NAME)
103
- RUBY_SO_NAME = ruby.3.3
104
- RUBYW_INSTALL_NAME =
105
- RUBY_VERSION_NAME = $(RUBY_BASE_NAME)-$(ruby_version)
106
- RUBYW_BASE_NAME = rubyw
107
- RUBY_BASE_NAME = ruby
108
-
109
- arch = arm64-darwin24
110
- sitearch = $(arch)
111
- ruby_version = 3.3.0
112
- ruby = $(bindir)/$(RUBY_BASE_NAME)
113
- RUBY = $(ruby)
114
- BUILTRUBY = $(bindir)/$(RUBY_BASE_NAME)
115
- ruby_headers = $(hdrdir)/ruby.h $(hdrdir)/ruby/backward.h $(hdrdir)/ruby/ruby.h $(hdrdir)/ruby/defines.h $(hdrdir)/ruby/missing.h $(hdrdir)/ruby/intern.h $(hdrdir)/ruby/st.h $(hdrdir)/ruby/subst.h $(arch_hdrdir)/ruby/config.h $(RUBY_EXTCONF_H)
116
-
117
- RM = rm -f
118
- RM_RF = rm -fr
119
- RMDIRS = rmdir -p
120
- MAKEDIRS = mkdir -p
121
- INSTALL = /usr/bin/install -c
122
- INSTALL_PROG = $(INSTALL) -m 0755
123
- INSTALL_DATA = $(INSTALL) -m 644
124
- COPY = cp
125
- TOUCH = exit >
126
-
127
- #### End of system configuration section. ####
128
-
129
- preload =
130
- libpath = . $(libdir) /opt/local/lib
131
- LIBPATH = -L. -L$(libdir) -L/opt/local/lib
132
- DEFFILE =
133
-
134
- CLEANFILES = mkmf.log
135
- DISTCLEANFILES =
136
- DISTCLEANDIRS =
137
-
138
- extout =
139
- extout_prefix =
140
- target_prefix =
141
- LOCAL_LIBS =
142
- LIBS = -lpthread
143
- ORIG_SRCS =
144
- SRCS = $(ORIG_SRCS) escape.c markup.c query.c tag.c template.c xrb.c
145
- OBJS = escape.o markup.o query.o tag.o template.o xrb.o
146
- HDRS = $(srcdir)/extconf.h
147
- LOCAL_HDRS =
148
- TARGET = XRB_Extension
149
- TARGET_NAME = XRB_Extension
150
- TARGET_ENTRY = Init_$(TARGET_NAME)
151
- DLLIB = $(TARGET).bundle
152
- EXTSTATIC =
153
- STATIC_LIB =
154
-
155
- TIMESTAMP_DIR = .
156
- BINDIR = $(bindir)
157
- RUBYCOMMONDIR = $(sitedir)$(target_prefix)
158
- RUBYLIBDIR = $(sitelibdir)$(target_prefix)
159
- RUBYARCHDIR = $(sitearchdir)$(target_prefix)
160
- HDRDIR = $(sitehdrdir)$(target_prefix)
161
- ARCHHDRDIR = $(sitearchhdrdir)$(target_prefix)
162
- TARGET_SO_DIR =
163
- TARGET_SO = $(TARGET_SO_DIR)$(DLLIB)
164
- CLEANLIBS = $(TARGET_SO) $(TARGET_SO).dSYM
165
- CLEANOBJS = $(OBJS) *.bak
166
- TARGET_SO_DIR_TIMESTAMP = $(TIMESTAMP_DIR)/.sitearchdir.time
167
-
168
- all: $(DLLIB)
169
- static: $(STATIC_LIB)
170
- .PHONY: all install static install-so install-rb
171
- .PHONY: clean clean-so clean-static clean-rb
172
-
173
- clean-static::
174
- clean-rb-default::
175
- clean-rb::
176
- clean-so::
177
- clean: clean-so clean-static clean-rb-default clean-rb
178
- -$(Q)$(RM_RF) $(CLEANLIBS) $(CLEANOBJS) $(CLEANFILES) .*.time
179
-
180
- distclean-rb-default::
181
- distclean-rb::
182
- distclean-so::
183
- distclean-static::
184
- distclean: clean distclean-so distclean-static distclean-rb-default distclean-rb
185
- -$(Q)$(RM) Makefile $(RUBY_EXTCONF_H) conftest.* mkmf.log
186
- -$(Q)$(RM) core ruby$(EXEEXT) *~ $(DISTCLEANFILES)
187
- -$(Q)$(RMDIRS) $(DISTCLEANDIRS) 2> /dev/null || true
188
-
189
- realclean: distclean
190
- install: install-so install-rb
191
-
192
- install-so: $(DLLIB) $(TARGET_SO_DIR_TIMESTAMP)
193
- $(INSTALL_PROG) $(DLLIB) $(RUBYARCHDIR)
194
- clean-static::
195
- -$(Q)$(RM) $(STATIC_LIB)
196
- install-rb: pre-install-rb do-install-rb install-rb-default
197
- install-rb-default: pre-install-rb-default do-install-rb-default
198
- pre-install-rb: Makefile
199
- pre-install-rb-default: Makefile
200
- do-install-rb:
201
- do-install-rb-default:
202
- pre-install-rb-default:
203
- @$(NULLCMD)
204
- $(TARGET_SO_DIR_TIMESTAMP):
205
- $(Q) $(MAKEDIRS) $(@D) $(RUBYARCHDIR)
206
- $(Q) $(TOUCH) $@
207
-
208
- site-install: site-install-so site-install-rb
209
- site-install-so: install-so
210
- site-install-rb: install-rb
211
-
212
- .SUFFIXES: .c .m .cc .mm .cxx .cpp .o .S
213
-
214
- .cc.o:
215
- $(ECHO) compiling $(<)
216
- $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $(CSRCFLAG)$<
217
-
218
- .cc.S:
219
- $(ECHO) translating $(<)
220
- $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -S $(CSRCFLAG)$<
221
-
222
- .mm.o:
223
- $(ECHO) compiling $(<)
224
- $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $(CSRCFLAG)$<
225
-
226
- .mm.S:
227
- $(ECHO) translating $(<)
228
- $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -S $(CSRCFLAG)$<
229
-
230
- .cxx.o:
231
- $(ECHO) compiling $(<)
232
- $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $(CSRCFLAG)$<
233
-
234
- .cxx.S:
235
- $(ECHO) translating $(<)
236
- $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -S $(CSRCFLAG)$<
237
-
238
- .cpp.o:
239
- $(ECHO) compiling $(<)
240
- $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $(CSRCFLAG)$<
241
-
242
- .cpp.S:
243
- $(ECHO) translating $(<)
244
- $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -S $(CSRCFLAG)$<
245
-
246
- .c.o:
247
- $(ECHO) compiling $(<)
248
- $(Q) $(CC) $(INCFLAGS) $(CPPFLAGS) $(CFLAGS) $(COUTFLAG)$@ -c $(CSRCFLAG)$<
249
-
250
- .c.S:
251
- $(ECHO) translating $(<)
252
- $(Q) $(CC) $(INCFLAGS) $(CPPFLAGS) $(CFLAGS) $(COUTFLAG)$@ -S $(CSRCFLAG)$<
253
-
254
- .m.o:
255
- $(ECHO) compiling $(<)
256
- $(Q) $(CC) $(INCFLAGS) $(CPPFLAGS) $(CFLAGS) $(COUTFLAG)$@ -c $(CSRCFLAG)$<
257
-
258
- .m.S:
259
- $(ECHO) translating $(<)
260
- $(Q) $(CC) $(INCFLAGS) $(CPPFLAGS) $(CFLAGS) $(COUTFLAG)$@ -S $(CSRCFLAG)$<
261
-
262
- $(TARGET_SO): $(OBJS) Makefile
263
- $(ECHO) linking shared-object $(DLLIB)
264
- -$(Q)$(RM) $(@)
265
- $(Q) $(LDSHARED) -o $@ $(OBJS) $(LIBPATH) $(DLDFLAGS) $(LOCAL_LIBS) $(LIBS)
266
- $(Q) $(POSTLINK)
267
-
268
-
269
-
270
- $(OBJS): $(HDRS) $(ruby_headers)
Binary file
data/ext/escape.o DELETED
Binary file
data/ext/extconf.h DELETED
@@ -1,5 +0,0 @@
1
- #ifndef EXTCONF_H
2
- #define EXTCONF_H
3
- #define HAVE_RB_SYM2STR 1
4
- #define HAVE_RB_STR_CAT_CSTR 1
5
- #endif
data/ext/markup.o DELETED
Binary file
data/ext/query.o DELETED
Binary file
data/ext/tag.o DELETED
Binary file
data/ext/template.o DELETED
Binary file
data/ext/xrb.o DELETED
Binary file