Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support options for svg-based export #75

Open
wants to merge 2 commits into
base: v1.x
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/target
*.pdf
tarpaulin-report.html
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

### Added

- Support Draw.io Desktop v22.1.16
- --svg-theme is now supported for SVG format
- --svg-links-target is available for SVG format
- Move to Rust 2021 to be up-to-date

### Changes
Expand Down
17 changes: 17 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
dev-local: fmt clippy-fix idioms-fix fix audit build test

configure:
@echo + $@
rustup component add clippy
cargo install cargo-audit
cargo install cargo-tarpaulin

build:
@echo + $@
cargo build
Expand Down Expand Up @@ -40,6 +46,17 @@ audit:
@echo + $@
cargo audit

coverage:
@echo + $@
cargo tarpaulin --out Html

tarpaulin-report.html:
$(MAKE) coverage

coverage-view: tarpaulin-report.html
@echo + $@
open tarpaulin-report.html

DEPS=
deps:
@echo + $@
Expand Down
14 changes: 14 additions & 0 deletions src/bin/drawio-exporter/commands/exporter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,18 @@ pub fn args() -> Vec<Arg> {
.help("Embed Images in SVG file")
.long("embed-svg-images")
.action(ArgAction::SetTrue),
Arg::new("drawio-cli-svg-svg-theme")
.help("Theme of the exported SVG image")
.value_name("theme")
.value_parser(["dark", "light"])
.default_value("light")
.long("svg-theme"),
Arg::new("drawio-cli-svg-svg-links-target")
.help("Target of links in the exported SVG image")
.value_name("target")
.value_parser(["auto", "new-win", "same-win"])
.default_value("auto")
.long("svg-links-target"),
// Drawio Desktop cli options for multiple formats
Arg::new("drawio-cli-pdf-png-svg-embed-diagram")
.help("Includes a copy of the diagram for PDF, PNG, or SVG")
Expand Down Expand Up @@ -145,6 +157,8 @@ pub fn exec(args: &ArgMatches) -> Result<()> {
.get_one::<bool>("drawio-cli-svg-embed-svg-images")
.copied()
.unwrap(),
svg_theme: args.get_one("drawio-cli-svg-svg-theme"),
svg_links_target: args.get_one("drawio-cli-svg-svg-links-target"),
embed_diagram: args
.get_one::<bool>("drawio-cli-pdf-png-svg-embed-diagram")
.copied()
Expand Down
18 changes: 14 additions & 4 deletions src/drawio_exporter/core/drawio/drawio_desktop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,8 @@ impl<'a> DrawioDesktop<'a> {
if !command_output.status.success()
|| contains("Error: ").eval(command_output_string.as_str())
{
let stderr = match String::from_utf8(command_output.stderr) {
Ok(output) => output,
Err(err) => format!("unreadable output due to {}", err),
};
let stderr = String::from_utf8(command_output.stderr)
.unwrap_or_else(|err| format!("unreadable output due to {}", err));
anyhow::bail!("fail to export using draw.io desktop\n{}", stderr.as_str());
}
}
Expand Down Expand Up @@ -85,6 +83,8 @@ pub struct ExportArguments<'a> {
pub page_index: Option<&'a String>,
pub page_range: Option<&'a String>,
pub embed_svg_images: bool,
pub svg_theme: Option<&'a String>,
pub svg_links_target: Option<&'a String>,
pub enable_plugins: bool,
}

Expand Down Expand Up @@ -160,6 +160,16 @@ impl<'a> ExportArguments<'a> {
arguments.push("--embed-svg-images");
}

if let Some(svg_theme) = self.svg_theme {
arguments.push("--svg-theme");
arguments.push(svg_theme);
}

if let Some(svg_links_target) = self.svg_links_target {
arguments.push("--svg-links-target");
arguments.push(svg_links_target);
}

if self.enable_plugins {
arguments.push("--enable-plugins");
}
Expand Down
4 changes: 4 additions & 0 deletions src/drawio_exporter/ops/exporter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ pub struct ExporterOptions<'a> {
pub quality: &'a String,
pub uncompressed: bool,
pub embed_svg_images: bool,
pub svg_theme: Option<&'a String>,
pub svg_links_target: Option<&'a String>,
pub embed_diagram: bool,
}

Expand Down Expand Up @@ -150,6 +152,8 @@ pub fn exporter(options: ExporterOptions<'_>) -> Result<()> {
page_index: Some(&position.to_string()),
page_range: None,
embed_svg_images: options.embed_svg_images,
svg_theme: options.svg_theme,
svg_links_target: options.svg_links_target,
enable_plugins: options.enable_plugins,
})?;

Expand Down
62 changes: 41 additions & 21 deletions tests/commands/exporter.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use crate::commands::utils;
use crate::DrawioExporterCommand;
use anyhow::{anyhow, Result};
use anyhow::Result;
use assert_cmd::prelude::*;
use glob::glob;
use predicates::prelude::predicate::str::contains;
use std::ffi::OsStr;

#[test]
fn export_nothing() -> Result<()> {
Expand Down Expand Up @@ -37,7 +36,12 @@ fn export_files_with_a_name_collision() -> Result<()> {
.success()
.stdout(contains(output));

Ok(())
let output_files = vec![
"name-collision-Page-1.pdf",
//"name-Page-1.pdf",
];

utils::check_generate_files(&mut drawio_exporter, "pdf", output_files)
}

#[test]
Expand Down Expand Up @@ -72,7 +76,18 @@ fn export_files_from_a_folders_tree() -> Result<()> {
.success()
.stdout(contains(output));

Ok(())
let output_files = vec![
"file1-Page-1.pdf",
"file1-Page-2.pdf",
"file2.1-Page-1.pdf",
"file2.1-Page-2.pdf",
"file2.2-Page-1.pdf",
"file2.2-Page-2.pdf",
"file3-Page-1.pdf",
"file3-Page-2.pdf",
];

utils::check_generate_files(&mut drawio_exporter, "pdf", output_files)
}

#[test]
Expand All @@ -92,7 +107,12 @@ fn export_file_with_spaces() -> Result<()> {
.success()
.stdout(contains(output));

Ok(())
let output_files = vec![
"file with spaces-Page-1.pdf",
"file with spaces-Page-2.pdf",
];

utils::check_generate_files(&mut drawio_exporter, "pdf", output_files)
}

#[test]
Expand Down Expand Up @@ -180,19 +200,7 @@ fn export_file_with_illegal_characters() -> Result<()> {
"names-Page-comma.pdf",
];

let os_output_files = output_files.iter().map(OsStr::new).collect::<Vec<&OsStr>>();

let search_pattern = format!("{}/**/*.pdf", &drawio_exporter.current_dir.display());
let missing_files: Vec<_> = glob(search_pattern.as_str())?
.map(|entry| entry.unwrap())
.filter(|entry| !os_output_files.contains(&entry.as_path().file_name().unwrap()))
.collect();

if !missing_files.is_empty() {
return Err(anyhow!(format!("Missing files: {:#?}", missing_files)));
}

Ok(())
utils::check_generate_files(&mut drawio_exporter, "pdf", output_files)
}

#[test]
Expand All @@ -216,7 +224,14 @@ fn export_file_using_shapes() -> Result<()> {
.success()
.stdout(contains(output));

Ok(())
let output_files = vec![
"shapes-AWS.pdf",
"shapes-Azure.pdf",
"shapes-GCP.pdf",
"shapes-K8S.pdf",
];

utils::check_generate_files(&mut drawio_exporter, "pdf", output_files)
}

#[test]
Expand Down Expand Up @@ -252,5 +267,10 @@ fn export_file_from_vscode() -> Result<()> {
.success()
.stdout(contains(output));

Ok(())
let output_files = vec![
"vscode-Page-1.pdf",
"vscode-Page-2.pdf",
];

utils::check_generate_files(&mut drawio_exporter, "pdf", output_files)
}
51 changes: 51 additions & 0 deletions tests/commands/exporter_option_remove_page_suffix.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use crate::commands::utils;
use crate::DrawioExporterCommand;
use anyhow::Result;
use assert_cmd::prelude::*;
use predicates::prelude::predicate::str::contains;

#[test]
fn export_single_diagram_file_with_no_option_remove_page_suffix() -> Result<()> {
let mut drawio_exporter = DrawioExporterCommand::new_using_data("single_page", true)?;

let output = "+ export file : single_page/single-page.drawio
- export page 1 : Page-1
\\ generate pdf file";

drawio_exporter
.cmd
.arg(&drawio_exporter.current_dir)
.assert()
.success()
.stdout(contains(output));

let output_files = vec![
"single-page-Page-1.pdf",
];

utils::check_generate_files(&mut drawio_exporter, "pdf", output_files)
}

#[test]
fn export_single_diagram_file_with_option_remove_page_suffix() -> Result<()> {
let mut drawio_exporter = DrawioExporterCommand::new_using_data("single_page", true)?;

let output = "+ export file : single_page/single-page.drawio
- export page 1 : Page-1
\\ generate pdf file";

drawio_exporter
.cmd
.arg("--remove-page-suffix")
.arg(&drawio_exporter.current_dir)
.assert()
.success()
.stdout(contains(output));

let output_files = vec![
"single-page.pdf",
];

utils::check_generate_files(&mut drawio_exporter, "pdf", output_files)
}

Loading
Loading