Fenced code-block extraction from raw LLM text responses.
Pure parsing. No HTTP, no provider knowledge. Used by ask-code! to turn
a plain-text completion into a vector of tagged code blocks the caller
reads/evals directly.
Extraction rules — extract-code-blocks recognizes three shapes:
clojure\n…\n → {:lang "clojure" :source …}\n…\n → {:lang nil :source …}select-blocks then enforces strict lang matching: ONLY blocks whose
:lang equals the caller-supplied target survive. Untagged blocks
(:lang nil) — including the fenceless-fallback case — are DROPPED.
Models that want their code accepted MUST tag their fence with the
requested lang.
Fenced code-block extraction from raw LLM text responses.
Pure parsing. No HTTP, no provider knowledge. Used by `ask-code!` to turn
a plain-text completion into a vector of tagged code blocks the caller
reads/evals directly.
Extraction rules — `extract-code-blocks` recognizes three shapes:
1. Tagged fence: ```clojure\n…\n``` → {:lang "clojure" :source …}
2. Untagged fence: ```\n…\n``` → {:lang nil :source …}
3. No fence at all: entire response → {:lang nil :source …}
`select-blocks` then enforces strict lang matching: ONLY blocks whose
`:lang` equals the caller-supplied target survive. Untagged blocks
(`:lang nil`) — including the fenceless-fallback case — are DROPPED.
Models that want their code accepted MUST tag their fence with the
requested lang.(concat-sources blocks)Concatenate non-blank :source values, joined by a blank-line separator.
Concatenate non-blank `:source` values, joined by a blank-line separator.
(extract-code-blocks raw)Parse fenced code blocks from raw text.
Returns a vector of {:lang <str-or-nil> :source <str>}. :lang is the
tag after the opening ``` (lower-cased), or nil for untagged fences.
:source is the body verbatim (newlines preserved; no trim applied to
internal whitespace).
Lenient+ fallback: when raw contains NO fenced block at all, returns
one block {:lang nil :source <trimmed-raw>}. Empty / blank input
returns [].
NOTE: untagged blocks (:lang nil) — including the fenceless-fallback —
are dropped by select-blocks. Pre-select consumers see them; routed
ask-code! callers do not.
Parse fenced code blocks from `raw` text.
Returns a vector of `{:lang <str-or-nil> :source <str>}`. `:lang` is the
tag after the opening ``` (lower-cased), or `nil` for untagged fences.
`:source` is the body verbatim (newlines preserved; no trim applied to
internal whitespace).
Lenient+ fallback: when `raw` contains NO fenced block at all, returns
one block `{:lang nil :source <trimmed-raw>}`. Empty / blank input
returns `[]`.
NOTE: untagged blocks (`:lang nil`) — including the fenceless-fallback —
are dropped by `select-blocks`. Pre-select consumers see them; routed
`ask-code!` callers do not.(select-blocks blocks lang)Filter blocks to those whose :lang STRICTLY equals lang
(case-insensitive).
Untagged blocks (:lang nil) are DROPPED — NOT treated as a wildcard.
This is a deliberate strictness contract: models must tag their fence
with the requested lang to have their code accepted. The fenceless-
fallback path in extract-code-blocks also produces :lang nil and is
therefore likewise dropped here.
lang MUST be a non-blank string; pass "clojure", "python", etc.
nil / "" / whitespace raise IllegalArgumentException.
Filter `blocks` to those whose `:lang` STRICTLY equals `lang` (case-insensitive). Untagged blocks (`:lang nil`) are DROPPED — NOT treated as a wildcard. This is a deliberate strictness contract: models must tag their fence with the requested lang to have their code accepted. The fenceless- fallback path in `extract-code-blocks` also produces `:lang nil` and is therefore likewise dropped here. `lang` MUST be a non-blank string; pass `"clojure"`, `"python"`, etc. `nil` / `""` / whitespace raise `IllegalArgumentException`.
cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |