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

View File

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

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