Skip to content
Open
Show file tree
Hide file tree
Changes from 4 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
15 changes: 15 additions & 0 deletions ext/herb/extension.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,20 @@ static VALUE Herb_extract_ruby(VALUE self, VALUE source) {
return result;
}

static VALUE Herb_extract_ruby_with_semicolons(VALUE self, VALUE source) {
char* string = (char*) check_string(source);
hb_buffer_T output;

if (!hb_buffer_init(&output, strlen(string))) { return Qnil; }

herb_extract_ruby_to_buffer_with_semicolons(string, &output);

VALUE result = rb_utf8_str_new_cstr(output.value);
free(output.value);

return result;
}

static VALUE Herb_extract_html(VALUE self, VALUE source) {
char* string = (char*) check_string(source);
hb_buffer_T output;
Expand Down Expand Up @@ -137,6 +151,7 @@ void Init_herb(void) {
rb_define_singleton_method(mHerb, "parse_file", Herb_parse_file, 1);
rb_define_singleton_method(mHerb, "lex_file", Herb_lex_file, 1);
rb_define_singleton_method(mHerb, "extract_ruby", Herb_extract_ruby, 1);
rb_define_singleton_method(mHerb, "extract_ruby_with_semicolons", Herb_extract_ruby_with_semicolons, 1);
rb_define_singleton_method(mHerb, "extract_html", Herb_extract_html, 1);
rb_define_singleton_method(mHerb, "version", Herb_version, 0);
}
1 change: 1 addition & 0 deletions lib/herb/libherb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def self.library_path
attach_function :herb_parse, [:pointer], :pointer
attach_function :herb_extract_ruby_to_buffer, [:pointer, :pointer], :void
attach_function :herb_extract_html_to_buffer, [:pointer, :pointer], :void
attach_function :herb_extract_ruby_to_buffer_with_semicolons, [:pointer, :pointer], :void
attach_function :herb_version, [], :pointer
end
end
8 changes: 8 additions & 0 deletions lib/herb/libherb/libherb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ def self.extract_ruby(source)
end
end

def self.extract_ruby_with_semicolons(source)
LibHerb::Buffer.with do |output|
LibHerb.herb_extract_ruby_to_buffer_with_semicolons(source, output.pointer)

output.read
end
end

def self.extract_html(source)
LibHerb::Buffer.with do |output|
LibHerb.herb_extract_html_to_buffer(source, output.pointer)
Expand Down
3 changes: 3 additions & 0 deletions sig/herb_c_extension.rbs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

112 changes: 112 additions & 0 deletions test/extractor/extract_ruby_semicolons_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
# frozen_string_literal: true

require_relative "../test_helper"

module Extractor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

class ExtractRubySemicolonsTest < Minitest::Spec
test "basic silent" do
ruby = Herb.extract_ruby_with_semicolons("<h1><% RUBY_VERSION %></h1>")

assert_equal " RUBY_VERSION ; ", ruby
end

test "basic loud" do
ruby = Herb.extract_ruby_with_semicolons("<h1><%= RUBY_VERSION %></h1>")

assert_equal " RUBY_VERSION ; ", ruby
end

test "with newlines" do
actual = Herb.extract_ruby_with_semicolons(<<~HTML)
<h1>
<% RUBY_VERSION %>
</h1>
HTML

assert_equal " \n RUBY_VERSION ; \n \n", actual
end

test "nested" do
actual = Herb.extract_ruby_with_semicolons(<<~HTML)
<% array = [1, 2, 3] %>

<ul>
<% array.each do |item| %>
<li><%= item %></li>
<% end %>
</ul>
HTML

expected = " array = [1, 2, 3] ; \n\n \n array.each do |item| ; \n item ; \n end ; \n \n"

assert_equal expected, actual
end

test "erb comment" do
actual = Herb.extract_ruby_with_semicolons(<<~HTML)
<%# comment ' %>
HTML

expected = " ; \n"

assert_equal expected, actual
end

test "erb comment with ruby keyword" do
actual = Herb.extract_ruby_with_semicolons(<<~HTML)
<%# end %>
HTML

expected = " ; \n"

assert_equal expected, actual
end

test "erb comment broken up over multiple lines" do
actual = Herb.extract_ruby_with_semicolons(<<~HTML)
<%#
end
%>
HTML

expected = " ; \n"

assert_equal expected, actual
end

test "multi-line erb comment" do
actual = Herb.extract_ruby_with_semicolons(<<~HTML)
<%#
end
end
end
end
%>
HTML

expected = " ; \n"

assert_equal expected, actual
end

test "erb if/end and comment on same line" do
actual = Herb.extract_ruby_with_semicolons(<<~HTML)
<% if %><%# comment %><% end %>
HTML

expected = " if ; ; end ; \n"

assert_equal expected, actual
end

xtest "erb if/end and Ruby comment on same line" do
actual = Herb.extract_ruby_with_semicolons(<<~HTML)
<% if %><% # comment %><% end %>
HTML

expected = " if # comment end \n"

assert_equal expected, actual
end
end
end
Loading