Skip to content

Commit

Permalink
Fix block element in CRLF (#1290)
Browse files Browse the repository at this point in the history
  • Loading branch information
dinhlongviolin1 authored May 17, 2024
1 parent 3bf293a commit 0d2314d
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions taipy/gui/_renderers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
# an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
# specific language governing permissions and limitations under the License.

import re
import typing as t
from abc import ABC, abstractmethod
from os import path
Expand All @@ -22,7 +23,7 @@
from ._html import _TaipyHTMLParser

if t.TYPE_CHECKING:
from watchdog.observers import BaseObserverSubclassCallable
from watchdog.observers.api import BaseObserver

from ..gui import Gui

Expand All @@ -46,7 +47,8 @@ def __init__(self, **kwargs) -> None:
self._content = ""
self._base_element: t.Optional[_Element] = None
self._filepath = ""
self._observer: t.Optional["BaseObserverSubclassCallable"] = None
self._observer: t.Optional["BaseObserver"] = None
self._encoding: t.Optional[str] = kwargs.get("encoding", None)
if isinstance(content, str):
self.__process_content(content)
elif isinstance(content, _Element):
Expand All @@ -68,7 +70,7 @@ def __process_content(self, content: str) -> None:
if _is_in_notebook() and self._observer is None:
self.__observe_file_change(content)
return
self._content = content
self._content = self.__sanitize_content(content)

def __observe_file_change(self, file_path: str):
from watchdog.observers import Observer
Expand All @@ -84,13 +86,25 @@ def __parse_file_content(self, content):
with open(t.cast(str, content), "rb") as f:
file_content = f.read()
encoding = "utf-8"
if (detected_encoding := detect(file_content)["encoding"]) is not None:
if self._encoding is not None:
encoding = self._encoding
_TaipyLogger._get_logger().info(f"'{encoding}' encoding was used to decode file '{content}'.")
elif (detected_encoding := detect(file_content)["encoding"]) is not None:
encoding = detected_encoding
_TaipyLogger._get_logger().info(f"Detected '{encoding}' encoding for file '{content}'.")
self._content = file_content.decode(encoding)
else:
_TaipyLogger._get_logger().info(f"Using default '{encoding}' encoding for file '{content}'.")
self._content = self.__sanitize_content(file_content.decode(encoding))
# Save file path for error handling
self._filepath = content

def __sanitize_content(self, content: str) -> str:
# replace all CRLF (\r\n) with LF (\n)
text = re.sub(r'\r\n', '\n', content)
# replace all remaining CR (\r) with LF (\n)
text = re.sub(r'\r', '\n', content)
return text

def set_content(self, content: str) -> None:
"""Set a new page content.
Expand Down

0 comments on commit 0d2314d

Please sign in to comment.