hmote 1.2.0 → 1.3.0
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 +4 -4
- data/.gems +2 -2
- data/LICENSE +3 -1
- data/README.md +12 -7
- data/benchmarks/.gems +3 -2
- data/benchmarks/escaping.rb +4 -10
- data/benchmarks/{helper.rb → helpers/helper.rb} +1 -1
- data/benchmarks/{hmote_helper.rb → helpers/hmote_helper.rb} +1 -1
- data/benchmarks/{rails_helper.rb → helpers/rails_helper.rb} +0 -0
- data/benchmarks/memory.rb +24 -0
- data/benchmarks/safe.rb +4 -10
- data/hmote.gemspec +8 -6
- data/lib/hmote/render.rb +20 -7
- data/lib/hmote/version.rb +3 -0
- data/lib/hmote.rb +6 -6
- data/makefile +2 -1
- data/test/parsing.rb +8 -8
- data/test/render.rb +6 -17
- metadata +18 -17
- data/test/custom_views/custom.mote +0 -1
- data/test/custom_views/layout.mote +0 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 62b364d0904c64b47d40549e99b1b074f3b34830
|
4
|
+
data.tar.gz: 4f636c64838e7abfd047a963e37ce5d2de5392b8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cecb32632d690a9e5fccf139cf298c9d462633aa1438f2e935ddabc2b344e93d29249a9670ea2ee94dcd408c0b837deafdb1d1acb3aa979369411cc3d9827a56
|
7
|
+
data.tar.gz: 2df94a7b9b45bcf51978cb6bd326c3f86de5929777fc50ea91663f0abced820afc7aee35fceeb9c29c25ecf782189ad4735ab4886cffe6cc6edd6a45c0896976
|
data/.gems
CHANGED
data/LICENSE
CHANGED
@@ -1,4 +1,6 @@
|
|
1
|
-
Copyright (c) 2014
|
1
|
+
Copyright (c) 2011-2014 Michel Martens
|
2
|
+
Copyright (c) 2014-2015 Francesco Rodríguez
|
3
|
+
Copyright (c) 2014-2015 Mayn Ektvedt Kjær
|
2
4
|
|
3
5
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
6
|
of this software and associated documentation files (the "Software"), to deal
|
data/README.md
CHANGED
@@ -6,7 +6,7 @@ Minimal template engine with default escaping.
|
|
6
6
|
Description
|
7
7
|
-----------
|
8
8
|
|
9
|
-
HMote is
|
9
|
+
HMote is a fork of [Mote][mote] that uses [Hache][hache]
|
10
10
|
to auto-escape HTML special characters.
|
11
11
|
|
12
12
|
Basic Usage
|
@@ -22,18 +22,23 @@ template.call
|
|
22
22
|
# => "your template goes here!"
|
23
23
|
```
|
24
24
|
|
25
|
-
HMote recognizes
|
26
|
-
The difference between them is that while the `%`
|
27
|
-
the code, the `{{}}` tag also prints the result to the template.
|
25
|
+
HMote recognizes three tags to evaluate Ruby code: `%`, `{{}}` and `<? ?>`.
|
26
|
+
The difference between them is that while the `%` and `<? ?>` tags only
|
27
|
+
evaluate the code, the `{{}}` tag also prints the result to the template.
|
28
28
|
|
29
29
|
Imagine that your template looks like this:
|
30
30
|
|
31
31
|
```ruby
|
32
|
+
% # single-line code
|
32
33
|
% gems = ["rack", "cuba", "hmote"]
|
33
34
|
|
35
|
+
<?
|
36
|
+
# multi-line code
|
37
|
+
sorted = gems.sort
|
38
|
+
?>
|
39
|
+
|
34
40
|
<ul>
|
35
|
-
%
|
36
|
-
% gems.sort.each do |gem|
|
41
|
+
% sorted.each do |gem|
|
37
42
|
<li>{{ gem }}</li>
|
38
43
|
% end
|
39
44
|
</ul>
|
@@ -213,5 +218,5 @@ $ gem install hmote
|
|
213
218
|
|
214
219
|
[cuba]: http://cuba.is
|
215
220
|
[mote]: https://github.com/soveran/mote
|
216
|
-
[hache]: https://github.com/
|
221
|
+
[hache]: https://github.com/harmoni/hache
|
217
222
|
[xss]: http://en.wikipedia.org/wiki/Cross-Site_Scripting
|
data/benchmarks/.gems
CHANGED
@@ -1,2 +1,3 @@
|
|
1
|
-
actionview -v 4.
|
2
|
-
|
1
|
+
actionview -v 4.2.3
|
2
|
+
allocation_stats -v 0.1.5
|
3
|
+
benchmark-ips -v 2.3.0
|
data/benchmarks/escaping.rb
CHANGED
@@ -1,17 +1,11 @@
|
|
1
|
-
require_relative "helper"
|
2
|
-
require_relative "hmote_helper"
|
3
|
-
require_relative "rails_helper"
|
1
|
+
require_relative "helpers/helper"
|
2
|
+
require_relative "helpers/hmote_helper"
|
3
|
+
require_relative "helpers/rails_helper"
|
4
4
|
|
5
5
|
text = %q(some < text > inside & these " escapable' characters/1234)
|
6
6
|
|
7
7
|
Benchmark.ips do |x|
|
8
8
|
x.report("hmote") { hmote(text: text) }
|
9
9
|
x.report("rails") { rails(text: text) }
|
10
|
+
x.compare!
|
10
11
|
end
|
11
|
-
|
12
|
-
# Calculating -------------------------------------
|
13
|
-
# hmote 13.122k i/100ms
|
14
|
-
# rails 7.907k i/100ms
|
15
|
-
# -------------------------------------------------
|
16
|
-
# hmote 148.020k (± 3.6%) i/s - 747.954k
|
17
|
-
# rails 88.623k (± 2.7%) i/s - 442.792k
|
File without changes
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require_relative "helpers/helper"
|
2
|
+
require_relative "helpers/hmote_helper"
|
3
|
+
require "allocation_stats"
|
4
|
+
|
5
|
+
def benchmark(text)
|
6
|
+
stats = AllocationStats.new(burn: 5).trace do
|
7
|
+
hmote(text: text)
|
8
|
+
end
|
9
|
+
|
10
|
+
allocations = stats.allocations.all.size
|
11
|
+
memsize = stats.allocations.bytes.to_a.inject(&:+)
|
12
|
+
|
13
|
+
puts "total allocations: #{ allocations }"
|
14
|
+
puts "total memsize: #{ memsize }"
|
15
|
+
puts stats.allocations(alias_paths: true).to_text
|
16
|
+
end
|
17
|
+
|
18
|
+
puts "== Unsafe mode\n"
|
19
|
+
|
20
|
+
benchmark(%q(some < text > inside & these " escapable' characters/1234))
|
21
|
+
|
22
|
+
puts "\n== Safe mode\n"
|
23
|
+
|
24
|
+
benchmark(%q(some text without escapable characters))
|
data/benchmarks/safe.rb
CHANGED
@@ -1,17 +1,11 @@
|
|
1
|
-
require_relative "helper"
|
2
|
-
require_relative "hmote_helper"
|
3
|
-
require_relative "rails_helper"
|
1
|
+
require_relative "helpers/helper"
|
2
|
+
require_relative "helpers/hmote_helper"
|
3
|
+
require_relative "helpers/rails_helper"
|
4
4
|
|
5
5
|
text = %q(some text without escapable characters).html_safe
|
6
6
|
|
7
7
|
Benchmark.ips do |x|
|
8
8
|
x.report("hmote") { hmote(text: text) }
|
9
9
|
x.report("rails") { rails(text: text) }
|
10
|
+
x.compare!
|
10
11
|
end
|
11
|
-
|
12
|
-
# Calculating -------------------------------------
|
13
|
-
# hmote 29.035k i/100ms
|
14
|
-
# rails 14.607k i/100ms
|
15
|
-
# -------------------------------------------------
|
16
|
-
# hmote 367.044k (± 5.9%) i/s - 1.829M
|
17
|
-
# rails 157.936k (± 5.9%) i/s - 788.778k
|
data/hmote.gemspec
CHANGED
@@ -1,15 +1,17 @@
|
|
1
|
+
require_relative "lib/hmote/version"
|
2
|
+
|
1
3
|
Gem::Specification.new do |s|
|
2
4
|
s.name = "hmote"
|
3
|
-
s.version =
|
5
|
+
s.version = HMote::VERSION
|
4
6
|
s.summary = "A minimum operational template that escapes HTML tags by default."
|
5
|
-
s.description = s.summary
|
6
|
-
s.authors
|
7
|
-
s.email
|
7
|
+
s.description = s.summary + " Inspired by mote."
|
8
|
+
s.authors = ["Francesco Rodríguez", "Mayn Kjær"]
|
9
|
+
s.email = ["frodsan@protonmail.ch", "mayn.kjaer@gmail.com"]
|
8
10
|
s.homepage = "https://github.com/harmoni/hmote"
|
9
11
|
s.license = "MIT"
|
10
12
|
|
11
13
|
s.files = `git ls-files`.split("\n")
|
12
14
|
|
13
|
-
s.add_dependency "hache"
|
14
|
-
s.add_development_dependency "cutest"
|
15
|
+
s.add_dependency "hache", "~> 1.1"
|
16
|
+
s.add_development_dependency "cutest", "~> 1.2"
|
15
17
|
end
|
data/lib/hmote/render.rb
CHANGED
@@ -3,15 +3,22 @@ require_relative "../hmote"
|
|
3
3
|
module HMote::Render
|
4
4
|
include HMote::Helpers
|
5
5
|
|
6
|
+
CONTENT_TYPE = "Content-Type".freeze
|
7
|
+
DEFAULT_CONTENT_TYPE = "text/html; charset=utf-8".freeze
|
8
|
+
|
6
9
|
def self.setup(app)
|
7
10
|
app.settings[:hmote] ||= {}
|
8
|
-
|
9
|
-
app.
|
11
|
+
|
12
|
+
app.layout("layout")
|
13
|
+
app.view_path("views")
|
10
14
|
end
|
11
15
|
|
12
16
|
def render(template, params = {}, layout = settings[:hmote][:layout])
|
13
|
-
res.
|
17
|
+
res.status ||= 200
|
18
|
+
res.headers[CONTENT_TYPE] ||= DEFAULT_CONTENT_TYPE
|
14
19
|
res.write(view(template, params, layout))
|
20
|
+
|
21
|
+
halt(res.finish)
|
15
22
|
end
|
16
23
|
|
17
24
|
def view(template, params = {}, layout = settings[:hmote][:layout])
|
@@ -23,10 +30,16 @@ module HMote::Render
|
|
23
30
|
end
|
24
31
|
|
25
32
|
def template_path(template)
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
33
|
+
return File.join(settings[:hmote][:views], "#{template}.mote")
|
34
|
+
end
|
35
|
+
|
36
|
+
module ClassMethods
|
37
|
+
def layout(name)
|
38
|
+
settings[:hmote][:layout] = name
|
39
|
+
end
|
40
|
+
|
41
|
+
def view_path(path)
|
42
|
+
settings[:hmote][:views] = File.expand_path(path, Dir.pwd)
|
30
43
|
end
|
31
44
|
end
|
32
45
|
end
|
data/lib/hmote.rb
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
require "hache"
|
2
2
|
|
3
3
|
class HMote
|
4
|
-
PATTERN =
|
5
|
-
^\
|
6
|
-
(<\?)\s+(.*?)\s
|
7
|
-
(\{\{!?)(.*?)\}\}
|
4
|
+
PATTERN = /
|
5
|
+
^[^\S\n]*(%)[^\S\n]*(.*?)(?:\n|\z) | # % single-line code
|
6
|
+
(<\?)\s+(.*?)\s+\?> | # <? multi-line code ?>
|
7
|
+
(\{\{!?)(.*?)\}\} # {{ escape }} or {{! unescape }}
|
8
8
|
/mx
|
9
9
|
|
10
10
|
def self.parse(template, context = self, vars = [])
|
@@ -12,10 +12,10 @@ class HMote
|
|
12
12
|
parts = "Proc.new do |params, __o|\n params ||= {}; __o ||= ''\n"
|
13
13
|
|
14
14
|
vars.each do |var|
|
15
|
-
parts << "%s = params[%
|
15
|
+
parts << sprintf("%s = params[%p]\n", var, var)
|
16
16
|
end
|
17
17
|
|
18
|
-
while term = terms.shift
|
18
|
+
while (term = terms.shift)
|
19
19
|
case term
|
20
20
|
when "<?" then parts << "#{terms.shift}\n"
|
21
21
|
when "%" then parts << "#{terms.shift}\n"
|
data/makefile
CHANGED
data/test/parsing.rb
CHANGED
@@ -36,7 +36,7 @@ scope("parsing") do
|
|
36
36
|
|
37
37
|
test "multiline" do
|
38
38
|
example = HMote.parse("The\nMan\nAnd\n{{\"The\"}}\nSea")
|
39
|
-
assert_equal("The\nMan\nAnd\nThe\nSea", example
|
39
|
+
assert_equal("The\nMan\nAnd\nThe\nSea", example.call)
|
40
40
|
end
|
41
41
|
|
42
42
|
test "quotes" do
|
@@ -67,17 +67,17 @@ scope("parsing") do
|
|
67
67
|
|
68
68
|
test "multi-line XML-style directives" do
|
69
69
|
template = (<<-EOT).gsub(/^ /, "")
|
70
|
-
<?
|
71
|
-
|
72
|
-
|
73
|
-
|
70
|
+
<?
|
71
|
+
# Multiline code evaluation
|
72
|
+
lucky = [1, 3, 7, 9, 13, 15]
|
73
|
+
prime = [2, 3, 5, 7, 11, 13]
|
74
74
|
?>
|
75
|
-
|
75
|
+
|
76
|
+
{{ lucky & prime }}
|
76
77
|
EOT
|
77
78
|
|
78
79
|
example = HMote.parse(template)
|
79
|
-
|
80
|
-
assert_equal("\n1. 1\n2. 4\n3. 9\n\n", example.call)
|
80
|
+
assert_equal "\n\n[3, 7, 13]\n", example.call
|
81
81
|
end
|
82
82
|
|
83
83
|
test "preserve XML directives" do
|
data/test/render.rb
CHANGED
@@ -26,12 +26,9 @@ Cuba.define do
|
|
26
26
|
res.write partial("context")
|
27
27
|
end
|
28
28
|
|
29
|
-
on "
|
30
|
-
render("
|
31
|
-
|
32
|
-
|
33
|
-
on "absolute_layout" do
|
34
|
-
render("./test/custom_views/custom.mote", title: "Custom Layout")
|
29
|
+
on "halt" do
|
30
|
+
render("home", title: "Halt before error")
|
31
|
+
raise "This is invalid"
|
35
32
|
end
|
36
33
|
end
|
37
34
|
|
@@ -62,17 +59,9 @@ scope do
|
|
62
59
|
assert last_response.body["App"]
|
63
60
|
end
|
64
61
|
|
65
|
-
test "
|
66
|
-
get "/
|
67
|
-
|
68
|
-
assert last_response.body["<title>Custom</title>\n<h1>Custom</h1>"]
|
69
|
-
end
|
70
|
-
|
71
|
-
test "use of absolute path for layout" do
|
72
|
-
Cuba.settings[:hmote][:layout] = "./test/custom_views/layout.mote"
|
73
|
-
|
74
|
-
get "/absolute_layout"
|
62
|
+
test "render halts request" do
|
63
|
+
get "/halt"
|
75
64
|
|
76
|
-
assert
|
65
|
+
assert true
|
77
66
|
end
|
78
67
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hmote
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Francesco Rodríguez
|
@@ -9,39 +9,40 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2015-
|
12
|
+
date: 2015-08-17 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: hache
|
16
16
|
requirement: !ruby/object:Gem::Requirement
|
17
17
|
requirements:
|
18
|
-
- - "
|
18
|
+
- - "~>"
|
19
19
|
- !ruby/object:Gem::Version
|
20
|
-
version: '
|
20
|
+
version: '1.1'
|
21
21
|
type: :runtime
|
22
22
|
prerelease: false
|
23
23
|
version_requirements: !ruby/object:Gem::Requirement
|
24
24
|
requirements:
|
25
|
-
- - "
|
25
|
+
- - "~>"
|
26
26
|
- !ruby/object:Gem::Version
|
27
|
-
version: '
|
27
|
+
version: '1.1'
|
28
28
|
- !ruby/object:Gem::Dependency
|
29
29
|
name: cutest
|
30
30
|
requirement: !ruby/object:Gem::Requirement
|
31
31
|
requirements:
|
32
|
-
- - "
|
32
|
+
- - "~>"
|
33
33
|
- !ruby/object:Gem::Version
|
34
|
-
version: '
|
34
|
+
version: '1.2'
|
35
35
|
type: :development
|
36
36
|
prerelease: false
|
37
37
|
version_requirements: !ruby/object:Gem::Requirement
|
38
38
|
requirements:
|
39
|
-
- - "
|
39
|
+
- - "~>"
|
40
40
|
- !ruby/object:Gem::Version
|
41
|
-
version: '
|
42
|
-
description: A minimum operational template that escapes HTML tags by default.
|
41
|
+
version: '1.2'
|
42
|
+
description: A minimum operational template that escapes HTML tags by default. Inspired
|
43
|
+
by mote.
|
43
44
|
email:
|
44
|
-
- frodsan@
|
45
|
+
- frodsan@protonmail.ch
|
45
46
|
- mayn.kjaer@gmail.com
|
46
47
|
executables: []
|
47
48
|
extensions: []
|
@@ -52,18 +53,18 @@ files:
|
|
52
53
|
- README.md
|
53
54
|
- benchmarks/.gems
|
54
55
|
- benchmarks/escaping.rb
|
55
|
-
- benchmarks/helper.rb
|
56
|
-
- benchmarks/hmote_helper.rb
|
57
|
-
- benchmarks/rails_helper.rb
|
56
|
+
- benchmarks/helpers/helper.rb
|
57
|
+
- benchmarks/helpers/hmote_helper.rb
|
58
|
+
- benchmarks/helpers/rails_helper.rb
|
59
|
+
- benchmarks/memory.rb
|
58
60
|
- benchmarks/safe.rb
|
59
61
|
- benchmarks/templates/erb
|
60
62
|
- benchmarks/templates/mote
|
61
63
|
- hmote.gemspec
|
62
64
|
- lib/hmote.rb
|
63
65
|
- lib/hmote/render.rb
|
66
|
+
- lib/hmote/version.rb
|
64
67
|
- makefile
|
65
|
-
- test/custom_views/custom.mote
|
66
|
-
- test/custom_views/layout.mote
|
67
68
|
- test/foo.mote
|
68
69
|
- test/helper.rb
|
69
70
|
- test/helpers.rb
|
@@ -1 +0,0 @@
|
|
1
|
-
<h1>Custom</h1>
|