From 2b6a985a9559d0fe7d3d50dd40172e2ef084214a Mon Sep 17 00:00:00 2001 From: Victor Powell Date: Mon, 18 Nov 2024 13:57:30 -0800 Subject: [PATCH] Add support for web assembly output --- .github/workflows/ci.yml | 18 +-- .gitignore | 2 + Cargo.lock | 121 ++++++++++++++++++ Cargo.toml | 25 +++- justfile | 2 +- src/main.rs | 83 +----------- tex-fmt-lib/Cargo.toml | 29 +++++ {src => tex-fmt-lib/src}/cli.rs | 1 + {src => tex-fmt-lib/src}/comments.rs | 0 {src => tex-fmt-lib/src}/format.rs | 1 + {src => tex-fmt-lib/src}/ignore.rs | 0 {src => tex-fmt-lib/src}/indent.rs | 0 tex-fmt-lib/src/lib.rs | 94 ++++++++++++++ {src => tex-fmt-lib/src}/logging.rs | 10 +- {src => tex-fmt-lib/src}/read.rs | 0 {src => tex-fmt-lib/src}/regexes.rs | 0 {src => tex-fmt-lib/src}/subs.rs | 0 {src => tex-fmt-lib/src}/tests.rs | 0 {src => tex-fmt-lib/src}/verbatim.rs | 0 {src => tex-fmt-lib/src}/wrap.rs | 0 {src => tex-fmt-lib/src}/write.rs | 0 .../tests}/source/brackets.tex | 0 .../tests}/source/cam-thesis.cls | 0 .../tests}/source/comments.tex | 0 {tests => tex-fmt-lib/tests}/source/cv.tex | 0 .../tests}/source/document.tex | 0 .../tests}/source/environment_lines.tex | 0 .../tests}/source/heavy_wrap.tex | 0 .../source/higher_categories_thesis.bib | 0 .../source/higher_categories_thesis.tex | 0 .../tests}/source/ignore.tex | 0 {tests => tex-fmt-lib/tests}/source/lists.tex | 0 .../tests}/source/masters_dissertation.tex | 0 .../tests}/source/ociamthesis.cls | 0 .../tests}/source/phd_dissertation.tex | 0 .../tests}/source/phd_dissertation_refs.bib | 0 .../tests}/source/puthesis.cls | 0 .../tests}/source/quiver.sty | 0 .../tests}/source/readme.tex | 0 .../tests}/source/sections.tex | 0 .../tests}/source/short_document.tex | 0 .../tests}/source/tikz_network.sty | 0 .../tests}/source/unicode.tex | 0 .../tests}/source/verbatim.tex | 0 .../tests}/source/wgu-cv.cls | 0 {tests => tex-fmt-lib/tests}/source/wrap.tex | 0 .../tests}/target/brackets.tex | 0 .../tests}/target/cam-thesis.cls | 0 .../tests}/target/comments.tex | 0 {tests => tex-fmt-lib/tests}/target/cv.tex | 0 .../tests}/target/document.tex | 0 {tests => tex-fmt-lib/tests}/target/empty.tex | 0 .../tests}/target/environment_lines.tex | 0 .../tests}/target/heavy_wrap.tex | 0 .../target/higher_categories_thesis.bib | 0 .../target/higher_categories_thesis.tex | 0 .../tests}/target/ignore.tex | 0 {tests => tex-fmt-lib/tests}/target/lists.tex | 0 .../tests}/target/masters_dissertation.tex | 0 .../tests}/target/ociamthesis.cls | 0 .../tests}/target/phd_dissertation.tex | 0 .../tests}/target/phd_dissertation_refs.bib | 0 .../tests}/target/puthesis.cls | 0 .../tests}/target/quiver.sty | 0 .../tests}/target/readme.tex | 0 .../tests}/target/sections.tex | 0 .../tests}/target/short_document.tex | 0 .../tests}/target/tikz_network.sty | 0 .../tests}/target/unicode.tex | 0 .../tests}/target/verbatim.tex | 0 .../tests}/target/wgu-cv.cls | 0 {tests => tex-fmt-lib/tests}/target/wrap.tex | 0 tex-fmt-wasm/Cargo.toml | 22 ++++ tex-fmt-wasm/build.sh | 6 + tex-fmt-wasm/src/lib.rs | 40 ++++++ tex-fmt-wasm/test.mjs | 39 ++++++ 76 files changed, 395 insertions(+), 98 deletions(-) create mode 100644 tex-fmt-lib/Cargo.toml rename {src => tex-fmt-lib/src}/cli.rs (99%) rename {src => tex-fmt-lib/src}/comments.rs (100%) rename {src => tex-fmt-lib/src}/format.rs (99%) rename {src => tex-fmt-lib/src}/ignore.rs (100%) rename {src => tex-fmt-lib/src}/indent.rs (100%) create mode 100644 tex-fmt-lib/src/lib.rs rename {src => tex-fmt-lib/src}/logging.rs (95%) rename {src => tex-fmt-lib/src}/read.rs (100%) rename {src => tex-fmt-lib/src}/regexes.rs (100%) rename {src => tex-fmt-lib/src}/subs.rs (100%) rename {src => tex-fmt-lib/src}/tests.rs (100%) rename {src => tex-fmt-lib/src}/verbatim.rs (100%) rename {src => tex-fmt-lib/src}/wrap.rs (100%) rename {src => tex-fmt-lib/src}/write.rs (100%) rename {tests => tex-fmt-lib/tests}/source/brackets.tex (100%) rename {tests => tex-fmt-lib/tests}/source/cam-thesis.cls (100%) rename {tests => tex-fmt-lib/tests}/source/comments.tex (100%) rename {tests => tex-fmt-lib/tests}/source/cv.tex (100%) rename {tests => tex-fmt-lib/tests}/source/document.tex (100%) rename {tests => tex-fmt-lib/tests}/source/environment_lines.tex (100%) rename {tests => tex-fmt-lib/tests}/source/heavy_wrap.tex (100%) rename {tests => tex-fmt-lib/tests}/source/higher_categories_thesis.bib (100%) rename {tests => tex-fmt-lib/tests}/source/higher_categories_thesis.tex (100%) rename {tests => tex-fmt-lib/tests}/source/ignore.tex (100%) rename {tests => tex-fmt-lib/tests}/source/lists.tex (100%) rename {tests => tex-fmt-lib/tests}/source/masters_dissertation.tex (100%) rename {tests => tex-fmt-lib/tests}/source/ociamthesis.cls (100%) rename {tests => tex-fmt-lib/tests}/source/phd_dissertation.tex (100%) rename {tests => tex-fmt-lib/tests}/source/phd_dissertation_refs.bib (100%) rename {tests => tex-fmt-lib/tests}/source/puthesis.cls (100%) rename {tests => tex-fmt-lib/tests}/source/quiver.sty (100%) rename {tests => tex-fmt-lib/tests}/source/readme.tex (100%) rename {tests => tex-fmt-lib/tests}/source/sections.tex (100%) rename {tests => tex-fmt-lib/tests}/source/short_document.tex (100%) rename {tests => tex-fmt-lib/tests}/source/tikz_network.sty (100%) rename {tests => tex-fmt-lib/tests}/source/unicode.tex (100%) rename {tests => tex-fmt-lib/tests}/source/verbatim.tex (100%) rename {tests => tex-fmt-lib/tests}/source/wgu-cv.cls (100%) rename {tests => tex-fmt-lib/tests}/source/wrap.tex (100%) rename {tests => tex-fmt-lib/tests}/target/brackets.tex (100%) rename {tests => tex-fmt-lib/tests}/target/cam-thesis.cls (100%) rename {tests => tex-fmt-lib/tests}/target/comments.tex (100%) rename {tests => tex-fmt-lib/tests}/target/cv.tex (100%) rename {tests => tex-fmt-lib/tests}/target/document.tex (100%) rename {tests => tex-fmt-lib/tests}/target/empty.tex (100%) rename {tests => tex-fmt-lib/tests}/target/environment_lines.tex (100%) rename {tests => tex-fmt-lib/tests}/target/heavy_wrap.tex (100%) rename {tests => tex-fmt-lib/tests}/target/higher_categories_thesis.bib (100%) rename {tests => tex-fmt-lib/tests}/target/higher_categories_thesis.tex (100%) rename {tests => tex-fmt-lib/tests}/target/ignore.tex (100%) rename {tests => tex-fmt-lib/tests}/target/lists.tex (100%) rename {tests => tex-fmt-lib/tests}/target/masters_dissertation.tex (100%) rename {tests => tex-fmt-lib/tests}/target/ociamthesis.cls (100%) rename {tests => tex-fmt-lib/tests}/target/phd_dissertation.tex (100%) rename {tests => tex-fmt-lib/tests}/target/phd_dissertation_refs.bib (100%) rename {tests => tex-fmt-lib/tests}/target/puthesis.cls (100%) rename {tests => tex-fmt-lib/tests}/target/quiver.sty (100%) rename {tests => tex-fmt-lib/tests}/target/readme.tex (100%) rename {tests => tex-fmt-lib/tests}/target/sections.tex (100%) rename {tests => tex-fmt-lib/tests}/target/short_document.tex (100%) rename {tests => tex-fmt-lib/tests}/target/tikz_network.sty (100%) rename {tests => tex-fmt-lib/tests}/target/unicode.tex (100%) rename {tests => tex-fmt-lib/tests}/target/verbatim.tex (100%) rename {tests => tex-fmt-lib/tests}/target/wgu-cv.cls (100%) rename {tests => tex-fmt-lib/tests}/target/wrap.tex (100%) create mode 100644 tex-fmt-wasm/Cargo.toml create mode 100755 tex-fmt-wasm/build.sh create mode 100644 tex-fmt-wasm/src/lib.rs create mode 100644 tex-fmt-wasm/test.mjs diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 9fc454d..b510d92 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -22,7 +22,7 @@ jobs: - uses: dtolnay/rust-toolchain@stable - uses: Swatinem/rust-cache@v2 - name: Test - run: cargo test + run: cargo test --all format: name: Cargo format (${{ matrix.os }}) runs-on: ${{ matrix.os }} @@ -56,11 +56,11 @@ jobs: name: Nix build runs-on: ubuntu-latest steps: - - uses: actions/checkout@v4 - - uses: cachix/install-nix-action@v25 - with: - github_access_token: ${{ secrets.GITHUB_TOKEN }} - nix_path: nixpkgs=channel:nixos-unstable - - uses: DeterminateSystems/magic-nix-cache-action@v2 - - run: nix build - - run: nix flake check --all-systems + - uses: actions/checkout@v4 + - uses: cachix/install-nix-action@v25 + with: + github_access_token: ${{ secrets.GITHUB_TOKEN }} + nix_path: nixpkgs=channel:nixos-unstable + - uses: DeterminateSystems/magic-nix-cache-action@v2 + - run: nix build + - run: nix flake check --all-systems diff --git a/.gitignore b/.gitignore index af87737..46aee97 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ /debug/ +tex-fmt-lib/tests/target /target/ **/*.rs.bk *.pdb @@ -11,3 +12,4 @@ perf.data* *.pdf *.png cachegrind.out +tex-fmt-wasm/pkg diff --git a/Cargo.lock b/Cargo.lock index 763e436..b346741 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -60,6 +60,18 @@ dependencies = [ "windows-sys 0.59.0", ] +[[package]] +name = "bumpalo" +version = "3.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + [[package]] name = "clap" version = "4.5.21" @@ -157,6 +169,15 @@ version = "1.70.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" +[[package]] +name = "js-sys" +version = "0.3.72" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a88f1bda2bd75b0452a14784937d796722fdebfe50df998aeb3f0b7603019a9" +dependencies = [ + "wasm-bindgen", +] + [[package]] name = "lazy_static" version = "1.5.0" @@ -175,6 +196,12 @@ version = "2.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" +[[package]] +name = "once_cell" +version = "1.20.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1261fe7e33c73b354eab43b1273a57c8f967d0391e80353e51f764ac02cf6775" + [[package]] name = "proc-macro2" version = "1.0.89" @@ -247,6 +274,13 @@ dependencies = [ [[package]] name = "tex-fmt" +version = "0.0.0" +dependencies = [ + "tex-fmt-lib", +] + +[[package]] +name = "tex-fmt-lib" version = "0.4.7" dependencies = [ "clap", @@ -256,6 +290,18 @@ dependencies = [ "log", "regex", "similar", + "wasm-bindgen", + "web-sys", + "web-time", +] + +[[package]] +name = "tex-fmt-wasm" +version = "0.4.7" +dependencies = [ + "tex-fmt-lib", + "wasm-bindgen", + "web-sys", ] [[package]] @@ -270,6 +316,81 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" +[[package]] +name = "wasm-bindgen" +version = "0.2.95" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "128d1e363af62632b8eb57219c8fd7877144af57558fb2ef0368d0087bddeb2e" +dependencies = [ + "cfg-if", + "once_cell", + "wasm-bindgen-macro", +] + +[[package]] +name = "wasm-bindgen-backend" +version = "0.2.95" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb6dd4d3ca0ddffd1dd1c9c04f94b868c37ff5fac97c30b97cff2d74fce3a358" +dependencies = [ + "bumpalo", + "log", + "once_cell", + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.95" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e79384be7f8f5a9dd5d7167216f022090cf1f9ec128e6e6a482a2cb5c5422c56" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.95" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26c6ab57572f7a24a4985830b120de1594465e5d500f24afe89e16b4e833ef68" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-backend", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.95" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "65fc09f10666a9f147042251e0dda9c18f166ff7de300607007e96bdebc1068d" + +[[package]] +name = "web-sys" +version = "0.3.72" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f6488b90108c040df0fe62fa815cbdee25124641df01814dd7282749234c6112" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + +[[package]] +name = "web-time" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a6580f308b1fad9207618087a65c04e7a10bc77e02c8e84e9b00dd4b12fa0bb" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + [[package]] name = "windows-sys" version = "0.48.0" diff --git a/Cargo.toml b/Cargo.toml index ef6ed0b..5c42dc6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,15 @@ +[workspace] +members = [ + "tex-fmt-lib", + ".", + "tex-fmt-wasm", +] + [package] name = "tex-fmt" +edition = "2021" + +[workspace.package] version = "0.4.7" authors = ["William George Underwood"] license = "MIT" @@ -10,14 +20,15 @@ keywords = ["latex", "formatter"] categories = ["command-line-utilities", "development-tools"] exclude = ["tests/*", "extra/*", "*.nix", ".github/*"] +[[bin]] +name = "bin" +path = "src/main.rs" + [dependencies] -clap = { version = "4.5.21", features = ["derive"] } -colored = "2.1.0" -env_logger = "0.11.5" -lazy_static = "1.5.0" -log = "0.4.22" -regex = "1.11.1" -similar = "2.6.0" +tex-fmt-lib = { path = "./tex-fmt-lib" } + +[workspace.dependencies] +fs = "0.0.5" [profile.release] codegen-units = 1 diff --git a/justfile b/justfile index 4d268dd..3827deb 100644 --- a/justfile +++ b/justfile @@ -13,7 +13,7 @@ build: @cargo build -r test: - @cargo test + @cargo test --all doc: @cargo doc diff --git a/src/main.rs b/src/main.rs index f34e066..b4eb7bf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,81 +1,10 @@ //! tex-fmt //! An extremely fast LaTeX formatter written in Rust -#![warn(missing_docs)] -#![warn(clippy::nursery)] -#![warn(clippy::cargo)] -#![warn(clippy::missing_docs_in_private_items)] -#![warn(clippy::pedantic)] -#![allow(clippy::wildcard_imports)] -#![allow(clippy::multiple_crate_versions)] -#![allow(clippy::struct_excessive_bools)] -#![allow(clippy::module_name_repetitions)] +#[warn(unused_imports)] -use clap::Parser; -use std::fs; -use std::process::ExitCode; - -mod cli; -mod comments; -mod format; -mod ignore; -mod indent; -mod logging; -mod read; -mod regexes; -mod subs; -mod verbatim; -mod wrap; -mod write; -use crate::cli::*; -use crate::format::*; -use crate::logging::*; -use crate::read::*; -use crate::write::*; - -#[cfg(test)] -mod tests; - -#[cfg(target_family = "unix")] -/// Line ending for unix -const LINE_END: &str = "\n"; - -#[cfg(target_family = "windows")] -/// Line ending for Windows -const LINE_END: &str = "\r\n"; - -fn main() -> ExitCode { - let mut args = Cli::parse(); - init_logger(args.log_level()); - - let mut logs = Vec::::new(); - let mut exit_code = args.resolve(&mut logs); - - if exit_code == 0 { - if args.stdin { - // TODO combine the read and read_stdin functions to simplify this - if let Some((file, text)) = read_stdin(&mut logs) { - let new_text = format_file(&text, &file, &args, &mut logs); - exit_code = process_output( - &args, &file, &text, &new_text, exit_code, &mut logs, - ); - } else { - exit_code = 1; - } - } else { - for file in &args.files { - if let Some((file, text)) = read(file, &mut logs) { - let new_text = format_file(&text, &file, &args, &mut logs); - exit_code = process_output( - &args, &file, &text, &new_text, exit_code, &mut logs, - ); - } else { - exit_code = 1; - }; - } - } - } - - print_logs(&mut logs); - ExitCode::from(exit_code) -} +fn main() -> std::process::ExitCode { + let mut output: Option = None; + let exit_code = tex_fmt_lib::run(None, &mut output); + std::process::ExitCode::from(exit_code) +} \ No newline at end of file diff --git a/tex-fmt-lib/Cargo.toml b/tex-fmt-lib/Cargo.toml new file mode 100644 index 0000000..a2a6d16 --- /dev/null +++ b/tex-fmt-lib/Cargo.toml @@ -0,0 +1,29 @@ +[package] +name = "tex-fmt-lib" +description = "tex-fmt compiled to web assembly" +version.workspace = true +authors.workspace = true +license.workspace = true +repository.workspace = true +edition.workspace = true +keywords.workspace = true +categories.workspace = true +exclude.workspace = true + +[lib] +crate-type = ["cdylib", "rlib"] + +[dependencies] +clap = { version = "4.5.21", features = ["derive"] } +colored = "2.1.0" +env_logger = "0.11.5" +lazy_static = "1.5.0" +log = "0.4.22" +regex = "1.11.1" +similar = "2.6.0" +wasm-bindgen = "0.2" +web-time = "1.1.0" + +[dependencies.web-sys] +version = "0.3" +features = ["console"] diff --git a/src/cli.rs b/tex-fmt-lib/src/cli.rs similarity index 99% rename from src/cli.rs rename to tex-fmt-lib/src/cli.rs index dcf2285..d0d5d82 100644 --- a/src/cli.rs +++ b/tex-fmt-lib/src/cli.rs @@ -61,6 +61,7 @@ impl Cli { /// Ensure the provided arguments are consistent pub fn resolve(&mut self, logs: &mut Vec) -> u8 { + let mut exit_code = 0; self.verbose |= self.trace; self.print |= self.stdin; diff --git a/src/comments.rs b/tex-fmt-lib/src/comments.rs similarity index 100% rename from src/comments.rs rename to tex-fmt-lib/src/comments.rs diff --git a/src/format.rs b/tex-fmt-lib/src/format.rs similarity index 99% rename from src/format.rs rename to tex-fmt-lib/src/format.rs index 2d61685..a0d5e21 100644 --- a/src/format.rs +++ b/tex-fmt-lib/src/format.rs @@ -19,6 +19,7 @@ pub fn format_file( args: &Cli, logs: &mut Vec, ) -> String { + record_file_log(logs, Info, file, "Formatting started."); // Clean the source file and zip its lines with line numbers diff --git a/src/ignore.rs b/tex-fmt-lib/src/ignore.rs similarity index 100% rename from src/ignore.rs rename to tex-fmt-lib/src/ignore.rs diff --git a/src/indent.rs b/tex-fmt-lib/src/indent.rs similarity index 100% rename from src/indent.rs rename to tex-fmt-lib/src/indent.rs diff --git a/tex-fmt-lib/src/lib.rs b/tex-fmt-lib/src/lib.rs new file mode 100644 index 0000000..7a87503 --- /dev/null +++ b/tex-fmt-lib/src/lib.rs @@ -0,0 +1,94 @@ +//! tex-fmt-lib +//! An extremely fast LaTeX formatter written in Rust + +#![warn(missing_docs)] + +use std::fs; +use clap::Parser; +use cli::Cli; +use logging::*; +use read::*; +use format::*; +use write::*; + +mod logging; +mod cli; +mod read; +mod regexes; +mod format; +mod ignore; +mod indent; +mod comments; +mod subs; +mod verbatim; +mod wrap; +mod write; + + +#[cfg(test)] +mod tests; + +/// Default line ending +#[cfg(not(target_family = "windows"))] +const LINE_END: &str = "\n"; + +#[cfg(target_family = "windows")] +/// Line ending for Windows +const LINE_END: &str = "\r\n"; + +/// Executes the main program logic and returns an exit code. +pub fn run(input: Option<&str>, output: &mut Option) -> u8 { + + let mut args = Cli::parse(); + init_logger(args.log_level()); + + let mut logs = Vec::::new(); + + if input.is_some() { + args.stdin = true; + } + + let mut exit_code = args.resolve(&mut logs); + + if exit_code == 0 { + if let Some(text) = input { + let file = String::from(""); + let new_text = format_file(&text, &file, &args, &mut logs); + if let Some(ref mut output_text) = output { + // Write the result to output + output_text.push_str(&new_text); + exit_code = 1 + } else { + // Inspect the args to figure out what to do with the + // result. + exit_code = process_output( + &args, &file, &text, &new_text, exit_code, &mut logs, + ); + } + } else if args.stdin { + // TODO combine the read and read_stdin functions to simplify this + if let Some((file, text)) = read_stdin(&mut logs) { + let new_text = format_file(&text, &file, &args, &mut logs); + exit_code = process_output( + &args, &file, &text, &new_text, exit_code, &mut logs, + ); + } else { + exit_code = 1; + } + } else { + for file in &args.files { + if let Some((file, text)) = read(file, &mut logs) { + let new_text = format_file(&text, &file, &args, &mut logs); + exit_code = process_output( + &args, &file, &text, &new_text, exit_code, &mut logs, + ); + } else { + exit_code = 1; + }; + } + } + } + print_logs(&mut logs); + exit_code +} + diff --git a/src/logging.rs b/tex-fmt-lib/src/logging.rs similarity index 95% rename from src/logging.rs rename to tex-fmt-lib/src/logging.rs index f6bf900..1a3a8a6 100644 --- a/src/logging.rs +++ b/tex-fmt-lib/src/logging.rs @@ -8,7 +8,7 @@ use log::LevelFilter; use std::cmp::Reverse; use std::io::Write; use std::path::Path; -use std::time::Instant; +use web_time::Instant; /// Holds a log entry #[derive(Debug)] @@ -93,9 +93,9 @@ const fn get_log_color(log_level: Level) -> Color { } } -/// Start the logger +// Start the logger pub fn init_logger(level_filter: LevelFilter) { - Builder::new() + let _ = Builder::new() .filter_level(level_filter) .format(|buf, record| { writeln!( @@ -109,7 +109,9 @@ pub fn init_logger(level_filter: LevelFilter) { record.args() ) }) - .init(); + // Use try_init to avoid panic when running from web assembly + // which may call the main entry file multiple times. + .try_init(); } /// Display all of the logs collected diff --git a/src/read.rs b/tex-fmt-lib/src/read.rs similarity index 100% rename from src/read.rs rename to tex-fmt-lib/src/read.rs diff --git a/src/regexes.rs b/tex-fmt-lib/src/regexes.rs similarity index 100% rename from src/regexes.rs rename to tex-fmt-lib/src/regexes.rs diff --git a/src/subs.rs b/tex-fmt-lib/src/subs.rs similarity index 100% rename from src/subs.rs rename to tex-fmt-lib/src/subs.rs diff --git a/src/tests.rs b/tex-fmt-lib/src/tests.rs similarity index 100% rename from src/tests.rs rename to tex-fmt-lib/src/tests.rs diff --git a/src/verbatim.rs b/tex-fmt-lib/src/verbatim.rs similarity index 100% rename from src/verbatim.rs rename to tex-fmt-lib/src/verbatim.rs diff --git a/src/wrap.rs b/tex-fmt-lib/src/wrap.rs similarity index 100% rename from src/wrap.rs rename to tex-fmt-lib/src/wrap.rs diff --git a/src/write.rs b/tex-fmt-lib/src/write.rs similarity index 100% rename from src/write.rs rename to tex-fmt-lib/src/write.rs diff --git a/tests/source/brackets.tex b/tex-fmt-lib/tests/source/brackets.tex similarity index 100% rename from tests/source/brackets.tex rename to tex-fmt-lib/tests/source/brackets.tex diff --git a/tests/source/cam-thesis.cls b/tex-fmt-lib/tests/source/cam-thesis.cls similarity index 100% rename from tests/source/cam-thesis.cls rename to tex-fmt-lib/tests/source/cam-thesis.cls diff --git a/tests/source/comments.tex b/tex-fmt-lib/tests/source/comments.tex similarity index 100% rename from tests/source/comments.tex rename to tex-fmt-lib/tests/source/comments.tex diff --git a/tests/source/cv.tex b/tex-fmt-lib/tests/source/cv.tex similarity index 100% rename from tests/source/cv.tex rename to tex-fmt-lib/tests/source/cv.tex diff --git a/tests/source/document.tex b/tex-fmt-lib/tests/source/document.tex similarity index 100% rename from tests/source/document.tex rename to tex-fmt-lib/tests/source/document.tex diff --git a/tests/source/environment_lines.tex b/tex-fmt-lib/tests/source/environment_lines.tex similarity index 100% rename from tests/source/environment_lines.tex rename to tex-fmt-lib/tests/source/environment_lines.tex diff --git a/tests/source/heavy_wrap.tex b/tex-fmt-lib/tests/source/heavy_wrap.tex similarity index 100% rename from tests/source/heavy_wrap.tex rename to tex-fmt-lib/tests/source/heavy_wrap.tex diff --git a/tests/source/higher_categories_thesis.bib b/tex-fmt-lib/tests/source/higher_categories_thesis.bib similarity index 100% rename from tests/source/higher_categories_thesis.bib rename to tex-fmt-lib/tests/source/higher_categories_thesis.bib diff --git a/tests/source/higher_categories_thesis.tex b/tex-fmt-lib/tests/source/higher_categories_thesis.tex similarity index 100% rename from tests/source/higher_categories_thesis.tex rename to tex-fmt-lib/tests/source/higher_categories_thesis.tex diff --git a/tests/source/ignore.tex b/tex-fmt-lib/tests/source/ignore.tex similarity index 100% rename from tests/source/ignore.tex rename to tex-fmt-lib/tests/source/ignore.tex diff --git a/tests/source/lists.tex b/tex-fmt-lib/tests/source/lists.tex similarity index 100% rename from tests/source/lists.tex rename to tex-fmt-lib/tests/source/lists.tex diff --git a/tests/source/masters_dissertation.tex b/tex-fmt-lib/tests/source/masters_dissertation.tex similarity index 100% rename from tests/source/masters_dissertation.tex rename to tex-fmt-lib/tests/source/masters_dissertation.tex diff --git a/tests/source/ociamthesis.cls b/tex-fmt-lib/tests/source/ociamthesis.cls similarity index 100% rename from tests/source/ociamthesis.cls rename to tex-fmt-lib/tests/source/ociamthesis.cls diff --git a/tests/source/phd_dissertation.tex b/tex-fmt-lib/tests/source/phd_dissertation.tex similarity index 100% rename from tests/source/phd_dissertation.tex rename to tex-fmt-lib/tests/source/phd_dissertation.tex diff --git a/tests/source/phd_dissertation_refs.bib b/tex-fmt-lib/tests/source/phd_dissertation_refs.bib similarity index 100% rename from tests/source/phd_dissertation_refs.bib rename to tex-fmt-lib/tests/source/phd_dissertation_refs.bib diff --git a/tests/source/puthesis.cls b/tex-fmt-lib/tests/source/puthesis.cls similarity index 100% rename from tests/source/puthesis.cls rename to tex-fmt-lib/tests/source/puthesis.cls diff --git a/tests/source/quiver.sty b/tex-fmt-lib/tests/source/quiver.sty similarity index 100% rename from tests/source/quiver.sty rename to tex-fmt-lib/tests/source/quiver.sty diff --git a/tests/source/readme.tex b/tex-fmt-lib/tests/source/readme.tex similarity index 100% rename from tests/source/readme.tex rename to tex-fmt-lib/tests/source/readme.tex diff --git a/tests/source/sections.tex b/tex-fmt-lib/tests/source/sections.tex similarity index 100% rename from tests/source/sections.tex rename to tex-fmt-lib/tests/source/sections.tex diff --git a/tests/source/short_document.tex b/tex-fmt-lib/tests/source/short_document.tex similarity index 100% rename from tests/source/short_document.tex rename to tex-fmt-lib/tests/source/short_document.tex diff --git a/tests/source/tikz_network.sty b/tex-fmt-lib/tests/source/tikz_network.sty similarity index 100% rename from tests/source/tikz_network.sty rename to tex-fmt-lib/tests/source/tikz_network.sty diff --git a/tests/source/unicode.tex b/tex-fmt-lib/tests/source/unicode.tex similarity index 100% rename from tests/source/unicode.tex rename to tex-fmt-lib/tests/source/unicode.tex diff --git a/tests/source/verbatim.tex b/tex-fmt-lib/tests/source/verbatim.tex similarity index 100% rename from tests/source/verbatim.tex rename to tex-fmt-lib/tests/source/verbatim.tex diff --git a/tests/source/wgu-cv.cls b/tex-fmt-lib/tests/source/wgu-cv.cls similarity index 100% rename from tests/source/wgu-cv.cls rename to tex-fmt-lib/tests/source/wgu-cv.cls diff --git a/tests/source/wrap.tex b/tex-fmt-lib/tests/source/wrap.tex similarity index 100% rename from tests/source/wrap.tex rename to tex-fmt-lib/tests/source/wrap.tex diff --git a/tests/target/brackets.tex b/tex-fmt-lib/tests/target/brackets.tex similarity index 100% rename from tests/target/brackets.tex rename to tex-fmt-lib/tests/target/brackets.tex diff --git a/tests/target/cam-thesis.cls b/tex-fmt-lib/tests/target/cam-thesis.cls similarity index 100% rename from tests/target/cam-thesis.cls rename to tex-fmt-lib/tests/target/cam-thesis.cls diff --git a/tests/target/comments.tex b/tex-fmt-lib/tests/target/comments.tex similarity index 100% rename from tests/target/comments.tex rename to tex-fmt-lib/tests/target/comments.tex diff --git a/tests/target/cv.tex b/tex-fmt-lib/tests/target/cv.tex similarity index 100% rename from tests/target/cv.tex rename to tex-fmt-lib/tests/target/cv.tex diff --git a/tests/target/document.tex b/tex-fmt-lib/tests/target/document.tex similarity index 100% rename from tests/target/document.tex rename to tex-fmt-lib/tests/target/document.tex diff --git a/tests/target/empty.tex b/tex-fmt-lib/tests/target/empty.tex similarity index 100% rename from tests/target/empty.tex rename to tex-fmt-lib/tests/target/empty.tex diff --git a/tests/target/environment_lines.tex b/tex-fmt-lib/tests/target/environment_lines.tex similarity index 100% rename from tests/target/environment_lines.tex rename to tex-fmt-lib/tests/target/environment_lines.tex diff --git a/tests/target/heavy_wrap.tex b/tex-fmt-lib/tests/target/heavy_wrap.tex similarity index 100% rename from tests/target/heavy_wrap.tex rename to tex-fmt-lib/tests/target/heavy_wrap.tex diff --git a/tests/target/higher_categories_thesis.bib b/tex-fmt-lib/tests/target/higher_categories_thesis.bib similarity index 100% rename from tests/target/higher_categories_thesis.bib rename to tex-fmt-lib/tests/target/higher_categories_thesis.bib diff --git a/tests/target/higher_categories_thesis.tex b/tex-fmt-lib/tests/target/higher_categories_thesis.tex similarity index 100% rename from tests/target/higher_categories_thesis.tex rename to tex-fmt-lib/tests/target/higher_categories_thesis.tex diff --git a/tests/target/ignore.tex b/tex-fmt-lib/tests/target/ignore.tex similarity index 100% rename from tests/target/ignore.tex rename to tex-fmt-lib/tests/target/ignore.tex diff --git a/tests/target/lists.tex b/tex-fmt-lib/tests/target/lists.tex similarity index 100% rename from tests/target/lists.tex rename to tex-fmt-lib/tests/target/lists.tex diff --git a/tests/target/masters_dissertation.tex b/tex-fmt-lib/tests/target/masters_dissertation.tex similarity index 100% rename from tests/target/masters_dissertation.tex rename to tex-fmt-lib/tests/target/masters_dissertation.tex diff --git a/tests/target/ociamthesis.cls b/tex-fmt-lib/tests/target/ociamthesis.cls similarity index 100% rename from tests/target/ociamthesis.cls rename to tex-fmt-lib/tests/target/ociamthesis.cls diff --git a/tests/target/phd_dissertation.tex b/tex-fmt-lib/tests/target/phd_dissertation.tex similarity index 100% rename from tests/target/phd_dissertation.tex rename to tex-fmt-lib/tests/target/phd_dissertation.tex diff --git a/tests/target/phd_dissertation_refs.bib b/tex-fmt-lib/tests/target/phd_dissertation_refs.bib similarity index 100% rename from tests/target/phd_dissertation_refs.bib rename to tex-fmt-lib/tests/target/phd_dissertation_refs.bib diff --git a/tests/target/puthesis.cls b/tex-fmt-lib/tests/target/puthesis.cls similarity index 100% rename from tests/target/puthesis.cls rename to tex-fmt-lib/tests/target/puthesis.cls diff --git a/tests/target/quiver.sty b/tex-fmt-lib/tests/target/quiver.sty similarity index 100% rename from tests/target/quiver.sty rename to tex-fmt-lib/tests/target/quiver.sty diff --git a/tests/target/readme.tex b/tex-fmt-lib/tests/target/readme.tex similarity index 100% rename from tests/target/readme.tex rename to tex-fmt-lib/tests/target/readme.tex diff --git a/tests/target/sections.tex b/tex-fmt-lib/tests/target/sections.tex similarity index 100% rename from tests/target/sections.tex rename to tex-fmt-lib/tests/target/sections.tex diff --git a/tests/target/short_document.tex b/tex-fmt-lib/tests/target/short_document.tex similarity index 100% rename from tests/target/short_document.tex rename to tex-fmt-lib/tests/target/short_document.tex diff --git a/tests/target/tikz_network.sty b/tex-fmt-lib/tests/target/tikz_network.sty similarity index 100% rename from tests/target/tikz_network.sty rename to tex-fmt-lib/tests/target/tikz_network.sty diff --git a/tests/target/unicode.tex b/tex-fmt-lib/tests/target/unicode.tex similarity index 100% rename from tests/target/unicode.tex rename to tex-fmt-lib/tests/target/unicode.tex diff --git a/tests/target/verbatim.tex b/tex-fmt-lib/tests/target/verbatim.tex similarity index 100% rename from tests/target/verbatim.tex rename to tex-fmt-lib/tests/target/verbatim.tex diff --git a/tests/target/wgu-cv.cls b/tex-fmt-lib/tests/target/wgu-cv.cls similarity index 100% rename from tests/target/wgu-cv.cls rename to tex-fmt-lib/tests/target/wgu-cv.cls diff --git a/tests/target/wrap.tex b/tex-fmt-lib/tests/target/wrap.tex similarity index 100% rename from tests/target/wrap.tex rename to tex-fmt-lib/tests/target/wrap.tex diff --git a/tex-fmt-wasm/Cargo.toml b/tex-fmt-wasm/Cargo.toml new file mode 100644 index 0000000..ca568c6 --- /dev/null +++ b/tex-fmt-wasm/Cargo.toml @@ -0,0 +1,22 @@ +[package] +name = "tex-fmt-wasm" +version.workspace = true +authors = ["William George Underwood", "Victor Powell "] +license.workspace = true +repository.workspace = true +edition.workspace = true +description.workspace = true +keywords.workspace = true +categories.workspace = true +exclude.workspace = true + +[lib] +crate-type = ["cdylib"] + +[dependencies] +tex-fmt-lib = { path = "../tex-fmt-lib" } +wasm-bindgen = "0.2" + +[dependencies.web-sys] +version = "0.3" +features = ["console"] \ No newline at end of file diff --git a/tex-fmt-wasm/build.sh b/tex-fmt-wasm/build.sh new file mode 100755 index 0000000..7c85377 --- /dev/null +++ b/tex-fmt-wasm/build.sh @@ -0,0 +1,6 @@ +#!/usr/bin/env bash + +# wasm-pack needs to be configured for web or nodejs so we should +# probably update this to generate two separate packages, one for +# the browser, and one for node.js +wasm-pack build --debug --target web diff --git a/tex-fmt-wasm/src/lib.rs b/tex-fmt-wasm/src/lib.rs new file mode 100644 index 0000000..99fa776 --- /dev/null +++ b/tex-fmt-wasm/src/lib.rs @@ -0,0 +1,40 @@ +use wasm_bindgen::prelude::*; + +#[wasm_bindgen] +pub struct TexFmtResult { + code: u8, + output: Option, // Private field +} + +#[wasm_bindgen] +impl TexFmtResult { + #[wasm_bindgen(constructor)] + pub fn new(code: u8, output: Option) -> TexFmtResult { + TexFmtResult { code, output } + } + + #[wasm_bindgen(getter)] + pub fn code(&self) -> u8 { + self.code + } + + #[wasm_bindgen(getter)] + pub fn output(&self) -> String { + match self.output { + // Clone the String to return a copy + Some(ref value) => value.clone(), + None => String::new(), + } + } + + #[wasm_bindgen(setter)] + pub fn set_output(&mut self, output: String) { + self.output = Some(output); + } +} +#[wasm_bindgen] +pub fn run_tex_fmt(input: &str) -> TexFmtResult { + let mut output: Option = Some(String::new()); + let code = tex_fmt_lib::run(Some(input), &mut output); + TexFmtResult { code: code, output: output } +} diff --git a/tex-fmt-wasm/test.mjs b/tex-fmt-wasm/test.mjs new file mode 100644 index 0000000..51384e9 --- /dev/null +++ b/tex-fmt-wasm/test.mjs @@ -0,0 +1,39 @@ + +import texFmt from './pkg/tex_fmt_wasm.js'; + +const TEX_DOCUMENT = ` + +\\documentclass{article} +\\usepackage{tikz} +\\usepackage{amsmath} +\\begin{document} +\\begin{center} + +\\begin{tikzpicture} + +% Draw the main rectangle for the plot area +\\draw[thick] (0,0) rectangle (6,6); + +% Draw y-axis with tick marks up to 3700 only + +\\foreach \\y/\\label in {0/2900, 1.5/3100, 3/3300, 4.5/3500, 6/3700} { +\\draw (-0.1,\\y) -- (0,\\y); +% tick marks +\\node[left] at (-0.3,\\y) {\\label}; % y-axis labels moved further to the left +} + +\\end{tikzpicture} + +\\end{center} + +\\end{document} + +`; + +async function main() { + let result = texFmt.run_tex_fmt(TEX_DOCUMENT); + console.log(result.output); +} + +main(); +