mirror of
https://github.com/Ascyii/typstar.git
synced 2026-01-01 13:34:24 -05:00
fix(anki): unicode characters in flashcards
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
import hashlib
|
||||
|
||||
from pathlib import Path
|
||||
from typing import List
|
||||
|
||||
@@ -8,7 +7,7 @@ import tree_sitter
|
||||
|
||||
class FileHandler:
|
||||
file_path: Path
|
||||
file_content: List[str]
|
||||
file_content: List[bytes]
|
||||
|
||||
def __init__(self, path: Path):
|
||||
self.file_path = path
|
||||
@@ -19,17 +18,19 @@ class FileHandler:
|
||||
return self.file_path.parent
|
||||
|
||||
def get_bytes(self) -> bytes:
|
||||
return bytes("".join(self.file_content), encoding="utf-8")
|
||||
return b"".join(self.file_content)
|
||||
|
||||
def get_file_hash(self) -> str:
|
||||
return hashlib.md5("".join(self.file_content).encode(), usedforsecurity=False).hexdigest()
|
||||
return hashlib.md5(self.get_bytes(), usedforsecurity=False).hexdigest()
|
||||
|
||||
def get_node_content(self, node: tree_sitter.Node, remove_outer=False):
|
||||
content = "".join(self.file_content[node.start_point.row : node.end_point.row + 1])[
|
||||
node.start_point.column : -(
|
||||
len(self.file_content[node.end_point.row]) - node.end_point.column
|
||||
)
|
||||
]
|
||||
def get_node_content(self, node: tree_sitter.Node, remove_outer=False) -> str:
|
||||
content = (
|
||||
b"".join(self.file_content[node.start_point.row : node.end_point.row + 1])[
|
||||
node.start_point.column : -(
|
||||
len(self.file_content[node.end_point.row]) - node.end_point.column
|
||||
)
|
||||
]
|
||||
).decode()
|
||||
return content[1:-1] if remove_outer else content
|
||||
|
||||
def update_node_content(self, node: tree_sitter.Node, value):
|
||||
@@ -38,18 +39,18 @@ class FileHandler:
|
||||
last_line = self.file_content[node.end_point.row][node.end_point.column :]
|
||||
new_lines.extend(
|
||||
(
|
||||
line + "\n"
|
||||
for line in (first_line + str(value) + last_line).split("\n")
|
||||
if line != ""
|
||||
line + b"\n"
|
||||
for line in (first_line + str(value).encode() + last_line).split(b"\n")
|
||||
if line != b""
|
||||
)
|
||||
)
|
||||
new_lines.extend(self.file_content[node.end_point.row + 1 :])
|
||||
self.file_content = new_lines
|
||||
|
||||
def read(self):
|
||||
with self.file_path.open(encoding="utf-8") as f:
|
||||
with self.file_path.open("rb") as f:
|
||||
self.file_content = f.readlines()
|
||||
|
||||
def write(self):
|
||||
with self.file_path.open("w", encoding="utf-8") as f:
|
||||
with self.file_path.open("wb") as f:
|
||||
f.writelines(self.file_content)
|
||||
|
||||
Reference in New Issue
Block a user