Skip to content

Commit

Permalink
Add Parser class and implement custom @hidden directive
Browse files Browse the repository at this point in the history
  • Loading branch information
allmarkedup committed Jul 30, 2021
1 parent ca761de commit fe6a02c
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 8 deletions.
10 changes: 10 additions & 0 deletions lib/lookbook/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class Engine < Rails::Engine
Lookbook.autoload :Navigation, "lookbook/navigation"
Lookbook.autoload :Preview, "lookbook/preview"
Lookbook.autoload :PreviewExample, "lookbook/preview_example"
Lookbook.autoload :Parser, "lookbook/parser"

config.lookbook = ActiveSupport::OrderedOptions.new
config.lookbook.listen_paths ||= []
Expand Down Expand Up @@ -61,6 +62,7 @@ class Engine < Rails::Engine
if app.config.lookbook.auto_refresh
@listener = Listen.to(*app.config.lookbook.listen_paths, only: /\.(rb|html.*)$/) do |modified, added, removed|
if (modified.any? || removed.any?) && added.none?
Lookbook::Engine.parser.parse
Lookbook::Engine.websocket.broadcast("reload", {modified: modified, removed: removed})
end
end
Expand All @@ -76,6 +78,14 @@ class << self
def websocket
@websocket ||= ActionCable::Server::Base.new(config: Lookbook.cable)
end

def parser
return @parser if @parser
@parser = Lookbook::Parser.new(config.lookbook.listen_paths)
@parser.define_tags
@parser.parse
@parser
end
end
end
end
3 changes: 2 additions & 1 deletion lib/lookbook/navigation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ def initialize(previews, active_path = nil)
end

def previews
@previews.sort_by(&:normalized_name).filter { |preview| preview.unsorted_examples.any? }
valid = @previews.filter { |preview| preview.unsorted_examples.any? && !preview.hidden? }
valid.sort_by(&:normalized_name)
end

def flat
Expand Down
38 changes: 38 additions & 0 deletions lib/lookbook/parser.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
module Lookbook
class Parser

YARDOC_FILE_PATH = Rails.root.join("tmp/storage/.yardoc").to_s

def initialize(paths)
@paths = paths.map { |p| "#{p}/**/*.rb" }
registry.yardoc_file = YARDOC_FILE_PATH
end

def parse
registry.clear
YARD.parse(@paths)
registry.save(false, YARDOC_FILE_PATH)
end

def find(path)
registry.load!(YARDOC_FILE_PATH)
registry.at(path)
end

def tags_for(path, tag_name = nil)
code_object = find(path)
code_object.tags(tag_name)
end

def define_tags
YARD::Tags::Library.define_tag("Hidden status", :hidden)
end

private

def registry
YARD::Registry
end

end
end
8 changes: 7 additions & 1 deletion lib/lookbook/preview.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
module Lookbook
module Preview

def hidden?
hidden_tag = Lookbook::Engine.parser.tags_for(name).first
hidden_tag.present? && hidden_tag.text.strip != "false"
end

def example(example_name)
Lookbook::PreviewExample.new(example_name, self)
Lookbook::PreviewExample.new(example_name, name)
end

def label
Expand Down Expand Up @@ -32,5 +37,6 @@ def normalized_name
def unsorted_examples
public_instance_methods(false).map(&:to_s)
end

end
end
14 changes: 8 additions & 6 deletions lib/lookbook/preview_example.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,17 @@ class PreviewExample

attr_reader :name

def initialize(name, preview)
def initialize(name, preview_name)
@name = name
YARD::Registry.clear
YARD.parse(preview.full_path.to_s)
@code_object = YARD::Registry.at("#{preview.name}##{name}")
@preview_name = preview_name
end

def notes
@code_object.docstring.to_s.strip
code_object.docstring.to_s.strip
end

def method_source
@code_object.source.split("\n")[1..-2].join("\n").strip_heredoc
code_object.source.split("\n")[1..-2].join("\n").strip_heredoc
end

def template_source(template_path)
Expand All @@ -29,6 +27,10 @@ def template_lang(template_path)

private

def code_object
@code_object ||= Lookbook::Engine.parser.find("#{@preview_name}##{@name}")
end

def full_template_path(template_path)
base_path = Array(ViewComponent::Base.preview_paths).detect do |p|
Dir["#{p}/#{template_path}.html.*"].first
Expand Down

0 comments on commit fe6a02c

Please sign in to comment.