Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 0 additions & 27 deletions .github/workflows/main.yml

This file was deleted.

7 changes: 6 additions & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
AllCops:
TargetRubyVersion: 2.5
Exclude:
- 'benchmark/**/*'

Style/StringLiterals:
EnforcedStyle: double_quotes
Expand All @@ -10,8 +12,11 @@ Style/StringLiteralsInInterpolation:
Metrics/ModuleLength:
Max: 150

Metrics/MethodLength:
Max: 20

Metrics/AbcSize:
Max: 19
Max: 30

Style/Documentation:
Enabled: false
9 changes: 9 additions & 0 deletions benchmark/Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

source "https://rubygems.org"

# gem "rails"

gem "benchmark"
gem "erubi"
gem "html_slice"
18 changes: 18 additions & 0 deletions benchmark/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
GEM
remote: https://rubygems.org/
specs:
benchmark (0.4.0)
erubi (1.13.1)
html_slice (0.2.0)

PLATFORMS
ruby
x86_64-linux

DEPENDENCIES
benchmark
erubi
html_slice

BUNDLED WITH
2.5.17
45 changes: 45 additions & 0 deletions benchmark/benchmark.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# frozen_string_literal: true

require "erubi"
# require 'html_slice'
require_relative "../lib/html_slice"
require "benchmark"

module Partials
def hello_h1
h1 "hello"
end
end

class IndexHtml
include HtmlSlice
include Partials

def call
html_slice do
3.times do
h1 "Benchmark"
hello_h1
end
end
end
end

Benchmark.bm do |x|
x.report("(erubi) parsing an erb string") do
eval(Erubi::Engine.new("
<% 3.times do %>
<h1> Benchmark </h1>
<%= eval(Erubi::Engine.new('<h1>hello</h1>').src) %>
<% end %>
").src)
end

x.report("(erubi) reading and parsing an erb file") do
eval(Erubi::Engine.new(File.read("index.html.erb")).src)
end

x.report("html_slice") do
IndexHtml.new.call
end
end
1 change: 1 addition & 0 deletions benchmark/hello_h1.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>hello</p>
4 changes: 4 additions & 0 deletions benchmark/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<% 3.times do %>
<h1> Benchmark </h1>
<%= eval(Erubi::Engine.new(File.read("hello_h1.html.erb")).src) %>
<% end %>
Empty file added benchmark/index_html.rb
Empty file.
Binary file removed html_slice-0.2.0.gem
Binary file not shown.
Binary file added html_slice-0.2.1.gem
Binary file not shown.
9 changes: 6 additions & 3 deletions lib/html_slice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def html_layout(html_slice_current_id = DEFAULT_SLICE, &block)

def html_slice(html_slice_current_id = DEFAULT_SLICE, wrap: ["", ""], &block)
@html_slice_current_id = html_slice_current_id
if block_given?
if block
@html_slice ||= {}
@html_slice[@html_slice_current_id] = wrap[0].dup
instance_eval(&block)
Expand Down Expand Up @@ -122,8 +122,11 @@ def parse_html_tag_arguments(args)

def generate_and_append_html_tag(tag_name, content, attributes, &block)
open_tag = build_html_open_tag(tag_name, attributes)
@html_slice ||= {}
@html_slice_current_id ||= DEFAULT_SLICE
@html_slice[@html_slice_current_id] ||= +""

if block_given?
if block
@html_slice[@html_slice_current_id] << open_tag << ">"
instance_eval(&block)
@html_slice[@html_slice_current_id] << "</#{tag_name}>"
Expand All @@ -137,7 +140,7 @@ def generate_and_append_html_tag(tag_name, content, attributes, &block)
def build_html_open_tag(tag_name, attributes)
open_tag = "<#{tag_name}"
attributes.each do |key, value|
open_tag << " #{key.to_s.gsub("_", "-")}='#{value}'"
open_tag << " #{key.to_s.tr("_", "-")}='#{value}'"
end
open_tag
end
Expand Down
2 changes: 1 addition & 1 deletion lib/html_slice/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module HtmlSlice
VERSION = "0.2.0"
VERSION = "0.2.1"
end
10 changes: 10 additions & 0 deletions spec/html_slice_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,16 @@
expect(result).to eq("<div><h1>Hello World</h1></div>")
end

it "we can run tag methods without html_slice init" do
html_generator.tag :header do
meta charset: "utf-8"
end
result = html_generator.div do
h1 "hello"
end
expect(result).to eq("<header><meta charset='utf-8'/></header><div><h1>hello</h1></div>")
end

describe "wrapping" do
it "append wrap content in start and end of result" do
html_generator.html_slice wrap: %w[some thing] do
Expand Down