1
0
Fork 0
mirror of https://github.com/rust-unofficial/awesome-rust.git synced 2024-11-20 11:36:11 -05:00

Rewrite cleanup script in rust (#626)

* Rewrite cleanup script in rust

* Remove old python script
This commit is contained in:
Timofey Barinov 2019-03-29 11:37:58 +00:00 committed by kud1ing
parent 8cdb77234e
commit 61f6ba7c25
5 changed files with 46 additions and 55 deletions

1
.gitattributes vendored
View file

@ -1 +0,0 @@
cleanup.py linguist-vendored

2
.gitignore vendored
View file

@ -1,3 +1,5 @@
.idea/
Cargo.lock
target/
cleanup
cleanup.exe

View file

@ -1,50 +0,0 @@
# -*- coding: utf-8 -*-
"""
Cleans up `README.md`.
"""
import codecs
def fix_dashes(lines):
"""
"""
fixed_lines = []
# Distinguish between the prologue and the content.
within_content = False
# Iterate over the awesome lines.
for line in lines:
# The current line is within the content.
if within_content:
# Adjust the dash.
fixed_lines.append(line.replace(u' - ', u''))
#
# The current line is within the prologue.
else:
# The prologue has ended.
if line.startswith(u'## Applications'):
within_content = True
# Leave the current line unmodified.
fixed_lines.append(line)
return fixed_lines
# end def fix_dashes
# Read the awesome file.
with codecs.open('README.md', encoding='utf8') as awesome_file:
awesome_lines = awesome_file.readlines()
# Fix the dashes.
awesome_lines = fix_dashes(awesome_lines)
# Write the awesome file.
with codecs.open('README.md', 'wb', encoding='utf8') as awesome_file:
awesome_file.writelines(awesome_lines)

44
cleanup.rs Normal file
View file

@ -0,0 +1,44 @@
// Cleans up `README.md`
// Usage: rustc cleanup.rs && ./cleanup
use std::fs;
use std::fs::File;
use std::io::Read;
fn fix_dashes(lines: Vec<String>) -> Vec<String> {
let mut fixed_lines: Vec<String> = Vec::with_capacity(lines.len());
let mut within_content = false;
for line in lines {
if within_content {
fixed_lines.push(line.replace(" - ", ""));
} else {
if line.starts_with("## Applications") {
within_content = true;
}
fixed_lines.push(line.to_string());
}
}
return fixed_lines;
}
fn main() {
// Read the awesome file.
let mut file = File::open("README.md").expect("Failed to read the file");
let mut contents = String::new();
file.read_to_string(&mut contents)
.expect("Failed to read file contents");
// Split contents into lines.
let lines: Vec<String> = contents.lines().map(|l| l.to_string()).collect();
// Fix the dashes.
let fixed_contents = fix_dashes(lines);
// Write the awesome file.
fs::write("README.md", fixed_contents.join("\n").as_bytes()).expect("Failed to write to the file");
}

View file

@ -1,4 +0,0 @@
// A dummy file to give the repository a Rust flavor.
fn main() {
println!("Hello world!");
}