├── .github └── workflows │ └── ci.yml ├── .gitignore ├── CHANGELOG.md ├── Cargo.lock ├── Cargo.toml ├── LICENSE-APACHE ├── LICENSE-MIT ├── README.md ├── example ├── Cargo.toml └── src │ └── lib.rs ├── rust-toolchain.toml └── src ├── lib.rs ├── minimal_template.svg └── textproc.rs /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | on: [push, pull_request] 2 | 3 | name: CI 4 | 5 | jobs: 6 | clippy: 7 | name: Clippy 8 | runs-on: ubuntu-20.04 9 | timeout-minutes: 10 10 | steps: 11 | - uses: actions/checkout@v4 12 | - name: cargo clippy 13 | run: cargo clippy -p svgbobdoc --all-features 14 | 15 | test: 16 | name: Test 17 | runs-on: ubuntu-20.04 18 | timeout-minutes: 10 19 | steps: 20 | - uses: actions/checkout@v4 21 | - name: cargo test 22 | run: cargo test -p svgbobdoc 23 | - name: cargo test with `enable` 24 | run: cargo test -p svgbobdoc --features enable 25 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | target 2 | **/*.rs.bk 3 | .DS_Store 4 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to this project will be documented in this file. 4 | 5 | The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) 6 | and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). 7 | 8 | ## [Unreleased] 9 | 10 | ## [0.3.0] - 2022-03-16 11 | 12 | - Pinned `svgbob` to 0.6.6. (Fixes the compilation failure due to a breaking dependency change in `svgbob` 0.6.6.) 13 | 14 | ## [0.3.0-alpha.4] - 2021-12-18 15 | 16 | - Added Consolas to the diagram font list. 17 | - The macro now adds `transform: translate(0.5px, 0.5px)` to the root `svg` element to ensure pixel snapping on a low-DPI monitor. 18 | - **Breaking** `#[svgbobdoc::transform]` was removed. 19 | - **Breaking** Renamed `svgbobdoc::transform_mdstr!` to `svgbobdoc::transform!`. 20 | 21 | ## [0.3.0-alpha.3] - 2021-11-28 22 | 23 | - Fixed the UAX #11 width calculation of texts including characters that are translated to XML entity references by `svgbob`. 24 | 25 | ## [0.3.0-alpha.2] - 2021-11-23 26 | 27 | - **Breaking** Updated `svgbob` to 0.6. 28 | - The version specification of `syn` is now more specific. 29 | - Removed the dependency on `regex`. 30 | - When the `enable` Cargo feature is not enabled, SVG images with unprocessed texts are produced instead of not transforming code blocks at all. 31 | - Added support for `base64` 0.13. 32 | - Code blocks now accept a link label (e.g., `~~~svgbob,[link-label]`, which can be inserted to a different location by `![link-label]`). 33 | 34 | ## [0.3.0-alpha.1] - 2021-07-23 35 | 36 | - **Breaking** Updated `svgbob` to 0.5. 37 | - **Breaking** Code blocks indented by more than three spaces are now processed. 38 | - **Breaking** The macros are no-op by default. The `enable` Cargo feature must be enabled for the transformation to take place. 39 | - Added `svgbobdoc::transform_mdstr!`. 40 | - `#[svgbobdoc::transform]` is now deprecated. 41 | 42 | ## [0.2.3] - 2020-10-22 43 | 44 | - Fixed the version specification of `lazy_static`. 45 | - Unrecognized forms of `#[doc ...]` are now ignored. Examples: 46 | - `#[doc(cfg(windows))` ([rust-lang/rust#43781]) 47 | - `#[doc(include = "external-file.md")]` ([rust-lang/rust#44732]) 48 | - `#[doc(alias = "x")]` ([rust-lang/rust#50146]) 49 | 50 | [rust-lang/rust#43781]: https://github.com/rust-lang/rust/issues/43781 51 | [rust-lang/rust#44732]: https://github.com/rust-lang/rust/issues/44732 52 | [rust-lang/rust#50146]: https://github.com/rust-lang/rust/issues/50146 53 | 54 | ## [0.2.2] - 2020-03-30 55 | 56 | - Upgraded `syn`, `quote`, and `proc-macro2` to 1.x. 57 | - Added support for `base64` 0.11 and 0.12. 58 | 59 | ## [0.2.1] - 2020-01-08 60 | 61 | - Added a maintenance status badge 62 | 63 | ## [0.2.0] - 2019-05-30 64 | 65 | - **Breaking** Renamed `#[svgbobdoc::doc]` to `#[svgbobdoc::transform]` because it doesn't generate `#[doc = ...]` by itself but just transforms existing `#[doc = ...]`s. 66 | - When attached to a struct, union, or enum, `#[transform]` now transforms its fields as they cannot have attribute macros by themselves. 67 | 68 | ## 0.1.0 - 2019-05-29 69 | 70 | - Initial release. 71 | 72 | [Unreleased]: https://github.com/yvt/svgbobdoc/compare/0.3.0-alpha.4...HEAD 73 | [0.3.0-alpha.4]: https://github.com/yvt/svgbobdoc/compare/0.3.0-alpha.3...0.3.0-alpha.4 74 | [0.3.0-alpha.3]: https://github.com/yvt/svgbobdoc/compare/0.3.0-alpha.2...0.3.0-alpha.3 75 | [0.3.0-alpha.2]: https://github.com/yvt/svgbobdoc/compare/0.3.0-alpha.1...0.3.0-alpha.2 76 | [0.3.0-alpha.1]: https://github.com/yvt/svgbobdoc/compare/0.2.3...0.3.0-alpha.1 77 | [0.2.3]: https://github.com/yvt/svgbobdoc/compare/0.2.2...0.2.3 78 | [0.2.2]: https://github.com/yvt/svgbobdoc/compare/0.2.1...0.2.2 79 | [0.2.1]: https://github.com/yvt/svgbobdoc/compare/0.2.0...0.2.1 80 | [0.2.0]: https://github.com/yvt/svgbobdoc/compare/0.1.0...0.2.0 81 | -------------------------------------------------------------------------------- /Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | version = 3 4 | 5 | [[package]] 6 | name = "ahash" 7 | version = "0.8.11" 8 | source = "registry+https://github.com/rust-lang/crates.io-index" 9 | checksum = "e89da841a80418a9b391ebaea17f5c112ffaaa96f621d2c285b5174da76b9011" 10 | dependencies = [ 11 | "cfg-if", 12 | "once_cell", 13 | "version_check", 14 | "zerocopy", 15 | ] 16 | 17 | [[package]] 18 | name = "allocator-api2" 19 | version = "0.2.18" 20 | source = "registry+https://github.com/rust-lang/crates.io-index" 21 | checksum = "5c6cb57a04249c6480766f7f7cef5467412af1490f8d1e243141daddada3264f" 22 | 23 | [[package]] 24 | name = "approx" 25 | version = "0.5.1" 26 | source = "registry+https://github.com/rust-lang/crates.io-index" 27 | checksum = "cab112f0a86d568ea0e627cc1d6be74a1e9cd55214684db5561995f6dad897c6" 28 | dependencies = [ 29 | "num-traits", 30 | ] 31 | 32 | [[package]] 33 | name = "arrayvec" 34 | version = "0.7.6" 35 | source = "registry+https://github.com/rust-lang/crates.io-index" 36 | checksum = "7c02d123df017efcdfbd739ef81735b36c5ba83ec3c59c80a9d7ecc718f92e50" 37 | 38 | [[package]] 39 | name = "autocfg" 40 | version = "1.4.0" 41 | source = "registry+https://github.com/rust-lang/crates.io-index" 42 | checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26" 43 | 44 | [[package]] 45 | name = "base64" 46 | version = "0.22.1" 47 | source = "registry+https://github.com/rust-lang/crates.io-index" 48 | checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6" 49 | 50 | [[package]] 51 | name = "bitflags" 52 | version = "1.3.2" 53 | source = "registry+https://github.com/rust-lang/crates.io-index" 54 | checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" 55 | 56 | [[package]] 57 | name = "bstr" 58 | version = "1.10.0" 59 | source = "registry+https://github.com/rust-lang/crates.io-index" 60 | checksum = "40723b8fb387abc38f4f4a37c09073622e41dd12327033091ef8950659e6dc0c" 61 | dependencies = [ 62 | "memchr", 63 | "regex-automata", 64 | "serde", 65 | ] 66 | 67 | [[package]] 68 | name = "bumpalo" 69 | version = "3.14.0" 70 | source = "registry+https://github.com/rust-lang/crates.io-index" 71 | checksum = "7f30e7476521f6f8af1a1c4c0b8cc94f0bee37d91763d0ca2665f299b6cd8aec" 72 | 73 | [[package]] 74 | name = "bytemuck" 75 | version = "1.19.0" 76 | source = "registry+https://github.com/rust-lang/crates.io-index" 77 | checksum = "8334215b81e418a0a7bdb8ef0849474f40bb10c8b71f1c4ed315cff49f32494d" 78 | 79 | [[package]] 80 | name = "cfg-if" 81 | version = "1.0.0" 82 | source = "registry+https://github.com/rust-lang/crates.io-index" 83 | checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" 84 | 85 | [[package]] 86 | name = "doc-comment" 87 | version = "0.3.3" 88 | source = "registry+https://github.com/rust-lang/crates.io-index" 89 | checksum = "fea41bba32d969b513997752735605054bc0dfa92b4c56bf1189f2e174be7a10" 90 | 91 | [[package]] 92 | name = "downcast-rs" 93 | version = "1.2.1" 94 | source = "registry+https://github.com/rust-lang/crates.io-index" 95 | checksum = "75b325c5dbd37f80359721ad39aca5a29fb04c89279657cffdda8736d0c0b9d2" 96 | 97 | [[package]] 98 | name = "either" 99 | version = "1.13.0" 100 | source = "registry+https://github.com/rust-lang/crates.io-index" 101 | checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0" 102 | 103 | [[package]] 104 | name = "equivalent" 105 | version = "1.0.1" 106 | source = "registry+https://github.com/rust-lang/crates.io-index" 107 | checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" 108 | 109 | [[package]] 110 | name = "example" 111 | version = "0.1.0" 112 | dependencies = [ 113 | "svgbobdoc", 114 | ] 115 | 116 | [[package]] 117 | name = "hashbrown" 118 | version = "0.14.5" 119 | source = "registry+https://github.com/rust-lang/crates.io-index" 120 | checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1" 121 | dependencies = [ 122 | "ahash", 123 | "allocator-api2", 124 | ] 125 | 126 | [[package]] 127 | name = "hashbrown" 128 | version = "0.15.0" 129 | source = "registry+https://github.com/rust-lang/crates.io-index" 130 | checksum = "1e087f84d4f86bf4b218b927129862374b72199ae7d8657835f1e89000eea4fb" 131 | 132 | [[package]] 133 | name = "indexmap" 134 | version = "2.6.0" 135 | source = "registry+https://github.com/rust-lang/crates.io-index" 136 | checksum = "707907fe3c25f5424cce2cb7e1cbcafee6bdbe735ca90ef77c29e84591e5b9da" 137 | dependencies = [ 138 | "equivalent", 139 | "hashbrown 0.15.0", 140 | ] 141 | 142 | [[package]] 143 | name = "itertools" 144 | version = "0.11.0" 145 | source = "registry+https://github.com/rust-lang/crates.io-index" 146 | checksum = "b1c173a5686ce8bfa551b3563d0c2170bf24ca44da99c7ca4bfdab5418c3fe57" 147 | dependencies = [ 148 | "either", 149 | ] 150 | 151 | [[package]] 152 | name = "itoa" 153 | version = "1.0.11" 154 | source = "registry+https://github.com/rust-lang/crates.io-index" 155 | checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b" 156 | 157 | [[package]] 158 | name = "js-sys" 159 | version = "0.3.72" 160 | source = "registry+https://github.com/rust-lang/crates.io-index" 161 | checksum = "6a88f1bda2bd75b0452a14784937d796722fdebfe50df998aeb3f0b7603019a9" 162 | dependencies = [ 163 | "wasm-bindgen", 164 | ] 165 | 166 | [[package]] 167 | name = "libm" 168 | version = "0.2.11" 169 | source = "registry+https://github.com/rust-lang/crates.io-index" 170 | checksum = "8355be11b20d696c8f18f6cc018c4e372165b1fa8126cef092399c9951984ffa" 171 | 172 | [[package]] 173 | name = "log" 174 | version = "0.4.22" 175 | source = "registry+https://github.com/rust-lang/crates.io-index" 176 | checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24" 177 | 178 | [[package]] 179 | name = "longest-increasing-subsequence" 180 | version = "0.1.0" 181 | source = "registry+https://github.com/rust-lang/crates.io-index" 182 | checksum = "b3bd0dd2cd90571056fdb71f6275fada10131182f84899f4b2a916e565d81d86" 183 | 184 | [[package]] 185 | name = "matrixmultiply" 186 | version = "0.3.9" 187 | source = "registry+https://github.com/rust-lang/crates.io-index" 188 | checksum = "9380b911e3e96d10c1f415da0876389aaf1b56759054eeb0de7df940c456ba1a" 189 | dependencies = [ 190 | "autocfg", 191 | "rawpointer", 192 | ] 193 | 194 | [[package]] 195 | name = "memchr" 196 | version = "2.7.4" 197 | source = "registry+https://github.com/rust-lang/crates.io-index" 198 | checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" 199 | 200 | [[package]] 201 | name = "mt-dom" 202 | version = "0.59.2" 203 | source = "registry+https://github.com/rust-lang/crates.io-index" 204 | checksum = "6d9add2bc13d876029af62a1082409a658ffa8d029c442226515e099994acb41" 205 | dependencies = [ 206 | "longest-increasing-subsequence", 207 | ] 208 | 209 | [[package]] 210 | name = "nalgebra" 211 | version = "0.32.6" 212 | source = "registry+https://github.com/rust-lang/crates.io-index" 213 | checksum = "7b5c17de023a86f59ed79891b2e5d5a94c705dbe904a5b5c9c952ea6221b03e4" 214 | dependencies = [ 215 | "approx", 216 | "matrixmultiply", 217 | "nalgebra-macros", 218 | "num-complex", 219 | "num-rational", 220 | "num-traits", 221 | "simba", 222 | "typenum", 223 | ] 224 | 225 | [[package]] 226 | name = "nalgebra-macros" 227 | version = "0.2.2" 228 | source = "registry+https://github.com/rust-lang/crates.io-index" 229 | checksum = "254a5372af8fc138e36684761d3c0cdb758a4410e938babcff1c860ce14ddbfc" 230 | dependencies = [ 231 | "proc-macro2", 232 | "quote", 233 | "syn", 234 | ] 235 | 236 | [[package]] 237 | name = "num-complex" 238 | version = "0.4.6" 239 | source = "registry+https://github.com/rust-lang/crates.io-index" 240 | checksum = "73f88a1307638156682bada9d7604135552957b7818057dcef22705b4d509495" 241 | dependencies = [ 242 | "num-traits", 243 | ] 244 | 245 | [[package]] 246 | name = "num-derive" 247 | version = "0.4.2" 248 | source = "registry+https://github.com/rust-lang/crates.io-index" 249 | checksum = "ed3955f1a9c7c0c15e092f9c887db08b1fc683305fdf6eb6684f22555355e202" 250 | dependencies = [ 251 | "proc-macro2", 252 | "quote", 253 | "syn", 254 | ] 255 | 256 | [[package]] 257 | name = "num-integer" 258 | version = "0.1.46" 259 | source = "registry+https://github.com/rust-lang/crates.io-index" 260 | checksum = "7969661fd2958a5cb096e56c8e1ad0444ac2bbcd0061bd28660485a44879858f" 261 | dependencies = [ 262 | "num-traits", 263 | ] 264 | 265 | [[package]] 266 | name = "num-rational" 267 | version = "0.4.2" 268 | source = "registry+https://github.com/rust-lang/crates.io-index" 269 | checksum = "f83d14da390562dca69fc84082e73e548e1ad308d24accdedd2720017cb37824" 270 | dependencies = [ 271 | "num-integer", 272 | "num-traits", 273 | ] 274 | 275 | [[package]] 276 | name = "num-traits" 277 | version = "0.2.19" 278 | source = "registry+https://github.com/rust-lang/crates.io-index" 279 | checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" 280 | dependencies = [ 281 | "autocfg", 282 | "libm", 283 | ] 284 | 285 | [[package]] 286 | name = "once_cell" 287 | version = "1.20.2" 288 | source = "registry+https://github.com/rust-lang/crates.io-index" 289 | checksum = "1261fe7e33c73b354eab43b1273a57c8f967d0391e80353e51f764ac02cf6775" 290 | 291 | [[package]] 292 | name = "parry2d" 293 | version = "0.13.8" 294 | source = "registry+https://github.com/rust-lang/crates.io-index" 295 | checksum = "9fd94bf962ead112f14223469aac6f76e3c24e2c399e348f638924498b238c56" 296 | dependencies = [ 297 | "approx", 298 | "arrayvec", 299 | "bitflags", 300 | "downcast-rs", 301 | "either", 302 | "nalgebra", 303 | "num-derive", 304 | "num-traits", 305 | "rustc-hash", 306 | "simba", 307 | "slab", 308 | "smallvec", 309 | "spade", 310 | ] 311 | 312 | [[package]] 313 | name = "paste" 314 | version = "1.0.15" 315 | source = "registry+https://github.com/rust-lang/crates.io-index" 316 | checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a" 317 | 318 | [[package]] 319 | name = "phf" 320 | version = "0.11.2" 321 | source = "registry+https://github.com/rust-lang/crates.io-index" 322 | checksum = "ade2d8b8f33c7333b51bcf0428d37e217e9f32192ae4772156f65063b8ce03dc" 323 | dependencies = [ 324 | "phf_macros", 325 | "phf_shared", 326 | ] 327 | 328 | [[package]] 329 | name = "phf_generator" 330 | version = "0.11.2" 331 | source = "registry+https://github.com/rust-lang/crates.io-index" 332 | checksum = "48e4cc64c2ad9ebe670cb8fd69dd50ae301650392e81c05f9bfcb2d5bdbc24b0" 333 | dependencies = [ 334 | "phf_shared", 335 | "rand", 336 | ] 337 | 338 | [[package]] 339 | name = "phf_macros" 340 | version = "0.11.2" 341 | source = "registry+https://github.com/rust-lang/crates.io-index" 342 | checksum = "3444646e286606587e49f3bcf1679b8cef1dc2c5ecc29ddacaffc305180d464b" 343 | dependencies = [ 344 | "phf_generator", 345 | "phf_shared", 346 | "proc-macro2", 347 | "quote", 348 | "syn", 349 | ] 350 | 351 | [[package]] 352 | name = "phf_shared" 353 | version = "0.11.2" 354 | source = "registry+https://github.com/rust-lang/crates.io-index" 355 | checksum = "90fcb95eef784c2ac79119d1dd819e162b5da872ce6f3c3abe1e8ca1c082f72b" 356 | dependencies = [ 357 | "siphasher", 358 | ] 359 | 360 | [[package]] 361 | name = "pom" 362 | version = "3.4.0" 363 | source = "registry+https://github.com/rust-lang/crates.io-index" 364 | checksum = "6c972d8f86e943ad532d0b04e8965a749ad1d18bb981a9c7b3ae72fe7fd7744b" 365 | dependencies = [ 366 | "bstr", 367 | ] 368 | 369 | [[package]] 370 | name = "proc-macro-error" 371 | version = "1.0.4" 372 | source = "registry+https://github.com/rust-lang/crates.io-index" 373 | checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" 374 | dependencies = [ 375 | "proc-macro-error-attr", 376 | "proc-macro2", 377 | "quote", 378 | "version_check", 379 | ] 380 | 381 | [[package]] 382 | name = "proc-macro-error-attr" 383 | version = "1.0.4" 384 | source = "registry+https://github.com/rust-lang/crates.io-index" 385 | checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" 386 | dependencies = [ 387 | "proc-macro2", 388 | "quote", 389 | "version_check", 390 | ] 391 | 392 | [[package]] 393 | name = "proc-macro2" 394 | version = "1.0.89" 395 | source = "registry+https://github.com/rust-lang/crates.io-index" 396 | checksum = "f139b0662de085916d1fb67d2b4169d1addddda1919e696f3252b740b629986e" 397 | dependencies = [ 398 | "unicode-ident", 399 | ] 400 | 401 | [[package]] 402 | name = "proc-macro2-diagnostics" 403 | version = "0.10.1" 404 | source = "registry+https://github.com/rust-lang/crates.io-index" 405 | checksum = "af066a9c399a26e020ada66a034357a868728e72cd426f3adcd35f80d88d88c8" 406 | dependencies = [ 407 | "proc-macro2", 408 | "quote", 409 | "syn", 410 | "version_check", 411 | "yansi", 412 | ] 413 | 414 | [[package]] 415 | name = "quote" 416 | version = "1.0.37" 417 | source = "registry+https://github.com/rust-lang/crates.io-index" 418 | checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af" 419 | dependencies = [ 420 | "proc-macro2", 421 | ] 422 | 423 | [[package]] 424 | name = "rand" 425 | version = "0.8.5" 426 | source = "registry+https://github.com/rust-lang/crates.io-index" 427 | checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" 428 | dependencies = [ 429 | "rand_core", 430 | ] 431 | 432 | [[package]] 433 | name = "rand_core" 434 | version = "0.6.4" 435 | source = "registry+https://github.com/rust-lang/crates.io-index" 436 | checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" 437 | 438 | [[package]] 439 | name = "rawpointer" 440 | version = "0.2.1" 441 | source = "registry+https://github.com/rust-lang/crates.io-index" 442 | checksum = "60a357793950651c4ed0f3f52338f53b2f809f32d83a07f72909fa13e4c6c1e3" 443 | 444 | [[package]] 445 | name = "regex-automata" 446 | version = "0.4.8" 447 | source = "registry+https://github.com/rust-lang/crates.io-index" 448 | checksum = "368758f23274712b504848e9d5a6f010445cc8b87a7cdb4d7cbee666c1288da3" 449 | 450 | [[package]] 451 | name = "robust" 452 | version = "1.1.0" 453 | source = "registry+https://github.com/rust-lang/crates.io-index" 454 | checksum = "cbf4a6aa5f6d6888f39e980649f3ad6b666acdce1d78e95b8a2cb076e687ae30" 455 | 456 | [[package]] 457 | name = "rstml" 458 | version = "0.11.2" 459 | source = "registry+https://github.com/rust-lang/crates.io-index" 460 | checksum = "fe542870b8f59dd45ad11d382e5339c9a1047cde059be136a7016095bbdefa77" 461 | dependencies = [ 462 | "proc-macro2", 463 | "proc-macro2-diagnostics", 464 | "quote", 465 | "syn", 466 | "syn_derive", 467 | "thiserror", 468 | ] 469 | 470 | [[package]] 471 | name = "rustc-hash" 472 | version = "1.1.0" 473 | source = "registry+https://github.com/rust-lang/crates.io-index" 474 | checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2" 475 | 476 | [[package]] 477 | name = "ryu" 478 | version = "1.0.18" 479 | source = "registry+https://github.com/rust-lang/crates.io-index" 480 | checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f" 481 | 482 | [[package]] 483 | name = "safe_arch" 484 | version = "0.7.2" 485 | source = "registry+https://github.com/rust-lang/crates.io-index" 486 | checksum = "c3460605018fdc9612bce72735cba0d27efbcd9904780d44c7e3a9948f96148a" 487 | dependencies = [ 488 | "bytemuck", 489 | ] 490 | 491 | [[package]] 492 | name = "sauron" 493 | version = "0.60.7" 494 | source = "registry+https://github.com/rust-lang/crates.io-index" 495 | checksum = "fe6af589283e59ab1eaa7ce85a362e97f8479bee13a29038d4fbc2d77eeb3cc1" 496 | dependencies = [ 497 | "sauron-core", 498 | "sauron-macro", 499 | ] 500 | 501 | [[package]] 502 | name = "sauron-core" 503 | version = "0.60.7" 504 | source = "registry+https://github.com/rust-lang/crates.io-index" 505 | checksum = "234c15a59efffbf4ad1ab77f7b2375f3b7ada9587e776e30a68791aa76685c02" 506 | dependencies = [ 507 | "cfg-if", 508 | "doc-comment", 509 | "js-sys", 510 | "log", 511 | "mt-dom", 512 | "once_cell", 513 | "phf", 514 | "serde-wasm-bindgen", 515 | "thiserror", 516 | "wasm-bindgen", 517 | "wasm-bindgen-futures", 518 | "web-sys", 519 | ] 520 | 521 | [[package]] 522 | name = "sauron-macro" 523 | version = "0.60.7" 524 | source = "registry+https://github.com/rust-lang/crates.io-index" 525 | checksum = "69b79cfc63bae10d77025e053552475e51a69176f2f4262e5250db4a5204db1b" 526 | dependencies = [ 527 | "once_cell", 528 | "phf", 529 | "proc-macro2", 530 | "quote", 531 | "rstml", 532 | "sauron-core", 533 | "syn", 534 | ] 535 | 536 | [[package]] 537 | name = "serde" 538 | version = "1.0.214" 539 | source = "registry+https://github.com/rust-lang/crates.io-index" 540 | checksum = "f55c3193aca71c12ad7890f1785d2b73e1b9f63a0bbc353c08ef26fe03fc56b5" 541 | dependencies = [ 542 | "serde_derive", 543 | ] 544 | 545 | [[package]] 546 | name = "serde-wasm-bindgen" 547 | version = "0.5.0" 548 | source = "registry+https://github.com/rust-lang/crates.io-index" 549 | checksum = "f3b143e2833c57ab9ad3ea280d21fd34e285a42837aeb0ee301f4f41890fa00e" 550 | dependencies = [ 551 | "js-sys", 552 | "serde", 553 | "wasm-bindgen", 554 | ] 555 | 556 | [[package]] 557 | name = "serde_derive" 558 | version = "1.0.214" 559 | source = "registry+https://github.com/rust-lang/crates.io-index" 560 | checksum = "de523f781f095e28fa605cdce0f8307e451cc0fd14e2eb4cd2e98a355b147766" 561 | dependencies = [ 562 | "proc-macro2", 563 | "quote", 564 | "syn", 565 | ] 566 | 567 | [[package]] 568 | name = "serde_json" 569 | version = "1.0.132" 570 | source = "registry+https://github.com/rust-lang/crates.io-index" 571 | checksum = "d726bfaff4b320266d395898905d0eba0345aae23b54aee3a737e260fd46db03" 572 | dependencies = [ 573 | "itoa", 574 | "memchr", 575 | "ryu", 576 | "serde", 577 | ] 578 | 579 | [[package]] 580 | name = "simba" 581 | version = "0.8.1" 582 | source = "registry+https://github.com/rust-lang/crates.io-index" 583 | checksum = "061507c94fc6ab4ba1c9a0305018408e312e17c041eb63bef8aa726fa33aceae" 584 | dependencies = [ 585 | "approx", 586 | "num-complex", 587 | "num-traits", 588 | "paste", 589 | "wide", 590 | ] 591 | 592 | [[package]] 593 | name = "siphasher" 594 | version = "0.3.11" 595 | source = "registry+https://github.com/rust-lang/crates.io-index" 596 | checksum = "38b58827f4464d87d377d175e90bf58eb00fd8716ff0a62f80356b5e61555d0d" 597 | 598 | [[package]] 599 | name = "slab" 600 | version = "0.4.9" 601 | source = "registry+https://github.com/rust-lang/crates.io-index" 602 | checksum = "8f92a496fb766b417c996b9c5e57daf2f7ad3b0bebe1ccfca4856390e3d3bb67" 603 | dependencies = [ 604 | "autocfg", 605 | ] 606 | 607 | [[package]] 608 | name = "smallvec" 609 | version = "1.13.2" 610 | source = "registry+https://github.com/rust-lang/crates.io-index" 611 | checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67" 612 | 613 | [[package]] 614 | name = "spade" 615 | version = "2.9.0" 616 | source = "registry+https://github.com/rust-lang/crates.io-index" 617 | checksum = "9f4ec45f91925e2c9ab3b6a857ee9ed36916990df76a1c475d783a328e247cc8" 618 | dependencies = [ 619 | "hashbrown 0.14.5", 620 | "num-traits", 621 | "robust", 622 | "smallvec", 623 | ] 624 | 625 | [[package]] 626 | name = "svgbob" 627 | version = "0.7.2" 628 | source = "registry+https://github.com/rust-lang/crates.io-index" 629 | checksum = "fac14c89bc594525da479ab9333340fb7912c36bab40286461676f31379d2d43" 630 | dependencies = [ 631 | "indexmap", 632 | "itertools", 633 | "log", 634 | "nalgebra", 635 | "once_cell", 636 | "parry2d", 637 | "pom", 638 | "sauron", 639 | "unicode-width 0.1.14", 640 | ] 641 | 642 | [[package]] 643 | name = "svgbobdoc" 644 | version = "0.3.0" 645 | dependencies = [ 646 | "base64", 647 | "proc-macro2", 648 | "quote", 649 | "svgbob", 650 | "syn", 651 | "unicode-width 0.2.0", 652 | ] 653 | 654 | [[package]] 655 | name = "syn" 656 | version = "2.0.87" 657 | source = "registry+https://github.com/rust-lang/crates.io-index" 658 | checksum = "25aa4ce346d03a6dcd68dd8b4010bcb74e54e62c90c573f394c46eae99aba32d" 659 | dependencies = [ 660 | "proc-macro2", 661 | "quote", 662 | "unicode-ident", 663 | ] 664 | 665 | [[package]] 666 | name = "syn_derive" 667 | version = "0.1.8" 668 | source = "registry+https://github.com/rust-lang/crates.io-index" 669 | checksum = "1329189c02ff984e9736652b1631330da25eaa6bc639089ed4915d25446cbe7b" 670 | dependencies = [ 671 | "proc-macro-error", 672 | "proc-macro2", 673 | "quote", 674 | "syn", 675 | ] 676 | 677 | [[package]] 678 | name = "thiserror" 679 | version = "1.0.67" 680 | source = "registry+https://github.com/rust-lang/crates.io-index" 681 | checksum = "3b3c6efbfc763e64eb85c11c25320f0737cb7364c4b6336db90aa9ebe27a0bbd" 682 | dependencies = [ 683 | "thiserror-impl", 684 | ] 685 | 686 | [[package]] 687 | name = "thiserror-impl" 688 | version = "1.0.67" 689 | source = "registry+https://github.com/rust-lang/crates.io-index" 690 | checksum = "b607164372e89797d78b8e23a6d67d5d1038c1c65efd52e1389ef8b77caba2a6" 691 | dependencies = [ 692 | "proc-macro2", 693 | "quote", 694 | "syn", 695 | ] 696 | 697 | [[package]] 698 | name = "typenum" 699 | version = "1.17.0" 700 | source = "registry+https://github.com/rust-lang/crates.io-index" 701 | checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825" 702 | 703 | [[package]] 704 | name = "unicode-ident" 705 | version = "1.0.13" 706 | source = "registry+https://github.com/rust-lang/crates.io-index" 707 | checksum = "e91b56cd4cadaeb79bbf1a5645f6b4f8dc5bde8834ad5894a8db35fda9efa1fe" 708 | 709 | [[package]] 710 | name = "unicode-width" 711 | version = "0.1.14" 712 | source = "registry+https://github.com/rust-lang/crates.io-index" 713 | checksum = "7dd6e30e90baa6f72411720665d41d89b9a3d039dc45b8faea1ddd07f617f6af" 714 | 715 | [[package]] 716 | name = "unicode-width" 717 | version = "0.2.0" 718 | source = "registry+https://github.com/rust-lang/crates.io-index" 719 | checksum = "1fc81956842c57dac11422a97c3b8195a1ff727f06e85c84ed2e8aa277c9a0fd" 720 | 721 | [[package]] 722 | name = "version_check" 723 | version = "0.9.5" 724 | source = "registry+https://github.com/rust-lang/crates.io-index" 725 | checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" 726 | 727 | [[package]] 728 | name = "wasm-bindgen" 729 | version = "0.2.95" 730 | source = "registry+https://github.com/rust-lang/crates.io-index" 731 | checksum = "128d1e363af62632b8eb57219c8fd7877144af57558fb2ef0368d0087bddeb2e" 732 | dependencies = [ 733 | "cfg-if", 734 | "once_cell", 735 | "serde", 736 | "serde_json", 737 | "wasm-bindgen-macro", 738 | ] 739 | 740 | [[package]] 741 | name = "wasm-bindgen-backend" 742 | version = "0.2.95" 743 | source = "registry+https://github.com/rust-lang/crates.io-index" 744 | checksum = "cb6dd4d3ca0ddffd1dd1c9c04f94b868c37ff5fac97c30b97cff2d74fce3a358" 745 | dependencies = [ 746 | "bumpalo", 747 | "log", 748 | "once_cell", 749 | "proc-macro2", 750 | "quote", 751 | "syn", 752 | "wasm-bindgen-shared", 753 | ] 754 | 755 | [[package]] 756 | name = "wasm-bindgen-futures" 757 | version = "0.4.45" 758 | source = "registry+https://github.com/rust-lang/crates.io-index" 759 | checksum = "cc7ec4f8827a71586374db3e87abdb5a2bb3a15afed140221307c3ec06b1f63b" 760 | dependencies = [ 761 | "cfg-if", 762 | "js-sys", 763 | "wasm-bindgen", 764 | "web-sys", 765 | ] 766 | 767 | [[package]] 768 | name = "wasm-bindgen-macro" 769 | version = "0.2.95" 770 | source = "registry+https://github.com/rust-lang/crates.io-index" 771 | checksum = "e79384be7f8f5a9dd5d7167216f022090cf1f9ec128e6e6a482a2cb5c5422c56" 772 | dependencies = [ 773 | "quote", 774 | "wasm-bindgen-macro-support", 775 | ] 776 | 777 | [[package]] 778 | name = "wasm-bindgen-macro-support" 779 | version = "0.2.95" 780 | source = "registry+https://github.com/rust-lang/crates.io-index" 781 | checksum = "26c6ab57572f7a24a4985830b120de1594465e5d500f24afe89e16b4e833ef68" 782 | dependencies = [ 783 | "proc-macro2", 784 | "quote", 785 | "syn", 786 | "wasm-bindgen-backend", 787 | "wasm-bindgen-shared", 788 | ] 789 | 790 | [[package]] 791 | name = "wasm-bindgen-shared" 792 | version = "0.2.95" 793 | source = "registry+https://github.com/rust-lang/crates.io-index" 794 | checksum = "65fc09f10666a9f147042251e0dda9c18f166ff7de300607007e96bdebc1068d" 795 | 796 | [[package]] 797 | name = "web-sys" 798 | version = "0.3.72" 799 | source = "registry+https://github.com/rust-lang/crates.io-index" 800 | checksum = "f6488b90108c040df0fe62fa815cbdee25124641df01814dd7282749234c6112" 801 | dependencies = [ 802 | "js-sys", 803 | "wasm-bindgen", 804 | ] 805 | 806 | [[package]] 807 | name = "wide" 808 | version = "0.7.28" 809 | source = "registry+https://github.com/rust-lang/crates.io-index" 810 | checksum = "b828f995bf1e9622031f8009f8481a85406ce1f4d4588ff746d872043e855690" 811 | dependencies = [ 812 | "bytemuck", 813 | "safe_arch", 814 | ] 815 | 816 | [[package]] 817 | name = "yansi" 818 | version = "1.0.1" 819 | source = "registry+https://github.com/rust-lang/crates.io-index" 820 | checksum = "cfe53a6657fd280eaa890a3bc59152892ffa3e30101319d168b781ed6529b049" 821 | 822 | [[package]] 823 | name = "zerocopy" 824 | version = "0.7.35" 825 | source = "registry+https://github.com/rust-lang/crates.io-index" 826 | checksum = "1b9b4fd18abc82b8136838da5d50bae7bdea537c574d8dc1a34ed098d6c166f0" 827 | dependencies = [ 828 | "zerocopy-derive", 829 | ] 830 | 831 | [[package]] 832 | name = "zerocopy-derive" 833 | version = "0.7.35" 834 | source = "registry+https://github.com/rust-lang/crates.io-index" 835 | checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e" 836 | dependencies = [ 837 | "proc-macro2", 838 | "quote", 839 | "syn", 840 | ] 841 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "svgbobdoc" 3 | version = "0.3.0" 4 | authors = ["yvt "] 5 | edition = "2018" 6 | license = "MIT/Apache-2.0" 7 | readme = "README.md" 8 | repository = "https://github.com/yvt/svgbobdoc" 9 | description = """ 10 | Renders ASCII diagrams in doc comments as SVG images. 11 | """ 12 | keywords = ["rustdoc", "documentation", "svg", "diagram", "figure"] 13 | categories = ["development-tools"] 14 | 15 | [badges] 16 | maintenance = { status = "passively-maintained" } 17 | 18 | [features] 19 | default = [] 20 | enable = ["svgbob"] 21 | 22 | [dependencies] 23 | syn = "2" 24 | quote = "1" 25 | svgbob = { version = "0.7", optional = true } 26 | proc-macro2 = "1" 27 | base64 = ">= 0.21, < 0.23" 28 | unicode-width = ">= 0.1, < 0.3" 29 | 30 | [lib] 31 | proc-macro = true 32 | path = "src/lib.rs" 33 | 34 | [workspace] 35 | members = [ 36 | ".", 37 | "example", 38 | ] 39 | -------------------------------------------------------------------------------- /LICENSE-APACHE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /LICENSE-MIT: -------------------------------------------------------------------------------- 1 | Copyright 2017 yvt 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # svgbobdoc 2 | 3 | [docs.rs](https://docs.rs/svgbobdoc/) 4 | 5 | This crate provides a procedural macro that renders 6 | ASCII diagrams in doc comments as SVG images using [`svgbob`]. 7 | 8 | *Requires Rust version 1.73 or later or equivalent nightly builds.* 9 | 10 | [`svgbob`]: https://github.com/ivanceras/svgbob 11 | 12 | 14 | 15 | ## Usage 16 | 17 | Add the following line to `Cargo.toml`. 18 | 19 | ```toml 20 | [dependencies] 21 | svgbobdoc = { version = "0.2", features = ["enable"] } 22 | ``` 23 | 24 | ### `transform!` 25 | 26 | Wrap doc comments with `#[doc = transform!(...)]`. Use `svgbob` code blocks to write ASCII diagrams. 27 | 28 | #[doc = svgbobdoc::transform!( 29 | /// Some structure. 30 | /// 31 | /// ```svgbob 32 | /// .--------------------. 33 | /// | Diagrams here | 34 | /// `--------------------' 35 | /// ``` 36 | )] 37 | pub struct TestStruct {} 38 | 39 | 40 | See the `example` directory for a complete example. 41 | 42 | ### Tips 43 | 44 | - Using this macro increases the compilation time. The `enable` Cargo feature can be used to turn off the transformation and the compilation of most dependent packages. 45 | 46 | - A link reference definition (`[label]: data:...`) can be generated by providing a link label in a code fence header as in `~~~svgbob,[label]`. 47 | 48 | License: MIT/Apache-2.0 49 | -------------------------------------------------------------------------------- /example/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "example" 3 | version = "0.1.0" 4 | edition = "2018" 5 | publish = false 6 | 7 | [dependencies] 8 | svgbobdoc = { path = ".." } 9 | -------------------------------------------------------------------------------- /example/src/lib.rs: -------------------------------------------------------------------------------- 1 | //! Some module. 2 | //! 3 | //! This figure is referenced by a label (`![diagram]`): ![diagram] 4 | //! 5 | #![doc = transform!( 6 | //! ```svgbob,[diagram] 7 | //! .----------------------. 8 | //! | Another diagram here | 9 | //! `----------------------' 10 | //! ``` 11 | )] 12 | #![doc = transform!( 13 | //! ```svgbob, 14 | //! .----------. 15 | //! | Mutex | 16 | //! `----------' 17 | //! ``` 18 | )] 19 | #![doc = transform!(" 20 | ```svgbob, 21 | .--------------------. 22 | | Diagrams here | 23 | `--------------------' 24 | ``` 25 | ")] 26 | use svgbobdoc::transform; 27 | 28 | #[doc = transform!( 29 | /// Some module. 30 | /// 31 | /// ```svgbob, 32 | /// .---------------. 33 | /// | Diagrams here | 34 | /// `---------------' 35 | /// ``` 36 | )] 37 | pub mod module { 38 | #![doc = transform!( 39 | //! ```svgbob, 40 | //! hoge 41 | //! ``` 42 | )] 43 | 44 | use svgbobdoc::transform; 45 | } 46 | 47 | #[doc = transform!( 48 | /// Some function. 49 | /// 50 | /// ```svgbob, 51 | /// .--------------------. 52 | /// | Diagrams here | 53 | /// `--------------------' 54 | /// ``` 55 | )] 56 | pub fn test_function() {} 57 | 58 | #[doc = transform!( 59 | /// Some structure. 60 | /// 61 | /// ```svgbob, 62 | /// .--------------------. 63 | /// | Diagrams here | 64 | /// `--------------------' 65 | /// ``` 66 | )] 67 | pub struct TestStruct { 68 | #[doc = transform!( 69 | /// ```svgbob, 70 | /// .--------------------. 71 | /// | Diagrams here | 72 | /// `--------------------' 73 | /// ``` 74 | )] 75 | pub field1: u32, 76 | } 77 | 78 | #[doc = transform!( 79 | /** 80 | * Some impl. 81 | * 82 | * ```svgbob, 83 | * .--------------------. 84 | * | Diagrams here | 85 | * `--------------------' 86 | * ``` 87 | */ 88 | )] 89 | impl TestStruct { 90 | #[doc = transform!( 91 | /// Some method. 92 | /// 93 | /// ```svgbob, 94 | /// hoge 95 | /// ``` 96 | )] 97 | pub fn test_method() {} 98 | } 99 | -------------------------------------------------------------------------------- /rust-toolchain.toml: -------------------------------------------------------------------------------- 1 | [toolchain] 2 | channel = "1.73" 3 | -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | #![doc = include_str!("../README.md")] 2 | #![warn(rust_2018_idioms)] 3 | use proc_macro2::{Span, TokenStream}; 4 | use quote::ToTokens; 5 | use syn::{ 6 | self, 7 | parse::{Parse, ParseStream}, 8 | parse_macro_input, 9 | spanned::Spanned, 10 | AttrStyle, Attribute, Error, Expr, ExprLit, Lit, LitStr, Meta, MetaNameValue, Result, 11 | }; 12 | 13 | mod textproc; 14 | 15 | /// An `Attribute`, recognized as a doc comment or not. 16 | #[derive(Clone)] 17 | enum MaybeDocAttr { 18 | /// A doc comment attribute. 19 | /// 20 | /// The first `Attribute` only specifies the surround tokens. 21 | /// 22 | /// `MetaNameValue::value` must be a `Expr::Lit(_)`. 23 | Doc(Attribute, MetaNameValue), 24 | /// An unrecognized attribute that we don't care. 25 | Other(Attribute), 26 | } 27 | 28 | impl MaybeDocAttr { 29 | fn from_attribute(attr: Attribute) -> Result { 30 | if attr.path().is_ident("doc") { 31 | let meta = attr.meta.clone(); 32 | 33 | if let Meta::NameValue(nv) = meta { 34 | if let Expr::Lit(expr) = &nv.value { 35 | if let Lit::Str(_) = expr.lit { 36 | Ok(MaybeDocAttr::Doc(attr, nv.clone())) 37 | } else { 38 | Err(Error::new(expr.lit.span(), "doc comment must be a string")) 39 | } 40 | } else { 41 | Err(Error::new(nv.value.span(), "doc comment must be a string")) 42 | } 43 | } else { 44 | // Ignore unrecognized form 45 | Ok(MaybeDocAttr::Other(attr)) 46 | } 47 | } else { 48 | Ok(MaybeDocAttr::Other(attr)) 49 | } 50 | } 51 | } 52 | 53 | impl ToTokens for MaybeDocAttr { 54 | fn to_tokens(&self, tokens: &mut TokenStream) { 55 | match self { 56 | MaybeDocAttr::Doc(attr, nv) => { 57 | attr.pound_token.to_tokens(tokens); 58 | if let AttrStyle::Inner(ref b) = attr.style { 59 | b.to_tokens(tokens); 60 | } 61 | attr.bracket_token.surround(tokens, |tokens| { 62 | nv.to_tokens(tokens); 63 | }); 64 | } 65 | MaybeDocAttr::Other(attr) => attr.to_tokens(tokens), 66 | } 67 | } 68 | } 69 | 70 | impl From for Attribute { 71 | /// The mostly-lossless conversion to `Attribute`. 72 | fn from(val: MaybeDocAttr) -> Attribute { 73 | match val { 74 | MaybeDocAttr::Doc(mut attr, nv) => { 75 | attr.meta = Meta::NameValue(nv); 76 | attr 77 | } 78 | MaybeDocAttr::Other(attr) => attr, 79 | } 80 | } 81 | } 82 | 83 | enum StrOrDocAttrs { 84 | Str(LitStr), 85 | Attrs(Vec), 86 | } 87 | 88 | impl Parse for StrOrDocAttrs { 89 | fn parse(input: ParseStream<'_>) -> Result { 90 | if let Ok(lit_str) = input.parse() { 91 | Ok(Self::Str(lit_str)) 92 | } else { 93 | // `#[doc = ...]` sequence 94 | let mut attrs = Attribute::parse_inner(input)?; 95 | attrs.extend(Attribute::parse_outer(input)?); 96 | Ok(Self::Attrs(attrs)) 97 | } 98 | } 99 | } 100 | 101 | /// Render ASCII-diagram code blocks in a Markdown-formatted string literal or 102 | /// zero or more `#[doc = ...]` attributes as SVG images. 103 | /// 104 | /// See [the module-level documentation](../index.html) for more. 105 | #[proc_macro] 106 | pub fn transform(tokens: proc_macro::TokenStream) -> proc_macro::TokenStream { 107 | let input: StrOrDocAttrs = parse_macro_input!(tokens); 108 | let (mut iter1, mut iter2); 109 | let iter: &mut dyn Iterator> = 110 | match input { 111 | StrOrDocAttrs::Str(s) => { 112 | iter1 = std::iter::once(Ok(s)); 113 | &mut iter1 114 | } 115 | StrOrDocAttrs::Attrs(attrs) => { 116 | iter2 = 117 | attrs.into_iter().map(|attr| { 118 | match MaybeDocAttr::from_attribute(attr)? { 119 | MaybeDocAttr::Doc( 120 | _, 121 | MetaNameValue { 122 | value: Expr::Lit (ExprLit { lit: Lit::Str(s), .. }), 123 | .. 124 | }, 125 | ) => Ok(s), 126 | MaybeDocAttr::Doc(attr, _) | MaybeDocAttr::Other(attr) => { 127 | Err(Error::new_spanned( 128 | &attr, 129 | "only `#[doc = ...]` attributes or a string literal are allowed here", 130 | )) 131 | } 132 | } 133 | }); 134 | &mut iter2 135 | } 136 | }; 137 | 138 | handle_error(|| { 139 | let mut output = String::new(); 140 | use textproc::{TextProcOutput, TextProcState}; 141 | let mut text_proc = TextProcState::new(); 142 | for lit_str in iter { 143 | let lit_str = lit_str?; 144 | let st = lit_str.value(); 145 | match text_proc.step(&st, lit_str.span()) { 146 | TextProcOutput::Passthrough => output.push_str(&st), 147 | TextProcOutput::Fragment(fr) => output.push_str(&fr), 148 | TextProcOutput::Empty => {} 149 | } 150 | output.push('\n'); 151 | } 152 | text_proc.finalize()?; 153 | 154 | Ok(LitStr::new(&output, Span::call_site()) 155 | .into_token_stream() 156 | .into()) 157 | }) 158 | } 159 | 160 | fn handle_error(cb: impl FnOnce() -> Result) -> proc_macro::TokenStream { 161 | match cb() { 162 | Ok(tokens) => tokens, 163 | Err(e) => e.to_compile_error().into(), 164 | } 165 | } 166 | -------------------------------------------------------------------------------- /src/minimal_template.svg: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | {content} 7 | -------------------------------------------------------------------------------- /src/textproc.rs: -------------------------------------------------------------------------------- 1 | use base64::{engine::general_purpose, Engine as _}; 2 | use proc_macro2::Span; 3 | use syn::{Error, Result}; 4 | 5 | /// The current state of the code block finder. 6 | #[derive(Debug)] 7 | pub struct TextProcState { 8 | code_block: Option, 9 | } 10 | 11 | #[derive(Debug)] 12 | struct CodeBlock { 13 | fence: String, 14 | captured: Option, 15 | start: Span, 16 | } 17 | 18 | #[derive(Debug)] 19 | struct CapturedCodeBlock { 20 | content: String, 21 | params: CodeBlockParams, 22 | } 23 | 24 | #[derive(Debug)] 25 | struct CodeBlockParams { 26 | label: Option, 27 | } 28 | 29 | /// The output of `TextProcState::step`. 30 | #[derive(Debug)] 31 | pub enum TextProcOutput { 32 | /// Output the input fragment (`#[doc = "..."]`) without modification, 33 | /// preserving its positional information. 34 | Passthrough, 35 | /// Output nothing. 36 | Empty, 37 | /// Output a new documentation text. The positional association between the 38 | /// input fragment and `.0` is erased. 39 | Fragment(String), 40 | } 41 | 42 | impl TextProcState { 43 | pub fn new() -> Self { 44 | Self { code_block: None } 45 | } 46 | 47 | pub fn step(&mut self, fragment: &str, span: Span) -> TextProcOutput { 48 | let mut i = 0; 49 | 50 | let mut new_frag: Option = None; 51 | 52 | // If `new_frag` is `None`, then this flag indicates whether the input 53 | // fragment is outputed as-is. 54 | let mut passthrough = !matches!( 55 | self.code_block, 56 | Some(CodeBlock { 57 | captured: Some(_), 58 | .. 59 | }) 60 | ); 61 | 62 | // Disables "pass-through" mode, preparing `new_frag` for custom 63 | // generation. 64 | macro_rules! prepare_nonpassthrough_emission { 65 | () => { 66 | if new_frag.is_none() { 67 | new_frag = Some(if passthrough { 68 | fragment[0..i].to_owned() 69 | } else { 70 | String::new() 71 | }); 72 | } 73 | passthrough = false; 74 | }; 75 | } 76 | 77 | /// ```text 78 | /// ^( *(?:`{3,}|~{3,}))\s*(.*?)\s*$ 79 | /// ``` 80 | fn detect_fence(s: &str) -> Option<(&str, &str)> { 81 | let bytes = s.as_bytes(); 82 | let fence_len = { 83 | let indent = bytes.iter().take_while(|&&b| b == b' ').count(); 84 | let fence_ch = *bytes.get(indent)?; 85 | if !matches!(fence_ch, b'`' | b'~') { 86 | return None; 87 | } 88 | let fence = bytes[indent..] 89 | .iter() 90 | .take_while(|&&b| b == fence_ch) 91 | .count(); 92 | if fence < 3 { 93 | return None; 94 | } 95 | indent + fence 96 | }; 97 | 98 | let (fence, rest) = s.split_at(fence_len); 99 | 100 | Some((fence, rest.trim())) 101 | } 102 | 103 | fn remove_indent<'a>(mut line: &'a str, mut indent: &str) -> &'a str { 104 | while !line.is_empty() 105 | && !indent.is_empty() 106 | && line.as_bytes()[0] == indent.as_bytes()[0] 107 | && (indent.as_bytes()[0] == b' ' || indent.as_bytes()[0] == b'\t') 108 | { 109 | line = &line[1..]; 110 | indent = &indent[1..]; 111 | } 112 | line 113 | } 114 | 115 | loop { 116 | let next_break = fragment[i..].find('\n'); 117 | 118 | let line = &fragment[i..]; 119 | let line = if let Some(next_break) = next_break { 120 | &line[0..next_break] 121 | } else { 122 | line 123 | }; 124 | 125 | let mut close_code_block = false; 126 | let mut passthrough_line = true; 127 | 128 | if let Some(code_block) = &mut self.code_block { 129 | if line == code_block.fence { 130 | // Reached the end of the code block 131 | if let Some(mut captured) = code_block.captured.take() { 132 | passthrough_line = false; 133 | prepare_nonpassthrough_emission!(); 134 | 135 | // Convert this captured code block to a SVG diagram. 136 | captured.content.pop(); // Remove trailing "\n" 137 | convert_diagram( 138 | &captured.content, 139 | new_frag.as_mut().unwrap(), 140 | captured.params, 141 | ); 142 | } 143 | 144 | close_code_block = true; 145 | } else if let Some(captured) = &mut code_block.captured { 146 | captured.content += remove_indent(line, &code_block.fence); 147 | captured.content.push('\n'); 148 | passthrough_line = false; 149 | } 150 | } else { 151 | // Detect a code block 152 | if let Some((fence, language)) = detect_fence(line) { 153 | let mut code_block = CodeBlock { 154 | fence: fence.to_owned(), 155 | captured: None, 156 | start: span, 157 | }; 158 | 159 | let params: Option = language 160 | .strip_prefix("svgbob") 161 | .and_then(|rest| { 162 | if rest.is_empty() { 163 | Some("") // exactly "svgbob" 164 | } else { 165 | rest.strip_prefix(",") // `Some` if "svgbob,[...]" 166 | } 167 | }) 168 | .map(|params| params.parse().unwrap()); 169 | 170 | if let Some(params) = params { 171 | // This is the code blcok we are interested in. 172 | // Capture the contents. 173 | passthrough_line = false; 174 | code_block.captured = Some(CapturedCodeBlock { 175 | content: String::new(), 176 | params, 177 | }); 178 | } 179 | 180 | self.code_block = Some(code_block); 181 | } 182 | } 183 | 184 | if close_code_block { 185 | self.code_block = None; 186 | } 187 | 188 | if passthrough_line { 189 | if let Some(new_frag) = &mut new_frag { 190 | *new_frag += line; 191 | if next_break.is_some() { 192 | new_frag.push('\n'); 193 | } 194 | } 195 | } else if passthrough { 196 | prepare_nonpassthrough_emission!(); 197 | } 198 | 199 | if let Some(next_break) = next_break { 200 | i += next_break + 1; 201 | } else { 202 | break; 203 | } 204 | } 205 | 206 | if let Some(new_frag) = new_frag { 207 | TextProcOutput::Fragment(new_frag) 208 | } else if passthrough { 209 | TextProcOutput::Passthrough 210 | } else { 211 | TextProcOutput::Empty 212 | } 213 | } 214 | 215 | pub fn finalize(self) -> Result<()> { 216 | if let Some(code_block) = self.code_block { 217 | if code_block.captured.is_some() { 218 | return Err(Error::new(code_block.start, "unclosed code block")); 219 | } 220 | } 221 | Ok(()) 222 | } 223 | } 224 | 225 | impl std::str::FromStr for CodeBlockParams { 226 | type Err = std::convert::Infallible; 227 | 228 | fn from_str(s: &str) -> std::result::Result { 229 | let mut this = CodeBlockParams { label: None }; 230 | 231 | let parts = s.split(",").map(|s| s.trim()); 232 | for part in parts { 233 | if let Some(label) = part 234 | .strip_prefix("[") 235 | .and_then(|part| part.strip_suffix("]")) 236 | { 237 | this.label = Some(label.to_owned()); 238 | } 239 | } 240 | 241 | Ok(this) 242 | } 243 | } 244 | 245 | /// The font used for diagrams. 246 | /// 247 | /// The selection made here attempts to approximate the monospace font used by 248 | /// rustdoc's stylesheet. Source Code Pro isn't necessarily available because 249 | /// images can't access the containing page's `@font-face`. 250 | const DIAGRAM_FONT: &str = 251 | "'Source Code Pro','Andale Mono','Segoe UI Mono','Dejavu Sans Mono','Consolas',monospace"; 252 | 253 | fn convert_diagram(art: &str, output: &mut String, params: CodeBlockParams) { 254 | let svg_code = to_svg(art); 255 | 256 | // Output the SVG as an image element 257 | use std::fmt::Write; 258 | let svg_base64 = general_purpose::STANDARD.encode(&*svg_code); 259 | 260 | if let Some(label) = params.label { 261 | writeln!( 262 | output, 263 | "\n[{}]: data:image/svg+xml;base64,{}", 264 | label, svg_base64 265 | ) 266 | .unwrap(); 267 | } else { 268 | write!(output, "![](data:image/svg+xml;base64,{})", svg_base64).unwrap(); 269 | } 270 | } 271 | 272 | #[cfg(feature = "enable")] 273 | fn to_svg(art: &str) -> String { 274 | use svgbob::{ 275 | sauron::{html::attributes::AttributeValue, Attribute}, 276 | Node, 277 | }; 278 | 279 | // Convert the diagram to SVG 280 | let mut settings = svgbob::Settings::default(); 281 | settings.stroke_width = 1.0; 282 | settings.font_family = DIAGRAM_FONT.to_owned(); 283 | settings.font_size = 13; 284 | 285 | let cb = svgbob::CellBuffer::from(art); 286 | let (mut node, _, _): (svgbob::Node<()>, _, _) = cb.get_node_with_size(&settings); 287 | 288 | traverse_pre_order_mut(&mut node, &mut |node| { 289 | match node { 290 | Node::Element(elem) if elem.tag == "text" => { 291 | // Fix the horizontal layouting of texts by adding a `textLength` attribute 292 | // to `` elements. 293 | let mut width = 0; 294 | for child in elem.children() { 295 | if let Node::Leaf(leaf) = child { 296 | if leaf.is_text() { 297 | width += xml_text_width(leaf.as_text().unwrap()); 298 | } 299 | } 300 | } 301 | 302 | let text_len = width as f32 * settings.scale as f32; 303 | elem.attrs.push(Attribute::new( 304 | None, 305 | "textLength", 306 | AttributeValue::from(text_len), 307 | )); 308 | 309 | return false; 310 | } 311 | _ => {} 312 | } 313 | 314 | true 315 | }); 316 | 317 | // FIXME: Replace with let-else when stabilized 318 | let elem = if let svgbob::Node::Element(elem) = &mut node { 319 | elem 320 | } else { 321 | unreachable!() 322 | }; 323 | 324 | // Patch the root element (``) 325 | for attr in elem.attrs.iter_mut() { 326 | match *attr.name() { 327 | "height" => { 328 | // Fix the height of the image 329 | // 330 | let new_height = settings.scale * 2.0 * art.lines().count() as f32; 331 | *attr = Attribute::new(None, "height", AttributeValue::from(new_height)); 332 | } 333 | _ => {} 334 | } 335 | } 336 | elem.attrs.push(Attribute::new( 337 | None, 338 | "style", 339 | AttributeValue::from("transform:translate(0.5px,0.5px)"), 340 | )); 341 | 342 | use svgbob::Render; 343 | let mut svg_code = String::new(); 344 | node.render(&mut svg_code).unwrap(); 345 | 346 | svg_code 347 | } 348 | 349 | /// Like [`unicode_width::UnicodeWidthStr`] but handles some entity references 350 | /// (e.g., `&`). Assumes the input is in a valid form of an XML text node. 351 | #[cfg(feature = "enable")] 352 | fn xml_text_width(html_text: &str) -> usize { 353 | use unicode_width::UnicodeWidthStr; 354 | html_text 355 | .split('&') 356 | .enumerate() 357 | .map(|(i, mut part)| { 358 | if i > 0 { 359 | if let Some(k) = part.find(';') { 360 | // "& a m p ;" 361 | // ^ ^^^^^ ^ 362 | // │ │ └─ This part is preserved so that this entity is 363 | // │ │ counted as one cell 364 | // │ └─ We remove this part now 365 | // └─ This part is removed by `split` 366 | part = &part[k..]; 367 | } 368 | } 369 | part.width() 370 | }) 371 | .sum() 372 | } 373 | 374 | #[cfg(feature = "enable")] 375 | fn traverse_pre_order_mut( 376 | node: &mut svgbob::Node, 377 | cb: &mut dyn FnMut(&mut svgbob::Node) -> bool, 378 | ) { 379 | if cb(node) { 380 | if let Some(children) = node.children_mut() { 381 | for child in children.iter_mut() { 382 | traverse_pre_order_mut(child, cb); 383 | } 384 | } 385 | } 386 | } 387 | 388 | #[cfg(not(feature = "enable"))] 389 | fn to_svg(art: &str) -> String { 390 | use std::fmt::Write; 391 | use unicode_width::UnicodeWidthStr; 392 | 393 | let lines = art.lines(); 394 | let cols = lines 395 | .clone() 396 | .map(|line| line.width()) 397 | .fold(0, std::cmp::max); 398 | let rows = lines.clone().count(); 399 | 400 | let col_width = 8; 401 | let width = cols * col_width; 402 | let height = rows * 16; 403 | 404 | let mut content = String::new(); 405 | for (i, line) in lines.enumerate() { 406 | let mut x = 0; 407 | let y = i * 16 + 12; 408 | let mut last_i = 0; 409 | 410 | // Divide `line` by whitespace so that each text span is positioned 411 | // precisely at their endpoints 412 | split_whitespace_indices(line, |span, start_i| { 413 | x += line[last_i..start_i].width() * col_width; 414 | last_i = start_i; 415 | 416 | write!( 417 | content, 418 | r#""#, 419 | x, 420 | y, 421 | span.width() * col_width, 422 | ) 423 | .unwrap(); 424 | escape_html(span, &mut content); 425 | content.push_str(""); 426 | }); 427 | } 428 | 429 | fn split_whitespace_indices(mut s: &str, mut f: impl FnMut(&str, usize)) { 430 | // Skip the the first whitespace characters 431 | let s_trimmed = s.trim_start(); 432 | let mut offset = s.len() - s_trimmed.len(); 433 | s = s_trimmed; 434 | while !s.is_empty() { 435 | // Find the first whitespace character 436 | let i = s 437 | .char_indices() 438 | .find(|(_, c)| c.is_whitespace()) 439 | .map(|(i, _)| i); 440 | 441 | // Emit a span comprised of non-whitespace characters 442 | { 443 | let i = i.unwrap_or(s.len()); 444 | let part = &s[..i]; 445 | f(part, offset); 446 | offset += i; 447 | s = &s[i..]; 448 | } 449 | 450 | // Skip the subsequent whitespace characters 451 | let s_trimmed = s.trim_start(); 452 | offset += s.len() - s_trimmed.len(); 453 | s = s_trimmed; 454 | } 455 | } 456 | 457 | fn escape_html(mut s: &str, out: &mut String) { 458 | loop { 459 | let i = s 460 | .as_bytes() 461 | .iter() 462 | .position(|b| matches!(b, b'<' | b'>' | b'&' | 0)); 463 | out.push_str(&s[..i.unwrap_or(s.len())]); 464 | if let Some(i) = i { 465 | out.push_str(match s.as_bytes()[i] { 466 | b'<' => "<", 467 | b'>' => ">", 468 | b'&' => "&", 469 | 0 => " ", 470 | _ => unreachable!(), 471 | }); 472 | s = &s[i + 1..]; 473 | } else { 474 | break; 475 | } 476 | } 477 | } 478 | 479 | format!( 480 | include_str!("minimal_template.svg"), 481 | font = DIAGRAM_FONT, 482 | width = width, 483 | height = height, 484 | content = content, 485 | ) 486 | } 487 | --------------------------------------------------------------------------------