diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml deleted file mode 100644 index 12ea855..0000000 --- a/.github/workflows/main.yml +++ /dev/null @@ -1,27 +0,0 @@ -name: Ruby - -on: - push: - branches: - - master - - pull_request: - -jobs: - build: - runs-on: ubuntu-latest - name: Ruby ${{ matrix.ruby }} - strategy: - matrix: - ruby: - - '3.3.5' - - steps: - - uses: actions/checkout@v4 - - name: Set up Ruby - uses: ruby/setup-ruby@v1 - with: - ruby-version: ${{ matrix.ruby }} - bundler-cache: true - - name: Run the default task - run: bundle exec rake diff --git a/.rubocop.yml b/.rubocop.yml index 26d8434..0242c19 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,5 +1,7 @@ AllCops: TargetRubyVersion: 2.5 + Exclude: + - 'benchmark/**/*' Style/StringLiterals: EnforcedStyle: double_quotes @@ -10,8 +12,11 @@ Style/StringLiteralsInInterpolation: Metrics/ModuleLength: Max: 150 +Metrics/MethodLength: + Max: 20 + Metrics/AbcSize: - Max: 19 + Max: 30 Style/Documentation: Enabled: false diff --git a/benchmark/Gemfile b/benchmark/Gemfile new file mode 100644 index 0000000..0a94002 --- /dev/null +++ b/benchmark/Gemfile @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +source "https://rubygems.org" + +# gem "rails" + +gem "benchmark" +gem "erubi" +gem "html_slice" diff --git a/benchmark/Gemfile.lock b/benchmark/Gemfile.lock new file mode 100644 index 0000000..c35ab65 --- /dev/null +++ b/benchmark/Gemfile.lock @@ -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 diff --git a/benchmark/benchmark.rb b/benchmark/benchmark.rb new file mode 100644 index 0000000..990add3 --- /dev/null +++ b/benchmark/benchmark.rb @@ -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 %> +

Benchmark

+ <%= eval(Erubi::Engine.new('

hello

').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 diff --git a/benchmark/hello_h1.html.erb b/benchmark/hello_h1.html.erb new file mode 100644 index 0000000..302a01f --- /dev/null +++ b/benchmark/hello_h1.html.erb @@ -0,0 +1 @@ +

hello

diff --git a/benchmark/index.html.erb b/benchmark/index.html.erb new file mode 100644 index 0000000..ec54e51 --- /dev/null +++ b/benchmark/index.html.erb @@ -0,0 +1,4 @@ +<% 3.times do %> +

Benchmark

+ <%= eval(Erubi::Engine.new(File.read("hello_h1.html.erb")).src) %> +<% end %> diff --git a/benchmark/index_html.rb b/benchmark/index_html.rb new file mode 100644 index 0000000..e69de29 diff --git a/html_slice-0.2.0.gem b/html_slice-0.2.0.gem deleted file mode 100644 index 6ad54ee..0000000 Binary files a/html_slice-0.2.0.gem and /dev/null differ diff --git a/html_slice-0.2.1.gem b/html_slice-0.2.1.gem new file mode 100644 index 0000000..25c0625 Binary files /dev/null and b/html_slice-0.2.1.gem differ diff --git a/lib/html_slice.rb b/lib/html_slice.rb index f333a5a..550f2e4 100644 --- a/lib/html_slice.rb +++ b/lib/html_slice.rb @@ -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) @@ -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] << "" @@ -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 diff --git a/lib/html_slice/version.rb b/lib/html_slice/version.rb index da58daf..db89698 100644 --- a/lib/html_slice/version.rb +++ b/lib/html_slice/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module HtmlSlice - VERSION = "0.2.0" + VERSION = "0.2.1" end diff --git a/spec/html_slice_spec.rb b/spec/html_slice_spec.rb index f37c878..e270c33 100644 --- a/spec/html_slice_spec.rb +++ b/spec/html_slice_spec.rb @@ -112,6 +112,16 @@ expect(result).to eq("

Hello World

") 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("

hello

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