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

@ -1,18 +1,19 @@
# frozen_string_literal: true # frozen_string_literal: true
Gem::Specification.new do |spec| Gem::Specification.new do |spec|
spec.name = "landline" spec.name = "landline"
spec.version = "0.9.2" spec.version = "0.9.3"
spec.summary = "Elegant HTTP DSL" spec.summary = "Elegant HTTP DSL"
spec.description = <<~DESC spec.description = <<~DESC
Landline is a no-hard-dependencies HTTP routing DSL that was made entirely for fun. 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 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. It is usable for many menial tasks, and as long as it continues to be fun, it will keep growing.
DESC DESC
spec.authors = ["Yessiest"] spec.authors = ["Yessiest"]
spec.license = "AGPL-3.0" spec.license = "AGPL-3.0"
spec.email = "yessiest@text.512mb.org" spec.email = "yessiest@text.512mb.org"
spec.homepage = "https://adastra7.net/git/Yessiest/landline" spec.homepage = "https://adastra7.net/git/Yessiest/landline"
spec.files = Dir["lib/**/*"] spec.files = Dir["lib/**/*"]
spec.extra_rdoc_files = Dir["*.md"] spec.extra_rdoc_files = Dir["*.md"]
spec.required_ruby_version = ">= 3.0.6"
end end

View File

@ -8,8 +8,9 @@ module Landline
# @see {Landline::Template#new} # @see {Landline::Template#new}
def erb(input, vars = {}) def erb(input, vars = {})
Landline::Templates::ERB.new(input, Landline::Templates::ERB.new(input,
vars, vars,
parent: @origin) parent: @origin,
filename: caller_locations[0].path)
end end
# Create a new erb template using Erubi engine # Create a new erb template using Erubi engine
@ -18,10 +19,11 @@ module Landline
# @param capture [Boolean] whether to enable output capturing # @param capture [Boolean] whether to enable output capturing
def erubi(input, vars = {}, freeze: true, capture: false) def erubi(input, vars = {}, freeze: true, capture: false)
Landline::Templates::Erubi.new(input, Landline::Templates::Erubi.new(input,
vars, vars,
parent: @origin, parent: @origin,
freeze: freeze, freeze: freeze,
capture: capture) capture: capture,
filename: caller_locations[0].path)
end end
end end
end end

View File

@ -67,7 +67,9 @@ module Landline
object object
end 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 self.singleton_class.undef_method :run
end end
end end

View File

@ -36,8 +36,13 @@ module Landline
# @param input [String, File] template text # @param input [String, File] template text
# @param vars [Hash] local variables for tempalte # @param vars [Hash] local variables for tempalte
# @param parent [Landline::Node] parent node # @param parent [Landline::Node] parent node
def initialize(input, vars = {}, parent:) # @param filename [String] filename for eval if input is a string
@template = input.is_a?(File) ? input.read : input 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) @context = TemplateContext.new(parent, self)
@parent = parent @parent = parent
input.close if input.is_a? File input.close if input.is_a? File

View File

@ -8,7 +8,7 @@ module Landline
# ERB Template language adapter # ERB Template language adapter
class ERB < Landline::Template class ERB < Landline::Template
# @see {Landline::Template#new} # @see {Landline::Template#new}
def initialize(input, vars = nil, parent:) def initialize(input, vars = nil, parent:, filename:)
super super
varname = "_part_#{SecureRandom.hex(10)}".to_sym varname = "_part_#{SecureRandom.hex(10)}".to_sym
while @binding.local_variable_defined? varname while @binding.local_variable_defined? varname
@ -20,6 +20,7 @@ module Landline
# Run the template. # Run the template.
def run def run
@template.filename = @filename
@template.result @binding @template.result @binding
end end
end end

View File

@ -10,26 +10,24 @@ module Landline
# @see {Landline::Template#new} # @see {Landline::Template#new}
def initialize(input, def initialize(input,
vars = nil, vars = nil,
parent:, **ext)
freeze: true, super(input, vars, parent: ext[:parent], filename: ext[:filename])
capture: false)
super(input, vars, parent: parent)
varname = "_part_#{SecureRandom.hex(10)}" varname = "_part_#{SecureRandom.hex(10)}"
while @binding.local_variable_defined? varname.to_sym while @binding.local_variable_defined? varname.to_sym
varname = "_part_#{SecureRandom.hex(10)}" varname = "_part_#{SecureRandom.hex(10)}"
end end
properties = { properties = {
filename: input.is_a?(File) ? input.path : "(Inline)",
bufvar: varname, bufvar: varname,
freeze: freeze freeze: ext.fetch(:freeze, true)
} }
capture = ext.fetch(:capture, false)
engine = capture ? ::Erubi::CaptureEndEngine : ::Erubi::Engine engine = capture ? ::Erubi::CaptureEndEngine : ::Erubi::Engine
@template = engine.new(@template, properties) @template = engine.new(@template, properties)
end end
# Run the template. # Run the template.
def run def run
@binding.eval(@template.src) @binding.eval(@template.src, @filename)
end end
end end
end end