Remarkably 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,89 +1,62 @@
1
1
  module Remarkably
2
- def method_missing(sym, *args, &block)
3
- hash = args.last.is_a?(Hash) ? args.pop : {}
4
- tag!(sym, args, hash, &block)
5
- end
6
2
 
7
- def tag_xml!( tag, inline, attributes, &block )
8
- tag_attributes =
9
- attributes.inject([]){|s,(k,v)| s << %{#{k.to_s.downcase}="#{v}"} }
3
+ module Base
10
4
 
11
- @remarkably << ["<#{tag}", tag_attributes].flatten.grep(/\S/).join(' ')
5
+ class Engine
6
+ def initialize *args, &block
7
+ clear!
8
+ self.instance_eval( &block ) if block_given?
9
+ end
12
10
 
13
- if block_given? or not inline.empty?
14
- @remarkably << ">"
15
- @remarkably_method = :css if @remarkably_method == :xml and tag == "style"
16
- block.call if block_given?
17
- if @remarkably_method == :css and tag == "style"
18
- @remarkably_method = :xml
19
- @remarkably << "\n"
11
+ def missing_method(sym, context, *args, &block)
12
+ @context = context
13
+ hash = args.last.is_a?(Hash) ? args.pop : {}
14
+ if methods.index( sym.to_s )
15
+ self.send( sym, args, hash, &block )
16
+ else
17
+ method!(sym, args, hash, &block)
18
+ end
19
+ end
20
+
21
+ def method_missing(sym, *args, &block)
22
+ missing_method( sym, self, *args, &block )
23
+ end
24
+
25
+ def clear!
26
+ @output = ''
27
+ end
28
+
29
+ def method! sym, args, hash, &block
30
+ end
31
+
32
+ def to_s
33
+ result = @output
34
+ clear!
35
+ result
20
36
  end
21
- @remarkably << "#{inline.join}</#{tag}>"
22
- else
23
- @remarkably << "/>"
24
- end
25
- end
26
37
 
27
- def tag_css!( tag, inline, attributes, &block )
28
- @remarkably_method = :css_inner
29
- if block_given?
30
- @remarkably << "\n "
31
- tag_css_inner!( tag, inline, attributes, &block )
32
- @remarkably << "}"
33
38
  end
34
- @remarkably_method = :css
35
- end
36
39
 
37
- def inline_style &block
38
- current_remarkably = @remarkably
39
- @remarkably_method = :css_inner
40
- @remarkably = ''
41
- block.call if block_given?
42
- result = @remarkably
43
- @remarkably_method = :xml
44
- @remarkably = current_remarkably
45
- result
46
40
  end
47
41
 
48
- def tag_css_inner!( tag, inline, attributes, &block )
49
- if block_given?
50
- @remarkably.chop!
51
- @remarkably << "#{tag}#{inline.join}"
52
- @remarkably << ".#{attributes[:class]}" if attributes.has_key?( :class )
53
- @remarkably << "##{attributes[:id]}" if attributes.has_key?( :id )
54
- @remarkably << ":#{attributes[:pseudo]}" if attributes.has_key?( :pseudo )
55
- @remarkably << " {"
56
- block.call
57
- else
58
- tag.gsub!('_','-')
59
- @remarkably << "#{tag}:#{inline.join(' ')};"
42
+ module Common
43
+
44
+ def method_missing(sym, *args, &block)
45
+ @remarkably_engine ||= eval("Engines::#{Engines::constants[0]}.new")
46
+ @remarkably_engine.send( :missing_method, sym, self, *args, &block )
60
47
  end
61
- end
62
48
 
63
- def tag!(tag, inline=[], attributes={}, &block)
64
- tag = tag.to_s.downcase
65
- inline = [inline] if inline.class != Array
66
- @remarkably ||= ''
67
- @remarkably_method ||= :xml
68
-
69
- case @remarkably_method
70
- when :xml then tag_xml!( tag, inline, attributes, &block )
71
- when :css then tag_css!( tag, inline, attributes, &block )
72
- when :css_inner then tag_css_inner!( tag, inline, attributes, &block )
73
- else raise "Unknown remarkably method type '#{@remarkably_method}'"
49
+ def remarkably_engine= engine
50
+ @remarkably_engine = engine
74
51
  end
75
52
 
76
- self
77
- end
53
+ def remarkably_engine
54
+ @remarkably_engine
55
+ end
78
56
 
79
- def text content
80
- @remarkably << content
81
- self
82
57
  end
83
58
 
84
- def to_s
85
- result = @remarkably
86
- @remarkably = nil
87
- result
59
+ module Engines
88
60
  end
61
+
89
62
  end
@@ -0,0 +1,47 @@
1
+ require "#{File::dirname(__FILE__)}/../../remarkably"
2
+
3
+ module Remarkably
4
+ module Engines
5
+ class CSS < Base::Engine
6
+ def method! sym, args, hash, &block
7
+ sym = sym.to_s.downcase
8
+
9
+ if block_given?
10
+ current_prefix = @css_prefix
11
+ @css_prefix = (@css_prefix+" #{sym}#{args.map{|a|a.to_s}.join}").strip
12
+ @css_depth+=1
13
+ @css_prefix_rendered=false
14
+ block.call
15
+ @css_depth-=1
16
+ @css_prefix = current_prefix
17
+ else
18
+ unless @css_prefix_rendered
19
+ unless @css_first_use
20
+ @output.chop!
21
+ @output << "}"
22
+ end
23
+ @output << "\n#{@css_prefix} {"
24
+ @css_prefix_rendered = true
25
+ @css_first_use = false
26
+ end
27
+ @output << "#{sym.gsub('_','-')}:#{args.map{|a|a.to_s}.join(' ')};"
28
+ end
29
+ self
30
+ end
31
+
32
+ def to_s
33
+ "#{super.chop}}".strip+"\n"
34
+ end
35
+
36
+ def clear!
37
+ super
38
+ @css_prefix=''
39
+ @css_depth=0
40
+ @css_prefix_rendered=false
41
+ @css_first_use=true
42
+ end
43
+
44
+ end
45
+ end
46
+ end
47
+
@@ -0,0 +1,7 @@
1
+ [ Fixnum, Float ].each do |extend_class|
2
+ extend_class.class_eval do
3
+ %w{em ex cm in mm pt pc px}.each do |unit|
4
+ define_method( unit ) {"#{self}#{unit}"}
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,24 @@
1
+ require "#{File::dirname(__FILE__)}/../../remarkably"
2
+ require "#{File::dirname(__FILE__)}/xml"
3
+ require "#{File::dirname(__FILE__)}/css"
4
+
5
+ module Remarkably
6
+ module Engines
7
+ class HTML < XML
8
+ def initialize
9
+ super
10
+ @css_engine = Engines::CSS.new
11
+ end
12
+ def style args, hash, &block
13
+ if block_given?
14
+ @css_engine.clear!
15
+ @context.remarkably_engine = @css_engine
16
+ block.call
17
+ @context.remarkably_engine = self
18
+ args = ["\n#{@css_engine}"]+args
19
+ end
20
+ method!( :style, args, hash )
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,33 @@
1
+ require "#{File::dirname(__FILE__)}/../../remarkably"
2
+
3
+ module Remarkably
4
+ module Engines
5
+ class XML < Base::Engine
6
+
7
+ def method! sym, args, hash, &block
8
+ sym = sym.to_s.downcase
9
+
10
+ tag_attributes =
11
+ hash.inject([]){|s,(k,v)| s << %{#{k.to_s.downcase}="#{v}"} }
12
+
13
+ @output << ["<#{sym}", tag_attributes].flatten.grep(/\S/).join(' ')
14
+
15
+ if block_given? or not args.empty?
16
+ @output << ">"
17
+ block.call if block_given?
18
+ @output << "#{args.join}</#{sym}>"
19
+ else
20
+ @output << "/>"
21
+ end
22
+
23
+ self
24
+ end
25
+
26
+ def text args, hash, &block
27
+ @output << args.join
28
+ self
29
+ end
30
+
31
+ end
32
+ end
33
+ end
metadata CHANGED
@@ -1,10 +1,10 @@
1
1
  --- !ruby/object:Gem::Specification
2
- rubygems_version: 0.9.2
2
+ rubygems_version: 0.9.3
3
3
  specification_version: 1
4
4
  name: Remarkably
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.4.0
7
- date: 2007-05-16 00:00:00 +02:00
6
+ version: 0.5.0
7
+ date: 2007-05-24 00:00:00 +02:00
8
8
  summary: Remarkably is a very tiny Markaby-like XML builder
9
9
  require_paths:
10
10
  - lib
@@ -29,7 +29,14 @@ post_install_message:
29
29
  authors:
30
30
  - Clive Crous
31
31
  files:
32
+ - lib/remarkably
32
33
  - lib/remarkably.rb
34
+ - lib/remarkably/engines
35
+ - lib/remarkably/engines/css
36
+ - lib/remarkably/engines/xml.rb
37
+ - lib/remarkably/engines/html.rb
38
+ - lib/remarkably/engines/css.rb
39
+ - lib/remarkably/engines/css/helpers.rb
33
40
  test_files: []
34
41
 
35
42
  rdoc_options: []