bonus: bonus1 works (unoptimized). Removed loops page from scripting
git-svn-id: svn+ssh://atelier.inf.usi.ch/home/bevilj/group-1@212 a672b425-5310-4d7a-af5c-997e18724b81
This commit is contained in:
parent
a3897c32ea
commit
15a054297c
2 changed files with 27 additions and 59 deletions
|
@ -16,6 +16,7 @@ use std::process;
|
|||
use std::io::prelude::*;
|
||||
use regex::Regex;
|
||||
use walkdir::WalkDir;
|
||||
use std::path::Path;
|
||||
|
||||
fn read_file(path: &str) -> io::Result<String> {
|
||||
let mut file = File::open(path)?;
|
||||
|
@ -39,6 +40,14 @@ fn find_urls(html: &str) -> Vec<String> {
|
|||
results
|
||||
}
|
||||
|
||||
fn path_exists(path: &str) -> bool {
|
||||
Path::new(path).exists()
|
||||
}
|
||||
|
||||
fn is_external_url(url: &str) -> bool {
|
||||
url.starts_with("http://") || url.starts_with("https://") || url.starts_with("://")
|
||||
}
|
||||
|
||||
fn is_external_url_working(url: &str) -> bool {
|
||||
let res: Result<reqwest::Response, reqwest::Error> = reqwest::get(url);
|
||||
match res {
|
||||
|
@ -84,8 +93,24 @@ fn main() {
|
|||
|
||||
let urls: Vec<String> = find_urls(contents.as_str());
|
||||
for url in urls {
|
||||
if !is_external_url_working(&url) {
|
||||
println!("{}: '{}' is a broken link.", path, url);
|
||||
if is_external_url(&url) {
|
||||
if !is_external_url_working(&url) {
|
||||
println!("{}: '{}' is a broken link.", path, url);
|
||||
}
|
||||
} else {
|
||||
if url.starts_with("mailto:") || url.eq("#") || url.eq("{url}") {
|
||||
continue;
|
||||
} else if url.starts_with("/") {
|
||||
let lpath = format!("{}{}", dir, url);
|
||||
if !path_exists(&lpath) {
|
||||
println!("{}: '{}' is a broken link ({} does not exist).", path, url, lpath);
|
||||
}
|
||||
} else {
|
||||
let lpath = format!("{}/{}", en.path().parent().unwrap_or(Path::new("/")).to_str().unwrap(), url);
|
||||
if !path_exists(&lpath) {
|
||||
println!("{}: '{}' is a broken link ({} does not exist).", path, url, lpath);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,57 +0,0 @@
|
|||
---
|
||||
layout: page
|
||||
category-page: scripts
|
||||
category-title: Scripting
|
||||
tags: loop for while range do
|
||||
author: Marco Tereh
|
||||
title: Loops
|
||||
---
|
||||
|
||||
A very useful feature for scripting is loops; that is, the ability to write a repeating piece of code.<br>
|
||||
Loops take this form:
|
||||
<pre>
|
||||
[condition] do
|
||||
[repeated code]
|
||||
done
|
||||
</pre>
|
||||
|
||||
|
||||
In particular, there are two types of loops, for loops and while loops.<br>
|
||||
For loops are written like this:
|
||||
<pre>
|
||||
for [variable] in [list] do
|
||||
[code]
|
||||
done
|
||||
</pre>
|
||||
Their function is to <i>iterate</i> over a list. The list can take various forms, but the variable will always be
|
||||
one of its elements. In particular, it will be the first element of the list on the first iteration and advance by
|
||||
one element in every iteration after that. Most importantly, it can be accessed from inside the repeating code.<br>
|
||||
<br>
|
||||
The most basic kind of list is a numerical range, written like this:
|
||||
<pre>
|
||||
for i in {1..10}; do
|
||||
[code]
|
||||
done
|
||||
</pre>
|
||||
<code>{1..10}</code> means "the list of every number between 1 and 10 (inclusive)".
|
||||
<code>i</code> is the variable that will hold one of these numbers in every iteration.
|
||||
Here's a simple example of how to use it:
|
||||
<pre>
|
||||
for i in {1..10}; do
|
||||
echo $i;
|
||||
done
|
||||
</pre>
|
||||
<!-- TODO: insert the correct link here once the variables page is up -->
|
||||
<a href="#"><code>$i</code> is how you access the variable i</a>.
|
||||
Ranges can also count backward like this: <code>{10..1}</code>.
|
||||
Another way to say what <code>{1..10}</code> means is "The numbers between 10 and 1, <i>in increments of 1</i>".
|
||||
What if you want increments of two, though? What if you want every <i>even</i> number in a range?<br>
|
||||
Numerical ranges also allow you to specify the size of the increments or decrements between each element in the list,
|
||||
more generally called the <i>step size</i>.
|
||||
Here's an example:
|
||||
<pre>
|
||||
for i in {0..10..2}; do
|
||||
echo $i;
|
||||
done
|
||||
</pre>
|
||||
<!-- UNFINISHED -->
|
Loading…
Reference in a new issue