docblock

package
v1.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 23, 2026 License: MIT Imports: 8 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

Constants

This section is empty.

Variables

View Source
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 BlockLine

func BlockLine(l liner, src []byte) int

BlockLine returns the README line a code block's content starts on.

func BlockText

func BlockText(l liner, src []byte) string

BlockText returns the raw text inside a fenced or indented code block.

func CodeLines

func CodeLines(markdown string) []string

CodeLines returns every line the author marked as code, across all blocks.

func CommandHead

func CommandHead(flat string) string

CommandHead returns the command portion of a line, without a trailing comment, so placeholder checks ignore prose in comments.

func Flatten

func Flatten(logical string) string

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

func HasPlaceholder(s string) bool

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

func LogicalLines(raw []string) []string

LogicalLines groups physical lines into logical commands: a trailing backslash joins the next line, and a heredoc runs to its terminator.

func NotFoundCodes added in v1.1.0

func NotFoundCodes(markdown string) []int

NotFoundCodes returns the exit codes a document says mean that nothing was found, from a section headed as exit codes or a prose run that starts with "Exit codes:". Only a meaning of not found, no match, or nothing found counts. Every other documented code, including any failure a project documents, is left out, so a table cannot excuse a real error. Codes are nonzero ordinary exits only, in ascending order.

func PrepareLines

func PrepareLines(raw []string) []string

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

func ProseOnly(markdown string) string

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 ResolveToolPath added in v1.2.0

func ResolveToolPath(line string, owned func(string) bool) string

ResolveToolPath replaces a stand-in path to a program in command position, such as path/to/tool, /path/to/tool, <path/to/tool>, or <path/to/<tool>>, with the program's bare name when owned reports that the session installs it. pyenv documents its own flag as `<path/to/pyenv> --install`, and reading that as a placeholder left the one wrong line of its README untested. A stand-in path to anything else, such as path/to/file.txt or a tool the session does not install, is returned unchanged and stays a placeholder. Leading indentation and a "$ " prompt are kept.

func ShownFailures

func ShownFailures(block Block) map[string]bool

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

func SourceLineIndex(block Block) func(string) int

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

func StripComment(line string) string

StripComment removes a trailing shell comment from a code line.

func TrailingComment

func TrailingComment(flat string) string

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
	// Lead is the paragraph that ends directly above a fenced block, within
	// two lines and with no other block between them, empty otherwise. It is
	// where a tutorial names the file a block holds: "create a file named
	// `app.py`" followed by the file's body.
	Lead 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
	// Details numbers the outermost HTML details element the block sits in,
	// in document order from 1, and is 0 outside one. A README offers
	// alternative setups as collapsed details under one heading, and each is
	// its own section to a reader who opens only one.
	Details int
}

Block is one region of author-marked code in a README: a fenced block, an indented block, or an inline span.

func CodeBlocks

func CodeBlocks(markdown string) []Block

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL