mirror of
https://github.com/RubyMetric/chsrc
synced 2025-07-18 05:27:38 +08:00
Basic implementation of rawstr4c
This commit is contained in:
parent
747c29e85f
commit
a54f704719
@ -7,6 +7,10 @@
|
|||||||
# Last Modified : <2025-07-12>
|
# Last Modified : <2025-07-12>
|
||||||
#
|
#
|
||||||
# Generate raw strings for C programming language
|
# Generate raw strings for C programming language
|
||||||
|
#
|
||||||
|
# ---------------------------------------------------------------
|
||||||
|
# 全局设置称为 Global config,其他均称为 section config
|
||||||
|
# 每一部分称为 section,我们要处理的input,都在 code block 里,我们简称为 block
|
||||||
# ---------------------------------------------------------------
|
# ---------------------------------------------------------------
|
||||||
|
|
||||||
unless @*ARGS {
|
unless @*ARGS {
|
||||||
@ -15,37 +19,20 @@ unless @*ARGS {
|
|||||||
|
|
||||||
my $markdown-file = @*ARGS[0];
|
my $markdown-file = @*ARGS[0];
|
||||||
|
|
||||||
# 确保文件存在
|
|
||||||
unless $markdown-file.IO.e {
|
unless $markdown-file.IO.e {
|
||||||
die "Error: File '$markdown-file' not found.\n";
|
die "Error: File '$markdown-file' not found.\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
# 解析配置信息
|
|
||||||
sub parse-config($content) {
|
|
||||||
my %config;
|
|
||||||
|
|
||||||
for $content.lines -> $line {
|
|
||||||
# say "Processing line: $line";
|
|
||||||
if $line ~~ /^ '-' \s* 'translate' \s* '=' \s* '`' ':' (<[a..z\-]>+) '`' / {
|
|
||||||
%config<translation> = ~$0;
|
|
||||||
say "Global translation mode: " ~ %config<translation>;
|
|
||||||
last;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return %config;
|
|
||||||
}
|
|
||||||
|
|
||||||
# 根据转换模式处理字符
|
# 根据转换模式处理字符
|
||||||
sub convert-char($char, $mode) {
|
sub convert-char($char, $mode) {
|
||||||
my $byte = $char.encode('UTF-8')[0];
|
|
||||||
|
|
||||||
given $mode {
|
given $mode {
|
||||||
when 'octal' {
|
when 'oct' {
|
||||||
return "\\" ~ sprintf("%03o", $byte);
|
my $bytes = $char.encode('UTF-8');
|
||||||
|
return $bytes.map({ "\\" ~ sprintf("%03o", $_) }).join('');
|
||||||
}
|
}
|
||||||
when 'hex' {
|
when 'hex' {
|
||||||
return "\\x" ~ sprintf("%02x", $byte);
|
my $bytes = $char.encode('UTF-8');
|
||||||
|
return $bytes.map({ "\\x" ~ sprintf("%02x", $_) }).join('');
|
||||||
}
|
}
|
||||||
when 'escape' {
|
when 'escape' {
|
||||||
# 只转义必要的字符
|
# 只转义必要的字符
|
||||||
@ -66,24 +53,170 @@ sub convert-char($char, $mode) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# 处理字符串转换
|
||||||
|
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 流程开始 |