xrb 0.2.0 → 0.4.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/xrb/parsers.rb CHANGED
@@ -4,7 +4,6 @@
4
4
  # Copyright, 2012-2024, by Samuel Williams.
5
5
 
6
6
  require_relative 'native'
7
- require_relative 'parse_delegate'
8
7
 
9
8
  if defined? XRB::Native
10
9
  XRB::Parsers = XRB::Native
data/lib/xrb/template.rb CHANGED
@@ -20,7 +20,7 @@ module XRB
20
20
  end
21
21
 
22
22
  class Template
23
- # Returns the output produced by calling the given block.
23
+ # @returns [String] the output produced by calling the given block.
24
24
  def self.capture(*arguments, output: nil, &block)
25
25
  scope = block.binding
26
26
  previous_output = scope.local_variable_get(OUT)
@@ -34,7 +34,7 @@ module XRB
34
34
  scope.local_variable_set(OUT, previous_output)
35
35
  end
36
36
 
37
- return output
37
+ return output.to_str
38
38
  end
39
39
 
40
40
  # Returns the buffer used for capturing output.
@@ -46,13 +46,13 @@ module XRB
46
46
  def initialize(encoding: Encoding::UTF_8)
47
47
  @code = String.new.force_encoding(encoding)
48
48
  end
49
-
49
+
50
50
  attr :code
51
-
51
+
52
52
  # Output raw text to the template.
53
53
  def text(text)
54
54
  text = text.gsub("'", "\\\\'")
55
- @code << "#{OUT}<<'#{text}';"
55
+ @code << "#{OUT}.raw('#{text}');"
56
56
 
57
57
  # This is an interesting approach, but it doens't preserve newlines or tabs as raw characters, so template line numbers don't match up.
58
58
  # @parts << "#{OUT}<<#{text.dump};"
@@ -64,9 +64,8 @@ module XRB
64
64
  end
65
65
 
66
66
  # Output a string interpolation.
67
- def expression(text)
68
- # Double brackets are required here to handle expressions like #{foo rescue "bar"}.
69
- @code << "#{OUT}<<String(#{text});"
67
+ def expression(code)
68
+ @code << "#{OUT}<<(#{code});"
70
69
  end
71
70
  end
72
71
 
@@ -93,11 +92,11 @@ module XRB
93
92
  end
94
93
 
95
94
  def to_string(scope = Object.new, output = nil)
96
- output ||= output_buffer
95
+ builder = Builder.new(output, encoding: code.encoding)
97
96
 
98
- scope.instance_exec(output, &to_proc)
97
+ scope.instance_exec(builder, &to_proc)
99
98
 
100
- return output
99
+ return builder.output
101
100
  end
102
101
 
103
102
  def to_buffer(scope)
@@ -110,10 +109,6 @@ module XRB
110
109
 
111
110
  protected
112
111
 
113
- def output_buffer
114
- String.new.force_encoding(code.encoding)
115
- end
116
-
117
112
  def code
118
113
  @code ||= compile!
119
114
  end
@@ -130,35 +125,4 @@ module XRB
130
125
  assembler.code
131
126
  end
132
127
  end
133
-
134
- class MarkupTemplate < Template
135
- class Assembler < Template::Assembler
136
- # Output a string interpolation.
137
- def expression(code)
138
- @code << "#{OUT}<<(#{code});"
139
- end
140
-
141
- # Output raw text to the template.
142
- def text(text)
143
- text = text.gsub("'", "\\\\'")
144
- @code << "#{OUT}.raw('#{text}');"
145
- end
146
- end
147
-
148
- def to_string(scope = Object.new, output = nil)
149
- super.output
150
- end
151
-
152
- protected
153
-
154
- # We need an assembler which builds specific `Markup.append` sequences.
155
- def make_assembler
156
- Assembler.new
157
- end
158
-
159
- # The output of the markup template is encoded markup (e.g. with entities, tags, etc).
160
- def output_buffer
161
- Builder.new(encoding: code.encoding)
162
- end
163
- end
164
128
  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.2.0"
7
+ VERSION = "0.4.0"
8
8
  end
data/readme.md CHANGED
@@ -4,11 +4,52 @@ XRB is a templating system built loosely on top of XHTML markup. It uses efficie
4
4
 
5
5
  [![Development Status](https://github.com/socketry/xrb/workflows/Test/badge.svg)](https://github.com/socketry/xrb/actions?workflow=Test)
6
6
 
7
+ ``` xrb
8
+ <h1>XRB Examples</h1>
9
+
10
+ <ul>
11
+ <li>XRB uses normal string interpolation syntax: #{"Hello World"}</li>
12
+
13
+ <?r if true ?>
14
+ <li>XRB also adopts standard #{"<?r ... ?>"} script tags</li>
15
+ <?r end ?>
16
+
17
+ <!-- That's all of the syntax! -->
18
+ <ul>
19
+ ```
20
+
21
+ Generates:
22
+
23
+ ``` xml
24
+ <h1>XRB Examples</h1>
25
+
26
+ <ul>
27
+ <li>XRB uses normal string interpolation syntax: Hello World</li>
28
+
29
+ <li>XRB also adopts standard &lt;?r ... ?&gt; script tags</li>
30
+
31
+ <!-- That's all of the syntax! -->
32
+ <ul>
33
+ ```
34
+
35
+ ## Is it fast?
36
+
37
+ Yes. XRB is designed to be fast. It uses a combination of efficient native parsers and Ruby code generation to ensure that templates are compiled into efficient Ruby code. This means that XRB is suitable for use in high performance applications. In comparison to `ERB`, it is generally about 10x faster.
38
+
7
39
  ## Usage
8
40
 
41
+ Please see the [project documentation](https://socketry.github.io/xrb/) for more details.
42
+
43
+ - [Getting Started](https://socketry.github.io/xrb/guides/getting-started/index) - This guide gives a brief overview of the XRB templating system and how to use it.
44
+
45
+ - [Capturing Output](https://socketry.github.io/xrb/guides/capturing-output/index) - This guide explains how to capture intermediate output during template rendering.
46
+
47
+ - [Markup Parser](https://socketry.github.io/xrb/guides/markup-parser/index) - This guide explains how to parse and manipulate Markup using the XRB templating system.
48
+
9
49
  ## See Also
10
50
 
11
- - [xrb-vscode](https://github.com/socketry/xrb-vscode) package for Visual Studio Code.
51
+ - [xrb-vscode](https://github.com/socketry/xrb-vscode) - A syntax highlighting package for Visual Studio Code.
52
+ - [trenni](https://github.com/ioquatix/trenni) - The original templating system which XRB is based on.
12
53
 
13
54
  ## Contributing
14
55
 
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.2.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Williams
@@ -39,7 +39,7 @@ cert_chain:
39
39
  Q2K9NVun/S785AP05vKkXZEFYxqG6EW012U4oLcFl5MySFajYXRYbuUpH6AY+HP8
40
40
  voD0MPg1DssDLKwXyt1eKD/+Fq0bFWhwVM/1XiAXL7lyYUyOq24KHgQ2Csg=
41
41
  -----END CERTIFICATE-----
42
- date: 2024-04-25 00:00:00.000000000 Z
42
+ date: 2024-04-28 00:00:00.000000000 Z
43
43
  dependencies: []
44
44
  description:
45
45
  email:
@@ -80,7 +80,6 @@ files:
80
80
  - lib/xrb/fallback/template.rl
81
81
  - lib/xrb/markup.rb
82
82
  - lib/xrb/native.rb
83
- - lib/xrb/parse_delegate.rb
84
83
  - lib/xrb/parsers.rb
85
84
  - lib/xrb/query.rb
86
85
  - lib/xrb/reference.rb
metadata.gz.sig CHANGED
Binary file
@@ -1,19 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # Released under the MIT License.
4
- # Copyright, 2016-2024, by Samuel Williams.
5
-
6
- module XRB
7
- # This is a sample delegate for capturing all events. It's only use is for testing.
8
- class ParseDelegate
9
- def initialize
10
- @events = []
11
- end
12
-
13
- attr :events
14
-
15
- def method_missing(*args)
16
- @events << args
17
- end
18
- end
19
- end