foshow 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (7) hide show
  1. data/Gemfile +4 -0
  2. data/LICENSE.md +22 -0
  3. data/README.md +61 -0
  4. data/Rakefile +122 -0
  5. data/foshow.gemspec +43 -0
  6. data/lib/foshow.rb +119 -0
  7. metadata +120 -0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in foshow.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Micah Cooper
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,61 @@
1
+ # Foshow
2
+
3
+ Foshow allows you to display the application code in your rails application.
4
+
5
+ I use it for presentations where I don't want to make slides and or switch back and forth between my application and my code. This allows me to have a working app, and display the code it took to create it, right next to it. It can be very usefull for introducing new ideas to people.
6
+
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ gem 'foshow'
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install foshow
21
+
22
+ ## Usage
23
+
24
+ ###Default - Rails conventions
25
+ This gem only works with rails apps and assumes you are following rails basing MVC naming conventions.
26
+ For example, a model named "user" has a controller named "app/controllers/users_controller.rb" and a view directory called "app/views/users/"
27
+
28
+ All you have to do is add this to your application layout ('apps/views/layouts/application.html.erb').
29
+
30
+ Foshow.render(self)
31
+
32
+ This, by default, displays the code for the model, view, and controller of the resource you're viewing.
33
+
34
+ ###Configurating beyond the defaults
35
+ If you want to display more code for a page, do it here: 'config/initializers/foshow.rb'
36
+
37
+ To display more code for a resource(page) configure Foshow with a Hash.
38
+
39
+ The keys of this hash can be: models, views, controllers, javascripts, helpers
40
+
41
+ The values of these keys can be a Hash or an Array. It's kinda like your routes file. Observe:
42
+
43
+ #Use a Hash to specify what controller#action will display the files given.
44
+ #Use just an Array to make the files display on all controller#actions.
45
+
46
+ Foshow.configure do
47
+ {
48
+ views: { 'controller#action_name' => ['app/views/controller/other_file_name.html.erb']}
49
+ javascripts: ['app/assets/javascripts/global.js']
50
+ }
51
+ end
52
+
53
+ __note__: The file paths must always be in an array. They will not render otherwise
54
+
55
+ ## Contributing
56
+
57
+ 1. Fork it
58
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
59
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
60
+ 4. Push to the branch (`git push origin my-new-feature`)
61
+ 5. Create new Pull Request
@@ -0,0 +1,122 @@
1
+ #############################################################################
2
+ #
3
+ # Helper functions
4
+ #
5
+ #############################################################################
6
+
7
+ def name
8
+ @name ||= Dir['*.gemspec'].first.split('.').first
9
+ end
10
+
11
+ def version
12
+ line = File.read("lib/#{name}.rb")[/^\s*VERSION\s*=\s*.*/]
13
+ line.match(/.*VERSION\s*=\s*['"](.*)['"]/)[1]
14
+ end
15
+
16
+ def date
17
+ Time.now.strftime('%Y-%m-%d')
18
+ end
19
+
20
+ def gemspec_file
21
+ "#{name}.gemspec"
22
+ end
23
+
24
+ def gem_file
25
+ "#{name}-#{version}.gem"
26
+ end
27
+
28
+ def replace_header(head, header_name)
29
+ head.sub!(/(\.#{header_name}\s*= ').*'/) { "#{$1}#{send(header_name)}'"}
30
+ end
31
+
32
+ #############################################################################
33
+ #
34
+ # Standard tasks
35
+ #
36
+ #############################################################################
37
+
38
+ require 'rspec'
39
+ require 'rspec/core/rake_task'
40
+
41
+ desc "Run all specs"
42
+ task RSpec::Core::RakeTask.new('spec')
43
+
44
+ task :default => "spec"
45
+
46
+ desc "Open an irb session preloaded with this library"
47
+ task :console do
48
+ sh "irb -rubygems -r ./lib/#{name}.rb"
49
+ end
50
+
51
+ #############################################################################
52
+ #
53
+ # Custom tasks (add your own tasks here)
54
+ #
55
+ #############################################################################
56
+
57
+
58
+ #############################################################################
59
+ #
60
+ # Packaging tasks
61
+ #
62
+ #############################################################################
63
+
64
+ desc "Create tag v#{version} and build and push #{gem_file} to Rubygems"
65
+ task :release => :build do
66
+ unless `git branch` =~ /^\* master$/
67
+ puts "You must be on the master branch to release!"
68
+ exit!
69
+ end
70
+ sh "git commit --allow-empty -a -m 'Release #{version}'"
71
+ sh "git tag v#{version}"
72
+ sh "git push origin master"
73
+ sh "git push origin v#{version}"
74
+ sh "gem push pkg/#{name}-#{version}.gem"
75
+ end
76
+
77
+ desc "Build #{gem_file} into the pkg directory"
78
+ task :build => :gemspec do
79
+ sh "mkdir -p pkg"
80
+ sh "gem build #{gemspec_file}"
81
+ sh "mv #{gem_file} pkg"
82
+ end
83
+
84
+ desc "Generate #{gemspec_file}"
85
+ task :gemspec => :validate do
86
+ # read spec file and split out manifest section
87
+ spec = File.read(gemspec_file)
88
+ head, manifest, tail = spec.split(" # = MANIFEST =\n")
89
+
90
+ # replace name version and date
91
+ replace_header(head, :name)
92
+ replace_header(head, :version)
93
+ replace_header(head, :date)
94
+
95
+ # determine file list from git ls-files
96
+ files = `git ls-files`.
97
+ split("\n").
98
+ sort.
99
+ reject { |file| file =~ /^\./ }.
100
+ reject { |file| file =~ /^(rdoc|pkg)/ }.
101
+ map { |file| " #{file}" }.
102
+ join("\n")
103
+
104
+ # piece file back together and write
105
+ manifest = " s.files = %w[\n#{files}\n ]\n"
106
+ spec = [head, manifest, tail].join(" # = MANIFEST =\n")
107
+ File.open(gemspec_file, 'w') { |io| io.write(spec) }
108
+ puts "Updated #{gemspec_file}"
109
+ end
110
+
111
+ desc "Validate #{gemspec_file}"
112
+ task :validate do
113
+ libfiles = Dir['lib/*'] - ["lib/#{name}.rb", "lib/#{name}"]
114
+ unless libfiles.empty?
115
+ puts "Directory `lib` should only contain a `#{name}.rb` file and `#{name}` dir."
116
+ exit!
117
+ end
118
+ unless Dir['VERSION*'].empty?
119
+ puts "A `VERSION` file at root level violates Gem best practices."
120
+ exit!
121
+ end
122
+ end
@@ -0,0 +1,43 @@
1
+ # -*- encoding: utf-8 -*-
2
+ Gem::Specification.new do |s|
3
+ s.specification_version = 2 if s.respond_to? :specification_version=
4
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
5
+ s.rubygems_version = '1.3.5'
6
+
7
+ s.name = 'foshow'
8
+ s.version = '0.0.0'
9
+ s.date = '2012-08-10'
10
+
11
+ s.summary = %q{Show the code it took to make your rails app, in your rails app}
12
+ s.description = %q{
13
+ Foshow provides a very simple interface that allows you to display your code in your application.
14
+ This can be very useful for presentations when you don't want to create slides or jump back and
15
+ forth between you code and your application.}
16
+
17
+ s.authors = ["Micah Cooper"]
18
+ s.email = ["mrmicahcooper@gmail.com"]
19
+ s.homepage = "https://github.com/mrmicahcooper/foshow"
20
+
21
+ s.require_paths = %w[lib]
22
+
23
+ s.extra_rdoc_files = %w[README.md LICENSE.md]
24
+
25
+ s.add_dependency 'coderay'
26
+
27
+ s.add_development_dependency 'rake'
28
+ s.add_development_dependency 'rspec'
29
+ s.add_development_dependency 'pry'
30
+
31
+ # = MANIFEST =
32
+ s.files = %w[
33
+ Gemfile
34
+ LICENSE.md
35
+ README.md
36
+ Rakefile
37
+ foshow.gemspec
38
+ lib/foshow.rb
39
+ ]
40
+ # = MANIFEST =
41
+
42
+ s.test_files = s.files.grep(%r{^spec/})
43
+ end
@@ -0,0 +1,119 @@
1
+ require 'ostruct'
2
+ require 'singleton'
3
+
4
+ class Foshow
5
+
6
+ include Singleton
7
+
8
+ VERSION = "0.0.0"
9
+
10
+ attr_accessor :views, :renderer
11
+
12
+ def self.configure(&block)
13
+ yield instance.config
14
+ end
15
+
16
+ def self.render(renderer)
17
+ instance.renderer = renderer
18
+ instance.render
19
+ end
20
+
21
+ def config
22
+ @config ||= OpenStruct.new
23
+ end
24
+
25
+ def render
26
+ renderer.content_tag(:section, id: 'code_viewer') do
27
+ build_navigation + build_elements
28
+ end.html_safe
29
+ end
30
+
31
+ private
32
+
33
+ def show_keys
34
+ @show_keys ||= config.marshal_dump.keys | defaults.marshal_dump.keys
35
+ end
36
+
37
+ def build_navigation
38
+ renderer.content_tag(:nav) do
39
+ "".tap do |html_container|
40
+ show_keys.each do |name|
41
+ html_container << " #{renderer.link_to(name, '#', class: name)}"
42
+ end
43
+ end.html_safe
44
+ end
45
+ end
46
+
47
+
48
+ def defaults
49
+ @defaults ||= OpenStruct.new({
50
+ views: ["app/views/#{renderer.controller_name}/#{renderer.action_name}.html.haml"],
51
+ controllers: ["app/controllers/#{renderer.controller_name}_controller.rb"],
52
+ model: ["app/models/#{renderer.controller_name.singularize}.rb"],
53
+ all: []
54
+ })
55
+ end
56
+
57
+ def build_elements
58
+ "".tap do |html_container|
59
+ show_keys.each do |key|
60
+ html_container << renderer.content_tag(:div, class: "#{key.to_s}") { show_code(defaults.send(key) || []).html_safe }
61
+ html_container << renderer.content_tag(:div, class: "#{key.to_s}") { show_code(config_directories(key) || []).html_safe }
62
+ end
63
+ end.html_safe
64
+ end
65
+
66
+ def show_code(file_array, lang=:ruby)
67
+ files = file_array.flatten
68
+ "".tap do |html_string|
69
+ files.each do |file|
70
+ html_string << highlighted_code(file)
71
+ end
72
+ end
73
+ end
74
+
75
+ def config_directories(key)
76
+ pages = config.send(key) || []
77
+ a = pages.compact.map do |page|
78
+ if page.is_a?(Hash)
79
+ page["#{renderer.controller_name}##{renderer.action_name}"] || []
80
+ else
81
+ page
82
+ end
83
+ end
84
+ end
85
+
86
+ def highlighted_code(file)
87
+ CodeRay.scan(" \#// #{file}\n\n" + code_string(file), file_lang(file)).div
88
+ end
89
+
90
+ def code_string(file)
91
+ puts file
92
+ read_file(file)
93
+ end
94
+
95
+ def read_file(file)
96
+ begin
97
+ File.read(file)
98
+ rescue Errno::ENOENT
99
+ "not found"
100
+ end
101
+ end
102
+
103
+ def file_extension(file)
104
+ File.extname(file)[1..-1]
105
+ end
106
+
107
+ def supported_file_types
108
+ {
109
+ ruby: "rb",
110
+ javascript: "js",
111
+ haml: "haml"
112
+ }
113
+ end
114
+
115
+ def file_lang(file)
116
+ supported_file_types.invert[file_extension(file)]
117
+ end
118
+
119
+ end
metadata ADDED
@@ -0,0 +1,120 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: foshow
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Micah Cooper
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-08-10 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: coderay
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
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: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: pry
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
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'
78
+ description: ! "\n Foshow provides a very simple interface that allows you to display
79
+ your code in your application.\n This can be very useful for presentations when
80
+ you don't want to create slides or jump back and\n forth between you code and your
81
+ application."
82
+ email:
83
+ - mrmicahcooper@gmail.com
84
+ executables: []
85
+ extensions: []
86
+ extra_rdoc_files:
87
+ - README.md
88
+ - LICENSE.md
89
+ files:
90
+ - Gemfile
91
+ - LICENSE.md
92
+ - README.md
93
+ - Rakefile
94
+ - foshow.gemspec
95
+ - lib/foshow.rb
96
+ homepage: https://github.com/mrmicahcooper/foshow
97
+ licenses: []
98
+ post_install_message:
99
+ rdoc_options: []
100
+ require_paths:
101
+ - lib
102
+ required_ruby_version: !ruby/object:Gem::Requirement
103
+ none: false
104
+ requirements:
105
+ - - ! '>='
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ required_rubygems_version: !ruby/object:Gem::Requirement
109
+ none: false
110
+ requirements:
111
+ - - ! '>='
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ requirements: []
115
+ rubyforge_project:
116
+ rubygems_version: 1.8.24
117
+ signing_key:
118
+ specification_version: 2
119
+ summary: Show the code it took to make your rails app, in your rails app
120
+ test_files: []