sculpt 0.1

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,72 @@
1
+ class Tag < ElementContainer
2
+ include SculptHelpers
3
+
4
+ attr_accessor :name, :text, :attrs, :singelton, :inline
5
+
6
+ def initialize(name,text = '', attrs = {},&block)
7
+ @name = name
8
+ @attrs = {}
9
+ @elements = []
10
+ @inline = false
11
+
12
+ if text.kind_of?(Hash)
13
+ if attrs.kind_of? Tag
14
+ @inline = attrs
15
+ @elements << attrs
16
+ return self
17
+ else
18
+ @attrs = attrs
19
+ @attrs.merge!(text)
20
+ @text = '' # otherwise stays nil
21
+ end
22
+ elsif text.kind_of?(Tag)
23
+ @inline = text
24
+ @elements << text
25
+ @text = ''
26
+ @attrs = attrs
27
+ return self
28
+ else
29
+ @text = text
30
+ @attrs = attrs
31
+ end
32
+
33
+ if block_given?
34
+ @elements += elements_from_block(&block)
35
+ end
36
+ @singleton = is_singleton? name
37
+ end
38
+
39
+ def generate_html(ugly = false)
40
+ pp = Sculpt.pretty and not ugly
41
+
42
+ n = @name.to_s
43
+ open = "<#{n}"
44
+ @attrs.each do |k, v|
45
+ open += " #{k}=\"#{v}\""
46
+ end
47
+ open += '>'
48
+ return open if @singleton
49
+ close = "</#{n}>"
50
+ return open + @text + close unless @elements.any?
51
+ html = ''
52
+ @elements.each do |element|
53
+ html += element.generate_html
54
+ html += "\n" if pp unless inline or element.kind_of? Static
55
+ end
56
+ return "#{open}\n#{html}#{close}" if pp and not inline
57
+ return open + html + close
58
+ end
59
+ end
60
+
61
+ class Static
62
+ attr_accessor :string
63
+
64
+ def initialize(string)
65
+ @string = string
66
+ end
67
+
68
+ def generate_html
69
+ return @string+"\n" if Sculpt.pretty?
70
+ @string
71
+ end
72
+ end
@@ -0,0 +1,29 @@
1
+ module SculptHelpers
2
+ def elements_from_block(&block)
3
+ result = Sculpture.new
4
+ result.instance_eval(&block)
5
+ result.elements
6
+ end
7
+
8
+ def is_singleton? tag
9
+ [:area, :base, :br, :col, :command, :embed, :hr, :img, :input, :link, :meta, :param, :source].include? tag
10
+ end
11
+
12
+ def special_attr(att, val, extra)
13
+ attrs = {att => val}
14
+ return attrs.merge(extra) if extra.kind_of? Hash
15
+ attrs
16
+ end
17
+ end
18
+
19
+ # plus some string additions from mynyml/unindent on github
20
+
21
+ class String
22
+ def unindent
23
+ indent = self.split("\n").select {|line| !line.strip.empty? }.map {|line| line.index(/[^\s]/) }.compact.min || 0
24
+ self.gsub(/^[[:blank:]]{#{indent}}/, '')
25
+ end
26
+ def unindent!
27
+ self.replace(self.unindent)
28
+ end
29
+ end
@@ -0,0 +1,133 @@
1
+ class Sculpture < ElementContainer
2
+ #
3
+ # This is a container class for elements.
4
+ # It responds to all the method calls in a block that generates HTMl.
5
+ #
6
+
7
+ include SculptHelpers
8
+
9
+ def generate_html
10
+ @elements.map(&:generate_html).join('')
11
+ end
12
+
13
+ def add_tag(tag)
14
+ @elements << tag
15
+ @elements.delete(tag.inline) if tag.inline # dupe prevention
16
+ tag
17
+ end
18
+
19
+ #
20
+ # block methods
21
+ # use these to make elements
22
+ #
23
+
24
+ # kenrnel overrides here
25
+
26
+ def p(text='',attrs = {},&block)
27
+ # necessary to override the Kernel#p method
28
+ add_tag Tag.new(:p, text.unindent.lstrip, attrs, &block)
29
+ end
30
+
31
+ def puts(text)
32
+ @elements << Static.new(text.unindent.lstrip)
33
+ end
34
+
35
+ def pbr(text)
36
+ # adds raw text plus a line break
37
+ @elements << Static.new(text + '<br>')
38
+ end
39
+
40
+ # other constructors here
41
+
42
+ def doctype
43
+ @elements << Static.new('<!DOCTYPE html>') # enforce HTML5
44
+ end
45
+
46
+ def js(*args)
47
+ args.each do |arg|
48
+ attrs = {type:"text/javascript", src:arg}
49
+ add_tag Tag.new(:script, attrs)
50
+ end
51
+ end
52
+
53
+ private
54
+ def _stylesheet(name)
55
+ attrs = {type:"text/css", rel:"stylesheet", href:name}
56
+ add_tag Tag.new(:link, attrs)
57
+ end
58
+
59
+ def css(*args)
60
+ if args[0].respond_to? :to_a
61
+ args[0].each {|sheet| _stylesheet sheet}
62
+ end
63
+ args.each {|sheet| _stylesheet sheet}
64
+ end
65
+
66
+ def a(text, href = '', ahash = {}, &block)
67
+ # funky constructor for easier linking
68
+ attrs = special_attr(:href, href, ahash)
69
+ add_tag Tag.new(:a, text, attrs, &block)
70
+ end
71
+
72
+ def img(src, ahash = {})
73
+ # funky img constructor
74
+ attrs = special_attr(:src, src, ahash)
75
+ add_tag Tag.new(:img, attrs)
76
+ end
77
+
78
+ private
79
+ def _listgen(list_type, tarr, attrs, &block)
80
+ # constructor for lists from arrays. e.g. => ul [1,2,3]
81
+ tag = Tag.new(list_type)
82
+ tag.attrs = attrs
83
+ if tarr.kind_of? Array or tarr.kind_of? Range
84
+ tarr.each do |item|
85
+ tag.elements << Tag.new(:li, item)
86
+ end
87
+ elsif tarr.respond_to? :to_s and not tarr.empty?
88
+ tag.text = tarr
89
+ elsif block_given?
90
+ tag.elements += elements_from_block(&block)
91
+ end
92
+ add_tag tag
93
+ end
94
+
95
+ def ul(tarr = '', attrs = {}, &block)
96
+ _listgen(:ul, tarr, attrs, &block)
97
+ end
98
+
99
+ def ol(tarr, attrs = {}, &block)
100
+ _listgen(:ol, tarr, attrs, &block)
101
+ end
102
+
103
+ def method_missing(method, *args, &block)
104
+ #
105
+ # general tag constructor
106
+ # custom tag initialisation here
107
+ #
108
+
109
+ arg1 = args[0]
110
+ arg1 = '' unless arg1
111
+ arg2 = args[1]
112
+ arg2 = {} unless arg2
113
+ arg3 = args[2]
114
+
115
+ m = method.to_s
116
+ if m.end_with? '_s'
117
+ # inline string methods
118
+ meth = m[0..-3].to_sym
119
+ case meth
120
+ when :a
121
+ attrs = special_attr(:href, arg2, arg3)
122
+ tag = Tag.new(:a, arg1, attrs, &block)
123
+ when :img
124
+ attrs = special_attr(:src, arg1, arg2)
125
+ tag = Tag.new(:img, attrs)
126
+ else
127
+ tag = Tag.new(meth, arg1, arg2, &block)
128
+ end
129
+ return tag.generate_html
130
+ end
131
+ add_tag Tag.new(method, arg1, arg2, &block)
132
+ end
133
+ end
data/lib/sculpt.rb ADDED
@@ -0,0 +1,56 @@
1
+ #
2
+ # Sculpt
3
+ #
4
+ # An HTMl5 generator in Ruby.
5
+ # Syntax is everything.
6
+ #
7
+
8
+ class ElementContainer
9
+ attr_accessor :elements
10
+
11
+ def initialize
12
+ @elements = []
13
+ end
14
+ end
15
+
16
+ require_relative 'sculpt/sculpt_helpers'
17
+ require_relative 'sculpt/elements'
18
+ require_relative 'sculpt/sculpture'
19
+
20
+ class Sculpt
21
+ #
22
+ # The Sculpt class is used externally.
23
+ # Most of the time this is the only class users will play with.
24
+ #
25
+
26
+ class << self
27
+ attr_accessor :pretty
28
+ end
29
+
30
+ def self.pretty?
31
+ @pretty
32
+ end
33
+
34
+ def self.render_doc(&block)
35
+ puts self.make_doc(&block)
36
+ end
37
+
38
+ def self.make_doc(&block)
39
+ self.make do
40
+ doctype
41
+ html(&block)
42
+ end
43
+ end
44
+
45
+ def self.render(&block)
46
+ puts self.make(&block)
47
+ end
48
+
49
+ def self.make(&block)
50
+ result = Sculpture.new
51
+ result.instance_eval(&block)
52
+ result.generate_html
53
+ end
54
+ end
55
+
56
+ Sculpt.pretty = true # pretty print by default
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sculpt
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Alex Coplan
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-06-10 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: ! "Sculpt is an HTML generator in Ruby.\n Syntax is everything"
15
+ email: lexy0202@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/sculpt/elements.rb
21
+ - lib/sculpt/sculpt_helpers.rb
22
+ - lib/sculpt/sculpture.rb
23
+ - lib/sculpt.rb
24
+ homepage: http://github.com/alexcoplan/sculpt
25
+ licenses: []
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ! '>='
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ required_rubygems_version: !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ! '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ requirements: []
43
+ rubyforge_project:
44
+ rubygems_version: 1.8.24
45
+ signing_key:
46
+ specification_version: 3
47
+ summary: An HTML generator in Ruby.
48
+ test_files: []