From 1be0cb5a3fa5b2abe5f5d10ec827f06617861d4b Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Tue, 30 Sep 2014 22:07:44 +0200 Subject: First version --- .gitignore | 4 +++ src/example.go | 31 +++++++++++++++++++++++ src/vparse/vparse.go | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++ vcal.ics | 12 +++++++++ vcard.vcs | 16 ++++++++++++ 5 files changed, 132 insertions(+) create mode 100644 src/example.go create mode 100644 src/vparse/vparse.go create mode 100644 vcal.ics create mode 100644 vcard.vcs diff --git a/.gitignore b/.gitignore index daf913b..8c7125a 100644 --- a/.gitignore +++ b/.gitignore @@ -22,3 +22,7 @@ _testmain.go *.exe *.test *.prof + +# IDEA +.idea +*.iml diff --git a/src/example.go b/src/example.go new file mode 100644 index 0000000..9196336 --- /dev/null +++ b/src/example.go @@ -0,0 +1,31 @@ +package main + +import ( + "encoding/json" + "fmt" + "os" + "vparse" +) + +func check(err error) { + if err != nil { + panic(err) + } +} + +func vToJson(filename string) string { + f, err := os.Open(filename) + check(err) + + parsed := vparse.Parse(f) + + jsonEncoded, err := json.MarshalIndent(parsed, "", "\t") + check(err) + + return string(jsonEncoded) +} + +func main() { + fmt.Println(vToJson("vcal.ics")) + fmt.Println(vToJson("vcard.vcs")) +} diff --git a/src/vparse/vparse.go b/src/vparse/vparse.go new file mode 100644 index 0000000..624d0a5 --- /dev/null +++ b/src/vparse/vparse.go @@ -0,0 +1,69 @@ +package vparse + +import ( + "bufio" + "io" + "strings" +) + +type Node struct { + Type string + Properties map[string]string + Children []Node +} + +func parseNode(scanner *bufio.Scanner, nodeType string) Node { + node := Node{nodeType, make(map[string]string), make([]Node, 0)} + + var lastProperty string + var expectColon bool = false + +SC: + for scanner.Scan() { + + line := scanner.Text() + + if strings.HasPrefix(line, " ") { + value := line[1:] + if expectColon { + value = strings.SplitN(value, ":", 2)[1] + } + + node.Properties[lastProperty] += value + + expectColon = false + continue + } + + if !strings.Contains(line, ":") { + lastProperty = line + expectColon = true + continue + } + + splitLine := strings.SplitN(line, ":", 2) + + switch splitLine[0] { + + case "END": + break SC + + case "BEGIN": + node.Children = append(node.Children, parseNode(scanner, splitLine[1])) + break + + default: + node.Properties[splitLine[0]] = splitLine[1] + lastProperty = splitLine[0] + + } + + } + + return node +} + +func Parse(reader io.Reader) []Node { + scanner := bufio.NewScanner(reader) + return parseNode(scanner, "").Children +} diff --git a/vcal.ics b/vcal.ics new file mode 100644 index 0000000..46df067 --- /dev/null +++ b/vcal.ics @@ -0,0 +1,12 @@ +BEGIN:VCALENDAR +VERSION:2.0 +PRODID:-//hacksw/handcal//NONSGML v1.0//EN +BEGIN:VEVENT +UID:uid1@example.com +DTSTAMP:19970714T170000Z +ORGANIZER;CN=John Doe:MAILTO:john.doe@example.com +DTSTART:19970714T170000Z +DTEND:19970715T035959Z +SUMMARY:Bastille Day Party +END:VEVENT +END:VCALENDAR diff --git a/vcard.vcs b/vcard.vcs new file mode 100644 index 0000000..55ee959 --- /dev/null +++ b/vcard.vcs @@ -0,0 +1,16 @@ +BEGIN:VCARD +VERSION:4.0 +N:Gump;Forrest;;; +FN:Forrest Gump +ORG:Bubba Gump Shrimp Co. +TITLE:Shrimp Man +PHOTO;MEDIATYPE=image/gif:http://www.example.com/dir_photos/my_photo.gif +TEL;TYPE=work,voice;VALUE=uri:tel:+1-111-555-1212 +TEL;TYPE=home,voice;VALUE=uri:tel:+1-404-555-1212 +ADR;TYPE=work;LABEL="100 Waters Edge\nBaytown, LA 30314\nUnited States of America" + :;;100 Waters Edge;Baytown;LA;30314;United States of America +ADR;TYPE=home;LABEL="42 Plantation St.\nBaytown, LA 30314\nUnited States of America" + :;;42 Plantation St.;Baytown;LA;30314;United States of America +EMAIL:forrestgump@example.com +REV:20080424T195243Z +END:VCARD -- cgit v1.2.3