code.pablomurad.com

patchwork

Publishes the revisions of your pages as a feed, with notes and readable diffs, written in Go

internal/doc/ignore.go raw
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
package doc

import "strings"

const (
	ignoreOpen  = "<!-- patchwork:ignore -->"
	ignoreClose = "<!-- /patchwork:ignore -->"
)

// stripIgnored removes regions between ignore markers. Markers count only on
// their own line and outside fenced code, so a post that documents the
// markers can show them in a code block. An unclosed region runs to the end.
func stripIgnored(body string) string {
	if !strings.Contains(body, ignoreOpen) {
		return body
	}
	lines := strings.Split(body, "\n")
	out := lines[:0]
	var fence string
	ignoring := false
	for _, l := range lines {
		t := strings.TrimSpace(l)
		if !ignoring {
			if f := fenceMarker(l); f != "" {
				switch {
				case fence == "":
					fence = f
				case closesFence(l, fence):
					fence = ""
				}
			}
		}
		if fence == "" {
			if !ignoring && t == ignoreOpen {
				ignoring = true
				continue
			}
			if ignoring && t == ignoreClose {
				ignoring = false
				continue
			}
		}
		if !ignoring {
			out = append(out, l)
		}
	}
	return strings.Join(out, "\n")
}