Module: Magik::Docs
- Defined in:
- lib/magik/docs.rb
Overview
The documentation Magik ships inside its own gem, and the resolver that reads it.
Magik invents a DSL — model, screen, action, ledger, channel,
flow — that no language model has in its training data. An agent writing
Magik code without the reference in front of it reconstructs a Rails-shaped
API from memory, every time. Fetching the reference over the network is
slow, rate-limited and version-blind: it returns whatever is on main,
not what the installed gem actually does.
So the gem carries its own manual. Magik::Docs locates it, catalogues it
and searches it, using nothing but the standard library — the speed comes
from there being a few dozen small markdown files, not from an index.
This is not a spec-only subsystem. Every method below does what it says.
Defined Under Namespace
Classes: AmbiguousPageError, MissingTermError, PageNotFoundError, UnavailableError
Constant Summary collapse
- PACKAGED_GLOBS =
The globs, relative to the gem root, that define which files are documentation.
This constant is the single source of truth for the decision: magik.gemspec reads it to build
spec.files, and pages reads it to build the catalogue. Keeping one list means an INSTALLED gem's catalogue can never offer a page thatgem buildleft out. A checkout catalogue is wider by design:../../docs/**/*.mdreaches the repository-root trees, which the gemspec excludes from the package, so a checkout'smagik docs listincludes repository-only pages an installed gem neither ships nor advertises.Two entries carry their own story.
docs/**/*.mdis the GENERATED mirror tree — every checkout-rootdocs/page rendered into the gem at the same path by scripts/checks/shipped_mirrors.rb, which fails the gate when any mirror is stale.docs/idea/00-build-spec.md— the path Magik::Error::DEFAULT_FIX and every spec-only stub's error message name — is one of them, and so is every design page the shipped manual links, so an installed gem serves the page its own links and fix lines point at instead of whatevermaincarries. In a checkout the repository originals are ALSO catalogued under their ../../ spelling, and a basename shorthand that matches both refuses to guess.../../docs/**/*.mdreaches those originals, which live four directories above this file in a checkout. The gemspec drops every candidate that resolves outside the gem dir, so the checkout-only spelling never ships. test/magik/docs_test.rb asserts the split. %w[ llms.txt wiki/**/*.md docs/**/*.md ../../docs/**/*.md ].freeze
- ROOT_ENV =
Environment variable that overrides root resolution.
Set it when the docs live somewhere the candidate search cannot guess — a vendored copy, or a test fixture.
"MAGIK_DOCS_ROOT"- AUDIENCE_BY_PREFIX =
Audience, derived from the tree a page lives in. The trees already have distinct jobs (see
docs/README.md), so the audience is a property of the directory and never a hand-maintained per-file list.The repo-root trees are catalogued from a checkout under their
../../docs/spelling, so each docs prefix carries both spellings. { "wiki/" => "app", "docs/ops/" => "app", "../../docs/ops/" => "app", "docs/architecture/" => "framework", "../../docs/architecture/" => "framework", "docs/idea/" => "both", "../../docs/idea/" => "both" }.freeze
- DEFAULT_AUDIENCE =
Audience for a page that matches no prefix in AUDIENCE_BY_PREFIX — the index files,
llms.txtanddocs/README.md, which route both readers. "both"- AUDIENCE_LABELS =
Human labels for each audience key, used by
magik docs list. { "app" => "Building an app with Magik", "both" => "Both audiences — the design and the reference", "framework" => "Contributing to Magik itself" }.freeze
- AUDIENCE_ORDER =
The order audiences are printed and grouped in.
%w[app both framework].freeze
- SUMMARY_LIMIT =
Longest summary pages will derive from a page's opening paragraph, in characters, before it is truncated on a word boundary.
180- EXTENSION =
File extensions slug_for strips when turning a shipped path into a slug.
/\.(?:md|markdown|txt)\z/i- HORIZONTAL_RULE =
A markdown horizontal rule, which is never a page summary.
/\A(?:-{3,}|\*{3,}|_{3,})\z/
Class Method Summary collapse
-
.audience_for(path) ⇒ String
The audience a path serves, from AUDIENCE_BY_PREFIX.
-
.candidates ⇒ Array<Pathname>
Candidate roots, in resolution order, whether or not they contain docs.
-
.find(slug_or_path) ⇒ Hash{Symbol => String}
Look a page up by slug, by shipped path, or by an unambiguous shorthand.
-
.packaged?(dir) ⇒ Boolean
Does this directory hold at least one file matched by PACKAGED_GLOBS?.
-
.pages ⇒ Array<Hash{Symbol => String}>
The catalogue: one entry per shipped page, derived from the files themselves.
-
.read(slug_or_path) ⇒ String
The raw markdown of a page.
-
.relative_paths(dir) ⇒ Array<String>
Every packaged documentation file under
dir, as sorted paths relative to it. -
.reset! ⇒ void
Forget the memoised Docs.root and Docs.pages.
-
.resolve_root(dirs) ⇒ Pathname
Pick the first candidate that contains at least one packaged page.
-
.root ⇒ Pathname
The root of the shipped documentation tree — the directory that has
wiki/andllms.txtinside it and, in a repository checkout, the repo-rootdocs/trees one../../step above it. -
.search(term) ⇒ Array<Hash{Symbol => Object}>
Substring search across every shipped page, case-insensitively.
-
.slug_for(path) ⇒ String
The stable slug for a shipped path: the path, lowercased, with its documentation extension removed.
Class Method Details
.audience_for(path) ⇒ String
The audience a path serves, from AUDIENCE_BY_PREFIX.
323 324 325 |
# File 'lib/magik/docs.rb', line 323 def audience_for(path) AUDIENCE_BY_PREFIX.find { |prefix, _| path.start_with?(prefix) }&.last || DEFAULT_AUDIENCE end |
.candidates ⇒ Array<Pathname>
Candidate roots, in resolution order, whether or not they contain docs.
181 182 183 184 185 186 |
# File 'lib/magik/docs.rb', line 181 def candidates [ENV.fetch(ROOT_ENV, nil), Magik.root, gem_path] .compact .map { |dir| Pathname.new(dir.to_s). } .uniq end |
.find(slug_or_path) ⇒ Hash{Symbol => String}
Look a page up by slug, by shipped path, or by an unambiguous shorthand.
Accepted for wiki/Models.md: "wiki/models" (the slug),
"wiki/Models.md" (the path) and "models" (the basename). A numeric
prefix is optional, so the build spec also answers to "build-spec".
255 256 257 258 259 260 261 262 263 264 265 |
# File 'lib/magik/docs.rb', line 255 def find(slug_or_path) query = normalize(slug_or_path) exact = pages.find { |page| page[:slug] == query } return exact if exact matched = pages.select { |page| shorthands(page).include?(query) } return matched.first if matched.size == 1 raise AmbiguousPageError, ambiguous_cause(slug_or_path, matched) if matched.size > 1 raise PageNotFoundError, missing_cause(slug_or_path) end |
.packaged?(dir) ⇒ Boolean
Does this directory hold at least one file matched by PACKAGED_GLOBS?
212 213 214 |
# File 'lib/magik/docs.rb', line 212 def packaged?(dir) !relative_paths(dir).empty? end |
.pages ⇒ Array<Hash{Symbol => String}>
The catalogue: one entry per shipped page, derived from the files
themselves. Nothing here is hand-maintained — add a markdown file under
docs/ or wiki/ and it appears.
239 240 241 |
# File 'lib/magik/docs.rb', line 239 def pages @pages ||= relative_paths(root).map { |path| build_page(path) }.sort_by { |page| page[:slug] }.freeze end |
.read(slug_or_path) ⇒ String
The raw markdown of a page. Raw on purpose: the reader is an agent, not a terminal, and rendering only removes information.
276 277 278 |
# File 'lib/magik/docs.rb', line 276 def read(slug_or_path) page_body(find(slug_or_path)) end |
.relative_paths(dir) ⇒ Array<String>
Every packaged documentation file under dir, as sorted paths relative
to it.
223 224 225 226 227 228 |
# File 'lib/magik/docs.rb', line 223 def relative_paths(dir) base = Pathname.new(dir.to_s) return [] unless base.directory? Dir.glob(PACKAGED_GLOBS, base: base.to_s).select { |path| base.join(path).file? }.sort end |
.reset! ⇒ void
312 313 314 315 |
# File 'lib/magik/docs.rb', line 312 def reset! @root = nil @pages = nil end |
.resolve_root(dirs) ⇒ Pathname
Pick the first candidate that contains at least one packaged page.
Exposed so both layouts — a checkout and an installed gem — can be exercised directly by a test without changing the process environment.
198 199 200 201 202 203 204 |
# File 'lib/magik/docs.rb', line 198 def resolve_root(dirs) found = Array(dirs).map { |dir| Pathname.new(dir.to_s) }.find { |dir| packaged?(dir) } return found if found raise UnavailableError, "no shipped documentation found under #{Array(dirs).map(&:to_s).inspect}" end |
.root ⇒ Pathname
The root of the shipped documentation tree — the directory that has
wiki/ and llms.txt inside it and, in a repository checkout, the
repo-root docs/ trees one ../../ step above it.
Resolution order: the ROOT_ENV override, then Magik.root (which is
the gem dir — apps/magik — when Magik is loaded from source and the
gem root when it is installed), then the directory RubyGems recorded
for the loaded magik gem. The first candidate that actually contains
a packaged page wins, so a layout with no docs is skipped rather than
returned empty.
172 173 174 |
# File 'lib/magik/docs.rb', line 172 def root @root ||= resolve_root(candidates) end |
.search(term) ⇒ Array<Hash{Symbol => Object}>
Substring search across every shipped page, case-insensitively.
There is no index and no dependency: a few dozen small files are read in full, which is faster than any network round trip an agent would otherwise make. Pages whose headings match sort first, then pages with more hits, then alphabetically — so the answer is deterministic.
294 295 296 297 298 299 300 301 |
# File 'lib/magik/docs.rb', line 294 def search(term) needle = term.to_s.strip raise MissingTermError, "`magik docs search` was given no term to search for" if needle.empty? needle = needle.downcase pages.filter_map { |page| matched_page(page, needle) } .sort_by { |result| sort_key(result) } end |
.slug_for(path) ⇒ String
The stable slug for a shipped path: the path, lowercased, with its documentation extension removed. Unique by construction, and typeable.
334 335 336 |
# File 'lib/magik/docs.rb', line 334 def slug_for(path) path.sub(EXTENSION, "").downcase end |