fixed basepath resolution for templates, fixed library resolution within plugged in apps, added plugin example with directories and requiring from an inline template

This commit is contained in:
Yessiest 2023-09-26 01:16:23 +04:00
parent b0ef62fafe
commit 55e87603e5
11 changed files with 78 additions and 27 deletions

View File

@ -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

1
examples/plugins2/lib Symbolic link
View File

@ -0,0 +1 @@
../../lib

View File

@ -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
<!DOCTYPE HTML>
<% require_relative 'plugin_library' %>
<html>
<head>
<title> Hello and welcome to the most amazing plugin ever! </title>
</head>
<body>
<p>
<%= do_hello %>
</p>
</body>
</html>
CONTENT
).run
end
end
run app

View File

@ -0,0 +1 @@
../../../lib

View File

@ -0,0 +1,3 @@
def do_hello
"Hello world! Generated by libarary at #{__FILE__}, required from #{caller_locations.first.path}"
end

View File

@ -2,7 +2,7 @@
Gem::Specification.new do |spec|
spec.name = "landline"
spec.version = "0.9.2"
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.
@ -15,4 +15,5 @@ Gem::Specification.new do |spec|
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

View File

@ -9,7 +9,8 @@ module Landline
def erb(input, vars = {})
Landline::Templates::ERB.new(input,
vars,
parent: @origin)
parent: @origin,
filename: caller_locations[0].path)
end
# Create a new erb template using Erubi engine
@ -21,7 +22,8 @@ module Landline
vars,
parent: @origin,
freeze: freeze,
capture: capture)
capture: capture,
filename: caller_locations[0].path)
end
end
end

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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