十年匠心定制 · 商业建站与技术教学双线并行 咨询热线:400-886-1026 service@lmnt.cn
ARTICLE DETAIL

资讯详情

深耕网站建设与运营推广的一线实战洞察。

D2 如何用标准输入输出和 --stdout-format 把图表编译进 shell 管道?

D2 如何用标准输入输出和 --stdout-format 把图表编译进 shell 管道? D2 如何用标准输入输出和 --stdout-format 把图表编译进 shell 管道【免费下载链接】d2D2 is a modern diagram scripting language that turns text to diagrams.项目地址: https://gitcode.com/GitHub_Trending/d2/d2写构建脚本、生成 HTML 或串联多个工具时你常不希望 d2 先落一个中间文件再搬运而是把.d2源码或渲染结果直接放进管道D2 源码从 stdin 流入渲染出的 SVG、PNG、PDF 等字节从 stdout 流出再重定向给下一个程序。d2 CLI 用-占位符和--stdout-format完成这件事。本文基于仓库中的 man 页、CLI 源码和端到端测试说明如何把一次图表编译完整接入 shell 管道并验证结果。准备工作先在本机装好 d2 命令安装方式见 docs/INSTALL.md后文所有命令都默认d2已可用。-的约定从 stdin 读、向 stdout 写man 页 ci/release/template/man/d2.1 的说明只有一句话Pass - to have d2 read from stdin or write to stdout.具体行为依据 d2cli/main.go第一个参数输入传-d2 不读文件从标准输入读取 D2 源码第二个参数输出传-d2 把渲染结果写到标准输出而不是文件输入是-且没有指定输出时输出默认也是 stdoutd2cli/main.go输出不是-时格式按输出路径的文件后缀推断后缀缺失或不被识别时默认 SVGd2cli/export.go。最小例子输入x - y与仓库 e2e 测试stdin用例完全一致echo x - y | d2 -d2 -从 stdin 读到源码默认向 stdout 输出 SVG 文档。该 e2e 用例e2etests-cli/main_test.go把x - y经 stdin 喂给d2 -并断言 stdout 内容与对应的.svg测试数据一致——也就是说不指定格式时管道里得到的是 SVG。要把结果存成文件用 shell 重定向即可d2 diagram.d2 - diagram.svg用 --stdout-format 指定 stdout 的输出格式--stdout-format只在输出设为 stdout即第二个参数是-时生效支持的值来自 d2cli/export.gopng、svg、pdf、pptx、giftxt以及映射到同一文本输出的asciiflag 自身的 usage 说明就给出了一条完整命令见 d2cli/main.god2 input.d2 --stdout-format png - output.png按同一模式可以组装常见管道d2 diagram.d2 --stdout-format pdf - diagram.pdf cat diagram.d2 | d2 --stdout-format gif - diagram.gif d2 diagram.d2 --stdout-format txt - | less输出txt/ascii时字符集由--ascii-mode控制取值standard基础 ASCII 字符或extendedUnicode 字符默认extendedman 页 OPTIONS 一节。不想每次敲 flag 时可以改用 man 页 ENVIRONMENT VARIABLES 一节列出的等价环境变量export D2_STDOUT_FORMATpdf d2 diagram.d2 - diagram.pdf结果验证看退出状态不看“文件有没有出现”man 页里有一条明确的警告Never use the presence of the output file to check for success. Always use the exit status of d2.原因是渲染出错时 d2 仍可能写出一份部分渲染结果方便继续修改损坏的图表。管道场景下这条规则更关键—— diagram.pdf这类重定向总会创建目标文件文件存在不代表渲染成功。验证方式d2 diagram.d2 --stdout-format pdf - diagram.pdf echo $?退出码为 0 才算本次编译成功。对内容本身仓库 e2e 测试给出了判断依据stdout_pdf用例检查 stdout 前几个字节是%PDF-签名并且磁盘上没有生成字面名为-的文件stdout_pptx用例校验 stdout 是一个合法的 PPTXe2etests-cli/main_test.go。对应到管道中PDF 可以这样核对head -c 4 diagram.pdf应看到%PDF开头——这是仓库测试采用的验证方式属于格式签名检查而不是固定字节比对。管道场景下相关的几个 flag--no-xml-tag省略输出 SVG 的 XML 声明?xml ...?。man 页说明它 Useful when generating SVGs for direct HTML embedding即把管道输出的 SVG 直接拼进 HTML 文档时避免多余的声明头d2 diagram.d2 --no-xml-tag - page.html--salt给输出中的 ID 追加一个字符串盐值。man 页说明的用途是同一 HTML 文档里生成多份相同图表时避免重复 ID 造成 HTML 非法适合“循环生成 SVG 拼页”的管道写法。--target只渲染指定 board传空字符串--target只渲染根 board末尾带*会连同其 scenarios/steps/layers 一起渲染。--omit-version不在生成图中写入 D2 版本号。限制-w[atch]不能与 stdin 输入同时使用源码会直接报-w[atch] cannot be combined with reading input from stdind2cli/main.go。含 layers/scenarios/steps 的 multiboard 图表不能写到 stdout错误信息为multiboard output cannot be written to stdoutd2cli/main.go。管道路径只适用于单一 board。--stdout-format只用于输出为-的情况输出是真实文件时格式由文件后缀决定flag 不生效。man 页注明 PNG 导出每个维度最多支持 32768 像素且受渲染资源限制约束——--stdout-format png导出大图时这是硬边界。输入、输出同时走管道时d2 -且输出为 stdoutPPTX 的标题来源为stdin输入是真实文件时则取自输入文件名d2cli/main.goe2e 测试断言标题 “derived from the input”。【免费下载链接】d2D2 is a modern diagram scripting language that turns text to diagrams.项目地址: https://gitcode.com/GitHub_Trending/d2/d2创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表