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:
parent
b0ef62fafe
commit
55e87603e5
|
@ -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
|
|
@ -0,0 +1 @@
|
||||||
|
../../lib
|
|
@ -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
|
|
@ -0,0 +1 @@
|
||||||
|
../../../lib
|
|
@ -0,0 +1,3 @@
|
||||||
|
def do_hello
|
||||||
|
"Hello world! Generated by libarary at #{__FILE__}, required from #{caller_locations.first.path}"
|
||||||
|
end
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
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.
|
||||||
|
@ -15,4 +15,5 @@ Gem::Specification.new do |spec|
|
||||||
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
|
||||||
|
|
|
@ -9,7 +9,8 @@ module Landline
|
||||||
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
|
||||||
|
@ -21,7 +22,8 @@ module Landline
|
||||||
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
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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
|
||||||
|
|
Loading…
Reference in New Issue