From 55e87603e55b5d05fb5abf4e5af1323a167eb082 Mon Sep 17 00:00:00 2001 From: Yessiest Date: Tue, 26 Sep 2023 01:16:23 +0400 Subject: [PATCH] fixed basepath resolution for templates, fixed library resolution within plugged in apps, added plugin example with directories and requiring from an inline template --- examples/plugins2/config.ru | 10 ++++++++ examples/plugins2/lib | 1 + examples/plugins2/plugin/config.ru | 27 ++++++++++++++++++++++ examples/plugins2/plugin/lib | 1 + examples/plugins2/plugin/plugin_library.rb | 3 +++ landline.gemspec | 21 +++++++++-------- lib/landline/dsl/constructors_probe.rb | 14 ++++++----- lib/landline/dsl/methods_path.rb | 4 +++- lib/landline/template.rb | 9 ++++++-- lib/landline/template/erb.rb | 3 ++- lib/landline/template/erubi.rb | 12 ++++------ 11 files changed, 78 insertions(+), 27 deletions(-) create mode 100644 examples/plugins2/config.ru create mode 120000 examples/plugins2/lib create mode 100644 examples/plugins2/plugin/config.ru create mode 120000 examples/plugins2/plugin/lib create mode 100644 examples/plugins2/plugin/plugin_library.rb diff --git a/examples/plugins2/config.ru b/examples/plugins2/config.ru new file mode 100644 index 0000000..23da1d7 --- /dev/null +++ b/examples/plugins2/config.ru @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +$LOAD_PATH.unshift("#{File.dirname(__FILE__)}/lib") +require 'landline' + +app = Landline::Server.new do + plugin "./plugin/config.ru" +end + +run app diff --git a/examples/plugins2/lib b/examples/plugins2/lib new file mode 120000 index 0000000..58677dd --- /dev/null +++ b/examples/plugins2/lib @@ -0,0 +1 @@ +../../lib \ No newline at end of file diff --git a/examples/plugins2/plugin/config.ru b/examples/plugins2/plugin/config.ru new file mode 100644 index 0000000..35fd3e5 --- /dev/null +++ b/examples/plugins2/plugin/config.ru @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +$LOAD_PATH.unshift("#{File.dirname(__FILE__)}/lib") +require 'landline' + +app = Landline::Server.new do + get "/hello" do + header "content-type", "text/html" + erubi(<<~CONTENT + + <% require_relative 'plugin_library' %> + + + Hello and welcome to the most amazing plugin ever! + + +

+ <%= do_hello %> +

+ + + CONTENT + ).run + end +end + +run app diff --git a/examples/plugins2/plugin/lib b/examples/plugins2/plugin/lib new file mode 120000 index 0000000..a5bc743 --- /dev/null +++ b/examples/plugins2/plugin/lib @@ -0,0 +1 @@ +../../../lib \ No newline at end of file diff --git a/examples/plugins2/plugin/plugin_library.rb b/examples/plugins2/plugin/plugin_library.rb new file mode 100644 index 0000000..a312645 --- /dev/null +++ b/examples/plugins2/plugin/plugin_library.rb @@ -0,0 +1,3 @@ +def do_hello + "Hello world! Generated by libarary at #{__FILE__}, required from #{caller_locations.first.path}" +end diff --git a/landline.gemspec b/landline.gemspec index 1d7a2f9..2af2743 100644 --- a/landline.gemspec +++ b/landline.gemspec @@ -1,18 +1,19 @@ # frozen_string_literal: true Gem::Specification.new do |spec| - spec.name = "landline" - spec.version = "0.9.2" - spec.summary = "Elegant HTTP DSL" - spec.description = <<~DESC + spec.name = "landline" + spec.version = "0.9.3" + spec.summary = "Elegant HTTP DSL" + spec.description = <<~DESC Landline is a no-hard-dependencies HTTP routing DSL that was made entirely for fun. It runs on any HTTP server that supports the Rack 3.0 protocol. It is usable for many menial tasks, and as long as it continues to be fun, it will keep growing. DESC - spec.authors = ["Yessiest"] - spec.license = "AGPL-3.0" - spec.email = "yessiest@text.512mb.org" - spec.homepage = "https://adastra7.net/git/Yessiest/landline" - spec.files = Dir["lib/**/*"] - spec.extra_rdoc_files = Dir["*.md"] + spec.authors = ["Yessiest"] + spec.license = "AGPL-3.0" + spec.email = "yessiest@text.512mb.org" + spec.homepage = "https://adastra7.net/git/Yessiest/landline" + spec.files = Dir["lib/**/*"] + spec.extra_rdoc_files = Dir["*.md"] + spec.required_ruby_version = ">= 3.0.6" end diff --git a/lib/landline/dsl/constructors_probe.rb b/lib/landline/dsl/constructors_probe.rb index a221aec..5d46b2b 100644 --- a/lib/landline/dsl/constructors_probe.rb +++ b/lib/landline/dsl/constructors_probe.rb @@ -8,8 +8,9 @@ module Landline # @see {Landline::Template#new} def erb(input, vars = {}) Landline::Templates::ERB.new(input, - vars, - parent: @origin) + vars, + parent: @origin, + filename: caller_locations[0].path) end # Create a new erb template using Erubi engine @@ -18,10 +19,11 @@ module Landline # @param capture [Boolean] whether to enable output capturing def erubi(input, vars = {}, freeze: true, capture: false) Landline::Templates::Erubi.new(input, - vars, - parent: @origin, - freeze: freeze, - capture: capture) + vars, + parent: @origin, + freeze: freeze, + capture: capture, + filename: caller_locations[0].path) end end end diff --git a/lib/landline/dsl/methods_path.rb b/lib/landline/dsl/methods_path.rb index 908af0e..46ca7d0 100644 --- a/lib/landline/dsl/methods_path.rb +++ b/lib/landline/dsl/methods_path.rb @@ -67,7 +67,9 @@ module Landline object end - @origin.children.append(self.instance_eval(File.read(filename))) + @origin.children.append( + self.instance_eval(File.read(filename), filename) + ) self.singleton_class.undef_method :run end end diff --git a/lib/landline/template.rb b/lib/landline/template.rb index 9cfb5ee..625311c 100644 --- a/lib/landline/template.rb +++ b/lib/landline/template.rb @@ -36,8 +36,13 @@ module Landline # @param input [String, File] template text # @param vars [Hash] local variables for tempalte # @param parent [Landline::Node] parent node - def initialize(input, vars = {}, parent:) - @template = input.is_a?(File) ? input.read : input + # @param filename [String] filename for eval if input is a string + def initialize(input, vars = {}, parent:, filename:) + @template, @filename = if input.is_a?(File) + [input.read, input.path] + else + [input, filename] + end @context = TemplateContext.new(parent, self) @parent = parent input.close if input.is_a? File diff --git a/lib/landline/template/erb.rb b/lib/landline/template/erb.rb index 1756f77..2c11211 100644 --- a/lib/landline/template/erb.rb +++ b/lib/landline/template/erb.rb @@ -8,7 +8,7 @@ module Landline # ERB Template language adapter class ERB < Landline::Template # @see {Landline::Template#new} - def initialize(input, vars = nil, parent:) + def initialize(input, vars = nil, parent:, filename:) super varname = "_part_#{SecureRandom.hex(10)}".to_sym while @binding.local_variable_defined? varname @@ -20,6 +20,7 @@ module Landline # Run the template. def run + @template.filename = @filename @template.result @binding end end diff --git a/lib/landline/template/erubi.rb b/lib/landline/template/erubi.rb index 00e5bc0..4e0596d 100644 --- a/lib/landline/template/erubi.rb +++ b/lib/landline/template/erubi.rb @@ -10,26 +10,24 @@ module Landline # @see {Landline::Template#new} def initialize(input, vars = nil, - parent:, - freeze: true, - capture: false) - super(input, vars, parent: parent) + **ext) + super(input, vars, parent: ext[:parent], filename: ext[:filename]) varname = "_part_#{SecureRandom.hex(10)}" while @binding.local_variable_defined? varname.to_sym varname = "_part_#{SecureRandom.hex(10)}" end properties = { - filename: input.is_a?(File) ? input.path : "(Inline)", bufvar: varname, - freeze: freeze + freeze: ext.fetch(:freeze, true) } + capture = ext.fetch(:capture, false) engine = capture ? ::Erubi::CaptureEndEngine : ::Erubi::Engine @template = engine.new(@template, properties) end # Run the template. def run - @binding.eval(@template.src) + @binding.eval(@template.src, @filename) end end end