Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions calendar.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,25 @@ 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
// You can find more information in this section of the RFC-5545 : https://datatracker.ietf.org/doc/html/rfc5545#section-3.4
func NewCalendar(
calendarComponent components.Component,
propertyList ...properties.Property,
) (Calendar, error) {
) Calendar {
return &calendar{
Begin: properties.NewBeginProperty(
registries.Vcalendar,
Expand All @@ -46,7 +50,8 @@ func NewCalendar(
End: properties.NewEndProperty(
registries.Vcalendar,
),
}, nil
propertyLevel: 0,
}
}

func (c *calendar) GetProperty(
Expand All @@ -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++ {
Expand Down
31 changes: 27 additions & 4 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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)
Expand All @@ -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--
}
}