@@ -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\n fn hello() {\n some_code()\n \n todo!()\n }\n ```\n Postamble" ,
879+ "Preamble\n ```rust\n // Example: \ n fn hello() {\n some_code()\n \n todo!()\n }\n ```\n Postamble" ,
864880 vec ! [
865881 ( 1 , "Preamble" ) ,
866882 (
867883 2 ,
868- "```rust\n fn hello() {\n some_code()\n \n todo!()\n }\n ```" ,
884+ "```rust\n // Example: \ n fn 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 , "```\n First block\n ```" ) , //
887- ( 4 , "```\n Second 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\n fn hello() {\n some_code()\n \n todo!()\n }\n ```" ,
926+ "```rust\n fn 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\n BOB \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+ "```\n def 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