Documentation
¶
Overview ¶
Package docblock reads a markdown document into the regions of it that an author marked as code, and into the logical lines those regions contain.
It exists because the layer above it, which decides what a documented line means and whether to run it, was living in the same file as the parser that produces the lines. Two concerns in one file is why the planner could not be separated: anything importing the reader also imported the install model. Nothing here knows what an install step is, and nothing here may learn.
Index ¶
- Variables
- func BlockLine(l liner, src []byte) int
- func BlockText(l liner, src []byte) string
- func CodeLines(markdown string) []string
- func CommandHead(flat string) string
- func Flatten(logical string) string
- func HasPlaceholder(s string) bool
- func LogicalLines(raw []string) []string
- func PrepareLines(raw []string) []string
- func ProseOnly(markdown string) string
- func ShownFailures(block Block) map[string]bool
- func SourceLineIndex(block Block) func(string) int
- func StripComment(line string) string
- func TrailingComment(flat string) string
- type Block
Constants ¶
This section is empty.
Variables ¶
var ShellLangs = map[string]bool{ "": true, "sh": true, "bash": true, "shell": true, "zsh": true, "console": true, "shell-session": true, "text": true, "plain": true, }
ShellLangs are fence languages treated as shell recipes. The empty string covers indented blocks and fences with no info string.
Functions ¶
func CommandHead ¶
CommandHead returns the command portion of a line, without a trailing comment, so placeholder checks ignore prose in comments.
func Flatten ¶
Flatten renders a logical line as one analyzable string: continuations collapse to spaces and only a heredoc's first line is kept, since the body is data rather than commands.
func HasPlaceholder ¶ added in v0.25.0
HasPlaceholder reports whether s holds a token the reader must replace before running it. The planner asks it of an example line and the extractor asks it of an install line, so both read placeholders the same way.
func LogicalLines ¶
LogicalLines groups physical lines into logical commands: a trailing backslash joins the next line, and a heredoc runs to its terminator.
func PrepareLines ¶
PrepareLines normalizes a block's raw lines: prompt-style blocks keep only the prompted lines, and two-column usage blocks drop the prose column.
func ProseOnly ¶
ProseOnly returns a document with its fenced code blocks removed, so a rule about what a document says is not answered by what it demonstrates.
func ShownFailures ¶
ShownFailures returns the commands of a prompted block whose displayed output is an error. Docs sometimes show a command failing on purpose, to teach why the corrected form that follows is needed, and the demonstrated failure exiting nonzero is the document working as written.
func SourceLineIndex ¶
SourceLineIndex returns a lookup from a prepared logical line back to its 1-based README line. The prepared line may have lost a prompt marker or gained continuation lines, so the match compares the first physical line against each raw block line with the prompt stripped. Unmatched lines fall back to the block's first line, and 0 means the block's position is unknown.
func StripComment ¶
StripComment removes a trailing shell comment from a code line.
func TrailingComment ¶
TrailingComment returns the trailing shell comment of a line, or empty.
Types ¶
type Block ¶
type Block struct {
// Lang is the fence info language, empty for indented blocks and spans.
Lang string
// Heading is the text of the nearest section heading above the block.
Heading string
// Intro is the paragraph that introduces the block: the nearest prose
// above it under the same heading. Docs scope a block in the sentence
// before it, "on macOS run the following", and a planner that cannot see
// that sentence convicts commands written for another operating system.
Intro string
// Span reports whether this is an inline span rather than a block.
Span bool
// Line is the 1-based README line the block's content starts on.
Line int
// Lines are the raw code lines.
Lines []string
// Title is the title attribute of a fenced block's info string, such as
// process.py in “`python title="process.py"“`, empty when there is none.
// Documentation sites render it as the name of the file the block holds.
Title string
// List numbers the outermost list the block sits in, in document order
// from 1, and is 0 outside a list. A recipe written as list steps spreads
// its commands over spans in several items, and nested lists inside an
// item are the same recipe.
List int
// Item is the 1-based index of the outermost list's item the block sits
// in, and is 0 outside a list.
Item int
}
Block is one region of author-marked code in a README: a fenced block, an indented block, or an inline span.
func CodeBlocks ¶
CodeBlocks returns the code the author marked, grouped by block: each fenced or indented block is one group of lines, and each inline span is its own group.