Skip to content

Commit

Permalink
Normalize soft breaks to space
Browse files Browse the repository at this point in the history
This change makes the extracted messages ignore any wrapping done for
readability of the Markdown source. So

    This is a
    paragraph.

and

    This is a paragraph.

now becomes the same message in the PO file. This makes it possible
for people to freely reformat the source files, without having to
worry about invalidating existing translations.

Part of #19.
  • Loading branch information
mgeisler committed May 1, 2023
1 parent 57154e4 commit fa5b201
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 29 deletions.
24 changes: 4 additions & 20 deletions src/bin/mdbook-gettext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,20 +210,8 @@ mod tests {
fn test_translate_multiple_paragraphs_extra_newlines() {
// Notice how the translated paragraphs have more lines.
let catalog = create_catalog(&[
(
"first\n\
paragraph",
"FIRST\n\
TRANSLATED\n\
PARAGRAPH",
),
(
"last\n\
paragraph",
"LAST\n\
TRANSLATED\n\
PARAGRAPH",
),
("first paragraph", "FIRST TRANSLATED PARAGRAPH"),
("last paragraph", "LAST TRANSLATED PARAGRAPH"),
]);
// Paragraph separation is normalized when translating.
assert_eq!(
Expand All @@ -236,13 +224,9 @@ mod tests {
paragraph\n",
&catalog
),
"FIRST\n\
TRANSLATED\n\
PARAGRAPH\n\
"FIRST TRANSLATED PARAGRAPH\n\
\n\
LAST\n\
TRANSLATED\n\
PARAGRAPH"
LAST TRANSLATED PARAGRAPH"
);
}

Expand Down
3 changes: 1 addition & 2 deletions src/bin/mdbook-xgettext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,7 @@ mod tests {
&[
"The Foo Chapter",
"How to Foo",
"The first paragraph about Foo.\n\
Still the first paragraph."
"The first paragraph about Foo. Still the first paragraph.",
]
);

Expand Down
18 changes: 11 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ use pulldown_cmark_to_cmark::{cmark_resume_with_options, Options, State};
/// vec![
/// (1, Event::Start(Tag::Paragraph)),
/// (1, Event::Text("Hello,".into())),
/// (1, Event::SoftBreak),
/// (1, Event::Text(" ".into())),
/// (2, Event::Text("world!".into())),
/// (1, Event::End(Tag::Paragraph)),
/// ]
Expand Down Expand Up @@ -75,6 +75,10 @@ pub fn extract_events<'a>(text: &'a str, state: Option<State<'static>>) -> Vec<(
.into_offset_iter()
.map(|(event, range)| {
let lineno = offsets.partition_point(|&o| o < range.start) + 1;
let event = match event {
Event::SoftBreak => Event::Text(" ".into()),
_ => event,
};
(lineno, event)
})
.collect(),
Expand Down Expand Up @@ -285,8 +289,8 @@ pub fn reconstruct_markdown(
/// assert_eq!(
/// messages,
/// vec![
/// (1, "Hello, this is a\nlist in a quote.".into()),
/// (4, "This is the second\nparagraph.".into()),
/// (1, "Hello, this is a list in a quote.".into()),
/// (4, "This is the second paragraph.".into()),
/// ],
/// );
/// ```
Expand Down Expand Up @@ -349,7 +353,7 @@ mod tests {
\n\
Second paragraph.",
vec![
(1, "This is\nthe first\nparagraph.🦀"),
(1, "This is the first paragraph.🦀"),
(5, "Second paragraph."),
],
);
Expand All @@ -363,7 +367,7 @@ mod tests {
\n\
This is the\n\
first paragraph.",
vec![(4, "This is the\nfirst paragraph.")],
vec![(4, "This is the first paragraph.")],
);
}

Expand All @@ -374,7 +378,7 @@ mod tests {
a paragraph.\n\
\n\
\n",
vec![(1, "This is\na paragraph.")],
vec![(1, "This is a paragraph.")],
);
}

Expand Down Expand Up @@ -594,7 +598,7 @@ The document[^1] text.
"#,
vec![
(1, "Item 1."),
(2, "Item 2,\ntwo lines."),
(2, "Item 2, two lines."),
(5, "Sub 1."),
(6, "Sub 2."),
],
Expand Down

0 comments on commit fa5b201

Please sign in to comment.