neapolitan 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,13 +0,0 @@
1
- name : neapolitan
2
- date : 2010-09-14
3
- version : 0.2.0
4
-
5
- requires:
6
- - malt
7
- - rdiscount (optional, development)
8
- - redcloth (optional, development)
9
- - haml (optional, development)
10
- - rdoc 2.5+ (optional, development, document)
11
- - syckle (build)
12
- - qed (test)
13
-
@@ -1,36 +0,0 @@
1
- require 'neapolitan/factory'
2
-
3
- module Neapolitan
4
-
5
- # A Part is the section of a page. Pages can be segmented into
6
- # parts using the '--- FORMAT' notation.
7
- class Part
8
-
9
- # Markup format (html, rdoc, markdown, textile)
10
- attr :formats
11
-
12
- # Body of text as given in the part.
13
- attr :text
14
-
15
- #
16
- def initialize(text, *formats)
17
- @text = text
18
- @formats = formats
19
- end
20
-
21
- #
22
- def render(data, &block)
23
- formats.inject(text) do |rendering, format|
24
- factory.render(format, rendering, data, &block)
25
- end
26
- end
27
-
28
- #
29
- def factory
30
- Factory
31
- end
32
-
33
- end
34
-
35
- end
36
-
@@ -1,150 +0,0 @@
1
- require 'neapolitan/part'
2
-
3
- module Neapolitan
4
-
5
- #
6
- class Template
7
-
8
- # Template text.
9
- attr :text
10
-
11
- # Header data, also known as <i>front matter</i>.
12
- attr :header
13
-
14
- # Templating format to apply "whole-clothe".
15
- attr :stencil
16
-
17
- # Default format(s) for undecorated parts.
18
- # If not set defaults to 'html'.
19
- attr :default
20
-
21
- ## Output extension (defualt is 'html')
22
- #attr :extension
23
-
24
- # Provide template +text+, +data+ and yield +block+.
25
- def initialize(text, options={})
26
- @text = text
27
- @stencil = options[:stencil]
28
- @default = [options[:default] || 'html'].flatten
29
- @parts = []
30
- parse
31
- end
32
-
33
- #
34
- def inspect
35
- "<#{self.class}: @text='#{text[0,10]}'>"
36
- end
37
-
38
- # Unrendered template parts.
39
- def parts
40
- @parts
41
- end
42
-
43
- #
44
- def render(data={}, &block)
45
- Rendering.new(self, data, &block)
46
- end
47
-
48
- private
49
-
50
- #
51
- def parse
52
- sect = text.split(/^\-\-\-/)
53
-
54
- if sect.size == 1
55
- @header = {}
56
- @parts << Part.new(sect[0], *[@stencil, @default].compact.flatten)
57
- else
58
- sect.shift if sect.first.strip.empty?
59
- #void = sect.shift if sect.first.strip.empty?
60
- head = sect.shift
61
- head = YAML::load(head)
62
- parse_header(head)
63
-
64
- sect.each do |body|
65
- index = body.index("\n")
66
- formats = body[0...index].strip
67
- formats = formats.split(/\s+/) if String===formats
68
- formats = @default if formats.empty?
69
- formats << @stencil if @stencil
70
- text = body[index+1..-1]
71
- @parts << Part.new(text, *formats)
72
- end
73
- end
74
- end
75
-
76
- #
77
- def parse_header(head)
78
- @header = head
79
- @stencil = head.delete('stencil'){ @stencil }
80
- @default = head.delete('default'){ @default }
81
- #@extension = head.delete('extension'){ @extension }
82
- end
83
-
84
- end
85
-
86
- # Template Rendering
87
- class Rendering
88
-
89
- #
90
- attr :template
91
-
92
- # Source of data, can either be an
93
- # Hash, Binding or Object.
94
- attr :data
95
-
96
- #
97
- attr :block
98
-
99
- #
100
- def initialize(template, data, &block)
101
- @template = template
102
- @data = data
103
- @block = block
104
-
105
- if !@block
106
- case data
107
- when Hash
108
- yld = data.delete('yield')
109
- @block = Proc.new{ yld } if yld
110
- end
111
- @block = Proc.new{''} unless @block
112
- end
113
-
114
- render
115
- end
116
-
117
- def to_s
118
- #render unless @output
119
- @output
120
- end
121
-
122
- # Renderings of each part.
123
- def to_a
124
- #render unless @output
125
- @renders
126
- end
127
-
128
- # Summary is the rendering of the first part.
129
- def summary
130
- #render unless @output
131
- @summary
132
- end
133
-
134
- #
135
- def header
136
- @template.header
137
- end
138
-
139
- private
140
-
141
- def render
142
- @renders = @template.parts.map{ |part| part.render(@data, &@block) }
143
- @summary = @renders.first
144
- @output = @renders.join("\n")
145
- end
146
-
147
- end
148
-
149
- end
150
-
@@ -1,13 +0,0 @@
1
- name : neapolitan
2
- date : 2010-09-14
3
- version : 0.2.0
4
-
5
- requires:
6
- - malt
7
- - rdiscount (optional, development)
8
- - redcloth (optional, development)
9
- - haml (optional, development)
10
- - rdoc 2.5+ (optional, development, document)
11
- - syckle (build)
12
- - qed (test)
13
-
@@ -1,39 +0,0 @@
1
- <h1>Yummy Choclate </h1>
2
- <p>
3
- Hi Ginger,
4
- </p>
5
- <p>
6
- I know you want some of that yummy stuff.
7
- </p>
8
-
9
- <div class="CodeRay">
10
- <div class="code"><pre>
11
- <span style="background-color:#fff0f0;color:#D20"><span style="color:#710">%{</span><span style="">c h o c o l a t e s</span><span style="color:#710">}</span></span>.each <span style="color:#080;font-weight:bold">do</span> |letter|
12
- puts <span style="background-color:#fff0f0;color:#D20"><span style="color:#710">&quot;</span><span style="">Give me a </span><span style="background:#ddd;color:black"><span style="background:#ddd;font-weight:bold;color:#666">#{</span>letter<span style="background:#ddd;font-weight:bold;color:#666">}</span></span><span style="">!</span><span style="color:#710">&quot;</span></span>
13
- <span style="color:#080;font-weight:bold">end</span>
14
-
15
- puts <span style="background-color:#fff0f0;color:#D20"><span style="color:#710">&quot;</span><span style="">What's that spell?</span><span style="color:#710">&quot;</span></span>
16
-
17
- </pre></div>
18
- </div>
19
-
20
-
21
- <quote>
22
- What can I say?
23
-
24
- </quote>
25
-
26
-
27
- <table>
28
- <tr>
29
- <td> </td>
30
- <td> 2009 </td>
31
- <td> 2010 </td>
32
- </tr>
33
- <tr>
34
- <td> Has Choclates? </td>
35
- <td> No </td>
36
- <td> Yes! </td>
37
- </tr>
38
- </table>
39
- <p>As you can see. It's all <em>fun</em> and <em>games</em> here.</p>
@@ -1,33 +0,0 @@
1
- extension: html
2
-
3
- --- rdoc erb
4
-
5
- = Yummy Choclate
6
-
7
- Hi <%= name %>,
8
-
9
- I know you want some of that yummy stuff.
10
-
11
- --- coderay.ruby
12
-
13
- %{c h o c o l a t e s}.each do |letter|
14
- puts "Give me a #{letter}!"
15
- end
16
-
17
- puts "What's that spell?"
18
-
19
- --- html liquid
20
-
21
- <quote>
22
- {{ yield }}
23
- </quote>
24
-
25
- --- textile
26
-
27
- | | 2009 | 2010 |
28
- | Has Choclates? | No | Yes! |
29
-
30
- --- markdown
31
-
32
- As you can see. It's all _fun_ and _games_ here.
33
-
@@ -1,5 +0,0 @@
1
- ---
2
- name: Ginger
3
- yield: |
4
- What can I say?
5
-
@@ -1,81 +0,0 @@
1
- = Example Neapolitan Document
2
-
3
- Here is an example neapolitan file, 'example.choc':
4
-
5
- extension: html
6
-
7
- --- rdoc erb
8
-
9
- = Yummy Vanilla
10
-
11
- Hi <%= name %>,
12
-
13
- I know you want some of that yummy stuff.
14
-
15
- --- coderay.ruby
16
-
17
- %{S t r a w b e r r y}.each do |letter|
18
- puts "Give me a #{letter}!"
19
- end
20
-
21
- puts "What's that spell?"
22
-
23
- --- html liquid
24
-
25
- <quote>
26
- {{ yield }}
27
- </quote>
28
-
29
- --- textile
30
-
31
- | | 2009 | 2010 |
32
- | Has Choclate? | No | Yes! |
33
-
34
- --- markdown
35
-
36
- As you can see. It's all _fun_ and _games_ here.
37
-
38
- = Loading the Library
39
-
40
- Require the library.
41
-
42
- require 'neapolitan'
43
-
44
- = Reading a Neapolitan File
45
-
46
- Load our example template[fixtures/example.npt].
47
-
48
- file = "qed/fixtures/example.npt"
49
-
50
- document = Neapolitan::Document.new(file)
51
-
52
- = Rendering Data Sources
53
-
54
- Neapolitan uses Malt on the backend. Malt supports a three separate ways to pass
55
- data into a template.
56
-
57
- The most obvious data source is a Hash.
58
-
59
- data = {:name=>"Tom"}
60
-
61
- text = document.render(data).to_s
62
-
63
- text.assert =~ /Hi Tom/
64
-
65
- Templates can also be rendered given a Binding.
66
-
67
- name = "Huck"
68
-
69
- text = document.render(binding).to_s
70
-
71
- text.assert =~ /Hi Huck/
72
-
73
- And lastly, they can be renderedwith the scope of any other type of Object,
74
- including an instance of a Struct.
75
-
76
- scope = Struct.new(:name).new("Becky")
77
-
78
- text = document.render(scope).to_s
79
-
80
- text.assert =~ /Hi Becky/
81
-