-
Notifications
You must be signed in to change notification settings - Fork 228
/
main.rs
191 lines (178 loc) · 6.73 KB
/
main.rs
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
// Copyright 2015 Google Inc. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//! Command line tool to exercise pulldown-cmark.
#![forbid(unsafe_code)]
use pulldown_cmark::{html, BrokenLink, Options, Parser};
use std::env;
use std::fs::File;
use std::io::{self, Read};
use std::path::PathBuf;
fn dry_run(text: &str, opts: Options, broken_links: &mut Vec<BrokenLink<'static>>) {
let p = Parser::new_with_broken_link_callback(
text,
opts,
Some(|link: BrokenLink<'_>| {
broken_links.push(link.into_static());
None
}),
);
let count = p.count();
println!("{} events", count);
}
fn print_events(text: &str, opts: Options, broken_links: &mut Vec<BrokenLink<'static>>) {
let parser = Parser::new_with_broken_link_callback(
text,
opts,
Some(|link: BrokenLink<'_>| {
broken_links.push(link.into_static());
None
}),
)
.into_offset_iter();
for (event, range) in parser {
println!("{:?}: {:?}", range, event);
}
println!("EOF");
}
fn brief(program: &str) -> String {
format!(
"Usage: {} [options]\n\n{}",
program, "Reads markdown from file or standard input and emits HTML.",
)
}
pub fn main() -> std::io::Result<()> {
let args: Vec<_> = env::args().collect();
let mut opts = getopts::Options::new();
opts.optflag("h", "help", "this help message");
opts.optflag("d", "dry-run", "dry run, produce no output");
opts.optflag("e", "events", "print event sequence instead of rendering");
opts.optflag("T", "enable-tables", "enable GitHub-style tables");
opts.optflag("m", "enable-math", "enable LaTeX-style math");
opts.optflag("F", "enable-footnotes", "enable GitHub-style footnotes");
opts.optflag("", "enable-old-footnotes", "enable Hoedown-style footnotes");
opts.optflag(
"S",
"enable-strikethrough",
"enable GitHub-style strikethrough",
);
opts.optflag("L", "enable-tasklists", "enable GitHub-style task lists");
opts.optflag("P", "enable-smart-punctuation", "enable smart punctuation");
opts.optflag(
"H",
"enable-heading-attributes",
"enable heading attributes",
);
opts.optflag("M", "enable-metadata-blocks", "enable metadata blocks");
opts.optflag(
"R",
"reject-broken-links",
"fail if input file has broken links",
);
let matches = match opts.parse(&args[1..]) {
Ok(m) => m,
Err(f) => {
eprintln!("{}\n{}", f, opts.usage(&brief(&args[0])));
std::process::exit(1);
}
};
if matches.opt_present("help") {
println!("{}", opts.usage(&brief(&args[0])));
return Ok(());
}
let mut opts = Options::empty();
if matches.opt_present("enable-tables") {
opts.insert(Options::ENABLE_TABLES);
}
if matches.opt_present("enable-math") {
opts.insert(Options::ENABLE_MATH);
}
if matches.opt_present("enable-footnotes") {
opts.insert(Options::ENABLE_FOOTNOTES);
}
if matches.opt_present("enable-old-footnotes") {
opts.insert(Options::ENABLE_OLD_FOOTNOTES);
}
if matches.opt_present("enable-strikethrough") {
opts.insert(Options::ENABLE_STRIKETHROUGH);
}
if matches.opt_present("enable-tasklists") {
opts.insert(Options::ENABLE_TASKLISTS);
}
if matches.opt_present("enable-smart-punctuation") {
opts.insert(Options::ENABLE_SMART_PUNCTUATION);
}
if matches.opt_present("enable-heading-attributes") {
opts.insert(Options::ENABLE_HEADING_ATTRIBUTES);
}
if matches.opt_present("enable-metadata-blocks") {
opts.insert(Options::ENABLE_YAML_STYLE_METADATA_BLOCKS);
opts.insert(Options::ENABLE_PLUSES_DELIMITED_METADATA_BLOCKS);
}
let mut input = String::new();
let mut broken_links = vec![];
if !&matches.free.is_empty() {
for filename in &matches.free {
let real_path = PathBuf::from(filename);
let mut f = File::open(&real_path).expect("file not found");
f.read_to_string(&mut input)
.expect("something went wrong reading the file");
if matches.opt_present("events") {
print_events(&input, opts, &mut broken_links);
} else if matches.opt_present("dry-run") {
dry_run(&input, opts, &mut broken_links);
} else {
pulldown_cmark(&input, opts, &mut broken_links);
}
}
} else {
let _ = io::stdin().lock().read_to_string(&mut input);
if matches.opt_present("events") {
print_events(&input, opts, &mut broken_links);
} else if matches.opt_present("dry-run") {
dry_run(&input, opts, &mut broken_links);
} else {
pulldown_cmark(&input, opts, &mut broken_links);
}
}
if matches.opt_present("reject-broken-links") && !broken_links.is_empty() {
eprintln!("Error: {} broken links:", broken_links.len());
for link in broken_links {
let start = link.span.start;
let end = link.span.end;
let reference = link.reference;
eprintln!("[{start}-{end}]: {reference}");
}
std::process::exit(1);
}
Ok(())
}
pub fn pulldown_cmark(input: &str, opts: Options, broken_links: &mut Vec<BrokenLink<'static>>) {
let mut p = Parser::new_with_broken_link_callback(
input,
opts,
Some(|link: BrokenLink<'_>| {
broken_links.push(link.into_static());
None
}),
);
let stdio = io::stdout();
let buffer = std::io::BufWriter::with_capacity(1024 * 1024, stdio.lock());
let _ = html::write_html_io(buffer, &mut p);
}