summaryrefslogtreecommitdiffstats
path: root/src/go/doc/comment/markdown.go
blob: d8550f2e39d300b8e8100724fb361441d8e8980b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// Copyright 2022 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package comment

import (
	"bytes"
	"fmt"
	"strings"
)

// An mdPrinter holds the state needed for printing a Doc as Markdown.
type mdPrinter struct {
	*Printer
	headingPrefix string
	raw           bytes.Buffer
}

// Markdown returns a Markdown formatting of the Doc.
// See the [Printer] documentation for ways to customize the Markdown output.
func (p *Printer) Markdown(d *Doc) []byte {
	mp := &mdPrinter{
		Printer:       p,
		headingPrefix: strings.Repeat("#", p.headingLevel()) + " ",
	}

	var out bytes.Buffer
	for i, x := range d.Content {
		if i > 0 {
			out.WriteByte('\n')
		}
		mp.block(&out, x)
	}
	return out.Bytes()
}

// block prints the block x to out.
func (p *mdPrinter) block(out *bytes.Buffer, x Block) {
	switch x := x.(type) {
	default:
		fmt.Fprintf(out, "?%T", x)

	case *Paragraph:
		p.text(out, x.Text)
		out.WriteString("\n")

	case *Heading:
		out.WriteString(p.headingPrefix)
		p.text(out, x.Text)
		if id := p.headingID(x); id != "" {
			out.WriteString(" {#")
			out.WriteString(id)
			out.WriteString("}")
		}
		out.WriteString("\n")

	case *Code:
		md := x.Text
		for md != "" {
			var line string
			line, md, _ = strings.Cut(md, "\n")
			if line != "" {
				out.WriteString("\t")
				out.WriteString(line)
			}
			out.WriteString("\n")
		}

	case *List:
		loose := x.BlankBetween()
		for i, item := range x.Items {
			if i > 0 && loose {
				out.WriteString("\n")
			}
			if n := item.Number; n != "" {
				out.WriteString(" ")
				out.WriteString(n)
				out.WriteString(". ")
			} else {
				out.WriteString("  - ") // SP SP - SP
			}
			for i, blk := range item.Content {
				const fourSpace = "    "
				if i > 0 {
					out.WriteString("\n" + fourSpace)
				}
				p.text(out, blk.(*Paragraph).Text)
				out.WriteString("\n")
			}
		}
	}
}

// text prints the text sequence x to out.
func (p *mdPrinter) text(out *bytes.Buffer, x []Text) {
	p.raw.Reset()
	p.rawText(&p.raw, x)
	line := bytes.TrimSpace(p.raw.Bytes())
	if len(line) == 0 {
		return
	}
	switch line[0] {
	case '+', '-', '*', '#':
		// Escape what would be the start of an unordered list or heading.
		out.WriteByte('\\')
	case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
		i := 1
		for i < len(line) && '0' <= line[i] && line[i] <= '9' {
			i++
		}
		if i < len(line) && (line[i] == '.' || line[i] == ')') {
			// Escape what would be the start of an ordered list.
			out.Write(line[:i])
			out.WriteByte('\\')
			line = line[i:]
		}
	}
	out.Write(line)
}

// rawText prints the text sequence x to out,
// without worrying about escaping characters
// that have special meaning at the start of a Markdown line.
func (p *mdPrinter) rawText(out *bytes.Buffer, x []Text) {
	for _, t := range x {
		switch t := t.(type) {
		case Plain:
			p.escape(out, string(t))
		case Italic:
			out.WriteString("*")
			p.escape(out, string(t))
			out.WriteString("*")
		case *Link:
			out.WriteString("[")
			p.rawText(out, t.Text)
			out.WriteString("](")
			out.WriteString(t.URL)
			out.WriteString(")")
		case *DocLink:
			url := p.docLinkURL(t)
			if url != "" {
				out.WriteString("[")
			}
			p.rawText(out, t.Text)
			if url != "" {
				out.WriteString("](")
				url = strings.ReplaceAll(url, "(", "%28")
				url = strings.ReplaceAll(url, ")", "%29")
				out.WriteString(url)
				out.WriteString(")")
			}
		}
	}
}

// escape prints s to out as plain text,
// escaping special characters to avoid being misinterpreted
// as Markdown markup sequences.
func (p *mdPrinter) escape(out *bytes.Buffer, s string) {
	start := 0
	for i := 0; i < len(s); i++ {
		switch s[i] {
		case '\n':
			// Turn all \n into spaces, for a few reasons:
			//   - Avoid introducing paragraph breaks accidentally.
			//   - Avoid the need to reindent after the newline.
			//   - Avoid problems with Markdown renderers treating
			//     every mid-paragraph newline as a <br>.
			out.WriteString(s[start:i])
			out.WriteByte(' ')
			start = i + 1
			continue
		case '`', '_', '*', '[', '<', '\\':
			// Not all of these need to be escaped all the time,
			// but is valid and easy to do so.
			// We assume the Markdown is being passed to a
			// Markdown renderer, not edited by a person,
			// so it's fine to have escapes that are not strictly
			// necessary in some cases.
			out.WriteString(s[start:i])
			out.WriteByte('\\')
			out.WriteByte(s[i])
			start = i + 1
		}
	}
	out.WriteString(s[start:])
}