Sha256: 0238c5d6dd3d3ddcad708667703fec9e146115346840c6a249058f5f1bfc7c0d

Contents?: true

Size: 1.21 KB

Versions: 28

Compression:

Stored size: 1.21 KB

Contents

require "yard"

module Lookbook
  class PreviewParser
    def initialize(paths, tags = nil)
      @paths = paths
      @after_parse_callbacks = []
      @after_parse_once_callbacks = []
      @parsing = false

      define_tags(tags)
      YARD::Parser::SourceParser.after_parse_list { run_callbacks }
    end

    def parse(files = nil, &block)
      unless @parsing
        @parsing = true
        @after_parse_once_callbacks << block if block
        files_list = files ? files.select { |file| file.to_s.end_with?(".rb") } : paths

        YARD::Registry.clear
        YARD.parse(files_list)
      end
    end

    def after_parse(&block)
      @after_parse_callbacks << block
    end

    def paths
      PathUtils.normalize_paths(@paths).map { |path| "#{path}/**/*preview.rb" }
    end

    protected

    def callbacks
      [
        *@after_parse_callbacks,
        *@after_parse_once_callbacks
      ]
    end

    def run_callbacks
      callbacks.each { |cb| cb.call(YARD::Registry) }
      @after_parse_once_callbacks = []
      @parsing = false
    end

    def define_tags(tags = nil)
      tags.to_h.each do |name, tag|
        YARD::Tags::Library.define_tag(tag[:label], name, Lookbook::TagProvider)
      end
    end
  end
end

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
lookbook-2.3.4 lib/lookbook/preview_parser.rb
lookbook-2.3.3 lib/lookbook/preview_parser.rb
lookbook-2.3.2 lib/lookbook/preview_parser.rb
lookbook-2.3.0 lib/lookbook/preview_parser.rb
lookbook-2.2.2 lib/lookbook/preview_parser.rb
lookbook-2.2.1 lib/lookbook/preview_parser.rb
lookbook-2.2.0 lib/lookbook/preview_parser.rb
lookbook-2.1.1 lib/lookbook/preview_parser.rb
lookbook-2.1.0 lib/lookbook/preview_parser.rb
lookbook-2.0.5 lib/lookbook/preview_parser.rb
lookbook-2.0.4 lib/lookbook/preview_parser.rb
lookbook-2.0.3 lib/lookbook/preview_parser.rb
lookbook-2.0.2 lib/lookbook/preview_parser.rb
lookbook-2.0.1 lib/lookbook/preview_parser.rb
lookbook-2.0.0 lib/lookbook/preview_parser.rb
lookbook-2.0.0.rc.3 lib/lookbook/preview_parser.rb
lookbook-2.0.0.rc.2 lib/lookbook/preview_parser.rb
lookbook-2.0.0.rc.1 lib/lookbook/preview_parser.rb
lookbook-2.0.0.beta.9 lib/lookbook/preview_parser.rb
lookbook-2.0.0.beta.8 lib/lookbook/preview_parser.rb