Documentation
¶
Overview ¶
Package doc parses a markdown file into prose and fenced code blocks without losing a byte of the original, so output can be spliced back in.
Index ¶
- Constants
- func EscapeValue(v string) string
- func Expand(text string, lookup Lookup) (string, error)
- func RegionStart(id string) string
- func UnescapeValue(v string) string
- type Attrs
- type Block
- type Cond
- type Document
- type Include
- type Lookup
- type Options
- type Placeholder
- type Ref
- type Section
- type Span
Constants ¶
const ( Status = "?" // exit status StatusAs = "err?" // the same, spelled out Ran = "ran?" // "true" if the block ran, else "false" )
Block facts, which end in ? so they can't clash with shell variables.
const IncludeAttr = "<include"
IncludeAttr marks an include block:
```go {<include file=main.go from=10 to=20 number_on}
```
mdsh writes the selected lines into the block's body. The block never runs and has no name.
const RegionEnd = regionClose
RegionEnd is the line that closes an output region.
const TapeLang = "tape"
TapeLang is the language of a tape fence, which types the input for the tty block its for= names:
```tape {for=shell}
Type "exit"
Enter
```
It can be an include block too, to read the tape from a file.
Variables ¶
This section is empty.
Functions ¶
func EscapeValue ¶
EscapeValue quotes a value for the :"..." part of a placeholder.
func Expand ¶
Expand replaces each placeholder in text with its value from lookup, ignoring any stored :"..." value. A placeholder lookup doesn't know is an error.
func RegionStart ¶
RegionStart returns the line that opens an output region for id.
Types ¶
type Attrs ¶
Attrs are a block's Pandoc-style attributes. The block id is stored under "#". A bare key such as skip is stored with an empty value.
func ParseInfo ¶
ParseInfo splits a fence info string such as
sh {#build env=build share=GREETING skip}
into its language and attributes. The Pandoc form {.sh #build} is also accepted; its first class is the language.
type Block ¶
type Block struct {
ID string
Lang string
Attrs Attrs
Body string
// Span covers the fence lines, including the closing fence's newline.
Span Span
// BodySpan covers the lines between the fences, as they are in Src.
// It ends at Span.End when the fence is never closed.
BodySpan Span
// Fence is the opening fence's run of backticks or tildes, and Info
// its info string.
Fence, Info string
// Output covers any output already attached to this block, starting
// right after Span. It is empty (Start == End == Span.End) when there
// is none.
Output Span
// Prefix is what starts each line of a block nested in block quotes or
// list items, such as "> " or " ". Output written after the block
// starts each line with it. Empty at the top level.
Prefix string
// Lead is the part of the opening fence line that holds list markers
// when the fence opens on the same line as its list item, such as
// "- " or "> 1. ". Empty otherwise.
Lead string
// contains filtered or unexported fields
}
Block is a fenced code block.
func (*Block) Cond ¶
Cond returns the condition in a block attribute, and whether it is set. Parse has already checked that it is valid.
type Cond ¶
type Cond struct {
Ref *Ref // nil for a constant
Not bool
Op string // "", "==" or "!="
Value string // compared against, or the constant
Text string // as written
}
Cond is a condition such as
{{$DEBUG}} true unless empty, 0, false or no
!{{$DEBUG}} the opposite
{{#detect/env(OS)}}==Linux string comparison; != also works
{{$?}}==0 the most recent block's exit status
{{#build/?}}!=0 block build's exit status
{{#build/err?}} the same; true when build failed
{{$ran?}}, {{#build/ran?}} whether the block ran
true, false constants
type Document ¶
type Document struct {
Src []byte
// Shebang is the #! line, without its newline. Empty if there is none.
Shebang string
// FrontMatter covers the front matter, including both --- lines.
FrontMatter Span
Options Options
Blocks []*Block
// Placeholders are the env placeholders in the prose, in order.
// Empty when the front matter turns them off.
Placeholders []*Placeholder
// Sections are the mdsh:if ... mdsh:end sections, in order of their
// if lines. They may nest.
Sections []*Section
}
Document is a parsed markdown file.
func Parse ¶
Parse splits src into front matter and fenced blocks. Output fences and output regions written by mdsh are attached to the block they follow instead of being returned as blocks.
func (*Document) AppendBlock ¶ added in v1.1.0
AppendBlock adds a fenced block of lang holding body to the end of d, after a blank line, and returns it. Earlier blocks keep their spans, so results for them stay valid. It fails if the end of d would swallow the block, such as an unclosed fence.
func (*Document) BlockText ¶ added in v1.1.0
BlockText is b's source, opening fence to closing fence, without the list markers and block quote prefixes it is nested in. SetBlock takes it back.
func (*Document) Refs ¶
Refs lists every variable the document's placeholders, conditions and sections refer to.
func (*Document) SetBlock ¶ added in v1.1.0
SetBlock replaces b, which must be one of d's blocks, with text, as BlockText gives it, and parses d again. The fence, attributes and id may change; output saved for b follows a new id. Every *Block in d keeps its identity, with its spans moved to match the new source, so results that point at them stay valid. It fails if text isn't one block of the same kind, or would rename other blocks.
type Include ¶
type Include struct {
File string
From, To int
// Numbered asks for each line to be shown with its line number in
// the file. The numbers are for display, never stored in the body.
Numbered bool
}
Include is what an include block asks for. Lines are numbered from 1 and the range is inclusive. From 0 or 1 is the first line; To 0 is the end of the file.
type Options ¶
type Options struct {
// Shell runs sh and zsh blocks. Defaults to zsh when the document
// has zsh blocks, else /bin/sh.
Shell string `yaml:"shell"`
// OnError is "continue" (default) or "stop".
OnError string `yaml:"on_error"`
// Step asks before running each block, as if every block had the
// confirm attribute.
Step bool `yaml:"step"`
// Interpolate turns {{#block/env(VAR)}} placeholders on or off.
// Defaults to on.
Interpolate *bool `yaml:"interpolate"`
// ShowStderr shows every block's stderr. Blocks can override it
// with show-stderr=. By default stderr shows only for failed blocks.
ShowStderr *bool `yaml:"show_stderr"`
Env struct {
// Share lists the variables passed into the top-level env from
// the caller, and the default for sub-envs. Nil means all.
Share []string `yaml:"share"`
} `yaml:"env"`
}
Options are the document-wide settings under the mdsh: key of the front matter.
func (Options) Interpolating ¶
Interpolating reports whether placeholders are filled in.
type Placeholder ¶
type Placeholder struct {
Span Span
Ref
// Value is the stored value, unescaped. HasValue is false when the
// placeholder has no :"..." part.
Value string
HasValue bool
}
Placeholder is a reference in the prose to a variable, as a block left it or from the caller's environment:
{{#setup/env(GREETING)}}
{{#setup/env(GREETING):"hello"}}
{{$USER}}
{{$USER:"j"}}
{{$?}} the most recent block's exit status
{{#build/?:"0"}} block build's exit status
The quoted part holds the last value seen, so the file reads well without running it. Write-back fills it in.
func (*Placeholder) Source ¶
func (p *Placeholder) Source(value string) string
Source renders the placeholder as it is written in the file, holding value.
type Ref ¶
Ref names a variable: in the caller's environment ({{$VAR}}, BlockID empty), or as a block left it ({{#block/env(VAR)}}). Var can also be one of the block facts below, for the most recent block ({{$?}}) or a named one ({{#block/?}}).
func TextRefs ¶
TextRefs lists the placeholders in text, which unlike prose may be code: a tape's input, say.