homework_report 0.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.
data/.gitignore ADDED
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ *~
7
+ .DS_Store
8
+ Gemfile.lock
9
+ InstalledFiles
10
+ _yardoc
11
+ coverage
12
+ doc/
13
+ lib/bundler/man
14
+ pkg
15
+ rdoc
16
+ spec/reports
17
+ test/tmp
18
+ test/version_tmp
19
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in homework_report.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 David Lin
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,53 @@
1
+ # HomeworkReport
2
+
3
+ HomeworkReport is an utility program,
4
+ which generates HTML files with given template files.
5
+
6
+ ## Installation
7
+
8
+ ### You are a user
9
+
10
+ Install it yourself as:
11
+
12
+ $ gem install homework_report
13
+
14
+ ### You are a developer
15
+
16
+ Add this line to your application's Gemfile:
17
+
18
+ gem 'homework_report'
19
+
20
+ And then execute:
21
+
22
+ $ bundle
23
+
24
+ ## Usage
25
+
26
+ ### Supported Template Formats
27
+
28
+ FORMAT FILE EXTENSIONS REQUIRED LIBRARIES
29
+ ------------ --------------------------- ------------------------
30
+ ERB .erb, .rhtml none
31
+ Haml .haml haml
32
+ Slim .slim slim
33
+ Markdown .markdown, .mkd, .md redcarpet, maruku, or kramdown
34
+
35
+ ### Tool
36
+
37
+ It generates HTML text to the standard output or file
38
+
39
+ $ homework_report [options] FILE
40
+
41
+ You can type --help in options to see the available options
42
+
43
+ ### Lib
44
+
45
+ TODO: I'm working on it
46
+
47
+ ## Contributing
48
+
49
+ 1. Fork it
50
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
51
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
52
+ 4. Push to the branch (`git push origin my-new-feature`)
53
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new :spec do |t|
5
+ t.libs << "spec"
6
+ t.pattern = "spec/**/*_spec.rb"
7
+ end
8
+
9
+ desc "Open an irb session preloaded with this library"
10
+ task :console do
11
+ sh "irb -rubygems -I lib -r homework_report"
12
+ end
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'homework_report'
4
+
5
+ tool = HomeworkReport::CommandLineTool.new ARGV
6
+ tool.run
7
+
@@ -0,0 +1,32 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'homework_report/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "homework_report"
8
+ gem.version = HomeworkReport::VERSION
9
+ gem.authors = ["David Lin"]
10
+ gem.email = ["davll.xc@gmail.com"]
11
+ gem.description = %q{The utility program can help create reports in HTML format. It supports ERB, Haml, Slim, and Markdown template engines}
12
+ gem.summary = %q{The ruby gem that generates HTML with template engines}
13
+ gem.homepage = "https://github.com/davll/homework_report"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.required_ruby_version = ">= 1.9"
21
+
22
+ gem.add_dependency "activesupport", "~> 3.2.8"
23
+ gem.add_dependency "backports", "~> 2.6.4"
24
+ gem.add_dependency "tilt", "~> 1.3.3"
25
+
26
+ gem.add_development_dependency "rake", "~> 0.9.2.2"
27
+ gem.add_development_dependency "minitest", "~> 4.1.0"
28
+ gem.add_development_dependency "haml", "~> 3.1.7"
29
+ gem.add_development_dependency "slim", "~> 1.3.3"
30
+ gem.add_development_dependency "redcarpet", "~> 2.2.1"
31
+
32
+ end
@@ -0,0 +1,47 @@
1
+ require 'optparse'
2
+
3
+ module HomeworkReport
4
+ class CommandLineTool
5
+
6
+ attr_accessor :options, :template_filepath, :scope
7
+
8
+ def initialize(*args)
9
+ args = args.flatten
10
+
11
+ self.options = { output: :stdout }
12
+
13
+ OptionParser.new do |opts|
14
+ opts.banner = "Usage: #{opts.program_name} [options] TEMPLATE_FILE"
15
+ opts.separator "Outcome: a HTML file/text is generated"
16
+ opts.separator "Options:"
17
+
18
+ command_options opts
19
+ end.parse! args
20
+
21
+ unless self.template_filepath = args.shift
22
+ raise RuntimeError, "TEMPLATE_FILE is required"
23
+ end
24
+
25
+ self.scope = Scope.new
26
+ end
27
+
28
+ def run
29
+ lambda {|path, &y|
30
+ case path
31
+ when :stdout then y.call $stdout
32
+ when String then File.open path, "w", &y
33
+ else raise RuntimeError
34
+ end
35
+ }.call options.delete(:output) do |output|
36
+ output << scope.render(template_filepath)
37
+ end
38
+ end
39
+
40
+ private
41
+
42
+ def command_options(opts)
43
+ opts.on("-o", "--output FILE"){|p| options[:output] = p }
44
+ end
45
+
46
+ end
47
+ end
@@ -0,0 +1,51 @@
1
+ require "ostruct"
2
+
3
+ module HomeworkReport
4
+ class Scope
5
+
6
+ # Constructor
7
+ def initialize(&block)
8
+ self.instance_eval &block if block
9
+ end
10
+
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
39
+
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
49
+
50
+ end
51
+ end
@@ -0,0 +1,3 @@
1
+ module HomeworkReport
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,42 @@
1
+ # require: core extensions
2
+ require "active_support/core_ext"
3
+ require "active_support/concern"
4
+ require "active_support/dependencies"
5
+ require "backports"
6
+
7
+ # require: template engines
8
+ require "tilt"
9
+ require "erb"
10
+ begin
11
+ require "slim"
12
+ rescue
13
+ end
14
+ begin
15
+ require "haml"
16
+ rescue
17
+ end
18
+ begin
19
+ require "redcarpet"
20
+ rescue
21
+ begin
22
+ require "maruku"
23
+ rescue
24
+ begin
25
+ require "kramdown"
26
+ rescue
27
+ end
28
+ end
29
+ end
30
+
31
+
32
+ #
33
+ require "homework_report/version"
34
+
35
+ module HomeworkReport
36
+ extend ActiveSupport::Autoload
37
+
38
+ autoload :CommandLineTool
39
+ autoload :Scope
40
+
41
+ end
42
+
@@ -0,0 +1,51 @@
1
+ require 'spec_helper'
2
+
3
+ module HomeworkReport
4
+ describe Scope do
5
+ include Spec::Helpers
6
+
7
+ before do
8
+ @scope = Scope.new do
9
+ @title = "HomeworkReport"
10
+ @article = "Hello World"
11
+ end
12
+ end
13
+
14
+ %w(
15
+ sample1.erb sample1.haml sample1.slim
16
+ sample2.erb
17
+ ).each do |sample|
18
+ it "should work with the file #{sample}" do
19
+ output = @scope.render(template_path sample)
20
+ output.must_be_kind_of String
21
+ output.wont_be_empty
22
+ output.must_match /\A\s*<html>(\s|.)*<\/html>\s*\z/
23
+ output.must_match /.*<title>\s*HomeworkReport\s*<\/title>.*/
24
+ output.must_match /.*<p>\s*Hello World\s*<\/p>.*/
25
+ end
26
+ end
27
+
28
+ %w(sample1.markdown).each do |sample|
29
+ it "should work with the file #{sample}" do
30
+ output = @scope.render(template_path sample)
31
+ output.must_be_kind_of String
32
+ output.wont_be_empty
33
+ end
34
+ it 'the markdown engine does not support ruby scripting'
35
+ end
36
+
37
+ %w(sample3.erb).each do |sample|
38
+ it "should be able to pass local variables with #{sample}" do
39
+ options = { title: "Uncharted", article: "Nathan Drake" }
40
+ output = @scope.render (template_path sample), options
41
+ output.must_be_kind_of String
42
+ output.wont_be_empty
43
+ output.must_match /\A\s*<html>(\s|.)*<\/html>\s*\z/
44
+ output.must_match /.*<title>\s*Uncharted\s*<\/title>.*/
45
+ output.must_match /.*<p>\s*Nathan Drake\s*<\/p>.*/
46
+ end
47
+ end
48
+
49
+ end
50
+ end
51
+
@@ -0,0 +1,27 @@
1
+ # Bundler
2
+ require "rubygems"
3
+ require "bundler/setup"
4
+
5
+ # Test Framework
6
+ require "minitest/autorun"
7
+
8
+ #
9
+ require 'homework_report'
10
+
11
+ #
12
+ SPEC_ROOT = File.expand_path("..", __FILE__)
13
+
14
+ # load factories
15
+ Dir[File.join(SPEC_ROOT, "factories/**/*.rb")].each {|f| require f }
16
+
17
+ # helpers
18
+ module HomeworkReport::Spec
19
+ module Helpers
20
+
21
+ def template_path(name)
22
+ File.join SPEC_ROOT, "templates", name
23
+ end
24
+
25
+ end
26
+ end
27
+
@@ -0,0 +1,12 @@
1
+ <html>
2
+ <head>
3
+ <title>
4
+ <%= @title %>
5
+ </title>
6
+ </head>
7
+ <body>
8
+ <p>
9
+ <%= @article %>
10
+ </p>
11
+ </body>
12
+ </html>
@@ -0,0 +1,5 @@
1
+ %html
2
+ %head
3
+ %title= @title
4
+ %body
5
+ %p= @article
@@ -0,0 +1,4 @@
1
+ # {{ @title }}
2
+
3
+ {{ @article }}
4
+
@@ -0,0 +1,5 @@
1
+ html
2
+ head
3
+ title= @title
4
+ body
5
+ p= @article
@@ -0,0 +1,3 @@
1
+ <title>
2
+ <%= @title %>
3
+ </title>
@@ -0,0 +1,3 @@
1
+ <p>
2
+ <%= @article %>
3
+ </p>
@@ -0,0 +1,8 @@
1
+ <html>
2
+ <head>
3
+ <%= render 'sample2-1.erb' %>
4
+ </head>
5
+ <body>
6
+ <%= render 'sample2-2.erb' %>
7
+ </body>
8
+ </html>
@@ -0,0 +1,12 @@
1
+ <html>
2
+ <head>
3
+ <title>
4
+ <%= title %>
5
+ </title>
6
+ </head>
7
+ <body>
8
+ <p>
9
+ <%= article %>
10
+ </p>
11
+ </body>
12
+ </html>
metadata ADDED
@@ -0,0 +1,206 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: homework_report
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - David Lin
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-18 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 3.2.8
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 3.2.8
30
+ - !ruby/object:Gem::Dependency
31
+ name: backports
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 2.6.4
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 2.6.4
46
+ - !ruby/object:Gem::Dependency
47
+ name: tilt
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: 1.3.3
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 1.3.3
62
+ - !ruby/object:Gem::Dependency
63
+ name: rake
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: 0.9.2.2
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: 0.9.2.2
78
+ - !ruby/object:Gem::Dependency
79
+ name: minitest
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ~>
84
+ - !ruby/object:Gem::Version
85
+ version: 4.1.0
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ~>
92
+ - !ruby/object:Gem::Version
93
+ version: 4.1.0
94
+ - !ruby/object:Gem::Dependency
95
+ name: haml
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ~>
100
+ - !ruby/object:Gem::Version
101
+ version: 3.1.7
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ~>
108
+ - !ruby/object:Gem::Version
109
+ version: 3.1.7
110
+ - !ruby/object:Gem::Dependency
111
+ name: slim
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ~>
116
+ - !ruby/object:Gem::Version
117
+ version: 1.3.3
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ~>
124
+ - !ruby/object:Gem::Version
125
+ version: 1.3.3
126
+ - !ruby/object:Gem::Dependency
127
+ name: redcarpet
128
+ requirement: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ~>
132
+ - !ruby/object:Gem::Version
133
+ version: 2.2.1
134
+ type: :development
135
+ prerelease: false
136
+ version_requirements: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ~>
140
+ - !ruby/object:Gem::Version
141
+ version: 2.2.1
142
+ description: The utility program can help create reports in HTML format. It supports
143
+ ERB, Haml, Slim, and Markdown template engines
144
+ email:
145
+ - davll.xc@gmail.com
146
+ executables:
147
+ - homework_report
148
+ extensions: []
149
+ extra_rdoc_files: []
150
+ files:
151
+ - .gitignore
152
+ - Gemfile
153
+ - LICENSE.txt
154
+ - README.md
155
+ - Rakefile
156
+ - bin/homework_report
157
+ - homework_report.gemspec
158
+ - lib/homework_report.rb
159
+ - lib/homework_report/command_line_tool.rb
160
+ - lib/homework_report/scope.rb
161
+ - lib/homework_report/version.rb
162
+ - spec/scope_spec.rb
163
+ - spec/spec_helper.rb
164
+ - spec/templates/sample1.erb
165
+ - spec/templates/sample1.haml
166
+ - spec/templates/sample1.markdown
167
+ - spec/templates/sample1.slim
168
+ - spec/templates/sample2-1.erb
169
+ - spec/templates/sample2-2.erb
170
+ - spec/templates/sample2.erb
171
+ - spec/templates/sample3.erb
172
+ homepage: https://github.com/davll/homework_report
173
+ licenses: []
174
+ post_install_message:
175
+ rdoc_options: []
176
+ require_paths:
177
+ - lib
178
+ required_ruby_version: !ruby/object:Gem::Requirement
179
+ none: false
180
+ requirements:
181
+ - - ! '>='
182
+ - !ruby/object:Gem::Version
183
+ version: '1.9'
184
+ required_rubygems_version: !ruby/object:Gem::Requirement
185
+ none: false
186
+ requirements:
187
+ - - ! '>='
188
+ - !ruby/object:Gem::Version
189
+ version: '0'
190
+ requirements: []
191
+ rubyforge_project:
192
+ rubygems_version: 1.8.24
193
+ signing_key:
194
+ specification_version: 3
195
+ summary: The ruby gem that generates HTML with template engines
196
+ test_files:
197
+ - spec/scope_spec.rb
198
+ - spec/spec_helper.rb
199
+ - spec/templates/sample1.erb
200
+ - spec/templates/sample1.haml
201
+ - spec/templates/sample1.markdown
202
+ - spec/templates/sample1.slim
203
+ - spec/templates/sample2-1.erb
204
+ - spec/templates/sample2-2.erb
205
+ - spec/templates/sample2.erb
206
+ - spec/templates/sample3.erb