craftsman 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4d85ef98c37213ef3309d659b66e01626bfed19a
4
+ data.tar.gz: b2e1aa719efe68d2275ca9cc03da65c86d76ee41
5
+ SHA512:
6
+ metadata.gz: 30b4bf152a7047549e5bf59693c190c3dd57afbb9138da91467482f3b21b1bd4f6944ae17e90a422b9ad13109d321511b94c40514ba1321e8358dfc5d4a4bfda
7
+ data.tar.gz: a7e43f7dc49b6e20c7f1c406a0196af06e0b95e393ec9a259fc23a2f5dbb1bbf4e823a7582c898a2ff94f25ab480632279124067ff7b9521b94d4671c0a75fb0
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0.0
5
+ - 2.1.0
6
+ script: rspec
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in craftsman.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 agate
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.
@@ -0,0 +1,31 @@
1
+ # Craftsman
2
+
3
+ A set of helpers for building inline template. (Currently, only support haml)
4
+
5
+ [![Build Status](https://travis-ci.org/agate/craftsman.svg)](https://travis-ci.org/agate/craftsman)
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'craftsman'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install craftsman
20
+
21
+ ## Usage
22
+
23
+ See code in spec directory for more information.
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it ( https://github.com/agate/craftsman/fork )
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create a new Pull Request
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'craftsman/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "craftsman"
8
+ spec.version = Craftsman::VERSION
9
+ spec.authors = ["agate"]
10
+ spec.email = ["agate.hao@gmail.com"]
11
+ spec.summary = %q{A set of helpers for building inline template.}
12
+ spec.description = %q{Currently, only support haml.}
13
+ spec.homepage = "https://github.com/agate/craftsman"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.6"
22
+ spec.add_development_dependency "rake", "~> 10.3"
23
+ spec.add_development_dependency "rspec", "~> 3.1"
24
+ end
@@ -0,0 +1,14 @@
1
+ require "craftsman/version"
2
+
3
+ module Craftsman
4
+ def self.paint(package_class, package_id)
5
+ package = package_class.new(package_id)
6
+ Renderer.new(package).render
7
+ end
8
+ end
9
+
10
+ require "craftsman/package"
11
+ require "craftsman/renderer"
12
+ require "craftsman/decorator/base"
13
+ require "craftsman/decorator/helper"
14
+ require "craftsman/decorator/annotation"
@@ -0,0 +1,12 @@
1
+ module Craftsman::Decorator
2
+ class Annotation < Base
3
+ REGEX = /^\/\/@require\s+(.*)\s*$/
4
+
5
+ def process(content)
6
+ content.gsub(REGEX) do |m|
7
+ target = m.match(REGEX)[1]
8
+ build_filter(target)
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,54 @@
1
+ module Craftsman::Decorator
2
+ class Base
3
+ FILTER_EXT_MAPPING = {
4
+ 'js' => 'javascript',
5
+ 'rb' => 'ruby',
6
+ 'md' => 'markdown',
7
+ }
8
+
9
+ def initialize(package)
10
+ @package = package
11
+ end
12
+
13
+ def process(content)
14
+ raise 'need be implemented'
15
+ end
16
+
17
+ def self.process(package, content)
18
+ self.new(package).process(content)
19
+ end
20
+
21
+ private
22
+
23
+ def build_filter(target)
24
+ target_ext = target.match(/([^.]+)$/)[1]
25
+ filter_name = FILTER_EXT_MAPPING[target_ext] || target_ext
26
+
27
+ target_content = @package.get(target)
28
+
29
+ filter = <<FILTER.strip
30
+ :#{filter_name}
31
+ #{append_indent(target_content)}
32
+ FILTER
33
+
34
+ if ['js', 'coffee'].include? target_ext
35
+ filter = <<FILTER.strip
36
+ - content_for :javascripts do
37
+ #{append_indent(filter)}
38
+ FILTER
39
+ end
40
+
41
+ <<FILTER
42
+ ///////////// #{target} /////////////
43
+ #{filter}
44
+
45
+ FILTER
46
+ end
47
+
48
+ def append_indent(str)
49
+ str.lines.map do |l|
50
+ " #{l}"
51
+ end.join
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,17 @@
1
+ module Craftsman::Decorator
2
+ class Helper < Base
3
+ def process(content)
4
+ helper_targets.map do |t|
5
+ build_filter t
6
+ end.join + content
7
+ end
8
+
9
+ private
10
+
11
+ def helper_targets
12
+ @package.targets.select do |t|
13
+ t.match(/^helpers\/.*rb$/)
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,18 @@
1
+ # you need to implement your own Package class
2
+ # which is extended from "Craftsman::Package"
3
+ class Craftsman::Package
4
+ def initialize(id)
5
+ @id = id
6
+ end
7
+
8
+ # return: content of target(file)
9
+ def get(target)
10
+ raise 'needs to be implemented'
11
+ end
12
+
13
+ # return: string array of targets(files)
14
+ # in this package
15
+ def targets
16
+ raise 'needs to be implemented'
17
+ end
18
+ end
@@ -0,0 +1,18 @@
1
+ class Craftsman::Renderer
2
+ def initialize(package)
3
+ @package = package
4
+ end
5
+
6
+ def render
7
+ output = @package.get("index.html.haml")
8
+
9
+ [
10
+ ::Craftsman::Decorator::Annotation,
11
+ ::Craftsman::Decorator::Helper
12
+ ].each do |klass|
13
+ output = klass.process(@package, output)
14
+ end
15
+
16
+ output
17
+ end
18
+ end
@@ -0,0 +1,3 @@
1
+ module Craftsman
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,51 @@
1
+ require File.expand_path('../spec_helper', __FILE__)
2
+
3
+ describe Craftsman do
4
+ it "can paint correctly" do
5
+ output = Craftsman.paint(CustomPackage, 'package_a')
6
+ expect(output).to eq(<<OUTPUT)
7
+ ///////////// helpers/main.rb /////////////
8
+ :ruby
9
+ def display_date
10
+ Time.now.strftime("%Y-%m-%d")
11
+ end
12
+
13
+ ///////////// javascripts/foo.js /////////////
14
+ - content_for :javascripts do
15
+ :javascript
16
+ var foo = 'hello';
17
+
18
+
19
+ ///////////// javascripts/bar.coffee /////////////
20
+ - content_for :javascripts do
21
+ :coffee
22
+ console.log foo
23
+
24
+
25
+ ///////////// stylesheets/foo.sass /////////////
26
+ :sass
27
+ table
28
+ td:hover
29
+ background-color: green
30
+
31
+
32
+ = display_date
33
+
34
+ %table
35
+ %thead
36
+ %tr
37
+ %th Key
38
+ %th Value
39
+ %tbody
40
+ %tr
41
+ %td a
42
+ %td 1
43
+ %tr
44
+ %td b
45
+ %td 2
46
+ %tr
47
+ %td c
48
+ %td 3
49
+ OUTPUT
50
+ end
51
+ end
@@ -0,0 +1,20 @@
1
+ class CustomPackage < Craftsman::Package
2
+ DIR = File.expand_path('../resources', __FILE__)
3
+
4
+ def initialize(id)
5
+ super(id)
6
+ @dir = "#{DIR}/#{@id}"
7
+ end
8
+
9
+ def get(target)
10
+ File.read("#{@dir}/#{target}")
11
+ end
12
+
13
+ def targets
14
+ Dir["#{@dir}/**/*"].reject do |f|
15
+ File.directory? f
16
+ end.map do |f|
17
+ f.sub("#{@dir}/", '')
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,17 @@
1
+ require File.expand_path('../spec_helper', __FILE__)
2
+
3
+ describe :package do
4
+ before :all do
5
+ @package = CustomPackage.new("package_a")
6
+ end
7
+
8
+ it "should return all the targets" do
9
+ expect(@package.targets.size).to eq(5)
10
+ end
11
+
12
+ it "can get target's content" do
13
+ file = File.expand_path('../resources/package_a/helpers/main.rb', __FILE__)
14
+ content = File.read(file)
15
+ expect(@package.get('helpers/main.rb')).to eq(content)
16
+ end
17
+ end
@@ -0,0 +1,3 @@
1
+ def display_date
2
+ Time.now.strftime("%Y-%m-%d")
3
+ end
@@ -0,0 +1,21 @@
1
+ //@require javascripts/foo.js
2
+ //@require javascripts/bar.coffee
3
+ //@require stylesheets/foo.sass
4
+
5
+ = display_date
6
+
7
+ %table
8
+ %thead
9
+ %tr
10
+ %th Key
11
+ %th Value
12
+ %tbody
13
+ %tr
14
+ %td a
15
+ %td 1
16
+ %tr
17
+ %td b
18
+ %td 2
19
+ %tr
20
+ %td c
21
+ %td 3
@@ -0,0 +1 @@
1
+ console.log foo
@@ -0,0 +1 @@
1
+ var foo = 'hello';
@@ -0,0 +1,3 @@
1
+ table
2
+ td:hover
3
+ background-color: green
@@ -0,0 +1,2 @@
1
+ require 'craftsman'
2
+ require File.expand_path('../custom_package', __FILE__)
metadata ADDED
@@ -0,0 +1,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: craftsman
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - agate
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-09-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '10.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '10.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '3.1'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '3.1'
55
+ description: Currently, only support haml.
56
+ email:
57
+ - agate.hao@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - .travis.yml
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - craftsman.gemspec
69
+ - lib/craftsman.rb
70
+ - lib/craftsman/decorator/annotation.rb
71
+ - lib/craftsman/decorator/base.rb
72
+ - lib/craftsman/decorator/helper.rb
73
+ - lib/craftsman/package.rb
74
+ - lib/craftsman/renderer.rb
75
+ - lib/craftsman/version.rb
76
+ - spec/craftsman_spec.rb
77
+ - spec/custom_package.rb
78
+ - spec/package_spec.rb
79
+ - spec/resources/package_a/helpers/main.rb
80
+ - spec/resources/package_a/index.html.haml
81
+ - spec/resources/package_a/javascripts/bar.coffee
82
+ - spec/resources/package_a/javascripts/foo.js
83
+ - spec/resources/package_a/stylesheets/foo.sass
84
+ - spec/spec_helper.rb
85
+ homepage: https://github.com/agate/craftsman
86
+ licenses:
87
+ - MIT
88
+ metadata: {}
89
+ post_install_message:
90
+ rdoc_options: []
91
+ require_paths:
92
+ - lib
93
+ required_ruby_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - '>='
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - '>='
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ requirements: []
104
+ rubyforge_project:
105
+ rubygems_version: 2.2.2
106
+ signing_key:
107
+ specification_version: 4
108
+ summary: A set of helpers for building inline template.
109
+ test_files:
110
+ - spec/craftsman_spec.rb
111
+ - spec/custom_package.rb
112
+ - spec/package_spec.rb
113
+ - spec/resources/package_a/helpers/main.rb
114
+ - spec/resources/package_a/index.html.haml
115
+ - spec/resources/package_a/javascripts/bar.coffee
116
+ - spec/resources/package_a/javascripts/foo.js
117
+ - spec/resources/package_a/stylesheets/foo.sass
118
+ - spec/spec_helper.rb