Debian Codegraph @

internal/util/md/md.go

raw symbols SCIP
1 // Copyright 2020 Marius Ackerman
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 /*
16 Package md extracts code sections of markdown files
17 */
18 package md
19
20 import "os"
21
22 /*
23 GetSource returns code sections enclosed in triple backticks.
24 */
25 func GetSource(mdfile string) (string, error) {
26 inbuf, err := os.ReadFile(mdfile)
27 if err != nil {
28 return "", err
29 }
30 input := []rune(string(inbuf))
31 loadMd(input)
32 return string(input), nil
33 }
34
35 func loadMd(input []rune) {
36 i := 0
37 text := true
38 for i < len(input) {
39 if i <= len(input)-3 && input[i] == '`' && input[i+1] == '`' && input[i+2] == '`' {
40 text = !text
41 for j := 0; j < 3; j++ {
42 input[i+j] = ' '
43 }
44 i += 3
45 }
46 if i < len(input) {
47 if text {
48 if input[i] == '\n' {
49 input[i] = '\n'
50 } else {
51 input[i] = ' '
52 }
53 }
54 i += 1
55 }
56 }
57 }