genit 0.5 → 0.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (53) hide show
  1. data/NEWS +20 -0
  2. data/TODO +40 -4
  3. data/VERSION +1 -1
  4. data/bin/genit +10 -2
  5. data/data/pages/index.html +10 -11
  6. data/data/pages/index2.html +1 -0
  7. data/data/styles/screen.css +12 -3
  8. data/data/templates/html_5 +5 -0
  9. data/data/templates/main.html +0 -6
  10. data/data/templates/xhtml_1.0_strict +5 -0
  11. data/data/templates/xhtml_1.0_transitional +5 -0
  12. data/lib/genit.rb +5 -18
  13. data/lib/genit/builders.rb +8 -0
  14. data/lib/genit/{body_link_builder.rb → builders/body_link_builder.rb} +7 -3
  15. data/lib/genit/{builder.rb → builders/builder.rb} +0 -0
  16. data/lib/genit/{builder_base.rb → builders/builder_base.rb} +0 -0
  17. data/lib/genit/{head_link_builder.rb → builders/head_link_builder.rb} +5 -0
  18. data/lib/genit/builders/img_builder.rb +39 -0
  19. data/lib/genit/{menu_builder.rb → builders/menu_builder.rb} +0 -0
  20. data/lib/genit/builders/script_builder.rb +37 -0
  21. data/lib/genit/documents.rb +5 -0
  22. data/lib/genit/{document_writer.rb → documents/document_writer.rb} +0 -0
  23. data/lib/genit/{fragment.rb → documents/fragment.rb} +0 -0
  24. data/lib/genit/{html_document.rb → documents/html_document.rb} +0 -0
  25. data/lib/genit/{xml_document.rb → documents/xml_document.rb} +0 -0
  26. data/lib/genit/extensions.rb +2 -44
  27. data/lib/genit/extensions/nokogiri_extension.rb +37 -0
  28. data/lib/genit/extensions/string_extension.rb +17 -0
  29. data/lib/genit/project.rb +4 -0
  30. data/lib/genit/{compiler.rb → project/compiler.rb} +12 -0
  31. data/lib/genit/{page_compiler.rb → project/page_compiler.rb} +21 -2
  32. data/lib/genit/project/project_creator.rb +119 -0
  33. data/lib/genit/tags.rb +9 -0
  34. data/lib/genit/tags/class_fragment_tag.rb +30 -0
  35. data/lib/genit/tags/class_menu_tag.rb +36 -0
  36. data/lib/genit/tags/class_news_tag.rb +52 -0
  37. data/lib/genit/tags/class_pages_tag.rb +34 -0
  38. data/lib/genit/tags/class_tag.rb +38 -0
  39. data/lib/genit/{var_tag.rb → tags/here_tag.rb} +10 -5
  40. data/lib/genit/{tag.rb → tags/tag.rb} +0 -0
  41. data/lib/genit/{tag_processor.rb → tags/tag_processor.rb} +4 -3
  42. data/lib/genit/utils.rb +2 -0
  43. data/lib/genit/{file_writer.rb → utils/file_writer.rb} +0 -0
  44. data/spec/class_news_tag_spec.rb +36 -0
  45. data/spec/class_tag_spec.rb +13 -0
  46. data/spec/compiler_spec.rb +1 -1
  47. data/spec/menu_builder_spec.rb +0 -14
  48. data/spec/page_compiler_spec.rb +3 -4
  49. data/spec/project_creator_spec.rb +6 -2
  50. data/spec/tag_processor_spec.rb +8 -0
  51. metadata +46 -27
  52. data/lib/genit/class_tag.rb +0 -66
  53. data/lib/genit/project_creator.rb +0 -72
@@ -0,0 +1,37 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ module Nokogiri
4
+ module XML
5
+
6
+ # I think it's a bad idea to extend the Nokogiri library cause it makes a
7
+ # too big dependency on Genit.
8
+ # It will be much better to do a little wrapper over Nokogiri::XML::Node.
9
+ class Node
10
+
11
+ def genit_class?
12
+ if self['class']
13
+ true
14
+ else
15
+ false
16
+ end
17
+ end
18
+
19
+ def genit_here?
20
+ if self['here']
21
+ true
22
+ else
23
+ false
24
+ end
25
+ end
26
+
27
+ def genit_what?
28
+ if self['what']
29
+ true
30
+ else
31
+ false
32
+ end
33
+ end
34
+
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,17 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ class String
4
+
5
+ def force_html_extension
6
+ self.gsub /markdown$/, 'html'
7
+ end
8
+
9
+ def force_html_extension!
10
+ self.gsub! /markdown$/, 'html'
11
+ end
12
+
13
+ def markdown_ext?
14
+ self.end_with?('.markdown')
15
+ end
16
+
17
+ end
@@ -0,0 +1,4 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require 'genit/project/project_creator'
3
+ require 'genit/project/compiler'
4
+ require 'genit/project/page_compiler'
@@ -17,6 +17,7 @@ module Genit
17
17
  # Public: Compile the web site.
18
18
  def compile
19
19
  if genit_project_folder?
20
+ remove_content_of_www
20
21
  compile_site
21
22
  else
22
23
  puts 'Not a genit project folder'
@@ -29,9 +30,20 @@ module Genit
29
30
  File.exist?(File.join(@working_dir, '.genit'))
30
31
  end
31
32
 
33
+ def remove_content_of_www
34
+ Dir.foreach(File.join(@working_dir, 'www')) do |file|
35
+ next if (file == ".") or (file == "..")
36
+ filename = File.join(@working_dir, 'www', file)
37
+ FileUtils.remove_dir(filename) if File.directory?(filename)
38
+ FileUtils.remove_file(filename) if File.file?(filename)
39
+ end
40
+ end
41
+
32
42
  def compile_site
33
43
  compile_pages
34
44
  FileUtils.cp_r File.join(@working_dir, 'styles'), File.join(@working_dir, 'www')
45
+ FileUtils.cp_r File.join(@working_dir, 'public'), File.join(@working_dir, 'www')
46
+ FileUtils.cp_r File.join(@working_dir, 'scripts'), File.join(@working_dir, 'www')
35
47
  end
36
48
 
37
49
  def compile_pages
@@ -21,17 +21,36 @@ module Genit
21
21
  def compile
22
22
  compile_body
23
23
  compile_head
24
+
25
+ builder = ScriptBuilder.new @template
26
+ builder.build_for_page @filename
24
27
  end
25
28
 
26
29
  private
27
30
 
28
31
  def compile_body
32
+ # Pourquoi 2 fois ?
33
+ # Parce que la 1ere fois, on inclus essentiellement la page au sein du
34
+ # template, et la seconde fois, on s'occupe des tags restants (ceux qui
35
+ # étaient dans la page).
36
+ # Suivant comment la hiérarchie de tag évoluera, il est possible qu'on
37
+ # ai un jour besoin de faire une boucle du genre :
38
+ # "Tant qu'il reste des tags"
39
+ 2.times { replace_all_genit_tags }
40
+
41
+ builder = BodyLinkBuilder.new @template
42
+ @template = builder.build_for_page @filename
43
+
44
+ builder = ImgBuilder.new @template
45
+ @template = builder.build_for_page @filename
46
+ end
47
+
48
+ def replace_all_genit_tags
29
49
  genit_tags_in_template.each do |tag|
50
+ next if tag.genit_what?
30
51
  tp = TagProcessor.new(@working_dir, @template, @filename, tag)
31
52
  @template = tp.process
32
53
  end
33
- builder = BodyLinkBuilder.new @template
34
- @template = builder.build_for_page @filename
35
54
  end
36
55
 
37
56
  def compile_head
@@ -0,0 +1,119 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require "fileutils"
4
+
5
+ module Genit
6
+
7
+ # Create a skeleton project.
8
+ class ProjectCreator
9
+
10
+ # Sole constructor.
11
+ #
12
+ # name - The String name of the future project folder.
13
+ # doctype - The String document type definition.
14
+ # empty - A Boolean telling if we produce a smoke test or not.
15
+ def initialize name, doctype, empty
16
+ @name = name
17
+ @doctype = doctype
18
+ @empty = empty
19
+ end
20
+
21
+ # Public: Create the structure of the project, that is many
22
+ # files and folders.
23
+ #
24
+ # Returns nothing.
25
+ def create
26
+ begin
27
+ create_the_project
28
+ rescue SystemCallError
29
+ puts "Cannot create project..."
30
+ end
31
+ end
32
+
33
+ private
34
+
35
+ def create_the_project
36
+ FileUtils.makedirs @name
37
+ create_dirs ['fragments', 'news', 'pages', 'scripts', 'styles', 'templates', 'www',
38
+ 'styles/alsa', 'styles/yui', 'styles/images', 'public']
39
+ copy_main_template
40
+ copy_files ['templates/menu.html', 'styles/handheld.css', 'styles/print.css',
41
+ 'styles/alsa/all.css', 'styles/yui/all.css', 'styles/yui/base.css',
42
+ 'styles/yui/fonts.css', 'styles/yui/reset.css']
43
+ copy_index
44
+ copy_screen_css
45
+ FileUtils.touch "#{@name}/.genit"
46
+ end
47
+
48
+ # Create some subfolders inside the project folder.
49
+ #
50
+ # a_array - An Array of String subfolder names
51
+ #
52
+ # Examples
53
+ #
54
+ # create_dirs ['styles', 'scripts']
55
+ #
56
+ # create_dirs ['styles/css/alsa', 'styles/css/yui', 'styles/css/images']
57
+ #
58
+ # Returns nothing.
59
+ def create_dirs a_array
60
+ a_array.each {|dir| FileUtils.makedirs File.join(@name, dir) }
61
+ end
62
+
63
+ # Copy files to project.
64
+ #
65
+ # a_array - An Array of String "subfolder/file" names
66
+ #
67
+ # Example
68
+ #
69
+ # copy_files ['templates/main.html', 'pages/index.html']
70
+ #
71
+ # Returns nothing.
72
+ def copy_files a_array
73
+ a_array.each do |file|
74
+ src = File.join $GENIT_PATH, 'data', file
75
+ dest = File.join @name, file
76
+ FileUtils.cp src, dest
77
+ end
78
+ end
79
+
80
+ def copy_index
81
+ dest = File.join @name, 'pages/index.html'
82
+ if @empty
83
+ src = File.join $GENIT_PATH, 'data/pages/index2.html'
84
+ else
85
+ src = File.join $GENIT_PATH, 'data/pages/index.html'
86
+ end
87
+ FileUtils.cp src, dest
88
+ end
89
+
90
+ def copy_screen_css
91
+ dest = File.join @name, 'styles/screen.css'
92
+ if @empty
93
+ FileUtils.touch dest
94
+ else
95
+ src = File.join $GENIT_PATH, 'data/styles/screen.css'
96
+ FileUtils.cp src, dest
97
+ end
98
+ end
99
+
100
+ def copy_main_template
101
+ dest = File.join @name, 'templates', 'main.html'
102
+ copy_first_part dest
103
+ ProjectCreator.append_last_part dest
104
+ end
105
+
106
+ def copy_first_part dest
107
+ src = File.join $GENIT_PATH, 'data', 'templates', @doctype
108
+ FileUtils.cp src, dest
109
+ end
110
+
111
+ def self.append_last_part dest
112
+ src = File.join $GENIT_PATH, 'data', 'templates', 'main.html'
113
+ content = File.open(src, "r").read
114
+ File.open(dest, "a") {|out| out.puts content }
115
+ end
116
+
117
+ end
118
+
119
+ end
data/lib/genit/tags.rb ADDED
@@ -0,0 +1,9 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require 'genit/tags/tag_processor'
3
+ require 'genit/tags/tag'
4
+ require 'genit/tags/here_tag'
5
+ require 'genit/tags/class_tag'
6
+ require 'genit/tags/class_pages_tag'
7
+ require 'genit/tags/class_menu_tag'
8
+ require 'genit/tags/class_fragment_tag'
9
+ require 'genit/tags/class_news_tag'
@@ -0,0 +1,30 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ module Genit
4
+
5
+ # Replace the <genit class="fragment"/> in the template.
6
+ class ClassFragmentTag < Tag
7
+
8
+ # Public: Constructor.
9
+ #
10
+ # working_dir - The String working directory, where live the project.
11
+ # template - The Nokogiri::XML::Document into which we process the tag.
12
+ # filename - The String name of the page
13
+ # tag - The tag to process as a Nokogiri::XML::Element
14
+ def initialize working_dir, template, filename, tag
15
+ super working_dir, template, filename, tag
16
+ end
17
+
18
+ # Public: Do the replacement.
19
+ #
20
+ # Returns the template as a Nokogiri::XML::Document
21
+ def process
22
+ file = @tag['file']
23
+ fragment = HtmlDocument.build_page_content(File.join(@working_dir, 'fragments', file), @working_dir)
24
+ css_rule = "genit.fragment[file='#{file}']"
25
+ replace_tag_into_template! css_rule, fragment.to_s
26
+ end
27
+
28
+ end
29
+
30
+ end
@@ -0,0 +1,36 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ module Genit
4
+
5
+ # Replace the <genit class="menu"/> in the template.
6
+ class ClassMenuTag < Tag
7
+
8
+ # Public: Constructor.
9
+ #
10
+ # working_dir - The String working directory, where live the project.
11
+ # template - The Nokogiri::XML::Document into which we process the tag.
12
+ # filename - The String name of the page
13
+ # tag - The tag to process as a Nokogiri::XML::Element
14
+ def initialize working_dir, template, filename, tag
15
+ super working_dir, template, filename, tag
16
+ end
17
+
18
+ # Public: Do the replacement.
19
+ #
20
+ # Returns the template as a Nokogiri::XML::Document
21
+ def process
22
+ build_menu
23
+ replace_tag_into_template! 'genit.menu', @menu.to_html
24
+ end
25
+
26
+ private
27
+
28
+ def build_menu
29
+ menu = XmlDocument.open(File.join(@working_dir, "templates/menu.html"))
30
+ builder = MenuBuilder.new(menu)
31
+ @menu = builder.build_for_page(@filename)
32
+ end
33
+
34
+ end
35
+
36
+ end
@@ -0,0 +1,52 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ module Genit
4
+
5
+ # Replace the <genit class="news"/> in the template.
6
+ class ClassNewsTag < Tag
7
+
8
+ # Public: Constructor.
9
+ #
10
+ # working_dir - The String working directory, where live the project.
11
+ # template - The Nokogiri::XML::Document into which we process the tag.
12
+ # filename - The String name of the page
13
+ # tag - The tag to process as a Nokogiri::XML::Element
14
+ def initialize working_dir, template, filename, tag
15
+ super working_dir, template, filename, tag
16
+ end
17
+
18
+ # Public: Do the replacement.
19
+ #
20
+ # Returns the template as a Nokogiri::XML::Document
21
+ def process
22
+ replace_tag_into_template! 'genit.news', news_content
23
+ end
24
+
25
+ private
26
+
27
+ def news_content
28
+ news_string = ''
29
+ news_files.each do |file|
30
+ doc = HtmlDocument.open_as_string file
31
+ if @tag['wrapper']
32
+ news_string += "<div class='#{@tag['wrapper']}'>" + doc.to_s + '</div>'
33
+ else
34
+ news_string += doc.to_s
35
+ end
36
+ end
37
+ news_string
38
+ end
39
+
40
+ def news_files
41
+ files = Dir.glob(File.join(@working_dir, 'news', '*')).sort.reverse
42
+ number = @tag['number']
43
+ if number
44
+ files[0...(number).to_i]
45
+ else
46
+ files
47
+ end
48
+ end
49
+
50
+ end
51
+
52
+ end
@@ -0,0 +1,34 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ module Genit
4
+
5
+ # Replace the <genit class="pages"/> in the template.
6
+ class ClassPagesTag < Tag
7
+
8
+ # Public: Constructor.
9
+ #
10
+ # working_dir - The String working directory, where live the project.
11
+ # template - The Nokogiri::XML::Document into which we process the tag.
12
+ # filename - The String name of the page
13
+ # tag - The tag to process as a Nokogiri::XML::Element
14
+ def initialize working_dir, template, filename, tag
15
+ super working_dir, template, filename, tag
16
+ end
17
+
18
+ # Public: Do the replacement.
19
+ #
20
+ # Returns the template as a Nokogiri::XML::Document
21
+ def process
22
+ replace_tag_into_template! 'genit.pages', page_content
23
+ end
24
+
25
+ private
26
+
27
+ def page_content
28
+ filename = File.join(@working_dir, 'pages', @filename)
29
+ HtmlDocument.build_page_content filename, @working_dir
30
+ end
31
+
32
+ end
33
+
34
+ end
@@ -0,0 +1,38 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ module Genit
4
+
5
+ # A Genit general tag.
6
+ # Currently we have three tags:
7
+ # * <genit class="pages"/>
8
+ # * <genit class="menu"/>
9
+ # * <genit class="fragment" file="foo.html"/>
10
+ class ClassTag < Tag
11
+
12
+ # Public: Constructor.
13
+ #
14
+ # working_dir - The String working directory, where live the project.
15
+ # template - The Nokogiri::XML::Document into which we process the tag.
16
+ # filename - The String name of the page
17
+ # tag - The tag to process as a Nokogiri::XML::Element
18
+ def initialize working_dir, template, filename, tag
19
+ super working_dir, template, filename, tag
20
+ end
21
+
22
+ # Public: Replace something in the template.
23
+ #
24
+ # Returns the template as a Nokogiri::XML::Document
25
+ def process
26
+ case @tag['class']
27
+ when 'pages' then ClassPagesTag.new(@working_dir, @template, @filename, @tag).process
28
+ when 'menu' then ClassMenuTag.new(@working_dir, @template, @filename, @tag).process
29
+ when 'fragment' then ClassFragmentTag.new(@working_dir, @template, @filename, @tag).process
30
+ when 'news' then ClassNewsTag.new(@working_dir, @template, @filename, @tag).process
31
+ else
32
+ raise RuntimeError
33
+ end
34
+ end
35
+
36
+ end
37
+
38
+ end