Repository: Uni-Gal/Ayaka (language: rust)

Issue:
The LaTeX frontend cannot find media.
After we use vfs, the LaTeX frontend cannot find the right path of the images. We need to create a temp folder and copy all images into it.


Discussion / hints:

Fix patch:
diff --git a/Makefile b/Makefile
index 09f05a1e..fda01a2b 100644
--- a/Makefile
+++ b/Makefile
@@ -35,8 +35,9 @@ example-$(1): examples/$(1)/config.yaml plugins
 	cd bins && $$(MAKE) run FILE=$$(realpath $$<)
 example-$(1)-gui: examples/$(1).ayapack plugins
 	cd bins && $$(MAKE) run-gui FILE=$$(realpath $$<)
-examples/$(1)/config.tex: examples/$(1)/config.yaml plugins
-	cd bins && $$(MAKE) run-latex FILE=$$(realpath $$<)
+examples/$(1)/latex/config.tex: examples/$(1).ayapack plugins
+	mkdir -p $$(@D)
+	cd bins && $$(MAKE) run-latex FILE=$$(realpath $$<) TEXOUT=$$(abspath $$@)
 examples/$(1).ayapack:
 	frfs pack examples/$(1)/ examples/$(1).ayapack --magic-number-start AYAPACK --magic-number-end PACKEND
 
diff --git a/bins/Makefile b/bins/Makefile
index f574e65f..df007f12 100644
--- a/bins/Makefile
+++ b/bins/Makefile
@@ -19,4 +19,4 @@ run:
 run-gui:
 	cd ayaka-gui && $(MAKE) run FILE=$(FILE)
 run-latex:
-	cargo run --package ayaka-latex -- $(FILE) -o $(basename $(FILE)).tex
+	cargo run --package ayaka-latex -- $(FILE) -o $(TEXOUT)
diff --git a/bins/ayaka-latex/src/main.rs b/bins/ayaka-latex/src/main.rs
index 66787526..bc20624a 100644
--- a/bins/ayaka-latex/src/main.rs
+++ b/bins/ayaka-latex/src/main.rs
@@ -1,9 +1,12 @@
 mod writer;
 
-use ayaka_runtime::{anyhow::Result, *};
+use ayaka_runtime::{anyhow::Result, vfs::VfsPath, *};
 use clap::Parser;
 use flexi_logger::{LogSpecification, Logger};
-use std::ffi::OsString;
+use std::{
+    ffi::OsString,
+    path::{Path, PathBuf},
+};
 use writer::LaTeXWriter;
 
 #[derive(Debug, Parser)]
@@ -12,7 +15,7 @@ pub struct Options {
     #[clap(required = true)]
     input: Vec<OsString>,
     #[clap(short, long)]
-    output: OsString,
+    output: PathBuf,
     #[clap(short, long)]
     locale: Option<Locale>,
 }
@@ -20,6 +23,7 @@ pub struct Options {
 #[tokio::main(flavor = "current_thread")]
 async fn main() -> Result<()> {
     let opts = Options::parse();
+    let opt_root_path = opts.output.parent().unwrap();
     let spec = LogSpecification::parse("warn")?;
     let _log_handle = Logger::with(spec)
         .log_to_stdout()
@@ -70,6 +74,14 @@ async fn main() -> Result<()> {
                 if current_bg != bg {
                     current_bg = bg;
                     if let Some(bg) = &current_bg {
+                        let bg = if bg.starts_with('/') {
+                            bg.strip_prefix('/').unwrap()
+                        } else {
+                            bg
+                        };
+                        let bg_file = ctx.root_path().join(bg)?;
+                        let out_bg = opt_root_path.join(bg);
+                        copy_vfs(&bg_file, &out_bg).await?;
                         output
                             .environment_attr("figure", "!htbp", |output| async move {
                                 output.command0("centering").await?;
@@ -77,7 +89,7 @@ async fn main() -> Result<()> {
                                     .command_attr(
                                         "includegraphics",
                                         "width=1\\linewidth",
-                                        [bg.replace('\\', "/")],
+                                        [out_bg.to_string_lossy().replace('\\', "/")],
                                     )
                                     .await?;
                                 Ok(output)
@@ -119,3 +131,19 @@ async fn main() -> Result<()> {
         .await?;
     Ok(())
 }
+
+async fn copy_vfs(source: &VfsPath, target: &Path) -> Result<()> {
+    use tokio::io::AsyncWriteExt;
+    let mut source = source.open_file()?;
+    tokio::fs::create_dir_all(target.parent().unwrap()).await?;
+    let mut target = tokio::fs::File::create(target).await?;
+    let mut buffer = [0u8; 65536];
+    loop {
+        let count = source.read(&mut buffer)?;
+        if count == 0 {
+            break;
+        }
+        target.write_all(&buffer[..count]).await?;
+    }
+    Ok(())
+}


Test patch: