docrb 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,62 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Docrb
4
- # Renderer provides a Redcarpet renderer with Rouge extensions
5
- class Renderer < Redcarpet::Render::HTML
6
- def initialize(extensions = {})
7
- super extensions.merge(link_attributes: { target: "_blank" })
8
- end
9
- include Rouge::Plugins::Redcarpet
10
- end
11
-
12
- # InlineRenderer provides a renderer for inline contents. This renderer
13
- # does not emit paragraph tags.
14
- class InlineRenderer < Renderer
15
- def paragraph(text)
16
- text
17
- end
18
- end
19
-
20
- # Markdown provides utilities for generating HTML from markdown contents
21
- class Markdown
22
- # Internal: Creates a new renderer based on a provided type by setting
23
- # sensible defaults.
24
- #
25
- # type - Type of the renderer to be initialised. Use Docrb::Renderer or
26
- # InlineRenderer
27
- def self.make_render(type)
28
- Redcarpet::Markdown.new(
29
- type,
30
- fenced_code_blocks: true,
31
- autolink: true
32
- )
33
- end
34
-
35
- # Renders a given input using the default renderer.
36
- #
37
- # input - Markdown content to be rendered
38
- #
39
- # Returns an HTML string containing the rendered Markdown content
40
- def self.render(input)
41
- make_render(Renderer).render(input)
42
- end
43
-
44
- # Renders a given input using the inline renderer.
45
- #
46
- # input - Markdown content to be rendered
47
- #
48
- # Returns an HTML string containing the rendered Markdown content
49
- def self.inline(input)
50
- make_render(InlineRenderer).render(input)
51
- end
52
-
53
- # Renders a given Ruby source code into HTML
54
- #
55
- # source - Source code to be rendered
56
- #
57
- # Returns an HTML string containing the rendered source code
58
- def self.render_source(source)
59
- render("```ruby\n#{source}\n```")
60
- end
61
- end
62
- end
@@ -1,13 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- unless Class.respond_to? :try?
4
- class Object
5
- def try?(method, *args, **kwargs, &)
6
- send(method, *args, **kwargs, &) if respond_to?(method)
7
- end
8
-
9
- def self.try?(method, *args, **kwargs, &)
10
- send(method, *args, **kwargs, &) if respond_to?(method)
11
- end
12
- end
13
- end
@@ -1,178 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Docrb
4
- # Resolvable implements utilities for resolving names and class paths on a
5
- # container context (module/class)
6
- class Resolvable
7
- # Returns a matcher for a provided name
8
- #
9
- # name - Name to match against
10
- def by_name(name)
11
- ->(obj) { obj.name == name }
12
- end
13
-
14
- # Returns a matcher for a provided name and type
15
- #
16
- # name - Name to match against
17
- # type - Object type to match against (:def/:sdef/:class/:module...)
18
- def by_name_and_type(name, type)
19
- ->(obj) { obj.name == name && obj.type == type }
20
- end
21
-
22
- # Attempts to resolve a method with a provided name in the container's
23
- # context. This method attempts to match for instance methods, class
24
- # methods, and finally by recursively calling this method on the inherited
25
- # members, if any.
26
- #
27
- # name - Name of the method to be resolved.
28
- #
29
- # Returns a method structure, or nil, in case none is found.
30
- def resolve_method(name)
31
- if (local = try?(:defs)&.find(&by_name(name)))
32
- return local
33
- end
34
-
35
- if (local = try?(:sdefs)&.find(&by_name(name)))
36
- return local
37
- end
38
-
39
- # Inherited?
40
- if (inherited = try?(:inherits)&.try?(:resolve_method, name, type))
41
- return inherited
42
- end
43
-
44
- nil
45
- end
46
-
47
- # Resolves a container using a provided reference.
48
- #
49
- # ref - Either a reference hash (containing :class_path, :target, :name), or
50
- # a symbol representing the name of the container being resolved.
51
- #
52
- # Returns a matched container or nil, in case none is found.
53
- def resolve_container(ref)
54
- if ref.is_a? Hash
55
- path = ref
56
- .slice(:class_path, :target, :name)
57
- .values
58
- .flatten
59
- .compact
60
- return resolve_container(path)
61
- end
62
-
63
- ref = [ref] if ref.is_a? Symbol
64
-
65
- if ref.length == 1
66
- name = ref.first
67
- # module?
68
- if (mod = try?(:modules)&.find(&by_name(name)))
69
- return mod
70
- end
71
-
72
- # class?
73
- if (cls = try?(:classes)&.find(&by_name(name)))
74
- return cls
75
- end
76
-
77
- # parent?
78
- return @parent&.resolve_container(ref)
79
- end
80
-
81
- obj = self
82
- while ref.length.positive?
83
- obj = resolve_container(ref.shift)
84
- return nil if obj.nil?
85
- end
86
-
87
- obj
88
- end
89
-
90
- # Resolves a provided name in the current containter's context. This method
91
- # will attempt to return an object matching the provided name by looking for
92
- # it in the following subcontainers: modules, classes, methods, attributes,
93
- # and recursively performing the resolution on the container's parent, if
94
- # any.
95
- #
96
- # name - Name of the object being resolved
97
- #
98
- # Returns the first object found by the provided name, or nil, in case none
99
- # is found.
100
- def resolve(name)
101
- return nil if name.nil?
102
-
103
- name = name.to_sym
104
-
105
- # module?
106
- if (mod = try?(:modules)&.find(&by_name(name)))
107
- return mod
108
- end
109
-
110
- # class?
111
- if (cls = try?(:classes)&.find(&by_name(name)))
112
- return cls
113
- end
114
-
115
- # method?
116
- if (met = resolve_method(name))
117
- return met
118
- end
119
-
120
- # attribute?
121
- if (att = try?(:attributes)&.find(&by_name(name)))
122
- return att
123
- end
124
-
125
- if (obj = @parent&.resolve(name))
126
- return obj
127
- end
128
-
129
- nil
130
- end
131
-
132
- # Resolves a provided ref by creating its path string representation and
133
- # calling #resolve_qualified
134
- #
135
- # ref - Hash representing the reference being resolved.
136
- #
137
- # Returns the object under the provided reference, or nil.
138
- def resolve_ref(ref)
139
- path = ref
140
- .slice(:class_path, :target, :name)
141
- .values
142
- .flatten
143
- .compact
144
- return resolve_qualified(path.join("::")) if path.length > 1
145
-
146
- resolve(path[0])
147
- end
148
-
149
- # Resolves a qualified path under the current container's context.
150
- #
151
- # path - Path of the object being resolved. Must be a string containing the
152
- # object name being searched, or a classpath for it (e.g.
153
- # `Foo::Bar::Baz`)
154
- def resolve_qualified(path)
155
- components = path.split("::").map(&:to_sym)
156
- obj = root
157
- until components.empty?
158
- obj = obj.resolve(components.shift)
159
- break if obj.nil?
160
- end
161
- obj
162
- end
163
-
164
- # Returns the root object for the current container
165
- def root
166
- obj = self
167
- obj = obj.parent while obj.parent
168
- obj
169
- end
170
-
171
- # Returns the container's full qualified path
172
- def path
173
- return [] if parent.nil?
174
-
175
- parent.path + [name]
176
- end
177
- end
178
- end