File: elem_header.go

package info (click to toggle)
golang-github-johanneskaufmann-html-to-markdown 2.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,084 kB
  • sloc: makefile: 3
file content (77 lines) | stat: -rw-r--r-- 1,428 bytes parent folder | download | duplicates (2)
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
package escape

func IsAtxHeader(chars []byte, index int) int {
	if chars[index] != '#' {
		return -1
	}

	for i := index - 1; i >= 0; i-- {
		if chars[i] == '\n' {
			break
		}
		if chars[i] == placeholderByte {
			continue
		}
		if chars[i] == ' ' {
			continue
		}
		return -1
	}

	nPoundSigns := 1
	for i := index + 1; i < len(chars); i++ {
		if chars[i] == '#' {
			nPoundSigns++

			if nPoundSigns > 6 {
				return -1
			}
			continue
		}

		if chars[i] == placeholderByte {
			continue
		}
		if chars[i] == ' ' || chars[i] == '\t' || chars[i] == '\n' || chars[i] == '\r' {
			// TODO: fix calculation with placeholder (maybe own for loop construct?)
			// Returns the count of # that we encountered
			return i - index
		}

		return -1
	}
	return 1
}

func IsSetextHeader(chars []byte, index int) int {
	if chars[index] != '=' && chars[index] != '-' {
		return -1
	}

	var newlineCount int
	for i := index - 1; i >= 0; i-- {
		if chars[i] == placeholderByte || chars[i] == ' ' {
			continue
		}

		if chars[i] == '\n' {
			newlineCount++
			continue
		}

		if newlineCount == 0 {
			// Without any newlines, this character is on the same line
			// as the delimiter. So the delimiter is inside a normal text...
			return -1
		} else if newlineCount == 1 {
			// The heading content is on the line above the delimiter
			// which qualifies for a setext heading...
			return 1
		} else {
			return -1
		}

	}

	return -1
}