File: radius.rb

package info (click to toggle)
ruby-tilt 2.7.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 632 kB
  • sloc: ruby: 4,975; makefile: 7
file content (96 lines) | stat: -rw-r--r-- 2,083 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# frozen_string_literal: true

# = Radius (<tt>radius</tt>)
#
# {Radius}[http://radius.rubyforge.org] is the template language used by {Radiant CMS}[http://radiantcms.org]. It is
# a tag language designed to be valid XML/HTML.
#
# === Example
#
#     <html>
#     <body>
#       <h1><r:title /></h1>
#       <ul class="<r:type />">
#       <r:repeat times="3">
#         <li><r:hello />!</li>
#       </r:repeat>
#       </ul>
#       <r:yield />
#     </body>
#     </html>
#
# === Usage
#
# To render a template such as the one above.
#
#     scope = OpenStruct.new
#     scope.title = "Radius Example"
#     scope.hello = "Hello, World!"
#
#     require 'radius'
#     template = Tilt::RadiusTemplate.new('example.radius', :tag_prefix=>'r')
#     template.render(scope, :type=>'hlist'){ "Jackpot!" }
#
# The result will be:
#
#     <html>
#     <body>
#       <h1>Radius Example</h1>
#       <ul class="hlist">
#         <li>Hello, World!</li>
#         <li>Hello, World!</li>
#         <li>Hello, World!</li>
#       </ul>
#       Jackpot!
#     </body>
#     </html>
#
# === See also
#
# * {Radius}[http://radius.rubyforge.org]
# * {Radiant CMS}[http://radiantcms.org]
#
# === Related module
#
# * Tilt::RadiusTemplate

require_relative 'template'
require 'radius'

module Tilt
  # Radius Template
  # http://github.com/jlong/radius/
  class RadiusTemplate < Template
    class ContextClass < Radius::Context
      attr_accessor :tilt_scope

      def tag_missing(name, attributes)
        tilt_scope.__send__(name)
      end

      def dup
        i = super
        i.tilt_scope = tilt_scope
        i
      end
    end

    def evaluate(scope, locals, &block)
      context = ContextClass.new
      context.tilt_scope = scope
      context.define_tag("yield", &block) if block
      locals.each do |tag, value|
        context.define_tag(tag) do
          value
        end
      end

      @options[:tag_prefix] = 'r' unless @options.has_key?(:tag_prefix)
      Radius::Parser.new(context, @options).parse(@data)
    end

    def allows_script?
      false
    end
  end
end