|
| 1 | +package talk |
| 2 | + |
| 3 | +import ( |
| 4 | + "io/ioutil" |
| 5 | + "log" |
| 6 | + "path/filepath" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/BurntSushi/toml" |
| 10 | + "github.com/devopsdays/devopsdays-cli/helpers/paths" |
| 11 | + "github.com/devopsdays/devopsdays-cli/model" |
| 12 | + "github.com/gernest/front" |
| 13 | + "github.com/pkg/errors" |
| 14 | +) |
| 15 | + |
| 16 | +// GetTalkInfo loads in a talk file and returns a struct with the information, decoded |
| 17 | +// from the TOML in the frontmatter |
| 18 | +func GetTalkInfo(file, city, year string) (talk *model.Talk, err error) { |
| 19 | + |
| 20 | + filePath := filepath.Join(paths.EventContentPath(city, year), "program", file) |
| 21 | + dat, err := ioutil.ReadFile(filePath) |
| 22 | + if err != nil { |
| 23 | + return talk, errors.Wrap(err, "load files failed") |
| 24 | + } |
| 25 | + m := front.NewMatter() |
| 26 | + m.Handle("+++", TOMLHandler) |
| 27 | + |
| 28 | + f, body, err := m.Parse(strings.NewReader(string(dat))) |
| 29 | + if err != nil { |
| 30 | + return talk, errors.Wrap(err, "get list of talks failed") |
| 31 | + } |
| 32 | + |
| 33 | + talk = &model.Talk{ |
| 34 | + Name: file, |
| 35 | + Title: f["Title"].(string), |
| 36 | + Description: f["Description"].(string), |
| 37 | + Speakers: f["Speakers"].([]string), |
| 38 | + YouTube: f["YouTube"].(string), |
| 39 | + Vimeo: f["Vimeo"].(string), |
| 40 | + Speakerdeck: f["Speakerdeck"].(string), |
| 41 | + Slideshare: f["Slideshare"].(string), |
| 42 | + Googleslides: f["Googleslides"].(string), |
| 43 | + PDF: f["PDF"].(string), |
| 44 | + Slides: f["Slides"].(string), |
| 45 | + Abstract: body, |
| 46 | + } |
| 47 | + |
| 48 | + return |
| 49 | +} |
| 50 | + |
| 51 | +// TOMLHandler decodes TOML string into a go map[string]interface{} |
| 52 | +func TOMLHandler(front string) (map[string]interface{}, error) { |
| 53 | + |
| 54 | + var thisTalk model.Talk |
| 55 | + if _, err := toml.Decode(front, &thisTalk); err != nil { |
| 56 | + |
| 57 | + log.Fatal(err) |
| 58 | + } |
| 59 | + x := map[string]interface{}{ |
| 60 | + "Name": thisTalk.Name, |
| 61 | + "Title": thisTalk.Title, |
| 62 | + "Description": thisTalk.Description, |
| 63 | + "Speakers": thisTalk.Speakers, |
| 64 | + "YouTube": thisTalk.YouTube, |
| 65 | + "Vimeo": thisTalk.Vimeo, |
| 66 | + "Speakerdeck": thisTalk.Speakerdeck, |
| 67 | + "Slideshare": thisTalk.Slideshare, |
| 68 | + "Googleslides": thisTalk.Googleslides, |
| 69 | + "PDF": thisTalk.PDF, |
| 70 | + "Slides": thisTalk.Slides, |
| 71 | + "Abstract": thisTalk.Abstract, |
| 72 | + } |
| 73 | + |
| 74 | + return x, nil |
| 75 | +} |
0 commit comments