pencil_mustache 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1 @@
1
+ *.gem
data/LICENSE ADDED
@@ -0,0 +1,9 @@
1
+ (The MIT License)
2
+
3
+ Copyright (c) 2012 Benjamin Oakes
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,40 @@
1
+ ___________________/\___________________
2
+ / \
3
+ | |
4
+
5
+ # PencilMustache
6
+
7
+ Do you only need to replace some tokens? This is all you need (Ruby 1.9+). No need for 3000 lines of Ruby, like in Mustache proper.
8
+
9
+ ## Usage
10
+
11
+ Quick example ([similar to Mustache](https://github.com/defunkt/mustache#usage)):
12
+
13
+ >> require 'pencil_mustache' # Assuming it's in your $LOAD_PATH
14
+ => true
15
+ >> PencilMustache.render("Hello {{planet}}", :planet => "World!")
16
+ => "Hello World!"
17
+
18
+ ## Installing
19
+
20
+ It's so small ([15 lines][code]), you might as well copy it into your app. Or just use the same `gsub` feature when you need it.
21
+
22
+ But if you insist, it's a gem too:
23
+
24
+ gem install pencil_mustache
25
+
26
+ [code]: https://github.com/benjaminoakes/pencil_mustache/blob/master/lib/pencil_mustache.rb
27
+
28
+ ## Contributing
29
+
30
+ Run the specs using:
31
+
32
+ ruby spec/pencil_mustache_spec.rb
33
+
34
+ Build the gem using:
35
+
36
+ gem build pencil_mustache.gemspec
37
+
38
+ ## License
39
+
40
+ MIT. See LICENSE for a copy.
@@ -0,0 +1,54 @@
1
+ #!/usr/bin/env ruby
2
+ require_relative '../lib/pencil_mustache'
3
+
4
+ module WhyMadLib
5
+ # From [Why's Poignant Guide to Ruby](http://mislav.uniqpath.com/poignant-guide/book/chapter-3.html)
6
+ TEMPLATE = <<MUSTACHE
7
+ Say something loud! Maybe he'll use it in his examples!
8
+ Like what? Like "{{adjective}} {{noun}}"?
9
+
10
+ Come on, seriously. {{adjective_capitalized}} {{noun}}.
11
+ {{adjective_capitalized}}.
12
+ {{noun_capitalized}}.
13
+
14
+ {{adjective_capitalized}} {{noun}}.
15
+ {{adjective_capitalized}} {{noun}}.
16
+ {{adjective_capitalized}} {{noun}}.
17
+ {{adjective_capitalized}} {{noun}}.
18
+ {{adjective_capitalized}} {{noun}}.
19
+ {{adjective_capitalized}} {{noun}}.
20
+ {{adjective_capitalized}} {{noun}}.
21
+
22
+ Woohoo! I don't know what {{adjective}} {{noun}} is, but we did it!
23
+ We're in the book!
24
+ MUSTACHE
25
+
26
+ HEADER = <<MARKDOWN
27
+ Mad libs!
28
+ =========
29
+
30
+ MARKDOWN
31
+
32
+ def self.run
33
+ puts HEADER
34
+
35
+ print 'adjective: '
36
+ adjective = STDIN.gets.chomp
37
+ print 'noun: '
38
+ noun = STDIN.gets.chomp
39
+ puts
40
+
41
+ puts render(adjective, noun)
42
+ end
43
+
44
+ def self.render(adjective, noun)
45
+ doc = { :adjective => adjective,
46
+ :adjective_capitalized => adjective.capitalize,
47
+ :noun => noun,
48
+ :noun_capitalized => noun.capitalize }
49
+
50
+ PencilMustache.render(TEMPLATE, doc)
51
+ end
52
+ end
53
+
54
+ WhyMadLib.run
@@ -0,0 +1,15 @@
1
+ module PencilMustache
2
+ class << self
3
+ def render(template, doc)
4
+ template.gsub(/{{.*?}}/, add_whiskers(doc))
5
+ end
6
+
7
+ private
8
+
9
+ def add_whiskers(doc)
10
+ with_whiskers = {}
11
+ doc.keys.each { |k| with_whiskers["{{#{k}}}"] = doc[k] }
12
+ with_whiskers
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ Gem::Specification.new do |s|
3
+ s.name = 'pencil_mustache'
4
+ s.version = '0.1.0'
5
+ s.platform = Gem::Platform::RUBY
6
+ s.authors = ['Benjamin Oakes']
7
+ s.email = %w(hello@benjaminoakes.com)
8
+ s.license = 'MIT'
9
+ s.homepage = 'http://github.com/benjaminoakes/pencil_mustache'
10
+ s.summary = 'Need to replace some tokens? This is all you need (Ruby 1.9+).'
11
+ s.description = s.summary
12
+
13
+ s.rubyforge_project = 'pencil_mustache'
14
+
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
+ s.require_paths = %w(lib)
19
+ end
@@ -0,0 +1,15 @@
1
+ require 'minitest/spec'
2
+ require 'minitest/autorun'
3
+ require_relative '../lib/pencil_mustache'
4
+
5
+ describe PencilMustache do
6
+ describe '.render' do
7
+ it 'replaces tokens within mustaches' do
8
+ template = 'Say something loud! Maybe he\'ll use it in his examples! Like what? Like "{{adjective}} {{noun}}"?'
9
+ doc = { :adjective => 'chunky',
10
+ :noun => 'bacon' }
11
+
12
+ assert_equal('Say something loud! Maybe he\'ll use it in his examples! Like what? Like "chunky bacon"?', PencilMustache.render(template, doc))
13
+ end
14
+ end
15
+ end
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pencil_mustache
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Benjamin Oakes
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-05-02 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Need to replace some tokens? This is all you need (Ruby 1.9+).
15
+ email:
16
+ - hello@benjaminoakes.com
17
+ executables:
18
+ - why-mad-lib
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - .gitignore
23
+ - LICENSE
24
+ - README.md
25
+ - bin/why-mad-lib
26
+ - lib/pencil_mustache.rb
27
+ - pencil_mustache.gemspec
28
+ - spec/pencil_mustache_spec.rb
29
+ homepage: http://github.com/benjaminoakes/pencil_mustache
30
+ licenses:
31
+ - MIT
32
+ post_install_message:
33
+ rdoc_options: []
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ! '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ required_rubygems_version: !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ requirements: []
49
+ rubyforge_project: pencil_mustache
50
+ rubygems_version: 1.8.11
51
+ signing_key:
52
+ specification_version: 3
53
+ summary: Need to replace some tokens? This is all you need (Ruby 1.9+).
54
+ test_files: []
55
+ has_rdoc: