smartgen 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (50) hide show
  1. data/ChangeLog.md +4 -0
  2. data/Gemfile +11 -0
  3. data/Gemfile.lock +61 -0
  4. data/README.md +119 -0
  5. data/Rakefile +8 -0
  6. data/bin/smartgen +25 -0
  7. data/lib/smartgen.rb +21 -0
  8. data/lib/smartgen/configuration.rb +17 -0
  9. data/lib/smartgen/engines.rb +3 -0
  10. data/lib/smartgen/engines/base.rb +45 -0
  11. data/lib/smartgen/engines/markdown.rb +16 -0
  12. data/lib/smartgen/engines/textile.rb +16 -0
  13. data/lib/smartgen/generator.rb +104 -0
  14. data/lib/smartgen/markup_file.rb +47 -0
  15. data/lib/smartgen/object_hash.rb +41 -0
  16. data/lib/smartgen/renderers.rb +1 -0
  17. data/lib/smartgen/renderers/erb.rb +12 -0
  18. data/lib/smartgen/resource.rb +32 -0
  19. data/lib/smartgen/version.rb +4 -0
  20. data/spec/fixtures/expectations/common/another_index.html +13 -0
  21. data/spec/fixtures/expectations/common/index.html +8 -0
  22. data/spec/fixtures/expectations/common/other_index.html +13 -0
  23. data/spec/fixtures/expectations/with_layout/index.html +12 -0
  24. data/spec/fixtures/expectations/with_layout/index_with_metadata.html +43 -0
  25. data/spec/fixtures/expectations/with_layout/index_with_specific_metadata.html +44 -0
  26. data/spec/fixtures/src/assets/images/image.gif +0 -0
  27. data/spec/fixtures/src/assets/javascripts/somelib.js +2 -0
  28. data/spec/fixtures/src/assets/stylesheets/style.css +2 -0
  29. data/spec/fixtures/src/common/another_index.md +12 -0
  30. data/spec/fixtures/src/common/index.textile +10 -0
  31. data/spec/fixtures/src/common/other_index.markdown +12 -0
  32. data/spec/fixtures/src/layout.html.erb +5 -0
  33. data/spec/fixtures/src/layout_with_metadata.html.erb +22 -0
  34. data/spec/fixtures/src/layout_with_specific_metadata.html.erb +23 -0
  35. data/spec/fixtures/src/metadata.yml +43 -0
  36. data/spec/fixtures/src/with_layout/index.textile +10 -0
  37. data/spec/fixtures/src/with_layout/index_with_specific_metadata.textile +10 -0
  38. data/spec/lib/smartgen/configuration_spec.rb +30 -0
  39. data/spec/lib/smartgen/engines/base_spec.rb +71 -0
  40. data/spec/lib/smartgen/engines/markdown_spec.rb +23 -0
  41. data/spec/lib/smartgen/engines/textile_spec.rb +19 -0
  42. data/spec/lib/smartgen/generator_spec.rb +166 -0
  43. data/spec/lib/smartgen/markup_file_spec.rb +128 -0
  44. data/spec/lib/smartgen/object_hash_spec.rb +54 -0
  45. data/spec/lib/smartgen/renderers/erb_spec.rb +32 -0
  46. data/spec/lib/smartgen/resource_spec.rb +60 -0
  47. data/spec/lib/smartgen_spec.rb +18 -0
  48. data/spec/sandbox/.gitkeep +0 -0
  49. data/spec/spec_helper.rb +37 -0
  50. metadata +240 -0
@@ -0,0 +1,47 @@
1
+ require "active_support/inflector"
2
+ require 'active_support/core_ext/object/blank'
3
+
4
+ module Smartgen
5
+ class MarkupFile
6
+ attr_accessor :path, :filename, :extension, :engine
7
+
8
+ def initialize(path)
9
+ @path = path
10
+ @extension = File.extname(path)
11
+ @filename = File.basename(path, @extension)
12
+ @engine = engine_for(@extension) || self.class.engines.first
13
+ end
14
+
15
+ def raw_contents
16
+ File.read(path)
17
+ end
18
+
19
+ def contents
20
+ engine.process(raw_contents)
21
+ end
22
+
23
+ class << self
24
+ def engines
25
+ if @engines.blank?
26
+ @engines = [Smartgen::Engine::Textile.new, Smartgen::Engine::Markdown.new]
27
+ end
28
+
29
+ @engines
30
+ end
31
+
32
+ def register(engine)
33
+ engines.unshift engine.new
34
+ end
35
+ end
36
+
37
+ private
38
+
39
+ def engine_for(extension)
40
+ self.class.engines.each do |engine|
41
+ return engine if engine.supported?(extension)
42
+ end
43
+
44
+ nil
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,41 @@
1
+ require 'active_support/core_ext/hash'
2
+
3
+ module Smartgen
4
+ class ObjectHash < HashWithIndifferentAccess
5
+ def dup
6
+ Smartgen::ObjectHash.new(self)
7
+ end
8
+
9
+ def respond_to?(method)
10
+ has_key?(method) or super
11
+ end
12
+
13
+ protected
14
+ def method_missing(name, *args)
15
+ if has_key?(name)
16
+ self[name]
17
+ else
18
+ super
19
+ end
20
+ end
21
+
22
+ def convert_value(value)
23
+ case value
24
+ when Hash
25
+ value.with_object_hash
26
+ when Array
27
+ value.collect { |e| e.is_a?(Hash) ? e.with_object_hash : e }
28
+ else
29
+ value
30
+ end
31
+ end
32
+ end
33
+ end
34
+
35
+ class Hash
36
+ def with_object_hash
37
+ hash = Smartgen::ObjectHash.new(self)
38
+ hash.default = self.default
39
+ hash
40
+ end
41
+ end
@@ -0,0 +1 @@
1
+ require File.expand_path('renderers/erb', File.dirname(__FILE__))
@@ -0,0 +1,12 @@
1
+ module Smartgen
2
+ module Renderer
3
+ class ERB
4
+ def render(layout, markup_file, metadata={})
5
+ metadata = Smartgen::ObjectHash.new(metadata)
6
+
7
+ template = ::ERB.new(layout)
8
+ template.result(binding)
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,32 @@
1
+ module Smartgen
2
+ class Resource
3
+ def configure
4
+ yield config
5
+ end
6
+
7
+ def config
8
+ @config ||= Smartgen::Configuration.new
9
+ end
10
+
11
+ def generate!
12
+ generator.invoke_all
13
+ end
14
+
15
+ private
16
+ def generator
17
+ Smartgen::Generator.new(arguments, options)
18
+ end
19
+
20
+ def arguments
21
+ [config.src_files, config.output_folder]
22
+ end
23
+
24
+ def options
25
+ {
26
+ :metadata_file => config.metadata_file,
27
+ :layout => config.layout,
28
+ :assets => config.assets
29
+ }
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,4 @@
1
+ # -*- encoding: utf-8 -*-
2
+ module Smartgen
3
+ VERSION = "0.1.0"
4
+ end
@@ -0,0 +1,13 @@
1
+ <h1>Here is some header</h1>
2
+
3
+ <p>And here is a paragraph</p>
4
+
5
+ <ul>
6
+ <li>some bullet lists with <strong>style</strong></li>
7
+ <li>some bullet lists with <em>style</em></li>
8
+ </ul>
9
+
10
+
11
+ <h2>Another header</h2>
12
+
13
+ <p>With some text</p>
@@ -0,0 +1,8 @@
1
+ <h1>Here is some header</h1>
2
+ <p>And here is a paragraph</p>
3
+ <ul>
4
+ <li>some bullet lists with <strong>style</strong></li>
5
+ <li>some bullet lists with <em>style</em></li>
6
+ </ul>
7
+ <h2>Another header</h2>
8
+ <p>With some text</p>
@@ -0,0 +1,13 @@
1
+ <h1>Here is some header</h1>
2
+
3
+ <p>And here is a paragraph</p>
4
+
5
+ <ul>
6
+ <li>some bullet lists with <strong>style</strong></li>
7
+ <li>some bullet lists with <em>style</em></li>
8
+ </ul>
9
+
10
+
11
+ <h2>Another header</h2>
12
+
13
+ <p>With some text</p>
@@ -0,0 +1,12 @@
1
+ <html>
2
+ <body>
3
+ <h1>Here is some header</h1>
4
+ <p>And here is a paragraph</p>
5
+ <ul>
6
+ <li>some bullet lists with <strong>style</strong></li>
7
+ <li>some bullet lists with <em>style</em></li>
8
+ </ul>
9
+ <h2>Another header</h2>
10
+ <p>With some text</p>
11
+ </body>
12
+ </html>
@@ -0,0 +1,43 @@
1
+ <html>
2
+ <head>
3
+ <title>Common title</title>
4
+ </head>
5
+ <body>
6
+ <section id="menu">
7
+ <dl>
8
+
9
+ <dt>Common pages</dt>
10
+
11
+ <dd><a href="http://link.com" title="Some description of page">Some page</a></dd>
12
+
13
+ <dd><a href="http://index.com" title="Description for index page">Index Page</a></dd>
14
+
15
+ <dd><a href="http://other_link.com" title="Some description of other page">Some other page</a></dd>
16
+
17
+ <dd><a href="http://another_link.com" title="Some description of another page">Another page</a></dd>
18
+
19
+ <hr/>
20
+
21
+ <dt>Sample pages</dt>
22
+
23
+ <dd><a href="http://sample.com" title="Some description of sample page">Sample page</a></dd>
24
+
25
+ <dd><a href="http://sample_link.com" title="Some description of some sample page">Some sample page</a></dd>
26
+
27
+
28
+
29
+ </dl>
30
+ </section>
31
+
32
+ <section id="contents">
33
+ <h1>Here is some header</h1>
34
+ <p>And here is a paragraph</p>
35
+ <ul>
36
+ <li>some bullet lists with <strong>style</strong></li>
37
+ <li>some bullet lists with <em>style</em></li>
38
+ </ul>
39
+ <h2>Another header</h2>
40
+ <p>With some text</p>
41
+ </section>
42
+ </body>
43
+ </html>
@@ -0,0 +1,44 @@
1
+ <html>
2
+ <head>
3
+ <title>Index Page :: Common title</title>
4
+ <meta name="description" content="Description for index page"/>
5
+ </head>
6
+ <body>
7
+ <section id="menu">
8
+ <dl>
9
+
10
+ <dt>Common pages</dt>
11
+
12
+ <dd><a href="http://link.com" title="Some description of page">Some page</a></dd>
13
+
14
+ <dd><a href="http://index.com" title="Description for index page">Index Page</a></dd>
15
+
16
+ <dd><a href="http://other_link.com" title="Some description of other page">Some other page</a></dd>
17
+
18
+ <dd><a href="http://another_link.com" title="Some description of another page">Another page</a></dd>
19
+
20
+ <hr/>
21
+
22
+ <dt>Sample pages</dt>
23
+
24
+ <dd><a href="http://sample.com" title="Some description of sample page">Sample page</a></dd>
25
+
26
+ <dd><a href="http://sample_link.com" title="Some description of some sample page">Some sample page</a></dd>
27
+
28
+
29
+
30
+ </dl>
31
+ </section>
32
+
33
+ <section id="contents">
34
+ <h1>Here is some header</h1>
35
+ <p>And here is a paragraph</p>
36
+ <ul>
37
+ <li>some bullet lists with <strong>style</strong></li>
38
+ <li>some bullet lists with <em>style</em></li>
39
+ </ul>
40
+ <h2>Another header</h2>
41
+ <p>With some text</p>
42
+ </section>
43
+ </body>
44
+ </html>
@@ -0,0 +1,2 @@
1
+ // Some javascript file
2
+ var x = 0;
@@ -0,0 +1,2 @@
1
+ #some-id { color: #fff; }
2
+ .some-class { color: #000; }
@@ -0,0 +1,12 @@
1
+ Here is some header
2
+ ===================
3
+
4
+ And here is a paragraph
5
+
6
+ * some bullet lists with **style**
7
+ * some bullet lists with _style_
8
+
9
+ Another header
10
+ --------------
11
+
12
+ With some text
@@ -0,0 +1,10 @@
1
+ h1. Here is some header
2
+
3
+ And here is a paragraph
4
+
5
+ * some bullet lists with *style*
6
+ * some bullet lists with _style_
7
+
8
+ h2. Another header
9
+
10
+ With some text
@@ -0,0 +1,12 @@
1
+ Here is some header
2
+ ===================
3
+
4
+ And here is a paragraph
5
+
6
+ * some bullet lists with **style**
7
+ * some bullet lists with _style_
8
+
9
+ Another header
10
+ --------------
11
+
12
+ With some text
@@ -0,0 +1,5 @@
1
+ <html>
2
+ <body>
3
+ <%= markup_file.contents %>
4
+ </body>
5
+ </html>
@@ -0,0 +1,22 @@
1
+ <html>
2
+ <head>
3
+ <title><%= metadata.title %></title>
4
+ </head>
5
+ <body>
6
+ <section id="menu">
7
+ <dl>
8
+ <% metadata.menu.each do |entry| %>
9
+ <dt><%= entry.category %></dt>
10
+ <% entry.pages.each do |page| %>
11
+ <dd><a href="<%= page.link %>" title="<%= page.description %>"><%= page.title %></a></dd>
12
+ <% end %>
13
+ <% unless metadata.menu.last == entry %><hr/><% end %>
14
+ <% end %>
15
+ </dl>
16
+ </section>
17
+
18
+ <section id="contents">
19
+ <%= markup_file.contents %>
20
+ </section>
21
+ </body>
22
+ </html>
@@ -0,0 +1,23 @@
1
+ <html>
2
+ <head>
3
+ <title><%= metadata.current_page.title %> :: <%= metadata.title %></title>
4
+ <meta name="description" content="<%= metadata.current_page.description %>"/>
5
+ </head>
6
+ <body>
7
+ <section id="menu">
8
+ <dl>
9
+ <% metadata.menu.each do |entry| %>
10
+ <dt><%= entry.category %></dt>
11
+ <% entry.pages.each do |page| %>
12
+ <dd><a href="<%= page.link %>" title="<%= page.description %>"><%= page.title %></a></dd>
13
+ <% end %>
14
+ <% unless metadata.menu.last == entry %><hr/><% end %>
15
+ <% end %>
16
+ </dl>
17
+ </section>
18
+
19
+ <section id="contents">
20
+ <%= markup_file.contents %>
21
+ </section>
22
+ </body>
23
+ </html>
@@ -0,0 +1,43 @@
1
+ title: Common title
2
+ pages:
3
+ - &index
4
+ file: index_with_specific_metadata
5
+ title: Index Page
6
+ description: Description for index page
7
+ link: http://index.com
8
+ menu:
9
+ - {
10
+ category: Common pages,
11
+ pages:
12
+ [{
13
+ title: Some page,
14
+ description: Some description of page,
15
+ link: http://link.com
16
+ },
17
+ *index,
18
+ {
19
+ title: Some other page,
20
+ description: Some description of other page,
21
+ link: http://other_link.com
22
+ },
23
+ {
24
+ title: Another page,
25
+ description: Some description of another page,
26
+ link: http://another_link.com
27
+ }]
28
+ }
29
+ - {
30
+ category: Sample pages,
31
+ pages:
32
+ [{
33
+ title: Sample page,
34
+ description: Some description of sample page,
35
+ link: http://sample.com
36
+ },
37
+ {
38
+ title: Some sample page,
39
+ description: Some description of some sample page,
40
+ link: http://sample_link.com
41
+ }]
42
+ }
43
+