Skip to content

Commit e9a13ce

Browse files
committed
Initialize Ruby C-extension
1 parent 3a23d4c commit e9a13ce

File tree

12 files changed

+132
-0
lines changed

12 files changed

+132
-0
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66
/erbx
77
/run_erbx_tests
88

9+
# ERBx Ruby extension
10+
/ext/erbx/extconf.h
11+
/ext/erbx/erbx.bundle
12+
/ext/erbx/Makefile
13+
/lib/erbx/erbx.bundle
14+
915
# Prerequisites
1016
*.d
1117

Rakefile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
require "bundler/gem_tasks"
2+
require "rake/extensiontask"
3+
4+
Rake::ExtensionTask.new do |ext|
5+
ext.name = "erbx"
6+
ext.source_pattern = "*.{c,h}"
7+
ext.ext_dir = "ext/erbx"
8+
ext.lib_dir = "lib/erbx"
9+
ext.gem_spec = Gem::Specification.load("erbx.gemspec")
10+
end
11+
12+
task default: [:compile]

erbx.gemspec

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# frozen_string_literal: true
2+
3+
require_relative "lib/erbx/version"
4+
5+
Gem::Specification.new do |spec|
6+
spec.name = "erbx"
7+
spec.version = ERBX::VERSION
8+
spec.authors = ["Marco Roth"]
9+
spec.email = ["marco.roth@intergga.ch"]
10+
11+
spec.summary = "HTML-aware ERB parser"
12+
spec.homepage = "https://github.com/marcoroth/erbx"
13+
spec.license = "MIT"
14+
15+
spec.required_ruby_version = ">= 3.0.0"
16+
17+
spec.require_paths = ["lib"]
18+
spec.files = [
19+
"ext/erbx/extension.c",
20+
"lib/erbx.rb",
21+
"lib/erbx/version.rb"
22+
]
23+
24+
spec.extensions = ["ext/erbx/extconf.rb"]
25+
spec.metadata["allowed_push_host"] = "https://rubygems.org"
26+
spec.metadata["source_code_uri"] = "https://github.com/marcoroth/erbx"
27+
spec.metadata["changelog_uri"] = "https://github.com/marcoroth/erbx/releases"
28+
29+
spec.add_development_dependency "rake", "~> 1.9"
30+
spec.add_development_dependency "rake-compiler", "~> 0.8"
31+
end

examples/ruby/Gemfile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# frozen_string_literal: true
2+
3+
source "https://rubygems.org"
4+
5+
gem "erbx", path: "../.."

examples/ruby/Gemfile.lock

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
PATH
2+
remote: ../..
3+
specs:
4+
erbx (0.0.1)
5+
6+
GEM
7+
remote: https://rubygems.org/
8+
specs:
9+
10+
PLATFORMS
11+
ruby
12+
x86_64-darwin-23
13+
14+
DEPENDENCIES
15+
erbx!
16+
17+
BUNDLED WITH
18+
2.5.10

examples/ruby/test.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
require "erbx"
2+
3+
puts ERBX::Compiler.new.compile("<html><html>")

ext/erbx/extconf.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
require "mkmf"
2+
3+
lib_path = File.expand_path("../../", __dir__)
4+
include_path = File.expand_path("../../src/include", __dir__)
5+
6+
dir_config("erbx", include_path, lib_path)
7+
8+
unless find_header("erbx.h", include_path)
9+
abort "erbx.h can't be found"
10+
end
11+
12+
unless find_library("erbx", "erbx_compile_file", lib_path, "-Wl,-rpath,#{lib_path}")
13+
abort "liberbx.so can't be found"
14+
end
15+
16+
create_header
17+
create_makefile("erbx/erbx")

ext/erbx/extension.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include "ruby.h"
2+
#include "extconf.h"
3+
#include "extension.h"
4+
5+
#include "../../src/include/erbx.h"
6+
7+
static const char *check_string(VALUE value) {
8+
if (NIL_P(value)) {
9+
return NULL;
10+
}
11+
12+
if (!RB_TYPE_P(value, T_STRING)) {
13+
rb_raise(rb_eTypeError, "wrong argument type %" PRIsVALUE " (expected String)", rb_obj_class(value));
14+
}
15+
16+
return RSTRING_PTR(value);
17+
}
18+
19+
VALUE rb_erbx_compile(VALUE self, VALUE source) {
20+
const char *string = check_string(source);
21+
22+
erbx_compile((char *) string);
23+
24+
return Qnil;
25+
}
26+
27+
void Init_erbx() {
28+
VALUE ERBX = rb_define_module("ERBX");
29+
VALUE Compiler = rb_define_class_under(ERBX, "Compiler", rb_cObject);
30+
31+
rb_define_method(Compiler, "compile", rb_erbx_compile, 1);
32+
}

ext/erbx/extension.h

Whitespace-only changes.

ext/erbx/test.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
require "./erbx"
2+
3+
ERBX::Compiler.new.compile("<html><html>")

0 commit comments

Comments
 (0)