From 4a5724caccc33c7f5c7011940c2785c44adfc15d Mon Sep 17 00:00:00 2001 From: Aoran Zeng Date: Sat, 12 Jul 2025 16:54:15 +0800 Subject: [PATCH] Add `rawstr4c.raku` --- rawstr4c/README.md | 5 +++ rawstr4c/rawstr4c.raku | 89 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+) create mode 100644 rawstr4c/README.md create mode 100644 rawstr4c/rawstr4c.raku diff --git a/rawstr4c/README.md b/rawstr4c/README.md new file mode 100644 index 0000000..c267ddf --- /dev/null +++ b/rawstr4c/README.md @@ -0,0 +1,5 @@ +# rawstr4c + +## 配置选项 + +- translate = `:hex` | `:oct` | `:escape` diff --git a/rawstr4c/rawstr4c.raku b/rawstr4c/rawstr4c.raku new file mode 100644 index 0000000..5c6e97b --- /dev/null +++ b/rawstr4c/rawstr4c.raku @@ -0,0 +1,89 @@ +#!/usr/bin/env raku +# --------------------------------------------------------------- +# File Name : rawstr4c.raku +# File Authors : Aoran Zeng +# Contributors : Nul None +# Created On : <2025-07-12> +# Last Modified : <2025-07-12> +# +# Generate raw strings for C programming language +# --------------------------------------------------------------- + +unless @*ARGS { + die "Usage: rawstr4c \n"; +} + +my $markdown-file = @*ARGS[0]; + +# 确保文件存在 +unless $markdown-file.IO.e { + 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 = ~$0; + say "Global translation mode: " ~ %config; + last; + } + } + + return %config; +} + +# 根据转换模式处理字符 +sub convert-char($char, $mode) { + my $byte = $char.encode('UTF-8')[0]; + + given $mode { + when 'octal' { + return "\\" ~ sprintf("%03o", $byte); + } + when 'hex' { + return "\\x" ~ sprintf("%02x", $byte); + } + 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"; + } + } +} + +my $content = $markdown-file.IO.slurp; + +my %config = parse-config($content); +my $translation-mode = %config // 'octal'; + +my $in-code-block = False; +for $content.lines -> $line { + if $line ~~ /^ '```' / { + # 遇到代码块的开始或结束标记 + $in-code-block = !$in-code-block; + next; # 跳过代码块标记行 + } + + if $in-code-block { + # 如果在代码块内部,处理每一行 + for $line.comb -> $char { + print convert-char($char, $translation-mode); + } + print convert-char("\n", $translation-mode); # 处理换行符 + } +}