Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/pages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ jobs:
- name: Install rust toolchain
uses: dtolnay/rust-toolchain@stable
with:
toolchain: 1.81.0
toolchain: 1.91.0

- name: Setup Node.js
uses: actions/setup-node@v4
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
- name: Install rust toolchain
uses: dtolnay/rust-toolchain@stable
with:
toolchain: 1.81.0
toolchain: 1.91.0

- name: Build
run: cargo build --verbose
Expand Down
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ default-members=["crates/org-cli"]

[workspace.package]
version = "0.1.0"
rust-version = "1.74" # also change in ci.yml
rust-version = "1.91" # also change in ci.yml
authors = ["Laith Bahodi <laithbahodi@gmail.com>"]
edition = "2021"
edition = "2024"
homepage = "https://org-rust.pages.dev"
repository = "https://github.com/hydrobeam/org-rust"
readme = "README.org"
Expand Down
6 changes: 3 additions & 3 deletions crates/org-cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "org-rust"
version = "0.1.17"
version = "0.1.18"
description = "CLI tool for converting Org-Mode documents to other formats"
keywords = ["org-mode", "parser"]
categories = ["command-line-utilities"]
Expand Down Expand Up @@ -30,8 +30,8 @@ clap = { version = "4.3.11", features=["derive"]}
clap_complete = "4.3.2"
clap_mangen = "0.2.14"
serde = { version = "1.0.196", features=["derive"]}
org-exporter = { version = "0.1.8", path = "../org-exporter", package = "org-rust-exporter" }
org-parser = { version = "0.1.5", path = "../org-parser", package = "org-rust-parser" }
org-exporter = { version = "0.1.9", path = "../org-exporter", package = "org-rust-exporter" }
org-parser = { version = "0.1.6", path = "../org-parser", package = "org-rust-parser" }


# [[bin]]
Expand Down
2 changes: 1 addition & 1 deletion crates/org-cli/build.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use clap::CommandFactory;
use clap_complete::{generate_to, Shell};
use clap_complete::{Shell, generate_to};
use std::{env, io};
include!("src/cli.rs");

Expand Down
11 changes: 3 additions & 8 deletions crates/org-cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ pub struct Cli {
pub verbose: bool,
}

#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum, Deserialize)]
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum, Deserialize, Default)]
pub enum Backend {
#[default]
Html,
Org,
}
Expand All @@ -50,7 +51,7 @@ impl Backend {
self,
parsed: &org_parser::Parser,
buf: &mut String,
conf: ConfigOptions
conf: ConfigOptions,
) -> Result<(), Vec<org_exporter::ExportError>> {
match self {
Backend::Html => org_exporter::Html::export_tree(parsed, buf, conf),
Expand All @@ -65,9 +66,3 @@ impl Backend {
}
}
}

impl Default for Backend {
fn default() -> Self {
Backend::Html
}
}
34 changes: 15 additions & 19 deletions crates/org-cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
use anyhow::bail;
use org_exporter::ConfigOptions;
use std::fs::{self, read_to_string, OpenOptions};
use std::io::{stdout, BufWriter, Read, Write};
use std::fs::{self, OpenOptions, read_to_string};
use std::io::{BufWriter, Read, Write, stdout};
use std::path::{Path, PathBuf};
use template::Template;
use types::{CliError, InpType, OutType};
use utils::{mkdir_recursively, relative_path_from};

use clap::Parser;

mod template;
use crate::cli::Backend;
mod cli;
mod template;
mod types;
mod utils;

Expand Down Expand Up @@ -45,12 +44,12 @@ fn run() -> anyhow::Result<()> {
None => config_params.backend,
r => r,
};
let output_path = if cli_params.output == "" {
let output_path = if cli_params.output.is_empty() {
config_params.output
} else {
cli_params.output
};
let input_path = if cli_params.input == "" {
let input_path = if cli_params.input.is_empty() {
config_params.input
} else {
cli_params.input
Expand All @@ -62,10 +61,7 @@ fn run() -> anyhow::Result<()> {
config_params.verbose
};

let backend = match backend {
Some(b) => b,
None => Backend::default(),
};
let backend = backend.unwrap_or_default();

let f = Path::new(&input_path);
if !f.exists() {
Expand Down Expand Up @@ -129,11 +125,11 @@ fn run() -> anyhow::Result<()> {
// main loop to export files
for file_path in &paths {
if verbose {
writeln!(stdout, "input: {}", file_path.display()).map_err(|e| CliError::from(e))?;
writeln!(stdout, "input: {}", file_path.display()).map_err(CliError::from)?;
}
if file_path.extension().is_some_and(|x| x == "org") {
let mut input_file = std::fs::File::open(file_path)
.map_err(|e| CliError::from(e).with_path(&file_path))?;
.map_err(|e| CliError::from(e).with_path(file_path))?;
let _num_bytes = input_file.read_to_string(&mut file_contents).map_err(|e| {
CliError::from(e)
.with_path(file_path)
Expand Down Expand Up @@ -165,7 +161,7 @@ fn run() -> anyhow::Result<()> {
let mut build_str = String::new();
for e in err_vec {
build_str.push_str(&e.to_string());
build_str.push_str("\n");
build_str.push('\n');
}
Err(CliError::new().with_cause(&build_str))?
}
Expand All @@ -192,7 +188,7 @@ fn run() -> anyhow::Result<()> {
// the destination we are writing to
let mut full_output_path: PathBuf;
match dest {
OutType::File(ref output_file) => {
OutType::File(output_file) => {
full_output_path = output_file.to_path_buf();
}
OutType::Dir(dest_path) => {
Expand All @@ -210,7 +206,7 @@ fn run() -> anyhow::Result<()> {
}
}

mkdir_recursively(&full_output_path.parent().unwrap())?;
mkdir_recursively(full_output_path.parent().unwrap())?;
// truncate is needed to fully overwrite file contents
OpenOptions::new()
.create(true)
Expand All @@ -222,7 +218,7 @@ fn run() -> anyhow::Result<()> {
.with_path(&full_output_path)
.with_cause("error in writing to destination file")
})?
.write(exported_content.as_bytes())?;
.write_all(exported_content.as_bytes())?;

if verbose {
writeln!(
Expand Down Expand Up @@ -257,12 +253,12 @@ fn run() -> anyhow::Result<()> {
" -- symlinked: {}\n",
&full_output_path.canonicalize()?.display()
)
.map_err(|e| CliError::from(e))?;
.map_err(CliError::from)?;
}
} else {
fs::copy(file_path, &full_output_path).map_err(|e| {
CliError::from(e)
.with_path(&file_path)
.with_path(file_path)
.with_cause("error in copying file to destination")
})?;
if verbose {
Expand All @@ -271,7 +267,7 @@ fn run() -> anyhow::Result<()> {
" -- copied: {}\n",
&full_output_path.canonicalize()?.display()
)
.map_err(|e| CliError::from(e))?;
.map_err(CliError::from)?;
}
}
}
Expand Down
9 changes: 4 additions & 5 deletions crates/org-cli/src/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ impl<'a, 'template> Template<'a, 'template> {
// ? => not greedy
let re = regex::Regex::new(r#"\{\{\{(.*?)\}\}\}"#).unwrap();
// collect all matches to {{{.*}}} regex - things we want to replace with keywords
let mut captures = re.captures_iter(&self.template_contents).map(|capture| {
let mut captures = re.captures_iter(self.template_contents).map(|capture| {
let mtch = capture.get(1).unwrap();
// we expand the range of the capture to include the {{{}}}
(mtch.start() - 3, mtch.end() + 3, mtch.as_str().trim())
Expand All @@ -93,11 +93,11 @@ impl<'a, 'template> Template<'a, 'template> {
local_items.push_str(&self.template_contents[begin..start]);

if extract == "content" {
local_items.push_str(&self.exported_content);
local_items.push_str(self.exported_content);
} else if let Some(command) = Command::check(extract) {
match command {
Command::If(cond) => {
if let Some(_) = self.p.keywords.get(&*cond) {
if self.p.keywords.contains_key(cond) {
local_items.push_str(&self.process_captures(
captures,
self.end,
Expand Down Expand Up @@ -194,8 +194,7 @@ impl<'a, 'template> Template<'a, 'template> {
}
}
Command::Include(file) => {
let include_path =
relative_path_from(&self.template_path, Path::new(file))?;
let include_path = relative_path_from(self.template_path, Path::new(file))?;
let included_template = read_to_string(&include_path).map_err(|e| {
CliError::from(e)
.with_path(&include_path)
Expand Down
10 changes: 2 additions & 8 deletions crates/org-cli/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,7 @@ pub fn mkdir_recursively(path: &Path) -> Result<(), CliError> {
})
}

pub fn relative_path_from<'a, 'b>(
src: &'a Path,
added: &'b Path,
) -> Result<Cow<'b, Path>, CliError> {
pub fn relative_path_from<'b>(src: &Path, added: &'b Path) -> Result<Cow<'b, Path>, CliError> {
if added.is_relative() {
Ok(src
.parent()
Expand All @@ -60,10 +57,7 @@ pub fn relative_path_from<'a, 'b>(
.map_err(|e| {
CliError::from(e)
.with_path(&src.parent().unwrap().join(added))
.with_cause(&format!(
"Failed to locate path from: {}",
src.display()
))
.with_cause(&format!("Failed to locate path from: {}", src.display()))
})?
.into())
} else {
Expand Down
4 changes: 2 additions & 2 deletions crates/org-exporter/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "org-rust-exporter"
version = "0.1.8"
version = "0.1.9"
description = "exporter for org mode documents parsed with `org-rust-parser`"

homepage.workspace = true
Expand All @@ -14,7 +14,7 @@ rust-version.workspace = true
[dependencies]
latex2mathml = "0.2.3"
memchr = "2.5.0"
org-parser = { version = "0.1.5", path = "../org-parser", package = "org-rust-parser" }
org-parser = { version = "0.1.6", path = "../org-parser", package = "org-rust-parser" }
phf = {version = "0.11.1", features = ["macros"]}
thiserror = "1.0.63"

Expand Down
Loading