markababy 1.0.0 → 1.3.2
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.
- checksums.yaml +7 -0
- data/LICENSE.txt +4 -0
- data/README.md +64 -0
- data/lib/markababy.rb +10 -49
- data/lib/markababy/builder.rb +59 -0
- data/lib/markababy/{rails.rb → rails_template_context.rb} +1 -19
- data/lib/markababy/rails_template_handler.rb +10 -0
- data/lib/markababy/railtie.rb +9 -0
- data/markababy.gemspec +10 -5
- metadata +24 -45
- data/README.txt +0 -29
- data/Rakefile +0 -5
- data/spec/markababy_rails_spec.rb +0 -28
- data/spec/markababy_spec.rb +0 -73
- data/spec/views/index.html.rb +0 -7
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 2b6a178f6e549aad27f67e34231d7846cbc671071070c7b1c0278f6d68ae2f4b
|
4
|
+
data.tar.gz: 6c0168a1e33b4a38f9d8836996cb9a30f50a193e3faf6d73257dc1e6605702a6
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 992a470b24c2761a51027872afb0523968f3ec464bb0e13ef54c77402a974a14d17d9ba98ac8c9ed2145c111f8dc5ec4ef6f32b84674bd0fc50bded13def160e
|
7
|
+
data.tar.gz: 462b050b3c49ae95525b9d927e30bb1226d2902b244ae4dd646c1d6e0dde68eea40ac2579d959aa54e887f2253583508b446da0c40f9d56ce8e708b44a4226d5
|
data/LICENSE.txt
ADDED
data/README.md
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
# markababy
|
2
|
+
|
3
|
+
[](https://badge.fury.io/rb/markababy) [](https://travis-ci.org/timcraft/markababy)
|
4
|
+
|
5
|
+
|
6
|
+
Markaby's little sister. A small library for writing HTML in Ruby.
|
7
|
+
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
$ gem install markababy
|
12
|
+
|
13
|
+
|
14
|
+
## Example
|
15
|
+
|
16
|
+
Usage is similar to [Markaby](http://en.wikipedia.org/wiki/Markaby),
|
17
|
+
and easy to use directly from a Ruby script:
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
require 'markababy'
|
21
|
+
|
22
|
+
Markababy.markup do
|
23
|
+
html do
|
24
|
+
head { title 'Boats.com' }
|
25
|
+
body do
|
26
|
+
h1 'Boats.com has great deals'
|
27
|
+
ul do
|
28
|
+
li '$49 for a canoe'
|
29
|
+
li '$39 for a raft'
|
30
|
+
li '$29 for a huge boot that floats and can fit 5 people'
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
```
|
36
|
+
|
37
|
+
Use `Markababy.capture` if you want to capture
|
38
|
+
the output as a string instead of printing it to STDOUT.
|
39
|
+
|
40
|
+
|
41
|
+
## Rails template usage
|
42
|
+
|
43
|
+
Add markababy as a dependency to your Gemfile; do the bundle dance; then change
|
44
|
+
the extension on your template files from .erb to .rb and you can start writing
|
45
|
+
your templates in Ruby!
|
46
|
+
|
47
|
+
Controller instance variables and helpers will be available as methods.
|
48
|
+
|
49
|
+
|
50
|
+
## Less is more
|
51
|
+
|
52
|
+
Some differences from Markaby:
|
53
|
+
|
54
|
+
* No auto-stringification
|
55
|
+
* No element classes or IDs
|
56
|
+
* No validation
|
57
|
+
* No XHTML
|
58
|
+
|
59
|
+
|
60
|
+
## Alternatives
|
61
|
+
|
62
|
+
* https://github.com/timcraft/hom
|
63
|
+
* https://github.com/judofyr/tubby
|
64
|
+
* https://github.com/digital-fabric/rubyoshka
|
data/lib/markababy.rb
CHANGED
@@ -1,8 +1,9 @@
|
|
1
|
+
require 'markababy/builder'
|
1
2
|
require 'cgi'
|
2
3
|
|
3
4
|
module Markababy
|
4
5
|
def self.capture(options = {}, &block)
|
5
|
-
[].tap { |output| markup(options.merge(output
|
6
|
+
[].tap { |output| markup(options.merge(:output => output), &block) }.join
|
6
7
|
end
|
7
8
|
|
8
9
|
def self.markup(options = {}, &block)
|
@@ -10,58 +11,18 @@ module Markababy
|
|
10
11
|
|
11
12
|
options[:output] = $stdout unless options.has_key?(:output)
|
12
13
|
|
13
|
-
options[:output] <<
|
14
|
+
options[:output] << doctype if options[:doctype]
|
14
15
|
|
15
16
|
Builder.new(options, &block)
|
16
17
|
end
|
17
18
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
@output = @options[:output]
|
23
|
-
|
24
|
-
@escape = @options[:escape]
|
25
|
-
|
26
|
-
@context = @options[:context]
|
27
|
-
|
28
|
-
instance_eval(&block)
|
29
|
-
end
|
30
|
-
|
31
|
-
def method_missing(sym, *args, &block)
|
32
|
-
if !@context.nil? && @context.respond_to?(sym)
|
33
|
-
return @context.send(sym, *args, &block)
|
34
|
-
end
|
35
|
-
|
36
|
-
attributes, content = [], []
|
37
|
-
|
38
|
-
args.flatten.each do |arg|
|
39
|
-
if arg.respond_to?(:to_hash)
|
40
|
-
arg.to_hash.each { |k, v| attributes << ' %s="%s"' % [@escape[k.to_s], @escape[v.to_s]] }
|
41
|
-
elsif arg.respond_to?(:id2name)
|
42
|
-
attributes << ' %s' % @escape[arg.to_s]
|
43
|
-
elsif arg.respond_to?(:html_safe?) && arg.html_safe?
|
44
|
-
content << arg.to_s
|
45
|
-
else
|
46
|
-
content << @escape[arg.to_s]
|
47
|
-
end
|
48
|
-
end
|
49
|
-
|
50
|
-
@output << (attributes.empty? ? "<#{sym}>" : "<#{sym}#{attributes.join}>")
|
51
|
-
|
52
|
-
@output << content.join unless content.empty?
|
53
|
-
|
54
|
-
instance_eval(&block) unless block.nil?
|
55
|
-
|
56
|
-
@output << "</#{sym}>" unless content.empty? && block.nil?
|
57
|
-
end
|
19
|
+
def self.doctype
|
20
|
+
@doctype ||= "<!DOCTYPE html>\n".freeze
|
21
|
+
end
|
58
22
|
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
@output << @escape[content.to_s]
|
64
|
-
end
|
65
|
-
end
|
23
|
+
if defined?(Rails)
|
24
|
+
require 'markababy/rails_template_context'
|
25
|
+
require 'markababy/rails_template_handler'
|
26
|
+
require 'markababy/railtie'
|
66
27
|
end
|
67
28
|
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
module Markababy
|
2
|
+
class Builder < BasicObject
|
3
|
+
def initialize(options, &block)
|
4
|
+
@options = options
|
5
|
+
|
6
|
+
@output = @options[:output]
|
7
|
+
|
8
|
+
@escape = @options[:escape]
|
9
|
+
|
10
|
+
@context = @options[:context]
|
11
|
+
|
12
|
+
instance_eval(&block)
|
13
|
+
end
|
14
|
+
|
15
|
+
def context_responds_to?(name)
|
16
|
+
@context.respond_to?(name)
|
17
|
+
end
|
18
|
+
|
19
|
+
def method_missing(sym, *args, &block)
|
20
|
+
if @context && context_responds_to?(sym)
|
21
|
+
return @context.send(sym, *args, &block)
|
22
|
+
end
|
23
|
+
|
24
|
+
attributes, content = [], []
|
25
|
+
|
26
|
+
args.flatten.each do |arg|
|
27
|
+
if arg.respond_to?(:to_hash)
|
28
|
+
arg.to_hash.each { |k, v| attributes << ' %s="%s"' % [@escape[k.to_s], @escape[v.to_s]] }
|
29
|
+
elsif arg.respond_to?(:id2name)
|
30
|
+
attributes << ' %s' % @escape[arg.to_s]
|
31
|
+
elsif arg.respond_to?(:html_safe?) && arg.html_safe?
|
32
|
+
content << arg.to_s
|
33
|
+
else
|
34
|
+
content << @escape[arg.to_s]
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
@output << (attributes.empty? ? "<#{sym}>" : "<#{sym}#{attributes.join}>")
|
39
|
+
|
40
|
+
@output << content.join unless content.empty?
|
41
|
+
|
42
|
+
instance_eval(&block) unless block.nil?
|
43
|
+
|
44
|
+
@output << "</#{sym}>" unless content.empty? && block.nil?
|
45
|
+
end
|
46
|
+
|
47
|
+
def text(content)
|
48
|
+
if content.respond_to?(:html_safe?) && content.html_safe?
|
49
|
+
@output << content.to_s
|
50
|
+
else
|
51
|
+
@output << @escape[content.to_s]
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def self.const_missing(name)
|
56
|
+
::Object.const_get(name)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
@@ -1,12 +1,9 @@
|
|
1
|
-
require 'markababy'
|
2
|
-
require 'action_view'
|
3
|
-
|
4
1
|
module Markababy
|
5
2
|
class RailsTemplateContext
|
6
3
|
def initialize(controller)
|
7
4
|
@controller = controller
|
8
5
|
|
9
|
-
@ivars = @controller.instance_variables
|
6
|
+
@ivars = @controller.instance_variables.map(&:to_sym)
|
10
7
|
end
|
11
8
|
|
12
9
|
def respond_to_missing?(sym, include_private = false)
|
@@ -23,19 +20,4 @@ module Markababy
|
|
23
20
|
end
|
24
21
|
end
|
25
22
|
end
|
26
|
-
|
27
|
-
module RailsTemplateHandler
|
28
|
-
def self.call(template)
|
29
|
-
"self.output_buffer = ''\n" +
|
30
|
-
"Markababy.capture(output: self.output_buffer, context: Markababy::RailsTemplateContext.new(self)) do\n" +
|
31
|
-
"#{template.source}\n" +
|
32
|
-
"end\n"
|
33
|
-
end
|
34
|
-
|
35
|
-
def self.extended(base)
|
36
|
-
base.register_default_template_handler :rb, self
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
ActionView::Template.extend RailsTemplateHandler
|
41
23
|
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
module Markababy
|
2
|
+
module RailsTemplateHandler
|
3
|
+
def self.call(template, source)
|
4
|
+
"self.output_buffer = ''\n" +
|
5
|
+
"Markababy.capture(:output => self.output_buffer, :context => Markababy::RailsTemplateContext.new(self)) do\n" +
|
6
|
+
"#{template.source}\n" +
|
7
|
+
"end\n"
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
data/markababy.gemspec
CHANGED
@@ -1,14 +1,19 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = 'markababy'
|
3
|
-
s.version = '1.
|
3
|
+
s.version = '1.3.2'
|
4
|
+
s.license = 'LGPL-3.0'
|
4
5
|
s.platform = Gem::Platform::RUBY
|
5
6
|
s.authors = ['Tim Craft']
|
6
7
|
s.email = ['mail@timcraft.com']
|
7
|
-
s.homepage = '
|
8
|
+
s.homepage = 'https://github.com/timcraft/markababy'
|
8
9
|
s.description = 'Markaby\'s little sister'
|
9
10
|
s.summary = 'See description'
|
10
|
-
s.files = Dir.glob('
|
11
|
-
s.
|
12
|
-
s.add_development_dependency('actionpack', ['>= 3.0.3'])
|
11
|
+
s.files = Dir.glob('lib/**/*.rb') + %w(LICENSE.txt README.md markababy.gemspec)
|
12
|
+
s.required_ruby_version = '>= 1.9.3'
|
13
13
|
s.require_path = 'lib'
|
14
|
+
s.metadata = {
|
15
|
+
'homepage' => 'https://github.com/timcraft/markababy',
|
16
|
+
'source_code_uri' => 'https://github.com/timcraft/markababy',
|
17
|
+
'bug_tracker_uri' => 'https://github.com/timcraft/markababy/issues'
|
18
|
+
}
|
14
19
|
end
|
metadata
CHANGED
@@ -1,38 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: markababy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
5
|
-
prerelease:
|
4
|
+
version: 1.3.2
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Tim Craft
|
9
|
-
autorequire:
|
8
|
+
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
13
|
-
dependencies:
|
14
|
-
- !ruby/object:Gem::Dependency
|
15
|
-
name: activesupport
|
16
|
-
requirement: &10149360 !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
|
-
requirements:
|
19
|
-
- - ! '>='
|
20
|
-
- !ruby/object:Gem::Version
|
21
|
-
version: 3.0.3
|
22
|
-
type: :development
|
23
|
-
prerelease: false
|
24
|
-
version_requirements: *10149360
|
25
|
-
- !ruby/object:Gem::Dependency
|
26
|
-
name: actionpack
|
27
|
-
requirement: &10149030 !ruby/object:Gem::Requirement
|
28
|
-
none: false
|
29
|
-
requirements:
|
30
|
-
- - ! '>='
|
31
|
-
- !ruby/object:Gem::Version
|
32
|
-
version: 3.0.3
|
33
|
-
type: :development
|
34
|
-
prerelease: false
|
35
|
-
version_requirements: *10149030
|
11
|
+
date: 2021-01-04 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
36
13
|
description: Markaby's little sister
|
37
14
|
email:
|
38
15
|
- mail@timcraft.com
|
@@ -40,36 +17,38 @@ executables: []
|
|
40
17
|
extensions: []
|
41
18
|
extra_rdoc_files: []
|
42
19
|
files:
|
43
|
-
-
|
20
|
+
- LICENSE.txt
|
21
|
+
- README.md
|
44
22
|
- lib/markababy.rb
|
45
|
-
-
|
46
|
-
-
|
47
|
-
-
|
48
|
-
-
|
49
|
-
- Rakefile
|
23
|
+
- lib/markababy/builder.rb
|
24
|
+
- lib/markababy/rails_template_context.rb
|
25
|
+
- lib/markababy/rails_template_handler.rb
|
26
|
+
- lib/markababy/railtie.rb
|
50
27
|
- markababy.gemspec
|
51
|
-
homepage:
|
52
|
-
licenses:
|
53
|
-
|
28
|
+
homepage: https://github.com/timcraft/markababy
|
29
|
+
licenses:
|
30
|
+
- LGPL-3.0
|
31
|
+
metadata:
|
32
|
+
homepage: https://github.com/timcraft/markababy
|
33
|
+
source_code_uri: https://github.com/timcraft/markababy
|
34
|
+
bug_tracker_uri: https://github.com/timcraft/markababy/issues
|
35
|
+
post_install_message:
|
54
36
|
rdoc_options: []
|
55
37
|
require_paths:
|
56
38
|
- lib
|
57
39
|
required_ruby_version: !ruby/object:Gem::Requirement
|
58
|
-
none: false
|
59
40
|
requirements:
|
60
|
-
- -
|
41
|
+
- - ">="
|
61
42
|
- !ruby/object:Gem::Version
|
62
|
-
version:
|
43
|
+
version: 1.9.3
|
63
44
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
64
|
-
none: false
|
65
45
|
requirements:
|
66
|
-
- -
|
46
|
+
- - ">="
|
67
47
|
- !ruby/object:Gem::Version
|
68
48
|
version: '0'
|
69
49
|
requirements: []
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
specification_version: 3
|
50
|
+
rubygems_version: 3.2.3
|
51
|
+
signing_key:
|
52
|
+
specification_version: 4
|
74
53
|
summary: See description
|
75
54
|
test_files: []
|
data/README.txt
DELETED
@@ -1,29 +0,0 @@
|
|
1
|
-
Markaby's little sister.
|
2
|
-
|
3
|
-
Usage is similar to Markaby, and easy to use directly from a Ruby script:
|
4
|
-
|
5
|
-
require 'markababy'
|
6
|
-
|
7
|
-
Markababy.markup do
|
8
|
-
html do
|
9
|
-
head { title 'Boats.com' }
|
10
|
-
body do
|
11
|
-
h1 'Boats.com has great deals'
|
12
|
-
ul do
|
13
|
-
li '$49 for a canoe'
|
14
|
-
li '$39 for a raft'
|
15
|
-
li '$29 for a huge boot that floats and can fit 5 people'
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
Use Markababy.capture if you want to capture the output as a string instead
|
22
|
-
of printing it to $stdout.
|
23
|
-
|
24
|
-
Some differences from Markaby:
|
25
|
-
|
26
|
-
* No auto-stringification
|
27
|
-
* No element classes or IDs
|
28
|
-
* No validation
|
29
|
-
* No XHTML
|
data/Rakefile
DELETED
@@ -1,28 +0,0 @@
|
|
1
|
-
require 'minitest/autorun'
|
2
|
-
require 'abstract_controller'
|
3
|
-
require 'markababy'
|
4
|
-
require 'markababy/rails'
|
5
|
-
|
6
|
-
class DummyController < AbstractController::Base
|
7
|
-
# cf. http://amberbit.com/blog/render-views-partials-outside-controllers-rails-3
|
8
|
-
|
9
|
-
include AbstractController::Rendering
|
10
|
-
include AbstractController::Layouts
|
11
|
-
include AbstractController::Helpers
|
12
|
-
|
13
|
-
self.view_paths = 'spec/views'
|
14
|
-
|
15
|
-
def index
|
16
|
-
@message = 'Controller says hello!'
|
17
|
-
|
18
|
-
render template: 'index'
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
describe DummyController do
|
23
|
-
it 'should return the correct markup' do
|
24
|
-
output = '<html><head><title>Controller says hello!</title><p>12,345,678</p></head></html>'
|
25
|
-
|
26
|
-
DummyController.new.index.must_equal output
|
27
|
-
end
|
28
|
-
end
|
data/spec/markababy_spec.rb
DELETED
@@ -1,73 +0,0 @@
|
|
1
|
-
require 'minitest/autorun'
|
2
|
-
require 'markababy'
|
3
|
-
require 'active_support/core_ext/string/output_safety'
|
4
|
-
|
5
|
-
class ExampleContext
|
6
|
-
def baconize(word)
|
7
|
-
"#{word} bacon!"
|
8
|
-
end
|
9
|
-
end
|
10
|
-
|
11
|
-
describe Markababy do
|
12
|
-
it 'should render tags without attributes or content correctly' do
|
13
|
-
Markababy.capture { br }.must_equal '<br>'
|
14
|
-
end
|
15
|
-
|
16
|
-
it 'should render tags with content correctly' do
|
17
|
-
Markababy.capture { title 'Untitled' }.must_equal '<title>Untitled</title>'
|
18
|
-
end
|
19
|
-
|
20
|
-
it 'should escape content correctly' do
|
21
|
-
Markababy.capture { h1 'Apples & Oranges' }.must_equal '<h1>Apples & Oranges</h1>'
|
22
|
-
end
|
23
|
-
|
24
|
-
it 'should render tags with an attribute hash correctly' do
|
25
|
-
Markababy.capture { input type: :text, size: 40 }.must_equal '<input type="text" size="40">'
|
26
|
-
end
|
27
|
-
|
28
|
-
it 'should render tags with an attribute array correctly' do
|
29
|
-
Markababy.capture { input [{type: :text}, :disabled] }.must_equal '<input type="text" disabled>'
|
30
|
-
end
|
31
|
-
|
32
|
-
it 'should render tags with attributes and content correctly' do
|
33
|
-
Markababy.capture { div 'O hai', class: 'name' }.must_equal '<div class="name">O hai</div>'
|
34
|
-
end
|
35
|
-
|
36
|
-
it 'should render nested tags correctly' do
|
37
|
-
Markababy.capture { h1 { span 'Chunky bacon!' } }.must_equal '<h1><span>Chunky bacon!</span></h1>'
|
38
|
-
end
|
39
|
-
|
40
|
-
it 'should allow output target to be specified' do
|
41
|
-
output = []
|
42
|
-
|
43
|
-
Markababy.markup(output: output) { hr }
|
44
|
-
|
45
|
-
output.join.must_equal '<hr>'
|
46
|
-
end
|
47
|
-
|
48
|
-
it 'should allow context for method lookup to be specified' do
|
49
|
-
output = Markababy.capture(context: ExampleContext.new) { h1 baconize('Super chunky') }
|
50
|
-
|
51
|
-
output.must_equal '<h1>Super chunky bacon!</h1>'
|
52
|
-
end
|
53
|
-
|
54
|
-
it 'should provide a method for rendering text content' do
|
55
|
-
output = Markababy.capture { h1 { text 'Hello '; strong 'World' } }
|
56
|
-
|
57
|
-
output.must_equal '<h1>Hello <strong>World</strong></h1>'
|
58
|
-
end
|
59
|
-
|
60
|
-
it 'should respect the html_safe? method' do
|
61
|
-
Markababy.capture { text 'Hello World'.html_safe }.must_equal 'Hello World'
|
62
|
-
|
63
|
-
output = Markababy.capture { h1 'Hello <strong>World</strong>'.html_safe }
|
64
|
-
|
65
|
-
output.must_equal '<h1>Hello <strong>World</strong></h1>'
|
66
|
-
end
|
67
|
-
|
68
|
-
it 'should provide an option for including a doctype declaration' do
|
69
|
-
output = Markababy.capture(doctype: true) { html { body { p 'INSERT CONTENT HERE' } } }
|
70
|
-
|
71
|
-
output.must_equal "<!DOCTYPE html>\n<html><body><p>INSERT CONTENT HERE</p></body></html>"
|
72
|
-
end
|
73
|
-
end
|