1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::iter::IntoIterator;
use std::cmp::PartialEq;
use pulldown_cmark::{Parser, html};
pub struct Block {
comment: Vec<String>,
code: Vec<String>,
starting_line: usize,
}
pub struct Options {
pub title: String,
pub with_css: bool,
pub with_js: bool
}
impl Block {
pub fn new(starting_line: usize) -> Block {
return Block {
comment: Vec::new(),
code: Vec::new(),
starting_line,
}
}
pub fn new_file(title: &str, path: &str) -> Block {
return Block {
comment: vec![format!("**`{:}`** (in `{:}`)", title, path)],
code: vec![],
starting_line: 0,
}
}
pub fn has_code(&self) -> bool {
if self.code.len() == 0 { return false }
self.code.iter().find(|i| i.trim().len() > 0).is_some()
}
}
#[derive(PartialEq)]
enum CommentType {
Simple,
Bang,
Doc,
ANY
}
pub fn extract(path: String) -> Vec<Block> {
let file = File::open(path).expect("Unable to open the file");
let mut process_as_code = false;
let mut current_comment_type : CommentType = CommentType::ANY;
let mut blocks: Vec<Block> = Vec::new();
let mut current_block = Block::new(1);
for (idx, line) in BufReader::new(file).lines().into_iter().enumerate() {
let line_str = line.unwrap().to_string();
let stripped = line_str.trim();
if stripped.starts_with("//") {
if process_as_code {
blocks.push(current_block);
current_block = Block::new(idx + 1);
}
process_as_code = false;
} else {
process_as_code = true;
current_comment_type = CommentType::ANY;
}
if process_as_code {
current_block.code.push(line_str.to_string());
} else {
let (strip_pos, com_type) = {
if stripped.starts_with("///") {
(3, CommentType::Doc)
} else if stripped.starts_with("//!") {
(3, CommentType::Bang)
} else if stripped.starts_with("// !") {
(4, CommentType::Bang)
} else {
(2, CommentType::Simple)
}
};
let line = stripped.split_at(strip_pos).1;
if current_comment_type != CommentType::ANY &&
com_type != current_comment_type {
blocks.push(current_block);
current_block = Block::new(idx + 1);
}
current_comment_type = com_type;
current_block.comment.push(line.trim().to_string());
}
}
blocks.push(current_block);
return blocks;
}
pub fn build_html<I: IntoIterator<Item=Block>>(blocks: I, options: Options) -> String {
let mut html_output = String::new();
let css = if options.with_css {
include_str!("static/style.css").to_string()
} else {
"".to_owned()
};
let js = if options.with_js {
format!("<script>{} {} {}</script>",
include_str!("static/prism.min.js"),
include_str!("static/prism-rust.min.js"),
include_str!("static/line-numbers.js"),
)
} else {
"".to_owned()
};
for (i, block) in blocks.into_iter().enumerate() {
html_output.push_str(&format!(include_str!("static/block_before.html"), index=i));
html::push_html(&mut html_output, Parser::new(&block.comment.join("\n")));
if block.has_code() {
html_output.push_str(&format!(include_str!("static/block_code.html"),
code=block.code.join("\n").replace("<", "<"), start=block.starting_line));
}
html_output.push_str(include_str!("static/block_after.html"));
}
return format!(include_str!("static/template.html"),
title=options.title,
js=js,
css=css,
blocks=html_output);
}