regex.tin 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #nop vim: set filetype=tt:;
  2. /*
  3. 本文件属于 PaoTin++ 的一部分。
  4. PaoTin++ © 2020~2026 的所有版权均由担子炮(dzp <danzipao@gmail.com>) 享有并保留一切法律权利
  5. 你可以在遵照 GPLv3 协议的基础之上使用、修改及重新分发本程序。
  6. */
  7. #nop 本文件是 xtintin 的一部分,实现了一些正则表达式生成函数;
  8. ///=== {
  9. ///// 正则表达式生成函数:
  10. /////
  11. // #@ re.TableHeader <表格名称>
  12. // 生成一个正则表达式,用来匹配一个表格的表头。
  13. //
  14. // #@ re.TableTail
  15. // 生成一个正则表达式,用来匹配一个表格的结尾。
  16. //
  17. // #@ util.CurrentClass
  18. // 获取当前 class 的名称。
  19. // };
  20. #func {re.TableHeader} {
  21. #local title {%1};
  22. #local class {@util.CurrentClass{}};
  23. #return {^╭─{?:(?:─|┬)+}$title{?:(?:─|┬)+}─╮{|$class}$};
  24. };
  25. #func {re.TableTail} {
  26. #local class {@util.CurrentClass{}};
  27. #local end {@default{%0;$class}};
  28. #return {^╰─{?:(?:─|┴)+}%u{?:(?:─|┴)+}─╯{|$end}$};
  29. };
  30. #func {util.CurrentClass} {
  31. #line quiet #info classes save;
  32. #return {$info[CLASSES][1][arg1]};
  33. };
  34. VAR {服务器渲染的表格数据} gServerTable {};
  35. ///=== {
  36. // ## re.ParseServerTable [<回调代码>]
  37. // 通用的解析服务器表格的工具。
  38. // 如果提供了回调代码,那么当表格解析完成后,会调用它。
  39. // 本工具有两个变体,分别是 re.ParseServerTable/gag 和 re.ParseServerTable/nogag
  40. // 如果不使用变体后缀修饰,那么默认为 nogag 模式。
  41. // };
  42. #alias {re.ParseServerTable} {re.ParseServerTable.do {nogag} {%21}};
  43. #alias {re.ParseServerTable/gag} {re.ParseServerTable.do {gag} {%21}};
  44. #alias {re.ParseServerTable/nogag} {re.ParseServerTable.do {nogag} {%21}};
  45. #alias {re.ParseServerTable.do} {
  46. #local gag {%1};
  47. #local callback {%2};
  48. #var gServerTable {};
  49. #var gServerTable.helper[gag] {$gag};
  50. #var gServerTable.helper[callback] {$callback};
  51. #line oneshot #action {~{*UTF8}{?:^}%!c╭─{?:(?:─|┬)+}%c{\p{Han}+}%c{?:(?:─|┬)+}─╮%c{|re/ParseServerTable}$} {
  52. #var gServerTable[name] {%%2};
  53. #var gServerTable[nameColor] {@color.Trim{@str.Color{%%1}}};
  54. #if { "$gServerTable.helper[gag]" == "gag" } {
  55. #line gag;
  56. };
  57. re.ParseServerTable.body;
  58. };
  59. };
  60. #alias {re.ParseServerTable.body} {
  61. #class re.ParseServerTable open;
  62. #if { "$gServerTable.helper[gag]" == "gag" } {
  63. #gag {^%*{|re/ParseServerTable/body}$};
  64. };
  65. #local color {(?:\e\[[0-9;:]*m)*};
  66. #local color2 {(?:\e\[[0-9;:]*m|\s+)*};
  67. #line oneshot #action {~^{?:$color}│{($color\S+$color2│)+}%c{|re/ParseServerTable/header}$} {
  68. #local header {%%1};
  69. #list header explode {│};
  70. #var gServerTable[header] {$header};
  71. #local idx {};
  72. #foreach {*gServerTable[header][]} {idx} {
  73. #local header {$gServerTable[header][$idx]};
  74. #replace header {%*%C$} {&1};
  75. #replace header {^{(\e\[(|0|2;37;0)m)+}} {};
  76. #var gServerTable[header][$idx] {
  77. {color} {@color.Trim{@str.Color{$header}}}
  78. {text} {@str.Plain{$header}}
  79. };
  80. };
  81. } {4.9};
  82. #action {~^{?:$color}│%*%s%c│%c{|re/ParseServerTable/body}$} {
  83. #if { "%%1" != "%*│%*" } {
  84. #local note {%%1};
  85. #cat gServerTable[note] {$note};
  86. #return;
  87. };
  88. #local line {%%1};
  89. #list line explode {│};
  90. #local idx {};
  91. #local body {};
  92. #local colorBody {};
  93. #foreach {*line[]} {idx} {
  94. #local b {$line[$idx]};
  95. #replace b {%*%C$} {&1};
  96. #replace b {^{(\e\[(|0|2;37;0)m)+}} {};
  97. #local field {$gServerTable[header][$idx][text]};
  98. #local body[$field] {@str.Plain{$b}};
  99. #local colorBody[$field] {$b};
  100. };
  101. #local body[group] {$gServerTable.helper[group]};
  102. #local colorBody[group] {$gServerTable.helper[colorGroup]};
  103. #list gServerTable[body] add {{$body}};
  104. #list gServerTable[colorBody] add {{$colorBody}};
  105. } {4.95};
  106. #action {~{*UTF8}{?:^}%!c├─{?:(?:─|┼)*}%c{\p{Han}+}%c{?:(?:─|┼)+}──┤%c{|re/ParseServerTable/group}$} {
  107. #var gServerTable.helper[colorGroup] {%%1%%2};
  108. #var gServerTable.helper[group] {%%2};
  109. };
  110. #action {^├──{(─|┴)+}──┤$} {
  111. #var gServerTable[note] {};
  112. };
  113. #action {{*UTF8}{?:^}╰─{?:(?:─|┴)+}{\p{Han}+}{?:(?:─|┴)+}─╯{|re/ParseServerTable/body}$} {
  114. #class re.ParseServerTable kill;
  115. #if { "$gServerTable.helper[gag]" == "gag" } {
  116. #line gag;
  117. };
  118. #if { "$gServerTable.helper[callback]" !== "" } {
  119. #line sub {escapes;var;func} ga.Wait {
  120. $gServerTable.helper[callback];
  121. };
  122. };
  123. };
  124. #class re.ParseServerTable close;
  125. };