Skip to content

Commit 4676fae

Browse files
committed
change structure
1 parent f7d433e commit 4676fae

23 files changed

+226
-613
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ book/*
1010
**/*.rs.bk
1111
cargo.lock
1212
images/*
13-
13+
src/*.rs
1414

1515
#Added by cargo
1616
#

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ edition = "2018"
66

77
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
88

9-
[dependencies]
9+
[build-dependencies]
1010
pulldown-cmark = "0.8.0"
1111
walkdir = "2.3.1"
1212

13-
[dev-dependencies]
13+
[dependencies]
1414
plotters = "0.3.0"
1515
chrono = "0.4.15"

src/main.rs renamed to build.rs

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
11
use pulldown_cmark::{Parser, Event, Tag, CodeBlockKind};
22
use std::fs::File;
33
use std::io::{Read, Write};
4-
use std::path::{Path, PathBuf, Component};
5-
fn extract_code<P:AsRef<Path>>(path:P) -> Result<(), Box<dyn std::error::Error>> {
6-
let mut prefix = "examples/".to_string();
7-
for name in path.as_ref().components().filter_map(|c| match c {
4+
use std::path::{Path, Component};
5+
fn make_prefix(path: &Path) -> String {
6+
let mut prefix = String::new();
7+
for name in path.components().filter_map(|c| match c {
88
Component::Normal(name) => Some(name),
99
_ => None,
1010
}).skip(1).filter_map(|name| name.to_str()) {
1111
let name = AsRef::<Path>::as_ref(name).file_stem().unwrap().to_str().unwrap();
1212
prefix.push_str(name);
1313
prefix.push_str("_");
1414
}
15+
prefix
16+
}
17+
fn extract_code<P:AsRef<Path>>(path:P) -> Result<usize, Box<dyn std::error::Error>> {
18+
let mut prefix = "src/".to_string();
1519

20+
prefix.push_str(&make_prefix(path.as_ref()));
21+
1622
let mut content = String::new();
1723
File::open(path)?.read_to_string(&mut content)?;
1824

@@ -28,7 +34,7 @@ fn extract_code<P:AsRef<Path>>(path:P) -> Result<(), Box<dyn std::error::Error>>
2834
Event::Code(ref code) | Event::Text(ref code) if in_rust_code => {
2935
let filename = format!("{}{}.rs", prefix, counter);
3036
let mut code = code.clone().into_string();
31-
code.push_str("\n#[test]\nfn etnry_point() { main(); } \n");
37+
code.push_str("\npub fn entry_point() { main(); } \n");
3238
File::create(&filename)?.write_all(code.as_bytes())?;
3339
counter += 1;
3440
in_rust_code = false;
@@ -37,16 +43,25 @@ fn extract_code<P:AsRef<Path>>(path:P) -> Result<(), Box<dyn std::error::Error>>
3743
}
3844
}
3945

40-
Ok(())
46+
Ok(counter)
4147
}
4248

4349
fn main() -> Result<(), Box<dyn std::error::Error>> {
50+
let mut main_file = File::create("src/lib.rs")?;
51+
4452
for entry in walkdir::WalkDir::new("markdown").into_iter().filter_map(Result::ok).filter(|x| x.file_type().is_file()) {
53+
println!("cargo:rerun-if-changed={:?}", entry.path());
4554
if let Some(ext) = entry.path().extension() {
4655
if ext == "md" {
47-
extract_code(entry.path())?;
56+
let prefix = make_prefix(entry.path());
57+
for i in 1..extract_code(entry.path())? {
58+
main_file.write_all(format!("pub mod {}{};\n", prefix, i).as_bytes())?;
59+
main_file.write_all(format!("#[test]\n fn {}{}_entry_point(){{ {}{}::entry_point(); }}\n\n", prefix, i, prefix, i).as_bytes())?;
60+
}
4861
}
4962
}
5063
}
64+
65+
5166
Ok(())
5267
}

examples/basic_basic_data_plotting_1.rs

Lines changed: 0 additions & 23 deletions
This file was deleted.

examples/basic_basic_data_plotting_2.rs

Lines changed: 0 additions & 94 deletions
This file was deleted.

examples/basic_basic_data_plotting_3.rs

Lines changed: 0 additions & 30 deletions
This file was deleted.

examples/basic_basic_data_plotting_4.rs

Lines changed: 0 additions & 28 deletions
This file was deleted.

examples/basic_basic_data_plotting_5.rs

Lines changed: 0 additions & 28 deletions
This file was deleted.

examples/basic_basic_data_plotting_6.rs

Lines changed: 0 additions & 39 deletions
This file was deleted.

examples/basic_basic_data_plotting_7.rs

Lines changed: 0 additions & 36 deletions
This file was deleted.

0 commit comments

Comments
 (0)