From 176af76f2b7bb70df90c8d202502dfc58b227a43 Mon Sep 17 00:00:00 2001 From: Valentin REVERSAT Date: Thu, 26 Dec 2024 23:39:19 +0000 Subject: [PATCH] WIP --- calendar.go | 21 +++++++++++++++------ parser/parser.go | 31 +++++++++++++++++++++++++++---- 2 files changed, 42 insertions(+), 10 deletions(-) diff --git a/calendar.go b/calendar.go index c581c4e..3153151 100644 --- a/calendar.go +++ b/calendar.go @@ -22,13 +22,17 @@ type Calendar interface { // AddProperty add a new property to a Calendar AddProperty(property properties.Property) + + // SetComponent set a component to a calendar + SetComponent(component components.Component) } type calendar struct { - Begin properties.BeginProperty - Properties properties.Properties - Component components.Component - End properties.EndProperty + Begin properties.BeginProperty + Properties properties.Properties + Component components.Component + End properties.EndProperty + propertyLevel int } // NewCalendar create a iCalendar object @@ -36,7 +40,7 @@ type calendar struct { func NewCalendar( calendarComponent components.Component, propertyList ...properties.Property, -) (Calendar, error) { +) Calendar { return &calendar{ Begin: properties.NewBeginProperty( registries.Vcalendar, @@ -46,7 +50,8 @@ func NewCalendar( End: properties.NewEndProperty( registries.Vcalendar, ), - }, nil + propertyLevel: 0, + } } func (c *calendar) GetProperty( @@ -72,6 +77,10 @@ func (c *calendar) AddProperty(property properties.Property) { c.Properties = append(c.Properties, property) } +func (c *calendar) SetComponent(component components.Component) { + c.Component = component +} + func (c *calendar) SerializeToICSFormat(output io.Writer) { c.Begin.ToICalendarPropFormat(output) for i := 0; i < len(c.Properties); i++ { diff --git a/parser/parser.go b/parser/parser.go index e518728..4efdc46 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -7,7 +7,11 @@ import ( "os" "strings" + "github.com/vareversat/gics" + "github.com/vareversat/gics/components" "github.com/vareversat/gics/parameters" + "github.com/vareversat/gics/properties" + "github.com/vareversat/gics/registries" ) const ( @@ -41,11 +45,14 @@ func unfold(foldedFile *os.File, unfoldedData *bytes.Buffer) { } // parse take an unfoldedData to create parameters and properties -func parse(unfoldedData bytes.Buffer) { +func parse(unfoldedData bytes.Buffer) properties.Properties { unfoldedDataReader := bytes.NewReader(unfoldedData.Bytes()) scanner := bufio.NewScanner(unfoldedDataReader) + properties := properties.Properties{} // Iterate through all the unfolded file lineNumber := 0 + // Init new calendar + calendar := gics.NewCalendar(nil) for scanner.Scan() { line := scanner.Text() lexer := NewLexer(line) @@ -67,9 +74,25 @@ func parse(unfoldedData bytes.Buffer) { lexer.property.PropertyValue, parsedParameters..., ) - if parsedProperty != nil { - parsedProperty.ToICalendarPropFormat(os.Stdout) - } + properties = append(properties, parsedProperty) + calendar.AddProperty(parsedProperty) lineNumber++ } + return properties +} + +func compute(property properties.Property, level *int, calendar gics.Calendar) { + if property.GetName() == registries.BeginProp { + *level++ + if *level == 1 { + calendar = gics.NewCalendar(nil) + } else { + component, err := components.NewComponentFromName(registries.ComponentRegistry(property.GetValue())) + if err != nil && calendar != nil { + calendar.SetComponent(component) + } + } + } else if property.GetName() == registries.EndProp { + *level-- + } }