]> git.proxmox.com Git - rustc.git/blame - src/doc/book/tools/src/bin/convert_quotes.rs
New upstream version 1.43.0+dfsg1
[rustc.git] / src / doc / book / tools / src / bin / convert_quotes.rs
CommitLineData
13cf67c4
XL
1use std::io;
2use std::io::{Read, Write};
3
4fn main() {
5 let mut is_in_code_block = false;
6 let mut is_in_inline_code = false;
7 let mut is_in_html_tag = false;
8
9 let mut buffer = String::new();
10 if let Err(e) = io::stdin().read_to_string(&mut buffer) {
11 panic!(e);
12 }
13
14 for line in buffer.lines() {
13cf67c4
XL
15 if line.is_empty() {
16 is_in_inline_code = false;
17 }
18 if line.starts_with("```") {
19 is_in_code_block = !is_in_code_block;
20 }
21 if is_in_code_block {
22 is_in_inline_code = false;
23 is_in_html_tag = false;
24 write!(io::stdout(), "{}\n", line).unwrap();
25 } else {
9fa01778 26 let modified_line = &mut String::new();
13cf67c4
XL
27 let mut previous_char = std::char::REPLACEMENT_CHARACTER;
28 let mut chars_in_line = line.chars();
29
30 while let Some(possible_match) = chars_in_line.next() {
9fa01778 31 // Check if inside inline code.
13cf67c4
XL
32 if possible_match == '`' {
33 is_in_inline_code = !is_in_inline_code;
34 }
9fa01778 35 // Check if inside HTML tag.
13cf67c4
XL
36 if possible_match == '<' && !is_in_inline_code {
37 is_in_html_tag = true;
38 }
39 if possible_match == '>' && !is_in_inline_code {
40 is_in_html_tag = false;
41 }
42
9fa01778 43 // Replace with right/left apostrophe/quote.
74b04a01
XL
44 let char_to_push = if possible_match == '\''
45 && !is_in_inline_code
46 && !is_in_html_tag
47 {
48 if (previous_char != std::char::REPLACEMENT_CHARACTER
49 && !previous_char.is_whitespace())
50 || previous_char == '‘'
51 {
52 '’'
53 } else {
54 '‘'
55 }
56 } else if possible_match == '"'
57 && !is_in_inline_code
58 && !is_in_html_tag
59 {
60 if (previous_char != std::char::REPLACEMENT_CHARACTER
61 && !previous_char.is_whitespace())
62 || previous_char == '“'
63 {
64 '”'
13cf67c4 65 } else {
74b04a01
XL
66 '“'
67 }
68 } else {
69 // Leave untouched.
70 possible_match
71 };
13cf67c4
XL
72 modified_line.push(char_to_push);
73 previous_char = char_to_push;
74 }
75 write!(io::stdout(), "{}\n", modified_line).unwrap();
76 }
77 }
78}