@ gmb/ 比特马克- parser- 生成器
一个使用 Peggy.js 的比特马克分解器和生成器.
特征 :
- 将比特马克转换为 JSON, 反之亦然 .
- 程序上创建或修改比特标记.
- 在NodeJS或浏览器中工作.
- 验证并预设比特马克.
- 快活,有小浏览器足迹,不足60kB.
安装
软件包管理器
使用线条 :
$ yarn add @gmb/bitmark-parser-generator
使用 npm :
$ npm install @gmb/bitmark-parser-generator
光盘N
使用jsDelivr CDN(ES6 UMD模块):
<script src="https://cdn.jsdelivr.net/npm/@gmb/bitmark-parser-generator@latest/dist/browser/bitmark-parser-generator.min.js"></script>
使用unpkg CDN(ES6 UMD模块):
<script src="https://unpkg.com/@gmb/bitmark-parser-generator@latest/dist/browser/bitmark-parser-generator.min.js"></script>
替换 latest 上面任一URL中有一个特定的版本,用于使用特定的版本.
基本用途
导入
// Modules
import { BitmarkParserGenerator } from '@gmb/bitmark-parser-generator';
// CommonJS
const { BitmarkParserGenerator } = require('@gmb/bitmark-parser-generator');
// Browser UMD
const { BitmarkParserGenerator } = window.bitmarkParserGenerator;
改划
const bpg = new BitmarkParserGenerator();
// Convert bitmark markup to bitmark JSON
const json = await bpg.convert("[.article] Hello World");
// Convert bitmark JSON to bitmark markup
const bitmark = await bpg.convert('[{"bitmark": "[.article] Hello World","bit": { "type": "article", "format": "bitmark++", "bitLevel": 1, "body": "Hello World" }}]');
// Convert bitmark markup file to bitmark JSON
await bpg.convert("./input.bitmark", { output: "./output.json" });
// Convert bitmark JSON to bitmark markup
await bpg.convert("./input.json", { output: "./output.bitmark" });
转换选项
// Convert bitmark JSON to bitmark markup with options
await bpg.convert("./input.json", {
bitmarkParserType: 'peggy', // The parser type to use. 'peggy' or 'antlr'. Default: 'peggy'
outputFormat: 'ast', // Output AST rather than the default output. Default: automatic
outputFile: "./output.ast.json", // Output to file rather than <stdout>. Default: <stdout>
fileOptions: {
append: true, // Append to output file. Default: overwrite
encoding: 'utf8', // Specify a file encoding. Default: 'utf8'
},
jsonOptions: {
prettify: 2, // Prettify the JSON output. Default: not prettified
stringify: false, // Return JSON as a string. Default: plain JS object
includeExtraProperties: false, // Include extra (non-standard) properties from the markup in the JSON: Default: ignore
debugGenerationInline: false, // [development only] Include debugging tags in the generated output. Default: false
},
bitmarkOptions: {
explicitTextFormat: false, // Include bitmark text format even when it is the default (bitmark++). Default: false
prettifyJson: 2, // Prettify the body JSON output. Default: not prettified
cardSetVersion: 1, // Output markup using the specified cardSet format. Default: 1
debugGenerationInline: false, // [development only] Include debugging tags in the generated output. Default: false
}
});
程序符号创建
import { BitmarkParserGenerator, Builder, Ast, BitType, TextFormat } from 'bitmark-parser-generator';
const bpg = new BitmarkParserGenerator();
const builder = new Builder();
// Create bitmark AST programatically
const ast = builder.bitmark({
bits: [
builder.bit({
bitType: BitType.article,
textFormat: TextFormat.bitmarkText,
body: builder.body({
bodyParts: [
builder.bodyText({
text: "Hello World!"
})
]
}),
})
]
});
// Write the AST to bitmark markup
bpg.convert(ast, { output: "./output.bitmark" });
// Write the AST to bitmark JSON
bpg.convert(ast, { output: "./output.json", outputFormat: 'json' });
断头
一个文本可以在程序上被断开以位标记包含.
注:建议使用上文程序比特马克创建中记录的比特构建器,而不是 手编码比特马克的创建,因为它保证了比特马克是有效的,并且所有断面都是正确的.
当将文本拆解以比特马克形式包含时,应用的拆解取决于该文本出现在 比特马克。 下列四个文本位置需要不同的断面:
| 位格式 | 文本位置 |
|---|---|
| 比特马克++ | 机构 |
| 比特马克++ | 标签 |
| 文本( 不是比特马克++) | 机构 |
| 文本( 不是比特马克++) | 标签 |
另外,如果纯文本分隔符 ==== text ==== ,然后在分隔符后必须使用
text:body 纵然是 bitmark++。 。 。 .
import { BitmarkParserGenerator, BodyTextFormat, TextLocation, InputType } from 'bitmark-parser-generator';
const bpg = new BitmarkParserGenerator();
const breakscaped = bpg.breakscapeText("This is the [!text] to be breakscaped", {
inputFormat: InputType.string, // or "string"
textFormat: BodyTextFormat.bitmarkPlusPlus, // or "bitmark++"
textLocation: TextLocation.body, // or "body"
});
// breakscaped = "This is the [^!text] to be breakscaped"
const breakscaped = bpg.breakscapeText("This is the [!text] to be breakscaped", {
inputFormat: InputType.string, // or "string"
textFormat: BodyTextFormat.bitmarkPlusPlus, // or "bitmark++"
textLocation: TextLocation.tag, // or "tag"
});
// breakscaped = "This is the [!text^] to be breakscaped"
资讯
支持位的信息可以通过信息 API 重排
import { BitmarkParserGenerator } from 'bitmark-parser-generator';
const bpg = new BitmarkParserGenerator();
// Write supported bit info to the console
const info = bpg.info();
console.log(JSON.stringify(info, null, 2));
升级
比特马克-parser-生成器可以"升级"比特马克标记或JSON. 这将升级标记 / JSON 到最近 版本并删除任何无效代码或标记.
这种感觉应该用 与原因。 它会完全删除数据.
如果输入包含无效或无法识别的数据,则会从输出中删除. 因此,这是 建议使用此特性带有源代码控制或另一个数据备份系统.
const bpg = new BitmarkParserGenerator();
// Upgrade bitmark markup
const json = await bpg.convert("[.article] Hello World [$I will be removed as I am invalid]");
// Upgrade bitmark JSON
const bitmark = await bpg.convert('[{"bitmark": "[.article] Hello World","bit": { "type": "article", "format": "bitmark++",
"bitLevel": 1, "body": "Hello World", unknownProperty: "Will be removed" }}]');
// Upgrade bitmark markup file to another file
await bpg.convert("./input.bitmark", { output: "./output.bitmark" });
// Upgrade bitmark JSON file to another file
await bpg.convert("./input.json", { output: "./output.json" });
版本
// Get the bitmar-parser-generator version
const version = bpg.version();
高级使用
更先进的使用是可能的,例如实施自定义流式输出写法.
见 API 文档 更多信息.
许可证
此开源软件根据 ISC 许可证。 。 。 .