homework_report 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -11,6 +11,8 @@ Install it yourself as:
11
11
 
12
12
  $ gem install homework_report
13
13
 
14
+ Note that extra libraries are required if you want to use Haml, Slim, or Markdown instead of ERB
15
+
14
16
  ### You are a developer
15
17
 
16
18
  Add this line to your application's Gemfile:
@@ -38,7 +40,66 @@ It generates HTML text to the standard output or file
38
40
 
39
41
  $ homework_report [options] FILE
40
42
 
41
- You can type --help in options to see the available options
43
+ You can execute `$ homework_report --help` to see the available options
44
+
45
+ ### Simple Example
46
+
47
+ homework.slim
48
+
49
+ - @topic = "Uncharted"
50
+ - @name = "Nathan Drake"
51
+ doctype html
52
+ html
53
+ head
54
+ title= @topic
55
+ body
56
+ p
57
+ | Name: #{@name}
58
+
59
+ Execute the command to generate HTML
60
+
61
+ $ homework_report -o homework.html homework.slim
62
+
63
+ ### More Complex Example
64
+
65
+ homework.slim
66
+
67
+ - @course = "Digital Image Synthesis"
68
+ - @topic = "Assignment #1 - Height Field"
69
+ - @name = "David Lin"
70
+ - @submission_date = Date.today
71
+ = render 'template.slim'
72
+
73
+ template.slim
74
+
75
+ doctype html
76
+ html
77
+ head
78
+ title #{@course} - #{@topic}
79
+ body
80
+ div
81
+ h1= @topic
82
+ h3 Name: #{@name}
83
+ h3 Submission Date: #{@submission_date}
84
+ div
85
+ = render "description.md"
86
+ div
87
+ = render "final_images.slim"
88
+
89
+ description.md
90
+
91
+ ## Description of implementation approach and comments
92
+ blahblahblah
93
+
94
+ final_images.slim
95
+
96
+ h2 Final Images Rendered with my implementation of Heightfield2
97
+ p
98
+ img src="http://www.csie.ntu.edu.tw/~cyy/courses/rendering/12fall/assignments/proj1/smooth.jpg"
99
+
100
+ Finally, execute the command to generate homework.html
101
+
102
+ $ homework_report -o homework.html homework.slim
42
103
 
43
104
  ### Lib
44
105
 
@@ -9,26 +9,28 @@ require "tilt"
9
9
  require "erb"
10
10
  begin
11
11
  require "slim"
12
- rescue
12
+ rescue LoadError => e
13
+ warn "Warning: Slim is not enabled"
13
14
  end
14
15
  begin
15
16
  require "haml"
16
- rescue
17
+ rescue LoadError => e
18
+ warn "Warning: Haml is not enabled"
17
19
  end
18
20
  begin
19
21
  require "redcarpet"
20
- rescue
22
+ rescue LoadError => e
21
23
  begin
22
24
  require "maruku"
23
- rescue
25
+ rescue LoadError => e
24
26
  begin
25
27
  require "kramdown"
26
- rescue
28
+ rescue LoadError => e
29
+ warn "Warning: Markdown is not enabled"
27
30
  end
28
31
  end
29
32
  end
30
33
 
31
-
32
34
  #
33
35
  require "homework_report/version"
34
36
 
@@ -37,6 +39,6 @@ module HomeworkReport
37
39
 
38
40
  autoload :CommandLineTool
39
41
  autoload :Scope
40
-
42
+ autoload :Renderer
41
43
  end
42
44
 
@@ -34,6 +34,7 @@ module HomeworkReport
34
34
  end
35
35
  }.call options.delete(:output) do |output|
36
36
  output << scope.render(template_filepath)
37
+ output << "\n"
37
38
  end
38
39
  end
39
40
 
@@ -0,0 +1,74 @@
1
+ module HomeworkReport
2
+ class Renderer
3
+
4
+ # Attributes
5
+ attr_accessor :scope
6
+
7
+ # Constructor
8
+ def initialize(scope)
9
+ self.scope = scope
10
+ end
11
+
12
+ # Render the template
13
+ #
14
+ # Usage #1: s.render FILE [OPTIONS] { BLOCK }
15
+ #
16
+ def render(*args, &block)
17
+ args = args.flatten
18
+ options = args.extract_options!
19
+ path = args.first
20
+
21
+ inside_template(path) {|template|
22
+ # clone scope object to prevent from overriding instance variables
23
+ template.render(self.scope.clone, options, &block)
24
+ }
25
+ end
26
+
27
+ private
28
+
29
+ # Load template object according to the file path
30
+ def load_template(path)
31
+ path = if in_template?
32
+ File.expand_path path, File.dirname(template_stack.last.file)
33
+ else
34
+ File.expand_path path # in current working directory
35
+ end
36
+
37
+ unless File.file? path
38
+ raise ArgumentError, "#{path} is expected to be a existing file"
39
+ end
40
+
41
+ Tilt[path].new(path)
42
+ end
43
+
44
+ # Inside the template
45
+ def inside_template(template, &block)
46
+ # load template object if
47
+ template = case template
48
+ when String then load_template(template)
49
+ when Tilt::Template then template
50
+ else raise RuntimeError
51
+ end
52
+
53
+ # push the template to the stack as the current template
54
+ template_stack.push template
55
+ # do something with the template
56
+ result = block.call(template)
57
+ # remove the template from the stack
58
+ template_stack.pop
59
+ # return the result and prevent from HTML escaping
60
+ return result.html_safe
61
+ end
62
+
63
+ # Stack of Tilt templates
64
+ def template_stack
65
+ @template_stack ||= []
66
+ end
67
+
68
+ # Check if the scope is in a template file
69
+ def in_template?
70
+ not template_stack.empty?
71
+ end
72
+
73
+ end
74
+ end
@@ -1,5 +1,3 @@
1
- require "ostruct"
2
-
3
1
  module HomeworkReport
4
2
  class Scope
5
3
 
@@ -8,44 +6,11 @@ module HomeworkReport
8
6
  self.instance_eval &block if block
9
7
  end
10
8
 
11
- # Render the template
12
- #
13
- # Usage #1: s.render FILE [OPTIONS] { BLOCK }
14
- #
15
- def render(*args, &block)
16
- args = args.flatten
17
- options = args.extract_options!
18
- path = args.first
19
-
20
- # resolve template path
21
- path = if in_template?
22
- File.expand_path path, File.dirname(template_stack.last.file)
23
- else
24
- File.expand_path path # in current working directory
25
- end
26
-
27
- unless File.file? path
28
- raise ArgumentError, "#{path} is expected to be a existing file"
29
- end
30
-
31
- template = Tilt[path].new(path)
32
- template_stack.push template
33
- result = template.render(self, options, &block)
34
- template_stack.pop
35
- return result.html_safe # prevent from HTML escaping
36
- end
37
-
38
- private
9
+ # Render the template (Renderer#render)
10
+ delegate :render, to: :renderer
39
11
 
40
- # Stack of Tilt templates
41
- def template_stack
42
- @template_stack ||= []
43
- end
44
-
45
- # Check if the scope is in a template file
46
- def in_template?
47
- not template_stack.empty?
48
- end
12
+ # Renderer Object
13
+ def renderer; (@renderer ||= Renderer.new(self)); end
49
14
 
50
15
  end
51
16
  end
@@ -1,3 +1,3 @@
1
1
  module HomeworkReport
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: homework_report
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-18 00:00:00.000000000 Z
12
+ date: 2012-10-19 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -157,6 +157,7 @@ files:
157
157
  - homework_report.gemspec
158
158
  - lib/homework_report.rb
159
159
  - lib/homework_report/command_line_tool.rb
160
+ - lib/homework_report/renderer.rb
160
161
  - lib/homework_report/scope.rb
161
162
  - lib/homework_report/version.rb
162
163
  - spec/scope_spec.rb