阅读文档
kscript docs 会在你的浏览器中打开这些文档。文档已内置于 kscript 中,因此可以离线使用,并与你所用的版本保持一致:
| 命令 | 作用 |
|---|---|
kscript docs | 打开文档。 |
kscript docs replace | 搜索 "replace" 并打开。 |
在未运行 kscript 的情况下查看文档
若要在无法运行 kscript docs 的地方阅读文档,例如没有安装 KScript 的电脑、U 盘或你自己的 Web 服务器,可以将文档导出。--export-to 会将整个站点以普通文件的形式写入一个文件夹:
| 命令 | 作用 |
|---|---|
kscript docs --export-to kscript-docs | 将文档写入 kscript-docs。 |
kscript docs --export-to kscript-docs --syntax xml | 示例先以 XML 形式显示。 |
在浏览器中打开 kscript-docs/index.html。每个页面都是独立的 .html 文件,每个链接都是相对链接,因此该文件夹无需服务器和网络,直接从磁盘即可运行,复制或上传后也能正常使用。搜索同样可用:其索引位于 assets/search.js 中。再次导出到同一文件夹会替换页面,并保留其中的其他文件。
若要在同一网站上发布多个版本,请将每个版本分别导出到以版本命名的文件夹中,并列放置(docs/0.55.0/、docs/0.54.0/)。--versions url 会将页眉中的版本转换为版本选择器,它读取类似 {"latest": "0.55.0", "versions": ["0.55.0", "0.54.0"]} 的列表,并在所选版本中打开相同页面;--stylesheet url 会在文档自身样式表之后加载你自己的样式表,使其与站点其他部分保持一致。KScript 官网以这种方式在 /docs/<version>/ 发布每个版本的文档。
这些示例仍然可以阅读、复制,并在 .kscript 和 XML 之间切换,但无法运行,因为运行它们需要 kscript docs。因此省略了 Run 框和 playground 的编辑器,每个可运行示例都会留下一个隐藏占位符以代替原来的框:
<div class="kscript-example" data-example="guide-getting-started-1">
<!-- the example's code, to read -->
<template class="kscript-try" data-example="guide-getting-started-1" data-format="kscript"
data-kscript="echo 'Hello'" data-xml="<echo>Hello</echo>"></template>
</div>data-example 为示例命名:即页面的文件名及其在该页上的编号,因此每次导出时保持不变。data-format 是该示例所用的格式,data-kscript 和 data-xml 分别保存各格式下的代码,data-input 和 data-title 在示例包含相应内容时出现。<template> 从不显示,因此在你自己的脚本使用这些占位符之前,它们不会产生任何效果。若要用你自己的运行器运行这些示例,请在页面中添加一个脚本,将每个占位符替换为对应内容:
document.querySelectorAll('template.kscript-try').forEach(placeholder => {
const example = placeholder.closest('.kscript-example');
// The example's code in the reader's format, falling back to the one it was written in.
const format = document.documentElement.dataset.format;
const code = placeholder.dataset[format] ?? placeholder.dataset[placeholder.dataset.format];
example.replaceWith(myRunner(code, format)); // myRunner is yours: an editor and a Run button
});