mirror of
https://github.com/RubyMetric/chsrc
synced 2025-07-18 05:27:38 +08:00
Add MAIN
This commit is contained in:
parent
662898f628
commit
a63af77e02
@ -4,219 +4,46 @@
|
||||
# File Authors : Aoran Zeng <ccmywish@qq.com>
|
||||
# Contributors : Nul None <nul@none.org>
|
||||
# Created On : <2025-07-12>
|
||||
# Last Modified : <2025-07-12>
|
||||
# Last Modified : <2025-07-13>
|
||||
#
|
||||
# Generate raw strings for C programming language
|
||||
# rawstr4c:
|
||||
#
|
||||
# ---------------------------------------------------------------
|
||||
# 全局设置称为 Global config,其他均称为 section config
|
||||
# 每一部分称为 section,我们要处理的input,都在 code block 里,我们简称为 block
|
||||
# Raw strings for C programming language
|
||||
#
|
||||
# Usage:
|
||||
# rawstr4c <FILE.md> # 指定具体某文件名
|
||||
# rawstr4c <DIR> # 使用某一目录寻找 rawstr4c.md 文件
|
||||
#
|
||||
# 要注意的是,该程序一次性只能处理唯一一个文件
|
||||
# ---------------------------------------------------------------
|
||||
|
||||
unless @*ARGS {
|
||||
die "Usage: rawstr4c <FILE.md>\n";
|
||||
}
|
||||
use Parser;
|
||||
use Generator;
|
||||
|
||||
my $markdown-file = @*ARGS[0];
|
||||
sub MAIN(
|
||||
Str $input-path,
|
||||
Bool :$debug = False #= --debug
|
||||
) {
|
||||
my $markdown-file;
|
||||
|
||||
unless $markdown-file.IO.e {
|
||||
die "Error: File '$markdown-file' not found.\n";
|
||||
}
|
||||
|
||||
# 根据转换模式处理字符
|
||||
sub convert-char($char, $mode) {
|
||||
given $mode {
|
||||
when 'oct' {
|
||||
my $bytes = $char.encode('UTF-8');
|
||||
return $bytes.map({ "\\" ~ sprintf("%03o", $_) }).join('');
|
||||
}
|
||||
when 'hex' {
|
||||
my $bytes = $char.encode('UTF-8');
|
||||
return $bytes.map({ "\\x" ~ sprintf("%02x", $_) }).join('');
|
||||
}
|
||||
when 'escape' {
|
||||
# 只转义必要的字符
|
||||
given $char {
|
||||
when '"' { return '\\"'; }
|
||||
when "'" { return "\\'"; }
|
||||
when '\\' { return '\\\\'; }
|
||||
when "\n" { return '\\n'; }
|
||||
when "\t" { return '\\t'; }
|
||||
when "\r" { return '\\r'; }
|
||||
when "\0" { return '\\0'; }
|
||||
default { return $char; }
|
||||
}
|
||||
}
|
||||
default {
|
||||
die "Unknown translation mode: $mode";
|
||||
if $input-path.IO.d {
|
||||
$markdown-file = $input-path.IO.add("rawstr4c.md");
|
||||
unless $markdown-file.e {
|
||||
die "Error: No 'rawstr4c.md' file found in directory '$input-path'\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# 处理字符串转换
|
||||
sub process-content($content, $mode) {
|
||||
my $result = "";
|
||||
for $content.comb -> $char {
|
||||
$result ~= convert-char($char, $mode);
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
# 生成变量名
|
||||
sub generate-variable-name($global-config, $section-config, $title) {
|
||||
my $prefix = $global-config<prefix> // "_rawstr4c";
|
||||
my $postfix = $global-config<postfix> // "";
|
||||
|
||||
# 处理前缀
|
||||
$prefix = $prefix.subst(/^'`'/, '').subst(/'`'$/, '');
|
||||
|
||||
# 处理后缀
|
||||
if $postfix {
|
||||
$postfix = $postfix.subst(/^':'/, '');
|
||||
if $section-config<language> {
|
||||
my $lang = $section-config<language>.subst(/^'`'/, '').subst(/'`'$/, '');
|
||||
$postfix = $postfix.subst('use-language', "in_$lang");
|
||||
} else {
|
||||
$postfix = $postfix.subst('use-language', ''); # 默认为无语言
|
||||
}
|
||||
}
|
||||
|
||||
# 生成的变量名称
|
||||
my $name = $section-config<name> // $title.lc;
|
||||
$name = $name.subst(/^'`'/, '').subst(/'`'$/, '');
|
||||
# 处理标题中包含的空格
|
||||
$name = $name.subst(/\s+/, '_', :g);
|
||||
|
||||
my $var-name = $prefix;
|
||||
if $name {
|
||||
$var-name ~= "_" ~ $name;
|
||||
}
|
||||
|
||||
if $postfix {
|
||||
$var-name ~= "_" ~ $postfix;
|
||||
}
|
||||
|
||||
return $var-name;
|
||||
}
|
||||
|
||||
#`( 真正的 main 流程开始 |