New initializes a new Sheet
( title string, cheatpath string, path string, tags []string, readOnly bool, )
| 27 | |
| 28 | // New initializes a new Sheet |
| 29 | func New( |
| 30 | title string, |
| 31 | cheatpath string, |
| 32 | path string, |
| 33 | tags []string, |
| 34 | readOnly bool, |
| 35 | ) (Sheet, error) { |
| 36 | |
| 37 | // read the cheatsheet file |
| 38 | markdown, err := os.ReadFile(path) |
| 39 | if err != nil { |
| 40 | return Sheet{}, fmt.Errorf("failed to read file: %s, %v", path, err) |
| 41 | } |
| 42 | |
| 43 | // parse the raw cheatsheet text |
| 44 | fm, text, err := parse(string(markdown)) |
| 45 | if err != nil { |
| 46 | return Sheet{}, fmt.Errorf("failed to parse front-matter: %v", err) |
| 47 | } |
| 48 | |
| 49 | // merge the sheet-specific tags into the cheatpath tags |
| 50 | tags = append(tags, fm.Tags...) |
| 51 | |
| 52 | // sort strings so they pretty-print nicely |
| 53 | sort.Strings(tags) |
| 54 | |
| 55 | // initialize and return a sheet |
| 56 | return Sheet{ |
| 57 | Title: title, |
| 58 | CheatPath: cheatpath, |
| 59 | Path: path, |
| 60 | Text: text, |
| 61 | Tags: tags, |
| 62 | Syntax: fm.Syntax, |
| 63 | ReadOnly: readOnly, |
| 64 | }, nil |
| 65 | } |