summaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2022-05-16 17:56:23 +0200
committerLaurenz <laurmaedje@gmail.com>2022-05-16 17:56:36 +0200
commita741bd6b83d1e374c8218b5439e26522499cc4ae (patch)
tree796ef8b8ae2186a082f37a2aa4732c9bba7d2bdf /src/main.rs
parent6536e9e069616b862ebb774c7bef1b886c069350 (diff)
Absolute paths
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs19
1 files changed, 15 insertions, 4 deletions
diff --git a/src/main.rs b/src/main.rs
index daeff033..59ad5a71 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -22,9 +22,10 @@ USAGE:
OPTIONS:
-h, --help Print this help
+ --root <dir> Configure the root for absolute paths
ARGS:
- <input.typ> Path input Typst file
+ <input.typ> Path to input Typst file
[output.pdf] Path to output PDF
";
@@ -44,9 +45,17 @@ fn main() {
fn try_main(args: Args) -> Result<(), String> {
// Create a loader for fonts and files.
let mut loader = FsLoader::new();
+ let mut builder = Context::builder();
+ if let Some(root) = &args.root {
+ builder.root(root);
+ }
// Search for fonts in the project directory.
if let Some(dir) = args.input.parent() {
+ if args.root.is_none() {
+ builder.root(dir);
+ }
+
if dir.as_os_str().is_empty() {
// Just a filename, so directory is current directory.
loader.search_path(".");
@@ -60,7 +69,7 @@ fn try_main(args: Args) -> Result<(), String> {
// Create the context which holds loaded source files, fonts, images and
// cached artifacts.
- let mut ctx = Context::new(loader.wrap());
+ let mut ctx = builder.build(loader.wrap());
// Ensure that the source file is not overwritten.
if is_same_file(&args.input, &args.output).unwrap_or(false) {
@@ -94,6 +103,7 @@ fn try_main(args: Args) -> Result<(), String> {
struct Args {
input: PathBuf,
output: PathBuf,
+ root: Option<PathBuf>,
}
/// Parse command line arguments.
@@ -104,7 +114,8 @@ fn parse_args() -> Result<Args, String> {
std::process::exit(0);
}
- let input = args.free_from_str::<PathBuf>().map_err(|_| "missing input file")?;
+ let root = args.opt_value_from_str("--root").map_err(|_| "malformed root")?;
+ let input: PathBuf = args.free_from_str().map_err(|_| "missing input file")?;
let output = match args.opt_free_from_str().ok().flatten() {
Some(output) => output,
None => {
@@ -118,7 +129,7 @@ fn parse_args() -> Result<Args, String> {
Err("too many arguments")?;
}
- Ok(Args { input, output })
+ Ok(Args { input, output, root })
}
/// Print an application-level error (independent from a source file).