collective / icalendar

icalendar parser library for Python

Home Page:https://icalendar.readthedocs.io/en/latest/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Control over line breaks

Harold-D opened this issue · comments

I'm adding a very long line as "location" to an iCalendar event like so:

event['LOCATION'] = "this is a long sting with a newline character.\r\n iCalendar is breaking the line where I don't want."

which results in the following location line:

 'LOCATION:this is a long sting with a newline character.\\n iCalendar is '
 'bre\r\n'
 " aking the line where I don't want.\r\n"

Now, I want to format the location line myself. Because I don't want a '\r\n' line break in the middle of the word breaking (hence the newline character in my original line).
How could I achieve this?

iCalendar v5.0.0
Django 4.1.1
Python 3.9.2

commented

The RFC recomends to fold lines at 75 characters. The line break is only in the ics file. E.g. if you do:

from icalendar import Event

event = Event()
event['LOCATION'] = "this is a long sting with a newline character.\r\niCalendar is breaking the line where I don't want."
event1 = Event.from_ical(event.to_ical())
print(event1['LOCATION'])

you get...

this is a long sting with a newline character.
iCalendar is breaking the line where I don't want.

and not...

this is a long sting with a newline character.\\n iCalendar is
 bre
 aking the line where I don't want.

I hope this answers your questions :)

Is it important that the line is not there in the parsed object or do you want to not have it in the ICS file, too?

Thanks for the clear issue description! That makes it comparatively easy to talk about this.

You can remove all such line breaks in the file with

ics_bytes.replace("\r\n ", "")

They always get a space added.

I do not want the '\r\n in the ics file because when the ics file is imported by e.g. the apple calendar, this messes up my formatting of the (in this case) location string.

If I understand correctly, the ics_bytes.replace would replace all \r\n occurrences in the whole ics file.

This issue suggests: would you like to be able to control the line breaks so they do not appear?

Also, if the line breaks are not ignored by the software, it is good to go back to the Apple product and tell them. As a big company they should be able to create a complient product. They may not have noticed.

@Harold-D What do you think?

Thank you for your reply Nico.

I have tested the apearance of the text on an actual device.
Turns out the \r\n's are being ignored by the software.

Sorry for raising this subject to soon without proper testing how the text would be displayed.

Thanks for coming back to us and tell us!