Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SDoc::Renderer#inline #314

Merged
merged 2 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ jobs:
- '3.1'
- '3.2'
- 'ruby-head'
- 'truffleruby-head'

steps:
- uses: actions/checkout@v1
Expand Down
6 changes: 3 additions & 3 deletions lib/rdoc/generator/template/rails/class.rhtml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<meta charset="<%= @options.charset %>">
<title><%= page_title @context.full_name %></title>
<meta name="viewport" content="width=device-width,initial-scale=1">
<%= render "_head.rhtml", { :tree_keys => @context.full_name.split('::') } %>
<% inline "_head.rhtml", { :tree_keys => @context.full_name.split('::') } %>

<meta property="og:title" value="<%= og_title @context.full_name %>">
</head>
Expand All @@ -13,14 +13,14 @@
<a class="sr-only sr-only-focusable" href="#context" data-turbo="false">Skip to Content</a>
<a class="sr-only sr-only-focusable" href="#search" data-turbo="false">Skip to Search</a>

<%= render "_panel.rhtml" %>
<% inline "_panel.rhtml" %>

<main id="content">
<hgroup class="content__title">
<h1><span class="kind"><%= @context.type %></span> <%= module_breadcrumbs @context %></h1>
</hgroup>

<%= render "_context.rhtml" %>
<% inline "_context.rhtml" %>

<div class="content__divider">Definition files</div>
<%= more_less_ul @context.in_files.map { |file| link_to file }, 5..9 %>
Expand Down
6 changes: 3 additions & 3 deletions lib/rdoc/generator/template/rails/file.rhtml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<meta charset="<%= @options.charset %>">
<title><%= page_title @context.name %></title>
<meta name="viewport" content="width=device-width,initial-scale=1">
<%= render "_head.rhtml", { :tree_keys => [] } %>
<% inline "_head.rhtml", { :tree_keys => [] } %>

<meta property="og:title" value="<%= og_title @context.name %>">
</head>
Expand All @@ -13,7 +13,7 @@
<a class="sr-only sr-only-focusable" href="#context" data-turbo="false">Skip to Content</a>
<a class="sr-only sr-only-focusable" href="#search" data-turbo="false">Skip to Search</a>

<%= render "_panel.rhtml" %>
<% inline "_panel.rhtml" %>

<main id="content">
<div class="content__title">
Expand All @@ -26,7 +26,7 @@
<p class="content__source-link"><%= link_to_external "View on GitHub", source_url %></p>
<% end %>

<%= render "_context.rhtml" %>
<% inline "_context.rhtml" %>
</main>
</body>
</html>
6 changes: 3 additions & 3 deletions lib/rdoc/generator/template/rails/index.rhtml
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@
<meta charset="<%= @options.charset %>">
<title><%= page_title %></title>
<meta name="viewport" content="width=device-width,initial-scale=1">
<%= render "_head.rhtml", {tree_keys: []} %>
<% inline "_head.rhtml", {tree_keys: []} %>
</head>

<body>
<a class="sr-only sr-only-focusable" href="#context" data-turbo="false">Skip to Content</a>
<a class="sr-only sr-only-focusable" href="#search" data-turbo="false">Skip to Search</a>

<%= render "_panel.rhtml" %>
<% inline "_panel.rhtml" %>

<main id="content">
<%= render "_context.rhtml" %>
<% inline "_context.rhtml" %>
</main>
</body>
</html>
15 changes: 13 additions & 2 deletions lib/sdoc/renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,29 @@ class SDoc::Renderer
def self.compile_erb(path)
@compiled_erb ||= {}
@compiled_erb[path] ||= begin
erb = ERB.new(File.read(path), trim_mode: "<>")
erb = ERB.new(File.read(path), trim_mode: "<>", eoutvar: "self.buffer")
erb.filename = path
erb
end
end

attr_reader :buffer
def buffer=(*); end # Ignore buffer reset from ERB itself.

def initialize(context, rdoc_options)
@context = context
@options = rdoc_options
@buffer = nil
end

def render(...)
original_buffer, @buffer = @buffer, +""
inline(...)
ensure
@buffer = original_buffer
end

def render(template_path, local_assigns = {})
def inline(template_path, local_assigns = {})
template_path = File.expand_path(template_path, @options.template_dir)
_binding = binding
local_assigns.each { |name, value| _binding.local_variable_set(name, value) }
Expand Down
51 changes: 51 additions & 0 deletions spec/renderer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,56 @@ def create_template(name, erb)
_(SDoc::Renderer.new(nil, @rdoc_options).render("foo.erb")).
must_equal "foo &amp; bar"
end

it "is reentrant" do
create_template "foo.erb", %(1 + 1 = <%= render "two.erb" %>)
create_template "two.erb", %(<%= 1 + 1 %>)

_(SDoc::Renderer.new(nil, @rdoc_options).render("foo.erb")).
must_equal "1 + 1 = 2"
end
end

describe "#inline" do
it "supports isolated local variables" do
create_template "outer.erb", %(<%= foo %> <% inline "inner.erb", bar: "BAR" %> <%= foo %>)
create_template "inner.erb", %(<%= foo = bar %>)

_(SDoc::Renderer.new(nil, @rdoc_options).render("outer.erb", { foo: "FOO" })).
must_equal "FOO BAR FOO"
end

it "provides access to the same non-local values as #render" do
create_template "outer.erb", %(<% inline "inner.erb" %>)
create_template "inner.erb", %(<%= h @context[:foobar] %>)

_(SDoc::Renderer.new({ foobar: "foo & bar" }, @rdoc_options).render("outer.erb")).
must_equal "foo &amp; bar"
end

it "supports yield" do
create_template "outer.erb", '1 <% inline("inner.erb") { 3 } %> 5'
create_template "inner.erb", %(2 <%= yield %> 4)

_(SDoc::Renderer.new(nil, @rdoc_options).render("outer.erb")).
must_equal "1 2 3 4 5"
end

it "supports interleaved rendering" do
create_template "outer.erb", '1 <% inline("inner.erb") do %>3<% end %> 5'
create_template "inner.erb", %(2 <% yield %> 4)

_(SDoc::Renderer.new(nil, @rdoc_options).render("outer.erb")).
must_equal "1 2 3 4 5"
end

it "supports interleaved rendering with nested #inline calls" do
create_template "outer.erb", '1 <% inline("middle.erb") { inline("inner.erb") } %> 5'
create_template "middle.erb", %(2 <% yield %> 4)
create_template "inner.erb", %(3)

_(SDoc::Renderer.new(nil, @rdoc_options).render("outer.erb")).
must_equal "1 2 3 4 5"
end
end
end