-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.go
More file actions
33 lines (29 loc) · 686 Bytes
/
Copy pathparser.go
File metadata and controls
33 lines (29 loc) · 686 Bytes
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
package main
import (
"errors"
"fmt"
"os"
"regexp"
"strings"
)
func parseText(text string) (Article, error) {
pattern := regexp.MustCompile(`#\s+(.+)\s+##\sMeta\s+tags:\s+(.+)\s+(?s)(.+)`)
matches := pattern.FindStringSubmatch(text)
if len(matches) != 4 {
return Article{}, errors.New("unexpected file format: " + text)
}
return Article{
Title: matches[1],
Text: matches[3],
Tags: strings.Split(matches[2], ", "),
Series: "",
}, nil
}
func parseFile(filepath string) (Article, error) {
bytes, err := os.ReadFile(filepath)
if err != nil {
return Article{}, fmt.Errorf("reading article file: %v", err)
}
text := string(bytes)
return parseText(text)
}