lookbook 1.2.1 → 1.4.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (182) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE.txt +1 -1
  3. data/README.md +47 -14
  4. data/app/assets/lookbook/css/themes/blue.css +4 -2
  5. data/app/assets/lookbook/css/themes/green.css +66 -0
  6. data/app/assets/lookbook/css/themes/indigo.css +4 -2
  7. data/app/assets/lookbook/css/themes/rose.css +66 -0
  8. data/app/assets/lookbook/css/themes/zinc.css +4 -2
  9. data/app/components/lookbook/base_component.rb +2 -2
  10. data/app/components/lookbook/code/component.css +2 -2
  11. data/app/components/lookbook/code/component.html.erb +3 -2
  12. data/app/components/lookbook/code/component.rb +13 -2
  13. data/app/components/lookbook/code/highlight_github.css +406 -0
  14. data/app/components/lookbook/header/component.html.erb +1 -1
  15. data/app/components/lookbook/inspector_panel/component.rb +4 -6
  16. data/app/components/lookbook/nav/component.rb +8 -15
  17. data/app/components/lookbook/nav/directory/component.html.erb +28 -0
  18. data/app/components/lookbook/nav/directory/component.rb +4 -0
  19. data/app/components/lookbook/nav/{item → entity}/component.html.erb +8 -8
  20. data/app/components/lookbook/nav/entity/component.rb +49 -0
  21. data/app/components/lookbook/nav/item/component.css +15 -0
  22. data/app/components/lookbook/nav/item/component.js +4 -0
  23. data/app/components/lookbook/nav/item/component.rb +13 -56
  24. data/app/components/lookbook/params/editor/component.html.erb +2 -2
  25. data/app/components/lookbook/params/editor/component.rb +3 -10
  26. data/app/components/lookbook/params/field/component.css +3 -3
  27. data/app/components/lookbook/params/field/component.html.erb +8 -8
  28. data/app/components/lookbook/params/field/component.rb +21 -72
  29. data/app/components/lookbook/split_layout/component.html.erb +1 -1
  30. data/app/components/lookbook/tabs/component.html.erb +1 -1
  31. data/app/components/lookbook/tabs/component.js +4 -0
  32. data/app/components/lookbook/tag_component.rb +1 -1
  33. data/app/components/lookbook/viewport/component.css +1 -1
  34. data/app/components/lookbook/viewport/component.html.erb +1 -1
  35. data/app/components/lookbook/viewport/component.rb +1 -1
  36. data/app/controllers/concerns/lookbook/targetable_concern.rb +131 -0
  37. data/app/controllers/concerns/lookbook/with_preview_controller_concern.rb +13 -0
  38. data/app/controllers/lookbook/application_controller.rb +21 -9
  39. data/app/controllers/lookbook/inspector_controller.rb +45 -0
  40. data/app/controllers/lookbook/page_controller.rb +13 -9
  41. data/app/controllers/lookbook/pages_controller.rb +9 -15
  42. data/app/controllers/lookbook/previews_controller.rb +4 -210
  43. data/app/helpers/lookbook/application_helper.rb +2 -2
  44. data/app/helpers/lookbook/output_helper.rb +5 -5
  45. data/app/helpers/lookbook/page_helper.rb +7 -4
  46. data/app/views/layouts/lookbook/application.html.erb +40 -38
  47. data/app/views/layouts/lookbook/page.html.erb +2 -2
  48. data/app/views/layouts/lookbook/shell.html.erb +3 -2
  49. data/app/views/layouts/lookbook/skeleton.html.erb +7 -7
  50. data/app/views/lookbook/index.html.erb +13 -2
  51. data/app/views/lookbook/{previews → inspector}/inputs/_color.html.erb +0 -0
  52. data/app/views/lookbook/{previews → inspector}/inputs/_range.html.erb +0 -0
  53. data/app/views/lookbook/{previews → inspector}/inputs/_select.html.erb +0 -0
  54. data/app/views/lookbook/{previews → inspector}/inputs/_text.html.erb +0 -0
  55. data/app/views/lookbook/{previews → inspector}/inputs/_textarea.html.erb +0 -0
  56. data/app/views/lookbook/{previews → inspector}/inputs/_toggle.html.erb +5 -5
  57. data/app/views/lookbook/{previews → inspector}/panels/_content.html.erb +0 -0
  58. data/app/views/lookbook/{previews → inspector}/panels/_notes.html.erb +2 -2
  59. data/app/views/lookbook/{previews → inspector}/panels/_output.html.erb +0 -0
  60. data/app/views/lookbook/inspector/panels/_params.html.erb +15 -0
  61. data/app/views/lookbook/{previews → inspector}/panels/_preview.html.erb +0 -0
  62. data/app/views/lookbook/{previews → inspector}/panels/_source.html.erb +0 -0
  63. data/app/views/lookbook/{previews → inspector}/show.html.erb +5 -2
  64. data/config/app.yml +11 -1
  65. data/config/inputs.yml +12 -12
  66. data/config/languages.yml +41 -0
  67. data/config/panels.yml +6 -6
  68. data/config/routes.rb +5 -5
  69. data/config/tags.yml +8 -1
  70. data/lib/lookbook/engine.rb +103 -130
  71. data/lib/lookbook/entities/collections/component_collection.rb +4 -0
  72. data/lib/lookbook/entities/collections/concerns/hierarchical_collection.rb +23 -0
  73. data/lib/lookbook/entities/collections/entity_collection.rb +61 -0
  74. data/lib/lookbook/entities/collections/page_collection.rb +30 -0
  75. data/lib/lookbook/entities/collections/preview_collection.rb +41 -0
  76. data/lib/lookbook/entities/collections/preview_example_collection.rb +4 -0
  77. data/lib/lookbook/entities/component.rb +31 -0
  78. data/lib/lookbook/entities/concerns/annotatable.rb +58 -0
  79. data/lib/lookbook/entities/concerns/inspectable.rb +44 -0
  80. data/lib/lookbook/entities/concerns/locatable.rb +73 -0
  81. data/lib/lookbook/entities/concerns/navigable.rb +43 -0
  82. data/lib/lookbook/entities/entity.rb +53 -0
  83. data/lib/lookbook/entities/page.rb +80 -0
  84. data/lib/lookbook/entities/page_section.rb +43 -0
  85. data/lib/lookbook/entities/preview.rb +87 -0
  86. data/lib/lookbook/entities/preview_example.rb +100 -0
  87. data/lib/lookbook/entities/preview_group.rb +48 -0
  88. data/lib/lookbook/file_watcher.rb +47 -0
  89. data/lib/lookbook/lang.rb +12 -35
  90. data/lib/lookbook/param.rb +99 -0
  91. data/lib/lookbook/{preview_controller.rb → preview_actions.rb} +14 -3
  92. data/lib/lookbook/preview_parser.rb +53 -0
  93. data/lib/lookbook/process.rb +21 -0
  94. data/lib/lookbook/rendered_example.rb +37 -0
  95. data/lib/lookbook/services/code/code_beautifier.rb +21 -0
  96. data/lib/lookbook/services/code/code_highlighter.rb +69 -0
  97. data/lib/lookbook/services/code/code_indenter.rb +14 -0
  98. data/lib/lookbook/services/data/parsers/data_parser.rb +22 -0
  99. data/lib/lookbook/services/data/parsers/json_parser.rb +7 -0
  100. data/lib/lookbook/services/data/parsers/yaml_parser.rb +7 -0
  101. data/lib/lookbook/services/data/resolvers/data_resolver.rb +70 -0
  102. data/lib/lookbook/services/data/resolvers/eval_resolver.rb +10 -0
  103. data/lib/lookbook/services/data/resolvers/file_resolver.rb +28 -0
  104. data/lib/lookbook/services/data/resolvers/method_resolver.rb +10 -0
  105. data/lib/lookbook/services/data/resolvers/yaml_resolver.rb +18 -0
  106. data/lib/lookbook/services/entities/entity_tree_builder.rb +45 -0
  107. data/lib/lookbook/services/markdown_renderer.rb +29 -0
  108. data/lib/lookbook/services/position_prefix_parser.rb +16 -0
  109. data/lib/lookbook/services/string_value_caster.rb +60 -0
  110. data/lib/lookbook/services/tags/tag_options_parser.rb +62 -0
  111. data/lib/lookbook/services/templates/action_view_annotations_handler.rb +21 -0
  112. data/lib/lookbook/services/templates/action_view_annotations_stripper.rb +15 -0
  113. data/lib/lookbook/services/templates/frontmatter_extractor.rb +28 -0
  114. data/lib/lookbook/services/templates/styles_extractor.rb +38 -0
  115. data/lib/lookbook/services/{search_param_builder.rb → urls/search_param_builder.rb} +1 -1
  116. data/lib/lookbook/services/{search_param_parser.rb → urls/search_param_parser.rb} +1 -1
  117. data/lib/lookbook/stores/config_store.rb +12 -9
  118. data/lib/lookbook/stores/input_store.rb +7 -3
  119. data/lib/lookbook/stores/panel_store.rb +2 -2
  120. data/lib/lookbook/stores/tag_store.rb +3 -5
  121. data/lib/lookbook/support/null_object.rb +10 -0
  122. data/lib/lookbook/support/service.rb +2 -2
  123. data/lib/lookbook/support/store.rb +2 -35
  124. data/lib/lookbook/support/tree_node.rb +87 -0
  125. data/lib/lookbook/support/utils/path_utils.rb +32 -5
  126. data/lib/lookbook/support/utils/utils.rb +24 -0
  127. data/lib/lookbook/tags/component_tag.rb +13 -0
  128. data/lib/lookbook/tags/custom_tag.rb +61 -0
  129. data/lib/lookbook/tags/display_tag.rb +15 -0
  130. data/lib/lookbook/tags/hidden_tag.rb +13 -0
  131. data/lib/lookbook/tags/id_tag.rb +7 -0
  132. data/lib/lookbook/tags/label_tag.rb +4 -0
  133. data/lib/lookbook/tags/logical_path_tag.rb +7 -0
  134. data/lib/lookbook/tags/param_tag.rb +63 -0
  135. data/lib/lookbook/tags/position_tag.rb +16 -0
  136. data/lib/lookbook/tags/source_tag.rb +7 -0
  137. data/lib/lookbook/tags/tag_provider.rb +18 -0
  138. data/lib/lookbook/tags/yard_tag.rb +90 -0
  139. data/lib/lookbook/theme.rb +8 -0
  140. data/lib/lookbook/version.rb +1 -1
  141. data/lib/lookbook/websocket.rb +60 -0
  142. data/lib/lookbook.rb +13 -8
  143. data/public/lookbook-assets/css/lookbook.css +487 -411
  144. data/public/lookbook-assets/css/lookbook.css.map +1 -1
  145. data/public/lookbook-assets/css/themes/blue.css +3 -1
  146. data/public/lookbook-assets/css/themes/blue.css.map +1 -1
  147. data/public/lookbook-assets/css/themes/green.css +68 -0
  148. data/public/lookbook-assets/css/themes/green.css.map +1 -0
  149. data/public/lookbook-assets/css/themes/indigo.css +3 -1
  150. data/public/lookbook-assets/css/themes/indigo.css.map +1 -1
  151. data/public/lookbook-assets/css/themes/rose.css +68 -0
  152. data/public/lookbook-assets/css/themes/rose.css.map +1 -0
  153. data/public/lookbook-assets/css/themes/zinc.css +3 -1
  154. data/public/lookbook-assets/css/themes/zinc.css.map +1 -1
  155. data/public/lookbook-assets/js/embed.js +10 -1
  156. data/public/lookbook-assets/js/embed.js.map +1 -1
  157. data/public/lookbook-assets/js/lookbook.js +358 -629
  158. data/public/lookbook-assets/js/lookbook.js.map +1 -1
  159. metadata +96 -44
  160. data/app/components/lookbook/code/highlight_github_light.css +0 -217
  161. data/app/views/lookbook/previews/panels/_params.html.erb +0 -15
  162. data/lib/lookbook/code_formatter.rb +0 -68
  163. data/lib/lookbook/collection.rb +0 -161
  164. data/lib/lookbook/component.rb +0 -34
  165. data/lib/lookbook/entity.rb +0 -47
  166. data/lib/lookbook/markdown.rb +0 -22
  167. data/lib/lookbook/page.rb +0 -195
  168. data/lib/lookbook/page_collection.rb +0 -19
  169. data/lib/lookbook/page_section.rb +0 -29
  170. data/lib/lookbook/params.rb +0 -157
  171. data/lib/lookbook/parser.rb +0 -42
  172. data/lib/lookbook/preview.rb +0 -174
  173. data/lib/lookbook/preview_collection.rb +0 -23
  174. data/lib/lookbook/preview_example.rb +0 -93
  175. data/lib/lookbook/preview_group.rb +0 -62
  176. data/lib/lookbook/source_inspector.rb +0 -95
  177. data/lib/lookbook/support/utils/attribute_utils.rb +0 -9
  178. data/lib/lookbook/tag.rb +0 -122
  179. data/lib/lookbook/tag_options.rb +0 -111
  180. data/lib/lookbook/tags.rb +0 -17
  181. data/lib/lookbook/template_parser.rb +0 -72
  182. data/lib/lookbook/utils.rb +0 -105
@@ -1,34 +0,0 @@
1
- module Lookbook
2
- class Component < Entity
3
- attr_accessor :name
4
-
5
- def initialize(name)
6
- @name = name
7
- super(path)
8
- end
9
-
10
- def path
11
- name.underscore
12
- end
13
-
14
- def rel_path
15
- Pathname.new("#{path}.rb")
16
- end
17
-
18
- def full_path
19
- Pathname.new("#{Lookbook.config.components_path}/#{path}.rb")
20
- end
21
-
22
- def dir_path
23
- full_path.dirname
24
- end
25
-
26
- def template_path
27
- Dir.glob("#{Lookbook.config.components_path}/#{path}.*.erb").first
28
- end
29
-
30
- def inline?
31
- template_path.present?
32
- end
33
- end
34
- end
@@ -1,47 +0,0 @@
1
- module Lookbook
2
- class Entity
3
- include Utils
4
-
5
- def initialize(reference_path)
6
- @refpath = reference_path
7
- end
8
-
9
- def id
10
- generate_id(lookup_path)
11
- end
12
-
13
- def label
14
- lookup_path.split("/").last.titleize
15
- end
16
-
17
- def type
18
- :item
19
- end
20
-
21
- def path
22
- @refpath
23
- end
24
-
25
- def lookup_path
26
- @lookup_path ||= to_lookup_path(@refpath)
27
- end
28
-
29
- def hierarchy_depth
30
- @refpath.present? ? @refpath.split("/").size : 0
31
- end
32
-
33
- def position
34
- 0
35
- end
36
-
37
- def hidden?
38
- false
39
- end
40
-
41
- def matchers
42
- nil
43
- end
44
-
45
- alias_method :url_path, :lookup_path
46
- end
47
- end
@@ -1,22 +0,0 @@
1
- require "redcarpet"
2
-
3
- module Lookbook
4
- class Markdown
5
- def self.render(text)
6
- Utils.strip_action_view_annotations!(text)
7
- markdown = Redcarpet::Markdown.new(Renderer, Lookbook.config.markdown_options.to_h)
8
- markdown.render(text).html_safe
9
- end
10
-
11
- class Renderer < Redcarpet::Render::HTML
12
- def block_code(code, language = "ruby")
13
- line_numbers = language.to_s.end_with? "-numbered"
14
- ApplicationController.render(Lookbook::Code::Component.new(**{
15
- source: code,
16
- language: language.to_s.chomp("-numbered"),
17
- line_numbers: line_numbers
18
- }), layout: nil)
19
- end
20
- end
21
- end
22
- end
data/lib/lookbook/page.rb DELETED
@@ -1,195 +0,0 @@
1
- module Lookbook
2
- class Page < Entity
3
- FRONTMATTER_FIELDS = [
4
- :id,
5
- :label,
6
- :title,
7
- :hidden,
8
- :landing,
9
- :position,
10
- :markdown,
11
- :header,
12
- :footer,
13
- :data
14
- ]
15
-
16
- attr_reader :errors, :rel_path
17
- attr_accessor :sections
18
-
19
- def initialize(path, base_path)
20
- @pathname = Pathname.new path
21
- @base_path = Pathname.new base_path
22
- @options = nil
23
- @errors = []
24
- @sections = []
25
- @page_name = remove_position_prefix(path_name)
26
- @rel_path = @pathname.relative_path_from(@base_path)
27
- page_path = @rel_path.dirname.to_s == "." ? @page_name : "#{@rel_path.dirname}/#{@page_name}"
28
- super(page_path)
29
- end
30
-
31
- def url_path
32
- lookbook_page_path lookup_path
33
- end
34
-
35
- def full_path
36
- Pathname.new Rails.root.join(@pathname.to_s)
37
- end
38
-
39
- def name
40
- @page_name
41
- end
42
-
43
- def hidden?
44
- options[:hidden] == true
45
- end
46
-
47
- def markdown?
48
- options[:markdown] == true
49
- end
50
-
51
- def header?
52
- options[:header] == true
53
- end
54
-
55
- def footer?
56
- options[:footer] == true
57
- end
58
-
59
- def get(key)
60
- options[key]
61
- end
62
-
63
- def content
64
- @content ||= strip_frontmatter(file_contents).strip
65
- end
66
-
67
- def matchers
68
- normalize_matchers(label)
69
- end
70
-
71
- def parent_collections_names
72
- File.dirname(path).split("/")
73
- end
74
-
75
- def type
76
- :page
77
- end
78
-
79
- def id
80
- options[:id]
81
- end
82
-
83
- def position
84
- options[:position]
85
- end
86
-
87
- def hidden
88
- options[:hidden]
89
- end
90
-
91
- def label
92
- options[:label]
93
- end
94
-
95
- def method_missing(method_name, *args, &block)
96
- if args.none? && !block
97
- options[method_name]
98
- else
99
- super
100
- end
101
- end
102
-
103
- def respond_to_missing?(method_name, include_private = false)
104
- FRONTMATTER_FIELDS.include? method_name
105
- end
106
-
107
- protected
108
-
109
- def file_contents
110
- File.read(full_path)
111
- end
112
-
113
- def options
114
- return @options if @options
115
- begin
116
- frontmatter = (get_frontmatter(file_contents) || {}).deep_symbolize_keys
117
- rescue => exception
118
- frontmatter = {}
119
- line_number_match = exception.message.match(/.*line\s(\d+)/)
120
- @errors.push(Lookbook::Error.new(exception, **{
121
- title: "YAML frontmatter parsing error",
122
- file_path: @pathname.to_s,
123
- line_number: line_number_match ? line_number_match[1] : false
124
- }))
125
- end
126
- @options = Lookbook.config.page_options.deep_merge(frontmatter).with_indifferent_access
127
- @options[:id] = generate_id(@options[:id] || lookup_path)
128
- @options[:label] ||= name.titleize
129
- @options[:title] ||= @options[:label]
130
- @options[:hidden] ||= false
131
- @options[:landing] ||= false
132
- @options[:position] = @options[:position] ? @options[:position].to_i : get_position_prefix(path_name)
133
- @options[:markdown] ||= markdown_file?
134
- @options[:header] = true unless @options.key? :header
135
- @options[:footer] = true unless @options.key? :footer
136
- @options
137
- end
138
-
139
- def path_name
140
- @pathname.basename(@pathname.extname).to_s.gsub(/\.(html|md)$/, "")
141
- end
142
-
143
- def markdown_file?
144
- @pathname.basename(@pathname.extname).to_s.end_with?(".md")
145
- end
146
-
147
- class << self
148
- def find(path)
149
- all.find { |p| p.lookup_path == path }
150
- end
151
-
152
- def exists?(path)
153
- !!find(path)
154
- end
155
-
156
- def any?
157
- all.any?
158
- end
159
-
160
- def all
161
- pages, sections =
162
- Array(page_paths).flat_map do |dir|
163
- Dir["#{dir}/**/*.html.*", "#{dir}/**/*.md.*"].sort.map do |path|
164
- create(path, dir)
165
- end
166
- end.partition { |page| page.type == :page }
167
-
168
- sorted_pages = pages
169
- .uniq { |page| page.path }
170
- .sort_by { |page| [page.position, page.label] }
171
-
172
- page_dict = sorted_pages.index_by(&:path)
173
- sorted_sections = sections.sort_by { |section| [section.position, section.label] }
174
-
175
- sorted_sections.each do |section|
176
- page_dict[section.path].sections << section
177
- end
178
-
179
- PageCollection.new(sorted_pages)
180
- end
181
-
182
- def page_paths
183
- PathUtils.normalize_all(Lookbook.config.page_paths)
184
- end
185
-
186
- def section_path?(path)
187
- !!path.match(%r{\[(.*?\w+)\]})
188
- end
189
-
190
- def create(path, base_path)
191
- (section_path?(path) ? PageSection : Page).new(path, base_path)
192
- end
193
- end
194
- end
195
- end
@@ -1,19 +0,0 @@
1
- module Lookbook
2
- class PageCollection < Collection
3
- def id
4
- generate_id("page-collection", lookup_path || "root")
5
- end
6
-
7
- def label
8
- "Pages"
9
- end
10
-
11
- def type
12
- :page_collection
13
- end
14
-
15
- def self.describe_as
16
- "pages"
17
- end
18
- end
19
- end
@@ -1,29 +0,0 @@
1
- module Lookbook
2
- class PageSection < Page
3
- def name
4
- return @name if @name.present?
5
- matches = full_path.to_s.match(%r{\[(?<name>\w+)\]})
6
- @name ||= matches[:name]
7
- end
8
-
9
- def id
10
- "#{super}-#{name}"
11
- end
12
-
13
- def url_path
14
- nil
15
- end
16
-
17
- def path
18
- super.gsub("[#{name}]", "")
19
- end
20
-
21
- def type
22
- :page_section
23
- end
24
-
25
- def lookup_path
26
- "#{super}/#{name}"
27
- end
28
- end
29
- end
@@ -1,157 +0,0 @@
1
- require "active_model"
2
-
3
- module Lookbook
4
- module Params
5
- VALUE_TYPE_MATCH_REGEXP = /^(\[\s?([A-Z]{1}\w+)\s?\])/
6
- DESCRIPTION_MATCH_REGEXP = /"(.*[^\\])"$/
7
-
8
- PARAM_OPTION_KEYS = %i[name input label hint description value_type value_default].freeze
9
-
10
- class << self
11
- def build_param(param, default: nil, eval_scope: nil)
12
- input, value_type, options_str, description = parse_param_tag_text(param.text)
13
-
14
- tag_options = Lookbook::TagOptions.new(options_str,
15
- base_dir: (File.dirname(param.object.files.first[0]) if param.object.files.any?),
16
- eval_scope: eval_scope).options
17
-
18
- if tag_options.is_a? Array
19
- # handle special case legacy situation for selects where
20
- # options are an array of choices rather than a Hash
21
- tag_options = {choices: tag_options}
22
- end
23
-
24
- param_options = tag_options.select { |key| PARAM_OPTION_KEYS.include? key }
25
- input_options = tag_options.except(*PARAM_OPTION_KEYS)
26
-
27
- value_type ||= param_options[:value_type]
28
- input ||= param_options[:input] || guess_input(value_type, default)
29
- name = param.name.to_s
30
-
31
- {
32
- name: name,
33
- label: param_options[:label] || name.titleize,
34
- hint: param_options[:hint],
35
- description: description || param_options[:description],
36
- input: input.to_s.tr("_", "-"),
37
- input_options: input_options,
38
- value: nil,
39
- value_type: value_type || guess_value_type(input, default),
40
- value_default: default
41
- }
42
- end
43
-
44
- # Parses param tag strings with the format: `[<value_type>] <input> <description?> <opts?>`
45
- def parse_param_tag_text(text)
46
- text = (text.presence || "").strip
47
-
48
- value_type = nil
49
- text.match(VALUE_TYPE_MATCH_REGEXP) do |m|
50
- value_type = m[2]
51
- text.gsub!(VALUE_TYPE_MATCH_REGEXP, "").strip!
52
- end
53
-
54
- text, options_str = Lookbook::TagOptions.extract_options(text)
55
-
56
- description = nil
57
- text.match(DESCRIPTION_MATCH_REGEXP) do |m|
58
- description = m[1]
59
- text.gsub!(DESCRIPTION_MATCH_REGEXP, "").strip!
60
- end
61
-
62
- input, rest = text.split(" ", 2)
63
-
64
- [input, value_type, options_str, description, rest]
65
- end
66
-
67
- def parse_method_param_str(param_str)
68
- return nil if param_str[0].nil? || param_str[1].nil?
69
- name = param_str[0].chomp(":")
70
- value = param_str[1]&.strip
71
- value = case value
72
- when "nil"
73
- nil
74
- else
75
- if value&.first == ":"
76
- value.delete_prefix(":").to_sym
77
- else
78
- YAML.safe_load(value)
79
- end
80
- end
81
- [name, value]
82
- end
83
-
84
- def cast(value, type = "String")
85
- case type.downcase
86
- when "symbol"
87
- value.presence&.delete_prefix(":")&.to_sym
88
- when "hash"
89
- result = safe_parse_yaml(value, {})
90
- unless result.is_a? Hash
91
- Lookbook.logger.debug "Failed to parse '#{value}' into a Hash"
92
- result = {}
93
- end
94
- result
95
- when "array"
96
- result = safe_parse_yaml(value, [])
97
- unless result.is_a? Array
98
- Lookbook.logger.debug "Failed to parse '#{value}' into an Array"
99
- result = []
100
- end
101
- result
102
- when "datetime"
103
- begin
104
- result = DateTime.parse(value)
105
- rescue Date::Error
106
- Lookbook.logger.debug "Failed to parse '#{value}' into a DateTime"
107
- result = DateTime.now
108
- end
109
- result
110
- else
111
- begin
112
- type_class = "ActiveModel::Type::#{type}".constantize
113
- type_class.new.cast(value)
114
- rescue NameError
115
- raise ArgumentError, "'#{type}' is not a valid param type to cast to."
116
- end
117
- end
118
- end
119
-
120
- private
121
-
122
- def guess_input(value_type, default)
123
- if value_type&.downcase == "boolean" || (value_type.blank? && boolean?(default))
124
- "toggle"
125
- else
126
- "text"
127
- end
128
- end
129
-
130
- def guess_value_type(input, default)
131
- if input&.downcase == "toggle"
132
- "Boolean"
133
- elsif input&.downcase == "number"
134
- "Integer"
135
- elsif boolean?(default)
136
- "Boolean"
137
- elsif default.is_a? Symbol
138
- "Symbol"
139
- elsif ["date", "datetime-local"].include?(input&.downcase) || default.is_a?(DateTime)
140
- "DateTime"
141
- else
142
- "String"
143
- end
144
- end
145
-
146
- def safe_parse_yaml(value, fallback)
147
- value.present? ? YAML.safe_load(value) : fallback
148
- rescue Psych::SyntaxError
149
- fallback
150
- end
151
-
152
- def boolean?(value)
153
- value == true || value == false
154
- end
155
- end
156
- end
157
- end
@@ -1,42 +0,0 @@
1
- require "yard"
2
-
3
- module Lookbook
4
- class Parser
5
- attr_reader :registry_path
6
- def initialize(paths)
7
- @paths = paths.map { |p| "#{p}/**/*preview.rb" }
8
- @after_parse_callbacks = []
9
- @after_parse_once_callbacks = []
10
- @parsing = false
11
-
12
- YARD::Parser::SourceParser.after_parse_list do
13
- [*@after_parse_callbacks, *@after_parse_once_callbacks].each do |callback|
14
- callback.call(YARD::Registry)
15
- end
16
- @after_parse_once_callbacks = []
17
- @parsing = false
18
- end
19
- end
20
-
21
- def parse(&block)
22
- unless @parsing
23
- @parsing = true
24
- @after_parse_once_callbacks << block if block
25
- YARD::Registry.clear
26
- YARD.parse(@paths)
27
- end
28
- end
29
-
30
- def after_parse(&block)
31
- @after_parse_callbacks << block
32
- end
33
-
34
- class << self
35
- def define_tags(tags = nil)
36
- tags.to_h.each do |name, tag|
37
- YARD::Tags::Library.define_tag(tag[:label], name, tag[:yard_parser])
38
- end
39
- end
40
- end
41
- end
42
- end