From 6a4b313eac529a477052804ad7d6b4fa0fda81d9 Mon Sep 17 00:00:00 2001 From: Gabriel Simmer Date: Sun, 13 Aug 2023 17:42:19 +0100 Subject: [PATCH 1/4] Add Flake files --- .envrc | 1 + flake.lock | 198 +++++++++++++++++++++++++++++++++++++++++++++++++++++ flake.nix | 136 ++++++++++++++++++++++++++++++++++++ 3 files changed, 335 insertions(+) create mode 100644 .envrc create mode 100644 flake.lock create mode 100644 flake.nix diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..3550a30 --- /dev/null +++ b/.envrc @@ -0,0 +1 @@ +use flake diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..2755e26 --- /dev/null +++ b/flake.lock @@ -0,0 +1,198 @@ +{ + "nodes": { + "advisory-db": { + "flake": false, + "locked": { + "lastModified": 1689698236, + "narHash": "sha256-Qz9JxGKeA3jwuj1CdK9ejMJ7VsJRdiZniF8lx4mft9s=", + "owner": "rustsec", + "repo": "advisory-db", + "rev": "4aa517564d1d06f0e79784c8ad973a59d68aa9c8", + "type": "github" + }, + "original": { + "owner": "rustsec", + "repo": "advisory-db", + "type": "github" + } + }, + "crane": { + "inputs": { + "flake-compat": "flake-compat", + "flake-utils": "flake-utils", + "nixpkgs": [ + "nixpkgs" + ], + "rust-overlay": "rust-overlay" + }, + "locked": { + "lastModified": 1688772518, + "narHash": "sha256-ol7gZxwvgLnxNSZwFTDJJ49xVY5teaSvF7lzlo3YQfM=", + "owner": "ipetkov", + "repo": "crane", + "rev": "8b08e96c9af8c6e3a2b69af5a7fa168750fcf88e", + "type": "github" + }, + "original": { + "owner": "ipetkov", + "repo": "crane", + "type": "github" + } + }, + "fenix": { + "inputs": { + "nixpkgs": [ + "nixpkgs" + ], + "rust-analyzer-src": [] + }, + "locked": { + "lastModified": 1689834114, + "narHash": "sha256-btRpL43gvbP+5+gHjaaeZ9Uv6x8LkjaO1kyvEN5rQTE=", + "owner": "nix-community", + "repo": "fenix", + "rev": "d55d856bcc50ae831f9355465f6a651034f6c8d4", + "type": "github" + }, + "original": { + "owner": "nix-community", + "repo": "fenix", + "type": "github" + } + }, + "flake-compat": { + "flake": false, + "locked": { + "lastModified": 1673956053, + "narHash": "sha256-4gtG9iQuiKITOjNQQeQIpoIB6b16fm+504Ch3sNKLd8=", + "owner": "edolstra", + "repo": "flake-compat", + "rev": "35bb57c0c8d8b62bbfd284272c928ceb64ddbde9", + "type": "github" + }, + "original": { + "owner": "edolstra", + "repo": "flake-compat", + "type": "github" + } + }, + "flake-utils": { + "inputs": { + "systems": "systems" + }, + "locked": { + "lastModified": 1687709756, + "narHash": "sha256-Y5wKlQSkgEK2weWdOu4J3riRd+kV/VCgHsqLNTTWQ/0=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "dbabf0ca0c0c4bce6ea5eaf65af5cb694d2082c7", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "flake-utils_2": { + "inputs": { + "systems": "systems_2" + }, + "locked": { + "lastModified": 1689068808, + "narHash": "sha256-6ixXo3wt24N/melDWjq70UuHQLxGV8jZvooRanIHXw0=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "919d646de7be200f3bf08cb76ae1f09402b6f9b4", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1689752456, + "narHash": "sha256-VOChdECcEI8ixz8QY+YC4JaNEFwQd1V8bA0G4B28Ki0=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "7f256d7da238cb627ef189d56ed590739f42f13b", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixpkgs-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "advisory-db": "advisory-db", + "crane": "crane", + "fenix": "fenix", + "flake-utils": "flake-utils_2", + "nixpkgs": "nixpkgs" + } + }, + "rust-overlay": { + "inputs": { + "flake-utils": [ + "crane", + "flake-utils" + ], + "nixpkgs": [ + "crane", + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1688351637, + "narHash": "sha256-CLTufJ29VxNOIZ8UTg0lepsn3X03AmopmaLTTeHDCL4=", + "owner": "oxalica", + "repo": "rust-overlay", + "rev": "f9b92316727af9e6c7fee4a761242f7f46880329", + "type": "github" + }, + "original": { + "owner": "oxalica", + "repo": "rust-overlay", + "type": "github" + } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + }, + "systems_2": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..4fedd79 --- /dev/null +++ b/flake.nix @@ -0,0 +1,136 @@ +{ + description = "Build a cargo project"; + + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; + + crane = { + url = "github:ipetkov/crane"; + inputs.nixpkgs.follows = "nixpkgs"; + }; + + fenix = { + url = "github:nix-community/fenix"; + inputs.nixpkgs.follows = "nixpkgs"; + inputs.rust-analyzer-src.follows = ""; + }; + + flake-utils.url = "github:numtide/flake-utils"; + + advisory-db = { + url = "github:rustsec/advisory-db"; + flake = false; + }; + }; + + outputs = { self, nixpkgs, crane, fenix, flake-utils, advisory-db, ... }: + flake-utils.lib.eachDefaultSystem (system: + let + pkgs = import nixpkgs { + inherit system; + }; + + inherit (pkgs) lib; + + craneLib = crane.lib.${system}; + src = craneLib.cleanCargoSource (craneLib.path ./.); + + # Common arguments can be set here to avoid repeating them later + commonArgs = { + inherit src; + buildInputs = [ + ] ++ lib.optionals pkgs.stdenv.isDarwin [ + # Additional darwin specific inputs can be set here + pkgs.libiconv + ]; + }; + + craneLibLLvmTools = craneLib.overrideToolchain + (fenix.packages.${system}.complete.withComponents [ + "cargo" + "llvm-tools" + "rustc" + ]); + + # Build *just* the cargo dependencies, so we can reuse + # all of that work (e.g. via cachix) when running in CI + cargoArtifacts = craneLib.buildDepsOnly commonArgs; + + # Build the actual crate itself, reusing the dependency + # artifacts from above. + my-crate = craneLib.buildPackage (commonArgs // { + inherit cargoArtifacts; + }); + in + { + checks = { + # Build the crate as part of `nix flake check` for convenience + inherit my-crate; + + # Run clippy (and deny all warnings) on the crate source, + # again, resuing the dependency artifacts from above. + # + # Note that this is done as a separate derivation so that + # we can block the CI if there are issues here, but not + # prevent downstream consumers from building our crate by itself. + my-crate-clippy = craneLib.cargoClippy (commonArgs // { + inherit cargoArtifacts; + cargoClippyExtraArgs = "--all-targets -- --deny warnings"; + }); + + my-crate-doc = craneLib.cargoDoc (commonArgs // { + inherit cargoArtifacts; + }); + + # Check formatting + my-crate-fmt = craneLib.cargoFmt { + inherit src; + }; + + # Audit dependencies + my-crate-audit = craneLib.cargoAudit { + inherit src advisory-db; + }; + + # Run tests with cargo-nextest + # Consider setting `doCheck = false` on `my-crate` if you do not want + # the tests to run twice + my-crate-nextest = craneLib.cargoNextest (commonArgs // { + inherit cargoArtifacts; + partitions = 1; + partitionType = "count"; + }); + } // lib.optionalAttrs (system == "x86_64-linux") { + # NB: cargo-tarpaulin only supports x86_64 systems + # Check code coverage (note: this will not upload coverage anywhere) + my-crate-coverage = craneLib.cargoTarpaulin (commonArgs // { + inherit cargoArtifacts; + }); + }; + + packages = { + default = my-crate; + my-crate-llvm-coverage = craneLibLLvmTools.cargoLlvmCov (commonArgs // { + inherit cargoArtifacts; + }); + }; + + apps.default = flake-utils.lib.mkApp { + drv = my-crate; + }; + + devShells.default = pkgs.mkShell { + inputsFrom = builtins.attrValues self.checks.${system}; + + # Additional dev-shell environment variables can be set directly + # MY_CUSTOM_DEVELOPMENT_VAR = "something else"; + + # Extra inputs can be added here + nativeBuildInputs = with pkgs; [ + cargo + rustc + rust-analyzer + ]; + }; + }); +} -- 2.46.0 From 06dffb2f570b6b050d4b1111cf9736a60c18a632 Mon Sep 17 00:00:00 2001 From: Gabriel Simmer Date: Sun, 13 Aug 2023 18:05:22 +0100 Subject: [PATCH 2/4] Add basic support for rendering --- flake.nix | 1 + src/export/html.rs | 27 +++++++++++++++++++++------ 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/flake.nix b/flake.nix index 4fedd79..e3c6254 100644 --- a/flake.nix +++ b/flake.nix @@ -38,6 +38,7 @@ # Common arguments can be set here to avoid repeating them later commonArgs = { inherit src; + cargoVendorDir = null; buildInputs = [ ] ++ lib.optionals pkgs.stdenv.isDarwin [ # Additional darwin specific inputs can be set here diff --git a/src/export/html.rs b/src/export/html.rs index e0b0bd1..af46085 100644 --- a/src/export/html.rs +++ b/src/export/html.rs @@ -60,6 +60,7 @@ pub struct DefaultHtmlHandler; impl HtmlHandler for DefaultHtmlHandler { fn start(&mut self, mut w: W, element: &Element) -> IOResult<()> { + let image_pattern = ["png", "jpeg", "jpg", "gif", "tiff", "tif", "xbm", "xpm", "pbm", "pgm", "ppm", "pnm", "svg", "webp"]; match element { // container elements Element::SpecialBlock(_) => (), @@ -121,12 +122,26 @@ impl HtmlHandler for DefaultHtmlHandler { Element::Code { value } => write!(w, "{}", HtmlEscape(value))?, Element::FnRef(_fn_ref) => (), Element::InlineCall(_) => (), - Element::Link(link) => write!( - w, - "{}", - HtmlEscape(&link.path), - HtmlEscape(link.desc.as_ref().unwrap_or(&link.path)), - )?, + Element::Link(link) => { + let link_extension = &link.path.split(".").last().unwrap(); + // Orgmode considers something an image both if the pattern + // matches /and/ the description is empty. + if image_pattern.contains(link_extension) && link.desc.is_some() { + write!( + w, + "\"{}\"", + HtmlEscape(&link.path), + HtmlEscape(link.desc.as_ref().unwrap_or(&link.path)), + )? + } else { + write!( + w, + "{}", + HtmlEscape(&link.path), + HtmlEscape(link.desc.as_ref().unwrap_or(&link.path)), + )? + } + }, Element::Macros(_macros) => (), Element::RadioTarget => (), Element::Snippet(snippet) => { -- 2.46.0 From 97d74627899c7c39f572206df0a36a186dc615d2 Mon Sep 17 00:00:00 2001 From: Gabriel Simmer Date: Sun, 13 Aug 2023 18:08:47 +0100 Subject: [PATCH 3/4] Fix indentation in files --- src/export/html.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/export/html.rs b/src/export/html.rs index af46085..de627d5 100644 --- a/src/export/html.rs +++ b/src/export/html.rs @@ -60,7 +60,7 @@ pub struct DefaultHtmlHandler; impl HtmlHandler for DefaultHtmlHandler { fn start(&mut self, mut w: W, element: &Element) -> IOResult<()> { - let image_pattern = ["png", "jpeg", "jpg", "gif", "tiff", "tif", "xbm", "xpm", "pbm", "pgm", "ppm", "pnm", "svg", "webp"]; + let image_pattern = ["png", "jpeg", "jpg", "gif", "tiff", "tif", "xbm", "xpm", "pbm", "pgm", "ppm", "pnm", "svg", "webp"]; match element { // container elements Element::SpecialBlock(_) => (), @@ -126,15 +126,15 @@ impl HtmlHandler for DefaultHtmlHandler { let link_extension = &link.path.split(".").last().unwrap(); // Orgmode considers something an image both if the pattern // matches /and/ the description is empty. - if image_pattern.contains(link_extension) && link.desc.is_some() { - write!( + if image_pattern.contains(link_extension) && link.desc.is_some() { + write!( w, "\"{}\"", HtmlEscape(&link.path), HtmlEscape(link.desc.as_ref().unwrap_or(&link.path)), )? - } else { - write!( + } else { + write!( w, "{}", HtmlEscape(&link.path), -- 2.46.0 From 660c94fc9af91b2784174cd3a09799d08cdeca82 Mon Sep 17 00:00:00 2001 From: Gabriel Simmer Date: Sun, 13 Aug 2023 18:20:48 +0100 Subject: [PATCH 4/4] Correct logic for link description --- src/export/html.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/export/html.rs b/src/export/html.rs index de627d5..82df9f0 100644 --- a/src/export/html.rs +++ b/src/export/html.rs @@ -126,7 +126,7 @@ impl HtmlHandler for DefaultHtmlHandler { let link_extension = &link.path.split(".").last().unwrap(); // Orgmode considers something an image both if the pattern // matches /and/ the description is empty. - if image_pattern.contains(link_extension) && link.desc.is_some() { + if image_pattern.contains(link_extension) && link.desc.is_none() { write!( w, "\"{}\"", -- 2.46.0