Skip to content

Commit

Permalink
TMP
Browse files Browse the repository at this point in the history
  • Loading branch information
Morriar committed Aug 12, 2022
1 parent 70a9004 commit fba4c37
Show file tree
Hide file tree
Showing 11 changed files with 134 additions and 54 deletions.
87 changes: 53 additions & 34 deletions lib/spoom/docs/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ def erb
File.read(@template)
end

sig { returns(String) }
def html
ERB.new(erb).result(get_binding)
sig { params(binding: Binding).returns(String) }
def html(binding: get_binding)
ERB.new(erb).result(binding)
end

sig { returns(Binding) }
Expand Down Expand Up @@ -54,7 +54,7 @@ def link_to_symbol(symbol, anchor: false)
case symbol
when Spoom::Model::Scope
link_for_scope(symbol.fully_qualified_name)
when Spoom::Model::Prop
when Spoom::Model::Const, Spoom::Model::Prop
# TODO: handle top level
parent_scope = T.must(symbol.parent_scope)
"#{link_to_symbol(parent_scope)}##{symbol.name}"
Expand All @@ -63,6 +63,13 @@ def link_to_symbol(symbol, anchor: false)
end
end

# sig { params(name: String, bindings: T.nilable(Object)).returns(String) }
# def template(name, bindings: nil)
# template = Template.new("#{Spoom::SPOOM_PATH}/templates/docs/#{name}.erb")
# template.html(binding: bindings&.send(:binding) || template.get_binding)
# # template.html(binding: { foo: "bar" }.send(:binding))
# end

sig do
params(
symbol: Spoom::Model::Symbol,
Expand All @@ -78,6 +85,16 @@ def symbol_link(symbol, anchor: false, qualified_name: true)
def symbol_card(symbol)
Templates::SymbolCard.new(symbol)
end

sig { params(symbol: Spoom::Model::Symbol).returns(Templates::SymbolSig) }
def symbol_sig(symbol)
Templates::SymbolSig.new(symbol)
end

sig { params(symbol: Spoom::Model::Symbol).returns(Templates::SymbolNamespace) }
def symbol_namespace(symbol)
Templates::SymbolNamespace.new(symbol)
end
end

class Page < Template
Expand Down Expand Up @@ -189,35 +206,7 @@ def initialize(fully_qualified_name, symbols)

sig { override.returns(String) }
def title
@fully_qualified_name
end

sig { returns(T::Array[Spoom::Model::Scope]) }
def scope_constants
@scope_constants ||= T.let(
T.cast(
@symbols
.map { |symbol| symbol.symbols.select { |child| child.is_a?(Spoom::Model::Scope) } }
.flatten
.uniq
.sort_by(&:fully_qualified_name),
T::Array[Spoom::Model::Scope]
), T.nilable(T::Array[Spoom::Model::Scope])
)
end

sig { returns(T::Array[Spoom::Model::Prop]) }
def scope_properties
@scope_properties ||= T.let(
T.cast(
@symbols
.map { |symbol| symbol.symbols.select { |child| child.is_a?(Spoom::Model::Prop) } }
.flatten
.uniq
.sort_by(&:fully_qualified_name),
T::Array[Spoom::Model::Prop]
), T.nilable(T::Array[Spoom::Model::Prop])
)
@fully_qualified_name.delete_prefix("::")
end
end
end
Expand All @@ -227,7 +216,7 @@ class SymbolLink < Template

sig { params(symbol: Spoom::Model::Symbol, anchor: T::Boolean, qualified_name: T::Boolean).void }
def initialize(symbol, anchor: false, qualified_name: true)
super("#{Spoom::SPOOM_PATH}/templates/docs/partials/symbol_link.erb")
super("#{Spoom::SPOOM_PATH}/templates/docs/symbol_link.erb")
@symbol = symbol
@title = T.let(symbol.comment_string&.lstrip&.lines&.first&.strip, T.nilable(String))

Expand All @@ -248,6 +237,36 @@ def initialize(symbol)
@symbol = symbol
end
end

class SymbolNamespace < Template
extend T::Sig

sig { params(symbol: Spoom::Model::Symbol).void }
def initialize(symbol)
super("#{Spoom::SPOOM_PATH}/templates/docs/symbol_namespace.erb")
@symbol = symbol
end
end

class SymbolSig < Template
extend T::Sig

sig { params(symbol: Spoom::Model::Symbol).void }
def initialize(symbol)
super("#{Spoom::SPOOM_PATH}/templates/docs/symbol_sig.erb")
@symbol = symbol
end
end

class SymbolNamespace < Template
extend T::Sig

sig { params(symbol: Spoom::Model::Symbol).void }
def initialize(symbol)
super("#{Spoom::SPOOM_PATH}/templates/docs/symbol_namespace.erb")
@symbol = symbol
end
end
end
end
end
38 changes: 34 additions & 4 deletions lib/spoom/model/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,12 @@ def self.from_document_symbol(document_symbol, parent_scope:)
case kind
when "module", "class"
Scope.new(name, parent_scope: parent_scope)
when "def"
when "const"
Const.new(name, parent_scope: parent_scope)
when "constructor", "def", "field"
Prop.new(name, parent_scope: parent_scope)
else
raise "Unknown symbol kind: #{kind}"
end
end

Expand Down Expand Up @@ -96,9 +100,6 @@ def to_s
class Scope < Symbol
extend T::Sig

sig { returns(T::Array[Symbol]) }
attr_reader :symbols

sig { params(name: String, parent_scope: T.nilable(Scope)).void }
def initialize(name, parent_scope: nil)
super(name, parent_scope: parent_scope)
Expand All @@ -111,6 +112,35 @@ def fully_qualified_name

"#{parent_scope&.fully_qualified_name}::#{name}"
end

sig { returns(T::Array[Scope]) }
def namespace
symbols = T.let([], T::Array[Scope])
symbols << self
parent = T.let(parent_scope, T.nilable(Scope))
while parent
symbols << parent
parent = parent.parent_scope
end
symbols.reverse
end

sig { params(kind: T.nilable(T.class_of(Symbol))).returns(T::Array[Symbol]) }
def symbols(kind = nil)
return @symbols unless kind

@symbols.select { |s| s.is_a?(kind) }
end
end

class Const < Symbol
sig { override.returns(String) }
def fully_qualified_name
scope = parent_scope
return name unless scope

"#{parent_scope&.fully_qualified_name}::#{name}"
end
end

class Prop < Symbol
Expand Down
4 changes: 4 additions & 0 deletions templates/docs/page.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
background-color: #f2f2f2;
}

a {
text-decoration: none;
}

.signature {
color: #777;
font-family: monospace;
Expand Down
31 changes: 21 additions & 10 deletions templates/docs/pages/scope.erb
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
<% constants = @symbols.map { |symbol| symbol.symbols(Spoom::Model::Const) }.flatten.sort_by(&:fully_qualified_name) %>
<% scopes = @symbols.map { |symbol| symbol.symbols(Spoom::Model::Scope) }.flatten.sort_by(&:fully_qualified_name) %>
<% properties = @symbols.map { |symbol| symbol.symbols(Spoom::Model::Prop) }.flatten.sort_by(&:fully_qualified_name) %>

<div class="container-fluid">
<div class="row">
<div class="col-sm-3">
<% if scope_constants.any? %>
<p><strong>Constants</strong></p>
<% if constants.any? || scopes.any? %>
<p class="text-muted"><strong>Constants</strong></p>
<ul class="list-unstyled">
<% scope_constants.each do |symbol| %>
<% (constants + scopes).each do |symbol| %>
<li><%= symbol_link(symbol, qualified_name: false).html %></li>
<% end %>
</ul>
<% end %>
<% if scope_properties.any? %>
<p><strong>Properties</strong></p>
<% if properties.any? %>
<p class="text-muted"><strong>Properties</strong></p>
<ul class="list-unstyled">
<% scope_properties.each do |symbol| %>
<% properties.each do |symbol| %>
<li><%= symbol_link(symbol, qualified_name: false, anchor: true).html %></li>
<% end %>
</ul>
Expand All @@ -22,8 +26,8 @@
<div class="card">
<div class="card-body">
<h1 class="card-title"><%= @fully_qualified_name.split("::").last %></h1>

<% @symbols.each do |symbol| %>
<%= symbol_namespace(symbol).html %>
<% if symbol.comment_string %>
<p><%= symbol.comment_string %></p>
<% end %>
Expand All @@ -35,9 +39,16 @@
<% end %>
</div>
</div>
<% if scope_properties.any? %>
<h3 class="mt-3">Properties</h3>
<% scope_properties.each do |symbol| %>

<% if constants.any? || scopes.any? %>
<h4 class="mt-3 text-muted">Constants</h4>
<% (constants + scopes).each do |symbol| %>
<% end %>
<% end %>

<% if properties.any? %>
<h4 class="mt-3 text-muted">Properties</h4>
<% properties.each do |symbol| %>
<%= symbol_card(symbol).html %>
<% end %>
<% end %>
Expand Down
2 changes: 1 addition & 1 deletion templates/docs/partials/footer.erb
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<small>Generated by Spoom</small>
<p class="mt-5 text-center text-muted"><small>Generated by Spoom</small></p>
2 changes: 1 addition & 1 deletion templates/docs/partials/header.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<nav class="navbar fixed-top navbar-expand-lg navbar-dark bg-primary">
<div class="container-fluid">
<a class="navbar-brand" href="#">Spoom::Docs</a>
<a class="navbar-brand" href="./index.html">Spoom::Docs</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
Expand Down
3 changes: 0 additions & 3 deletions templates/docs/partials/symbol_link.erb

This file was deleted.

2 changes: 1 addition & 1 deletion templates/docs/symbol_card.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div class="card">
<div class="card-body">
<h5 class="card-title"><%= @symbol.name %></h5>
<h5 class="card-title text-primary"><%= symbol_sig(@symbol).html %></h5>
<p class="card-text"></p>
<% if @symbol.comment_string %>
<p><%= @symbol.comment_string %></p>
Expand Down
1 change: 1 addition & 0 deletions templates/docs/symbol_link.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<a href="<%= @link %>" title="<%= @title %>"><%= @text %></a>
17 changes: 17 additions & 0 deletions templates/docs/symbol_namespace.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<% namespace = @symbol.namespace %>

<nav style="--bs-breadcrumb-divider: '::';" aria-label="breadcrumb">
<ol class="breadcrumb">
<% namespace.each_with_index do |symbol, index| %>
<% if index < namespace.size - 1 %>
<li class="breadcrumb-item">
<%= symbol_link(symbol, qualified_name: false).html %>
</li>
<% else %>
<li class="breadcrumb-item active" aria-current="page">
<%= symbol.name %>
</li>
<% end %>
<% end %>
</ol>
</nav>
1 change: 1 addition & 0 deletions templates/docs/symbol_sig.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<span class="text-primary"><%= @symbol.name %></span>

0 comments on commit fba4c37

Please sign in to comment.