Skip to content

Commit 7de4221

Browse files
committed
Add support for @example pragmas.
1 parent 9d96885 commit 7de4221

File tree

6 files changed

+60
-1
lines changed

6 files changed

+60
-1
lines changed

lib/utopia/project/base.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ def initialize(root = Dir.pwd)
5959
# Return the absolute path for the given file name, if it exists in the project.
6060
# @parameter file_name [String] The relative path to the project file, e.g. `readme.md`.
6161
# @returns [String] The file-system path.
62+
#
63+
# @example Get README path
64+
# base = Utopia::Project::Base.new
65+
# base.path_for("readme.md") # => "/path/to/project/readme.md" or nil
6266
def path_for(file_name)
6367
full_path = File.expand_path(file_name, @root)
6468
if File.exist?(full_path)
@@ -86,6 +90,10 @@ def best(definitions)
8690

8791
# Given a lexical path, find the best definition for that path.
8892
# @returns [Tuple(Decode::Trie::Node, Decode::Definition)]
93+
#
94+
# @example Lookup a definition
95+
# base = Utopia::Project::Base.local
96+
# _, definition = base.lookup(%i[Utopia Project Base])
8997
def lookup(path)
9098
if node = @index.trie.lookup(path.map(&:to_sym))
9199
return node, best(node.values)
@@ -119,6 +127,10 @@ def linkify(text, definition, language: definition&.language)
119127
# Format the given text in the context of the given definition and language.
120128
# See {document} for details.
121129
# @returns [XRB::MarkupString]
130+
#
131+
# @example Format text with code links
132+
# base = Utopia::Project::Base.new
133+
# base.format("See {Utopia::Project::Base#guides}.") # => XRB::MarkupString
122134
def format(text, definition = nil, language: definition&.language, **options)
123135
if document = self.document(text, definition, language: language)
124136
return XRB::Markup.raw(
@@ -132,6 +144,11 @@ def format(text, definition = nil, language: definition&.language, **options)
132144
# Updates source code references (`{language identifier}`) into links.
133145
#
134146
# @returns [Document]
147+
#
148+
# @example Convert markdown to HTML
149+
# base = Utopia::Project::Base.new
150+
# doc = base.document("# Title")
151+
# doc.to_html # => "<h1>Title</h1>\n"
135152
def document(text, definition = nil, language: definition&.language)
136153
case text
137154
when Enumerable
@@ -145,6 +162,11 @@ def document(text, definition = nil, language: definition&.language)
145162

146163
# Compute a unique string which can be used as `id` attribute in the HTML output.
147164
# @returns [String]
165+
#
166+
# @example Compute id for a definition
167+
# base = Utopia::Project::Base.local
168+
# _, definition = base.lookup(%i[Utopia Project Base])
169+
# base.id_for(definition) # => "Utopia::Project::Base"
148170
def id_for(definition, suffix = nil)
149171
if suffix
150172
"#{definition.qualified_name}-#{suffix}"
@@ -155,6 +177,11 @@ def id_for(definition, suffix = nil)
155177

156178
# Compute a link href to the given definition for use within the HTML output.
157179
# @returns [XRB::Reference]
180+
#
181+
# @example Link to a definition
182+
# base = Utopia::Project::Base.local
183+
# _, definition = base.lookup(%i[Utopia Project Base])
184+
# base.link_for(definition).to_s # => "/source/utopia/project/index#Utopia::Project::Base"
158185
def link_for(definition)
159186
path = definition.lexical_path.map{|entry| entry.to_s}
160187

@@ -170,6 +197,12 @@ def link_for(definition)
170197
# @yields {|guide| ...} If a block is given.
171198
# @parameter guide [Guide]
172199
# @returns [Enumerator(Guide)] If no block given.
200+
#
201+
# @example List guide titles
202+
# base = Utopia::Project::Base.new
203+
# base.guides.each do |guide|
204+
# puts guide.title
205+
# end
173206
def guides
174207
return to_enum(:guides) unless block_given?
175208

pages/source/_examples.xnode

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?r
2+
base = self[:base]
3+
4+
symbol = attributes[:symbol]
5+
documentation = symbol.documentation
6+
7+
examples = documentation&.filter(Decode::Comment::Example).to_a
8+
9+
if examples&.any? ?>
10+
<?r examples.each do |example| ?>
11+
<details closed>
12+
<?r if title = example.title ?>
13+
<summary><h4>Example: #{title}</h4></summary>
14+
<?r end ?>
15+
<pre><code class="language-#{symbol.language.name}">#{example.code}</code></pre>
16+
</details>
17+
<?r end ?>
18+
<?r end ?>

pages/source/_signature.xnode

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
<dl><?r
1111
documentation.traverse do |node, descend|
1212
node.each do |child|
13+
next if child.is_a?(Decode::Comment::Example)
14+
1315
?><dt>
1416
<strong>#{child.directive}</strong><?r
1517
case child

pages/source/show.xnode

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
?>#{base.format(text.join("\n"), symbol)}<?r
1818
end
1919
?>
20+
#{partial 'content:examples', symbol: symbol}
2021
#{partial 'content:signature', symbol: symbol}
2122
<?r
2223
nested = node.children.map do |name, child|
@@ -56,6 +57,7 @@
5657
if documentation = symbol.documentation
5758
?>#{partial 'content:pragmas', symbol: symbol}<?r
5859
?>#{base.format(documentation.text, symbol)}<?r
60+
?>#{partial 'content:examples', symbol: symbol}<?r
5961
?>#{partial 'content:signature', symbol: symbol}<?r
6062
end
6163

releases.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changes
22

3+
## Unreleased
4+
5+
- Support for `@example` pragmas from the `decode` gem, allowing inline code examples to be rendered in API documentation.
6+
37
## v0.37.2
48

59
- Fix mermaid diagram text color in dark mode.

utopia-project.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Gem::Specification.new do |spec|
2525

2626
spec.required_ruby_version = ">= 3.2"
2727

28-
spec.add_dependency "decode", "~> 0.17"
28+
spec.add_dependency "decode", "~> 0.26"
2929
spec.add_dependency "falcon"
3030
spec.add_dependency "markly", "~> 0.7"
3131
spec.add_dependency "rackula", "~> 1.3"

0 commit comments

Comments
 (0)