Skip to content

Commit 316541c

Browse files
authored
♻️ refactor: rustify writer utility ass_escape (#314)
1 parent 23ac243 commit 316541c

File tree

7 files changed

+37
-4
lines changed

7 files changed

+37
-4
lines changed

packages/biliass/rust/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,6 @@ fn biliass_pyo3(m: &Bound<'_, PyModule>) -> PyResult<()> {
2626
m.add_function(wrap_pyfunction!(python::py_read_comments_from_xml, m)?)?;
2727
m.add_function(wrap_pyfunction!(python::py_read_comments_from_protobuf, m)?)?;
2828
m.add_function(wrap_pyfunction!(python::py_convert_timestamp, m)?)?;
29+
m.add_function(wrap_pyfunction!(python::py_ass_escape, m)?)?;
2930
Ok(())
3031
}

packages/biliass/rust/src/python/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ mod writer;
66
pub use comment::{PyComment, PyCommentPosition};
77
pub use proto::{PyDanmakuElem, PyDmSegMobileReply};
88
pub use reader::{py_read_comments_from_protobuf, py_read_comments_from_xml};
9-
pub use writer::py_convert_timestamp;
9+
pub use writer::{py_ass_escape, py_convert_timestamp};

packages/biliass/rust/src/python/writer.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,8 @@ use pyo3::prelude::*;
66
pub fn py_convert_timestamp(timestamp: f64) -> PyResult<String> {
77
Ok(writer::utils::convert_timestamp(timestamp))
88
}
9+
10+
#[pyfunction(name = "ass_escape")]
11+
pub fn py_ass_escape(text: &str) -> PyResult<String> {
12+
Ok(writer::utils::ass_escape(text))
13+
}

packages/biliass/rust/src/writer/utils.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,28 @@ pub fn convert_timestamp(timestamp: f64) -> String {
1414

1515
format!("{}:{:02}:{:02}.{:02}", hour, minute, second, centsecond)
1616
}
17+
18+
pub fn ass_escape(text: &str) -> String {
19+
text.replace("\\", "\\\\")
20+
.replace("{", "\\{")
21+
.replace("}", "\\}")
22+
.split('\n')
23+
.map(|line| {
24+
let stripped = line.trim_matches(' ');
25+
let size = line.len();
26+
if stripped.len() == size {
27+
line.to_owned()
28+
} else {
29+
let leading_spaces = line.len() - line.trim_start_matches(' ').len();
30+
let trailing_spaces = line.len() - line.trim_end_matches(' ').len();
31+
format!(
32+
"{}{}{}",
33+
"\u{2007}".repeat(leading_spaces),
34+
stripped,
35+
"\u{2007}".repeat(trailing_spaces)
36+
)
37+
}
38+
})
39+
.collect::<Vec<_>>()
40+
.join("\\N")
41+
}

packages/biliass/src/biliass/_core.pyi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,5 @@ class Comment:
5757

5858
def read_comments_from_xml(text: str, fontsize: float) -> list[Comment]: ...
5959
def read_comments_from_protobuf(data: bytes, fontsize: float) -> list[Comment]: ...
60-
def convert_timestamp(timestamp: int) -> float: ...
60+
def convert_timestamp(timestamp: float) -> float: ...
61+
def ass_escape(text: str) -> str: ...

packages/biliass/src/biliass/biliass.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from biliass._core import (
1313
Comment,
1414
CommentPosition,
15+
ass_escape,
1516
convert_timestamp,
1617
read_comments_from_protobuf,
1718
read_comments_from_xml,
@@ -466,7 +467,7 @@ def mark_comment_row(rows: list[Comment | None], comment: Comment, row: int):
466467
pass
467468

468469

469-
def ass_escape(s):
470+
def ass_escape_py(s):
470471
def replace_leading_space(s):
471472
sstrip = s.strip(" ")
472473
slen = len(s)

0 commit comments

Comments
 (0)