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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
|
use clap::Parser;
use pandoc::{
InputFormat,InputKind,OutputFormat,OutputKind,Pandoc
};
use pandoc_ast::Block;
use std::borrow::Cow;
use std::collections::HashMap;
use lazy_static::lazy_static;
use regex::{Captures,Regex};
use std::fs;
use std::io::Result;
use std::path::PathBuf;
const BASE: &str = "./";
/// A tangler for Literate Programming in Pandoc
#[derive(Parser, Debug)]
#[clap(author, version, about, long_about = None)]
struct Config {
/// Simply list entry points and exit
#[clap(short, long)]
list: bool,
/// Maximum substitution depth
#[clap(short, long, default_value_t=10, value_name="N")]
depth: u32,
/// Base output directory [default: './']
#[clap(short, long, value_name="PATH")]
output: Option<PathBuf>,
/// Limit entry points to those matching the given prefix
#[clap(short, long, value_name="PREFIX")]
target: Option<PathBuf>,
/// Input files
input: Vec<PathBuf>,
}
#[derive(Eq, Hash, PartialEq)]
enum Key {
Macro(String),
Entry(PathBuf)
}
impl Key {
fn get_path(&self) -> Option<&PathBuf> {
match self {
Self::Entry(s) => Some(&s),
Self::Macro(_) => None
}
}
}
type Blocks<'a> = HashMap<Key,Cow<'a,str>>;
fn build(
base: &Option<PathBuf>,
blocks: &Blocks,
max_depth: u32
) {
lazy_static! {
static ref MACRO: Regex =
Regex::new(
r"(?m)^([[:blank:]]*)<<([^>\s]+)>>"
).unwrap();
}
blocks
.iter()
.filter_map(|(key,code)| {
key.get_path().map(|k| (k,code))
})
.for_each(|(path,code)| {
let mut current_depth = 0;
let mut code = code.clone();
while let Cow::Owned(new_code) = MACRO.replace_all(
&code,
|caps: &Captures| {
if current_depth < max_depth {
let block = blocks
.get(&Key::Macro(caps[2].to_string()))
.unwrap_or_else(|| panic!(
"Block \"{}\" not present",
caps[2].to_string()))
.clone();
indent(block, caps[1].len())
} else {
eprintln!("Reached maximum depth, \
output might be truncated.\n\
Increase `--depth` accordingly.");
Cow::Owned(String::from(""))
}
}
) {
code = Cow::from(new_code);
current_depth += 1;
}
let file = base
.clone()
.unwrap_or(PathBuf::from(BASE))
.join(path);
write_to_file(file, &code).unwrap();
})
}
fn indent<'a>(
input: Cow<'a,str>,
indent: usize
) -> Cow<'a,str> {
if indent > 0 {
let prefix = format!("{:indent$}", "");
let size = input.len() + indent*input.lines().count();
let mut output = String::with_capacity(size);
input.lines().enumerate().for_each(|(i,line)| {
if i > 0 {
output.push('\n');
}
if !line.is_empty() {
output.push_str(&prefix);
output.push_str(line);
}
});
Cow::Owned(output)
} else {
input
}
}
fn write_to_file(
path: PathBuf, content: &str
) -> std::io::Result<()> {
if path.is_relative() {
fs::create_dir_all(path.parent().unwrap())?;
fs::write(path, content)?;
} else {
eprintln!(
"Absolute paths not supported: {}",
path.display()
)
}
Ok(())
}
fn main() -> Result<()> {
let config = Config::parse();
let target = config.target.unwrap_or_default();
let mut pandoc = Pandoc::new();
pandoc.set_input(InputKind::Files(config.input));
pandoc.set_input_format(InputFormat::Markdown, vec![]);
pandoc.set_output(OutputKind::Pipe);
pandoc.set_output_format(OutputFormat::Json, vec![]);
pandoc.add_filter(
move |json| pandoc_ast::filter(json,
|pandoc| {
let mut blocks: Blocks = HashMap::new();
pandoc.blocks.iter().for_each(|block|
if let Block::CodeBlock((id,clss,attrs), code) = block {
if !id.is_empty() {
let key = {
lazy_static! {
static ref PATH: Regex =
Regex::new(
r"^(?:[[:word:]\.-]+/)*[[:word:]\.-]+\.[[:alpha:]]+$"
).unwrap();
}
let entry = clss.contains(&String::from("entry"));
let path = attrs
.into_iter()
.find_map(|(k,p)|
if k == "path" { Some(p.clone()) } else { None });
if entry || path.is_some() || PATH.is_match(id) {
let path =
PathBuf::from(path.unwrap_or_default()).join(id);
if path.starts_with(&target) {
Some(Key::Entry(path))
} else {
None
}
} else {
Some(Key::Macro(id.to_string()))
}
};
if let Some(key) = key {
if clss.iter().any(|c| c == "override") {
blocks.insert(key, Cow::from(code));
} else {
blocks.entry(key)
.and_modify(|s| {
*s += "\n";
*s += Cow::from(code)
})
.or_insert(Cow::from(code));
}
}
} else {
eprintln!("Ignoring code block without ID:");
eprintln!("{}", indent(Cow::from(code),4));
}
}
);
if config.list {
blocks.keys().for_each(|k| match k {
Key::Entry(s) => println!("{}", s.display()),
Key::Macro(_) => {}
});
} else {
build(&config.output, &blocks, config.depth);
}
pandoc
}
)
);
pandoc.execute().unwrap();
Ok(())
}
|