]>
git.proxmox.com Git - rustc.git/blob - src/doc/book/2018-edition/tools/src/bin/remove_links.rs
1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
14 use std
::io
::{Read, Write}
;
15 use regex
::{Regex, Captures}
;
16 use std
::collections
::HashSet
;
19 let mut buffer
= String
::new();
20 if let Err(e
) = io
::stdin().read_to_string(&mut buffer
) {
24 let mut refs
= HashSet
::new();
26 // capture all links and link references
27 let regex
= r
"\[([^\]]+)\](?:(?:\[([^\]]+)\])|(?:\([^\)]+\)))(?i)<!-- ignore -->";
28 let link_regex
= Regex
::new(regex
).unwrap();
29 let first_pass
= link_regex
.replace_all(&buffer
, |caps
: &Captures
| {
31 // save the link reference we want to delete
32 if let Some(reference
) = caps
.at(2) {
33 refs
.insert(reference
.to_owned());
36 // put the link title back
37 caps
.at(1).unwrap().to_owned()
40 // search for the references we need to delete
41 let ref_regex
= Regex
::new(r
"\n\[([^\]]+)\]:\s.*\n").unwrap();
42 let out
= ref_regex
.replace_all(&first_pass
, |caps
: &Captures
| {
43 let capture
= caps
.at(1).unwrap().to_owned();
45 // check if we've marked this reference for deletion...
46 if refs
.contains(capture
.as_str()) {
47 return "".to_string();
50 //... else we put back everything we captured
51 caps
.at(0).unwrap().to_owned()
54 write
!(io
::stdout(), "{}", out
).unwrap();