Skip to content

Commit 3a6ed6a

Browse files
authored
Merge pull request #75 from dyoo/ignore-codeblock
Automatically ignore codeblocks that don't have lit-strings or line comments.
2 parents 08efd0b + 4edc414 commit 3a6ed6a

File tree

3 files changed

+93
-15
lines changed

3 files changed

+93
-15
lines changed

src/bin/mdbook-gettext.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -205,10 +205,10 @@ mod tests {
205205
fn test_translate_code_block() {
206206
let catalog = create_catalog(&[(
207207
"```rust,editable\n\
208-
fn foo() {\n\n let x = 10;\n\n}\n\
208+
fn foo() {\n\n let x = \"hello\";\n\n}\n\
209209
```",
210210
"```rust,editable\n\
211-
fn FOO() {\n\n let X = 10;\n\n}\n\
211+
fn FOO() {\n\n let X = \"guten tag\";\n\n}\n\
212212
```",
213213
)]);
214214
assert_eq!(
@@ -217,7 +217,7 @@ mod tests {
217217
\n\
218218
\n\
219219
```rust,editable\n\
220-
fn foo() {\n\n let x = 10;\n\n}\n\
220+
fn foo() {\n\n let x = \"hello\";\n\n}\n\
221221
```\n\
222222
\n\
223223
Text after.\n",
@@ -226,7 +226,7 @@ mod tests {
226226
"Text before.\n\
227227
\n\
228228
```rust,editable\n\
229-
fn FOO() {\n\n let X = 10;\n\n}\n\
229+
fn FOO() {\n\n let X = \"guten tag\";\n\n}\n\
230230
```\n\
231231
\n\
232232
Text after.",

src/bin/mdbook-i18n-normalize.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,11 +490,13 @@ mod tests {
490490
fn test_normalize_code_blocks() {
491491
let catalog = create_catalog(&[(
492492
"```rust,editable\n\
493+
// Example\n\
493494
foo\n\
494495
\n\
495496
* bar\n\
496497
```",
497498
"```rust,editable\n\
499+
// Beispiel\n\
498500
FOO\n\
499501
\n\
500502
* BAR\n\
@@ -504,11 +506,13 @@ mod tests {
504506
catalog,
505507
&[exact(
506508
"```rust,editable\n\
509+
// Example\n\
507510
foo\n\
508511
\n\
509512
* bar\n\
510513
```",
511514
"```rust,editable\n\
515+
// Beispiel\n\
512516
FOO\n\
513517
\n\
514518
* BAR\n\

src/lib.rs

Lines changed: 85 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,8 @@ pub fn group_events<'a>(events: &'a [(usize, Event<'a>)]) -> Vec<Group<'a>> {
220220
Group::Skip(&events[start..idx]),
221221
ctx.clear_skip_next_group(),
222222
)
223+
} else if is_nontranslatable_codeblock_group(&events[start..idx]) {
224+
(Group::Skip(&events[start..idx]), ctx)
223225
} else {
224226
(Group::Translate(&events[start..idx]), ctx)
225227
}
@@ -329,6 +331,20 @@ fn is_comment_skip_directive(html: &str) -> bool {
329331
re.is_match(html.trim())
330332
}
331333

334+
/// Returns true if the events appear to be a codeblock without translatable text.
335+
fn is_nontranslatable_codeblock_group(events: &[(usize, Event)]) -> bool {
336+
match events {
337+
[(_, Event::Start(Tag::CodeBlock(_))), .., (_, Event::End(Tag::CodeBlock(_)))] => {
338+
let (codeblock_text, _) = reconstruct_markdown(events, None);
339+
// Heuristic to check whether the codeblock nether has a
340+
// literal string nor a line comment. We may actually
341+
// want to use a lexer here to make this more robust.
342+
!codeblock_text.contains("\"") && !codeblock_text.contains("//")
343+
}
344+
_ => false,
345+
}
346+
}
347+
332348
/// Render a slice of Markdown events back to Markdown.
333349
///
334350
/// # Examples
@@ -860,14 +876,14 @@ The document[^1] text.
860876
#[test]
861877
fn extract_messages_code_block() {
862878
assert_extract_messages(
863-
"Preamble\n```rust\nfn hello() {\n some_code()\n\n todo!()\n}\n```\nPostamble",
879+
"Preamble\n```rust\n// Example:\nfn hello() {\n some_code()\n\n todo!()\n}\n```\nPostamble",
864880
vec![
865881
(1, "Preamble"),
866882
(
867883
2,
868-
"```rust\nfn hello() {\n some_code()\n\n todo!()\n}\n```",
884+
"```rust\n// Example:\nfn hello() {\n some_code()\n\n todo!()\n}\n```",
869885
),
870-
(9, "Postamble"),
886+
(10, "Postamble"),
871887
],
872888
);
873889
}
@@ -876,15 +892,15 @@ The document[^1] text.
876892
fn extract_messages_two_code_blocks() {
877893
assert_extract_messages(
878894
"```\n\
879-
First block\n\
895+
\"First\" block\n\
880896
```\n\
881897
```\n\
882-
Second block\n\
898+
\"Second\" block\n\
883899
```\n\
884900
",
885901
vec![
886-
(1, "```\nFirst block\n```"), //
887-
(4, "```\nSecond block\n```"),
902+
(1, "```\n\"First\" block\n```"), //
903+
(4, "```\n\"Second\" block\n```"),
888904
],
889905
);
890906
}
@@ -898,6 +914,7 @@ The document[^1] text.
898914
> fn hello() {\n\
899915
> some_code()\n\
900916
>\n\
917+
> // FIXME: do something here!\n\
901918
> todo!()\n\
902919
> }\n\
903920
> ```\n\
@@ -906,9 +923,9 @@ The document[^1] text.
906923
(1, "Preamble"),
907924
(
908925
2,
909-
"```rust\nfn hello() {\n some_code()\n\n todo!()\n}\n```",
926+
"```rust\nfn hello() {\n some_code()\n\n // FIXME: do something here!\n todo!()\n}\n```",
910927
),
911-
(9, "Postamble"),
928+
(10, "Postamble"),
912929
],
913930
);
914931
}
@@ -1023,7 +1040,7 @@ The document[^1] text.
10231040
// incorrectly combine CodeBlock and HTML.
10241041
assert_extract_messages(
10251042
r#"```bob
1026-
BOB
1043+
// BOB
10271044
```
10281045
10291046
<details>
@@ -1033,7 +1050,7 @@ BOB
10331050
</details>
10341051
"#,
10351052
vec![
1036-
(1, "```bob\nBOB\n```"), //
1053+
(1, "```bob\n// BOB\n```"), //
10371054
(7, "Blah blah"),
10381055
],
10391056
);
@@ -1200,4 +1217,61 @@ not-skipped",
12001217
vec![(1, "foo "), (4, "not-skipped")],
12011218
);
12021219
}
1220+
1221+
#[test]
1222+
fn extract_messages_automatic_skipping_nontranslatable_codeblocks_simple() {
1223+
assert_extract_messages(
1224+
r#"
1225+
```
1226+
def g(x):
1227+
this_should_be_skipped_no_strings_or_comments()
1228+
```
1229+
"#,
1230+
vec![],
1231+
);
1232+
}
1233+
1234+
#[test]
1235+
fn extract_messages_automatic_skipping_nontranslatable_codeblocks() {
1236+
assert_extract_messages(
1237+
r#"
1238+
```
1239+
def f(x):
1240+
print("this should be translated")
1241+
```
1242+
1243+
1244+
```
1245+
def g(x):
1246+
but_this_should_not()
1247+
```
1248+
"#,
1249+
vec![(
1250+
2,
1251+
"```\ndef f(x):\n print(\"this should be translated\")\n```",
1252+
)],
1253+
);
1254+
}
1255+
1256+
#[test]
1257+
fn is_nontranslatable_codeblock_group_true() {
1258+
let events = extract_events(
1259+
r#"```
1260+
f(x)
1261+
```"#,
1262+
None,
1263+
);
1264+
assert!(is_nontranslatable_codeblock_group(&events));
1265+
}
1266+
1267+
#[test]
1268+
fn is_nontranslatable_codeblock_group_false() {
1269+
let events = extract_events(
1270+
r#"```
1271+
f("hello world")
1272+
```"#,
1273+
None,
1274+
);
1275+
assert!(is_nontranslatable_codeblock_group(&events) == false);
1276+
}
12031277
}

0 commit comments

Comments
 (0)