package turn import ( "errors" "fmt" "strings" ) var ( // ErrEmptyOutput means the turn produced no markdown at all. ErrEmptyOutput = errors.New("turn output: empty") // ErrMissingOutcome means no outcome heading and value were found. ErrMissingOutcome = errors.New("turn output: missing outcome") // ErrDuplicateOutcome means more than one outcome heading was found. ErrDuplicateOutcome = errors.New("turn output: duplicate outcome") // ErrOutcomeNotAllowed means the value was not one of the declared words. ErrOutcomeNotAllowed = errors.New("turn output: outcome not allowed") ) // ParseOutput extracts the single outcome section from a turn's markdown. // // A heading whose text is "outcome" (any ATX level) is followed by the first // non-blank line, which must contain exactly one allowed word. Matching is // trim-and-lower-case, and the canonical spelling from allowedOutcomes is // returned. The body is the whole document with only that heading and its value // line removed, so markdown written before the outcome section (the natural // place for a trailing "# outcome") is kept too, in its original order and // bytes. Headings and values inside fenced code blocks are never considered. func ParseOutput(markdown string, allowedOutcomes []string) (Outcome string, Body string, err error) { if strings.TrimSpace(markdown) == "" { return "", "", ErrEmptyOutput } lines := markdownLines(markdown) var headings []int var fence fenceState for i, line := range lines { if fence.open() { if fence.closes(line.text) { fence = fenceState{} } continue } if marker, ok := openingFence(line.text); ok { fence = marker continue } if isOutcomeHeading(line.text) { headings = append(headings, i) } } switch len(headings) { case 0: return "", "", ErrMissingOutcome case 1: default: return "", "", fmt.Errorf("%w: found %d outcome headings", ErrDuplicateOutcome, len(headings)) } heading := headings[0] value := heading + 1 for value < len(lines) && strings.TrimSpace(lines[value].text) == "" { value++ } if value == len(lines) { return "", "", fmt.Errorf("%w: outcome heading has no value", ErrMissingOutcome) } word := strings.TrimSpace(lines[value].text) canonical, ok := allowedOutcome(word, allowedOutcomes) if !ok { return "", "", fmt.Errorf("%w: %q", ErrOutcomeNotAllowed, word) } // Remove the outcome section and nothing else: a document that states its // result first and its outcome last must still hand its whole body forward. sectionStart := 0 if heading > 0 { sectionStart = lines[heading-1].next } return canonical, markdown[:sectionStart] + markdown[lines[value].next:], nil } func allowedOutcome(word string, outcomes []string) (string, bool) { key := strings.ToLower(word) for _, outcome := range outcomes { outcome = strings.TrimSpace(outcome) if outcome != "" && strings.ToLower(outcome) == key { return outcome, true } } return "", false } type markdownLine struct { text string next int } // markdownLines records each line's content without its line ending and the // byte position immediately after that line ending. Body slices therefore keep // CRLF and every other byte exactly as supplied. func markdownLines(markdown string) []markdownLine { lines := make([]markdownLine, 0, strings.Count(markdown, "\n")+1) for start := 0; start < len(markdown); { next := len(markdown) text := markdown[start:] if relative := strings.IndexByte(markdown[start:], '\n'); relative >= 0 { next = start + relative + 1 text = markdown[start : start+relative] } lines = append(lines, markdownLine{ text: strings.TrimSuffix(text, "\r"), next: next, }) if next == len(markdown) { break } start = next } return lines } func isOutcomeHeading(text string) bool { text = strings.TrimSpace(text) hashes := 0 for hashes < len(text) && text[hashes] == '#' { hashes++ } if hashes == 0 || hashes == len(text) { return false } // Markdown ATX headings require whitespace between the marker and text. if text[hashes] != ' ' && text[hashes] != '\t' { return false } return strings.EqualFold(strings.TrimSpace(text[hashes:]), "outcome") } type fenceState struct { char byte length int } func (f fenceState) open() bool { return f.length > 0 } func (f fenceState) closes(text string) bool { char, length, ok := fenceMarker(text) if !ok || char != f.char || length < f.length { return false } trimmed := strings.TrimLeft(text, " \t") return strings.TrimSpace(trimmed[length:]) == "" } func openingFence(text string) (fenceState, bool) { char, length, ok := fenceMarker(text) if !ok { return fenceState{}, false } return fenceState{char: char, length: length}, true } func fenceMarker(text string) (byte, int, bool) { trimmed := strings.TrimLeft(text, " \t") if len(trimmed) < 3 || (trimmed[0] != '`' && trimmed[0] != '~') { return 0, 0, false } char := trimmed[0] length := 0 for length < len(trimmed) && trimmed[length] == char { length++ } if length < 3 { return 0, 0, false } return char, length, true }