-
Notifications
You must be signed in to change notification settings - Fork 1
/
progdoc.zig
3084 lines (2824 loc) · 110 KB
/
progdoc.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
const std=@import("std");
const expect=std.testing.expect;
const expectEqualSlices=std.testing.expectEqualSlices;
const expectEqual=std.testing.expectEqual;
const expectError=std.testing.expectError;
const print=std.debug.print;
const assert=std.debug.assert;
const process=std.process;
//编写说明:
//1.按规范编写方式,不应该把所有的写到1个源代码文件中,应该根据功能分为若干个源代码文件。
//但考虑到这个程序功能单一,规模又小,就不费那个劲了。
//2.编程最难的是起名,最烦的是中后期debug。
//做为一个英文非母语的中国程序员,起一个对应中文名字的英文名太难了。
//所以把函数名和主要变量、类型名均起成中文名字,写起程序来顺畅多了,而且少写太多的注释。
//这个程序基本定型了,也不指望有多少人(包括国外程序员)有贡献,所以不用考虑外国人看不懂中文名字了。
//总体流程:
//逐行解析出 @"当前行类别",并且和@"上一行状态",一起推导出@"当前行状态";
//根据@"当前行状态",执行对应的函数处理当前行;
//对于表格而言,还须结合@"上一行表格状态",执行对应函数处理当前表格行;
//对于表格的每个单元格,还需要判定@"当前表格单元格类别",来填充单元格内容。
//一些大写字符含义:T 表示类型; S表示开始,C表示继续,E表示结束。
//绝大部分函数都有对应的测试用例,每写一个函数,就顺手定了测试,这是zig语言的优势所在,很方便。
const @"T当前行状态"=enum{
@"格式正文行",
@"标题行",
@"多行代码行S",
@"多行代码行E",
@"多行代码行C",
@"列表S",
@"列表C",
@"表格S",
@"表格C",
@"内嵌html多行S",
@"内嵌html多行C",
@"内嵌html多行E",
};
//单行列表行、多行列表行S、多行列表行E前面可以有空格、制表符这两类空白字符;其余类别必须是在行首。
//单行列表行、多行列表行S、多行列表行E可以在表格单元格内,其余类别不可以。
//为了区分,多行代码SE、多行列表行SE后面不能有任何字符,只能是换行符或文件尾。
//多行表格行SE、内嵌html多行S、内嵌html多行E后面到行尾中间可以有字符,这些字符无任何用处。
const @"T当前行类别"=enum{
@"格式正文行",
@"标题行", // 1.1. AAA
@"多行代码S", // ``` AAA.zig
@"多行代码SE", // ```
@"单行列表行", // #- AAA
@"多行列表行S", // #-- AAA
@"多行列表行E", // #--
@"多行表格行SE", // #|-
@"单行表格行", // #| AAA
@"内嵌html多行S", // #<<
@"内嵌html多行E", // #>>
};
const @"T上一行状态"=enum{
@"无后续影响",
@"多行代码行C",
@"列表C",
@"表格C",
@"内嵌html多行C",
};
const @"T当前表格单元格类别"=enum{
@"格式正文行",
@"单行列表行",
@"多行列表行S",
@"多行列表行E",
};
//多行表格行是指多行表示1个表格行。
const @"T上一行表格状态"=enum{
@"单行表格行C",
@"多行表格开始行", // 整个表格开始行,后面是多行表格行
@"多行表格行开始行", // 在表格中的多行表格开始行
@"多行表格行C",
};
const @"Tprogdoc格式转换状态机"=struct{
in:[]const u8, // 读取缓冲区
out:std.ArrayList(u8), // 多行表格行输出时,需要暂时变到多行输出缓冲
@"行号":usize=1, // 主要用于出错信息显示
@"当前行开始索引":usize=0,
@"当前行类别":@"T当前行类别"=undefined,
@"上一行状态":@"T上一行状态"=.@"无后续影响",
index:usize=0, // 当前行解析的当前字符索引
@"标题级数":u8=0,
@"目录":std.ArrayList(u8), // 目录暂时缓冲区,文本处理完后插入到输出流的@"插入目录位置"
@"插入目录位置":usize=0,
@"当前行状态":@"T当前行状态"=.@"格式正文行",
@"is输入尾部":bool=false, // 当 true 时表示读到文件尾部,需要结束处理。
@"文档列表栈":@"T列表栈"=.{}, // 为了处理多级列表,引入多行列表行,需要用栈来实现。
@"表格总列数":usize=0, // 处理表格时,单元格的总个数。
@"表格当前列序号":usize=0,
@"表格单元格列表栈":std.ArrayList(@"T列表栈"), // 处理多行表格行中的列表,以实现在表格单元格中写列表。
@"当前表格单元格类别":@"T当前表格单元格类别"=undefined,
@"上一行表格状态":@"T上一行表格状态"=undefined,
@"多行表格行缓冲":@"T多行表格行缓冲", // 处理多行表格行时的暂时缓冲输出。
@"多行表格temp":std.ArrayList(u8), // 处理多行表格行时的配套变量。
@"内链外链图片temp":std.ArrayList(u8), //处理相应格式时的配套变量。
//
};
// 使用gpa分配器,还可使用arena或其它分配器。检测出多行表格行缓冲和主程序的文件名动态数组有泄漏,但影响不大,不处理了。
//var gpa=std.heap.GeneralPurposeAllocator(.{}){};
//const al=gpa.allocator();
//var al=std.testing.allocator;
var arena=std.heap.ArenaAllocator.init(std.heap.page_allocator);
//defer arena.deinit();
const al=arena.allocator();
//输入的是待解析的文本。后续函数参数使用的均是指针,是为了在函数中能改变状态机的属性值。
fn @"Fn新建状态机"(inbuf:[]const u8) !@"Tprogdoc格式转换状态机"{
var list=try std.ArrayList(u8).initCapacity(al,80);
var list1=try std.ArrayList(u8).initCapacity(al,80);
try list1.appendSlice("<ul>\r\n");
var list2=try std.ArrayList(@"T列表栈").initCapacity(al,16);
var list3=try std.ArrayList(u8).initCapacity(al,80);
var list4=try std.ArrayList(u8).initCapacity(al,80);
var b=try @"Fn新建多行表格行缓冲"();
return .{.in=inbuf, .out=list,
.@"目录"=list1,
.@"表格单元格列表栈"=list2,
.@"多行表格行缓冲"=b,
.@"多行表格temp"=list3,
.@"内链外链图片temp"=list4,};
}
fn @"Fn清空状态机"(s:*@"Tprogdoc格式转换状态机") void{
s.*.in=undefined;
s.*.out.deinit();
s.*.@"目录".deinit();
s.*.@"表格单元格列表栈".deinit();
s.*.@"多行表格temp".deinit();
s.*.@"内链外链图片temp".deinit();
@"Fn删除多行表格行缓冲"(&s.*.@"多行表格行缓冲");
s.*.out=undefined;
}
const progdoc_err=error{
@"短代码无结束`字符",
@"内链无结束]]字符串",
@"内链中含有[[字符串",
@"外链说明无#](结束字符串",
@"外链说明中含有#[或#]字符串",
@"外链链接中含有(字符",
@"外链链接无)结束字符",
@"图片说明无#](结束字符串",
@"图片说明中含有#[或#]字符串",
@"图片链接中含有(字符",
@"图片链接无)结束字符",
@"当前行是多行代码行S但上一行状态是多行代码行C",
@"多行代码没有结束行",
@"push时列表栈满",
@"pop时列表栈空",
@"get时列表栈空",
@"set时列表栈空",
@"多行列表行结束行没有对应的开始行",
@"多行列表行没有对应的结束行",
@"多行表格行缓冲溢出错误",
@"与上一行表格列数不相等",
@"多行表格行是空行",
@"内嵌html结束行没有对应的开始行",
@"当前行是内嵌html多行S但上一行状态是内嵌html多行C",
@"内嵌html多行没有结束行",
};
// 因表格总列数和表格单元格字符数根据实际输入来定,不能在编译期确定,所以设计了简陋的二维动态数组数据结构。
const @"T多行表格行缓冲"=struct {
@"单元格":std.ArrayList(std.ArrayList(u8)),
@"使用长度":usize=0,
@"容量":usize=0,
fn @"Fn获取多行表格行单元格缓冲"(self:@"T多行表格行缓冲",@"序号":usize) !std.ArrayList(u8){
if(@"序号">=self.@"使用长度"){
return progdoc_err.@"多行表格行缓冲溢出错误";
}
return self.@"单元格".items[@"序号"];
}
fn @"Fn多行表格行单元格缓冲追加字符串"(self:*@"T多行表格行缓冲",@"序号":usize,str:[]const u8) !void{
if(@"序号">=self.@"使用长度"){
return progdoc_err.@"多行表格行缓冲溢出错误";
}
try self.@"单元格".items[@"序号"].appendSlice(str);
}
fn @"Fn增加多行表格行单元格缓冲"(self:*@"T多行表格行缓冲") !void{
if(self.@"使用长度">=self.@"容量"){
var i=self.@"容量";
self.@"容量"+=10;
while(i<self.@"容量"):(i+=1){
var j=try std.ArrayList(u8).initCapacity(al,80);
try self.@"单元格".append(j);
}
}else{
self.@"单元格".items[self.@"使用长度"].items.len=0;
}
self.@"使用长度"+=1;
}
fn @"Fn清空多行表格行单元格缓冲"(self:*@"T多行表格行缓冲") void{
var i:usize=0;
while(i<self.@"使用长度"):(i+=1){
self.@"单元格".items[i].items.len=0;
}
}
fn @"Fn多行表格行单元格缓冲全部追加字符串"(self:*@"T多行表格行缓冲",str:[]const u8) !void{
var i:usize=0;
while(i<self.@"使用长度"):(i+=1){
try self.@"单元格".items[i].appendSlice(str);
}
}
};
fn @"Fn新建多行表格行缓冲"() !@"T多行表格行缓冲"{
const c=try std.ArrayList(std.ArrayList(u8)).initCapacity(al,10);
var r:@"T多行表格行缓冲"=.{.@"单元格"=c,.@"容量"=10};
var i:usize=0;
r.@"使用长度"=0;
while(i<r.@"容量"):(i+=1){
var j=try std.ArrayList(u8).initCapacity(al,80);
try r.@"单元格".append(j);
}
return r;
}
fn @"Fn删除多行表格行缓冲"(r:*@"T多行表格行缓冲") void{
//var i:usize=0;
for(r.*.@"单元格".items) |*v|{
v.*.deinit();
}
r.*.@"单元格".deinit();
r.*.@"容量"=0;
r.*.@"使用长度"=0;
r.*.@"单元格"=undefined;
}
test "多行表格行缓冲" {
var r=try @"Fn新建多行表格行缓冲"();
var i:usize=0;
while(i<13):(i+=1){
try r.@"Fn增加多行表格行单元格缓冲"();
}
try expect(r.@"容量"==20);
try expect(r.@"使用长度"==13);
try expectError(progdoc_err.@"多行表格行缓冲溢出错误",r.@"Fn获取多行表格行单元格缓冲"(13));
try r.@"Fn多行表格行单元格缓冲追加字符串"(0,"test");
var x=try r.@"Fn获取多行表格行单元格缓冲"(0);
try expectEqualSlices(u8,x.items,"test");
try r.@"Fn多行表格行单元格缓冲追加字符串"(1,"test1");
try r.@"Fn多行表格行单元格缓冲追加字符串"(1,"test2");
x=try r.@"Fn获取多行表格行单元格缓冲"(1);
try expectEqualSlices(u8,x.items,"test1test2");
try r.@"Fn多行表格行单元格缓冲全部追加字符串"("<end>");
x=try r.@"Fn获取多行表格行单元格缓冲"(0);
try expectEqualSlices(u8,x.items,"test<end>");
x=try r.@"Fn获取多行表格行单元格缓冲"(1);
try expectEqualSlices(u8,x.items,"test1test2<end>");
x=try r.@"Fn获取多行表格行单元格缓冲"(2);
try expectEqualSlices(u8,x.items,"<end>");
x=try r.@"Fn获取多行表格行单元格缓冲"(10);
try x.append('a');
try x.append('b');
try expectEqualSlices(u8,x.items,"<end>ab");
r.@"Fn清空多行表格行单元格缓冲"();
try expect(r.@"容量"==20);
@"Fn删除多行表格行缓冲"(&r);
}
// strs是编译期确定个数的字符串数组,从buf的第start个字符开始,按strs中的先后顺序,逐个和strs中的字符串比对。
// 如果找到任1个,start值增加找到字符串的长度。
// 找到第n个字符串,返回n;没找到,返回 -1。
// strs的顺序非常重要,调用时要考虑好了。如:
// .{"\r\n","\r","\n"} 可正确找出对应字符串,当有"\r\n"时,返回0。
// .{"\r","\r\n","\n"} 不能正确找出,当有"\r\n"时,返回0,而不是返回想要的1。
fn @"Fnis包含字符串"(buf:[]const u8,start:*usize,comptime strs:anytype) i8{
inline for(strs) |str,i|{
if(buf.len-start.*>=str.len){
for(str) |v,j| {
if(v!=buf[start.*+j]){
break;
}
}else{
start.*+=str.len;
return i;
}
}
}
return -1;
}
test "Fnis包含字符串" {
var i:i8=undefined;
var s:usize=0;
i=@"Fnis包含字符串"("#| abc",&s,.{"#-","#--","#|","#|-"});
try expect(i==2);
try expect(s==2);
s=3;
i=@"Fnis包含字符串"("1\r\n#| abc",&s,.{"#-","#--","#|","#|-"});
try expect(i==2);
try expect(s==5);
s=0;
i=@"Fnis包含字符串"("#",&s,.{"#-","#--","#|","#|-"});
try expect(i==-1);
try expect(s==0);
s=0;
i=@"Fnis包含字符串"("#-",&s,.{"#-","#--","#|","#|-"});
try expect(i==0);
try expect(s==2);
s=0;
i=@"Fnis包含字符串"("``\r",&s,.{"```","``\r","``\n","``","`"});
try expect(i==1);
try expect(s==3);
s=0;
i=@"Fnis包含字符串"("",&s,.{"```","``\r","``\n","``","`"});
try expect(i==-1);
try expect(s==0);
}
fn @"Fn判断当前行类别"(ptr:*@"Tprogdoc格式转换状态机") void {
var i=ptr.*.@"当前行开始索引";
var j:i8=-1;
switch(ptr.*.in[i]) {
'`' => {
j=@"Fnis包含字符串"(ptr.*.in,&i,.{"```\r","```\n","```",});
switch(j) {
0,1 => {
ptr.*.@"当前行类别"=.@"多行代码SE";
i-=1;
},
2 => {
if(i==ptr.*.in.len){
ptr.*.@"当前行类别"=.@"多行代码SE";
}else{
ptr.*.@"当前行类别"=.@"多行代码S";
}
},
else => {
ptr.*.@"当前行类别"=.@"格式正文行";
},
}
},
'0'...'9' => {
var @"is标题":bool=false;
var @"句点个数":u8=0;
while(i<ptr.*.in.len):(i+=1){
if(ptr.*.in[i]!='.' and (ptr.*.in[i]<'0' or ptr.*.in[i]>'9')){
break;
}
if(ptr.*.in[i]=='.'){
@"句点个数"+=1;
if(i==ptr.*.in.len-1){
@"is标题"=true;
break;
}
if(ptr.*.in[i+1]<'0' or ptr.*.in[i+1]>'9'){
@"is标题"=true;
break;
}
}
}
if(@"is标题"){
i+=1;
ptr.*.@"当前行类别"=.@"标题行";
ptr.*.@"标题级数"=@"句点个数";
}else{
i=ptr.*.@"当前行开始索引";
ptr.*.@"当前行类别"=.@"格式正文行";
}
},
' ','\t','#' => {
j=@"Fnis包含字符串"(ptr.*.in,&i,.{"#|-","#|","#<<","#>>"});
switch(j){
0 => {
ptr.*.@"当前行类别"=.@"多行表格行SE";
},
1 => {
ptr.*.@"当前行类别"=.@"单行表格行";
},
2 => {
ptr.*.@"当前行类别"=.@"内嵌html多行S";
},
3 => {
ptr.*.@"当前行类别"=.@"内嵌html多行E";
},
else => {
while(i<ptr.*.in.len):(i+=1){
if(ptr.*.in[i]!=' ' and ptr.*.in[i]!='\t'){
break;
}
}
j=@"Fnis包含字符串"(ptr.*.in,&i,.{"#--\r","#--\n","#--","#-"});
switch(j) {
0,1 => {
ptr.*.@"当前行类别"=.@"多行列表行E";
i-=1;
},
2 => {
if(i==ptr.*.in.len){
ptr.*.@"当前行类别"=.@"多行列表行E";
}else{
ptr.*.@"当前行类别"=.@"多行列表行S";
}
},
3 => {
ptr.*.@"当前行类别"=.@"单行列表行";
},
else => {
i=ptr.*.@"当前行开始索引";
ptr.*.@"当前行类别"=.@"格式正文行";
},
}
},
}
},
else => {
ptr.*.@"当前行类别"=.@"格式正文行";
}
}
ptr.*.index=i;
}
test "Fn判断当前行类别 内嵌html" {
var s=try @"Fn新建状态机"("#>>");
@"Fn判断当前行类别"(&s);
try expect(s.@"当前行类别"==.@"内嵌html多行E");
try expect(s.index==3);
s.in="#<<";
@"Fn判断当前行类别"(&s);
try expect(s.@"当前行类别"==.@"内嵌html多行S");
try expect(s.index==3);
@"Fn清空状态机"(&s);
}
test "Fn判断当前行类别 多行代码S 多行代码SE"{
var s=try @"Fn新建状态机"("");
s.in="`";
@"Fn判断当前行类别"(&s);
try expect(s.@"当前行类别"==.@"格式正文行");
try expect(s.index==0);
s.in="```";
@"Fn判断当前行类别"(&s);
try expect(s.@"当前行类别"==.@"多行代码SE");
try expect(s.index==3);
s.in="``";
@"Fn判断当前行类别"(&s);
try expect(s.@"当前行类别"==.@"格式正文行");
try expect(s.index==0);
s.in="```\n";
@"Fn判断当前行类别"(&s);
try expect(s.@"当前行类别"==.@"多行代码SE");
try expect(s.index==3);
s.in="```\r\n";
@"Fn判断当前行类别"(&s);
try expect(s.@"当前行类别"==.@"多行代码SE");
try expect(s.index==3);
s.in="````";
@"Fn判断当前行类别"(&s);
try expect(s.@"当前行类别"==.@"多行代码S");
try expect(s.index==3);
s.in="``` hello.zig";
@"Fn判断当前行类别"(&s);
try expect(s.@"当前行类别"==.@"多行代码S");
try expect(s.index==3);
s.in="012345\r\n```\r\n``` test.zig\r\n";
s.@"当前行开始索引"=8;
@"Fn判断当前行类别"(&s);
try expect(s.@"当前行类别"==.@"多行代码SE");
try expect(s.index==11);
s.@"当前行开始索引"=13;
@"Fn判断当前行类别"(&s);
try expect(s.@"当前行类别"==.@"多行代码S");
try expect(s.index==16);
@"Fn清空状态机"(&s);
}
test "Fn判断当前行类别 标题"{
var s=try @"Fn新建状态机"("");
s.in="1";
@"Fn判断当前行类别"(&s);
try expect(s.@"当前行类别"==.@"格式正文行");
try expect(s.index==0);
s.in=".";
@"Fn判断当前行类别"(&s);
try expect(s.@"当前行类别"==.@"格式正文行");
try expect(s.index==0);
s.in="1.";
@"Fn判断当前行类别"(&s);
try expect(s.@"当前行类别"==.@"标题行");
try expect(s.@"标题级数"==1);
try expect(s.index==2);
s.in="1.\r\n";
@"Fn判断当前行类别"(&s);
try expect(s.@"当前行类别"==.@"标题行");
try expect(s.@"标题级数"==1);
try expect(s.index==2);
s.in=" 12.135..12";
@"Fn判断当前行类别"(&s);
try expect(s.@"当前行类别"==.@"格式正文行");
try expect(s.index==0);
s.in="12.135..12";
@"Fn判断当前行类别"(&s);
try expect(s.@"当前行类别"==.@"标题行");
try expect(s.@"标题级数"==2);
try expect(s.index==7);
s.in="12.1.1.5. test";
@"Fn判断当前行类别"(&s);
try expect(s.@"当前行类别"==.@"标题行");
try expect(s.@"标题级数"==4);
try expect(s.index==9);
s.in="123456";
@"Fn判断当前行类别"(&s);
try expect(s.@"当前行类别"==.@"格式正文行");
try expect(s.index==0);
s.in="0123\n1";
s.@"当前行开始索引"=5;
@"Fn判断当前行类别"(&s);
try expect(s.@"当前行类别"==.@"格式正文行");
try expect(s.index==5);
s.in="0123\n.";
s.@"当前行开始索引"=5;
@"Fn判断当前行类别"(&s);
try expect(s.@"当前行类别"==.@"格式正文行");
try expect(s.index==5);
s.in="0123\n1.\r\n";
s.@"当前行开始索引"=5;
@"Fn判断当前行类别"(&s);
try expect(s.@"当前行类别"==.@"标题行");
try expect(s.@"标题级数"==1);
try expect(s.index==7);
s.in="0123\n 12.135..12";
s.@"当前行开始索引"=5;
@"Fn判断当前行类别"(&s);
try expect(s.@"当前行类别"==.@"格式正文行");
try expect(s.index==5);
s.in="0123\n12.135..12";
s.@"当前行开始索引"=5;
@"Fn判断当前行类别"(&s);
try expect(s.@"当前行类别"==.@"标题行");
try expect(s.@"标题级数"==2);
try expect(s.index==12);
s.in="0123\n12.1.1.5. test";
s.@"当前行开始索引"=5;
@"Fn判断当前行类别"(&s);
try expect(s.@"当前行类别"==.@"标题行");
try expect(s.@"标题级数"==4);
try expect(s.index==14);
s.in="0123\n123456";
s.@"当前行开始索引"=5;
@"Fn判断当前行类别"(&s);
try expect(s.@"当前行类别"==.@"格式正文行");
try expect(s.index==5);
@"Fn清空状态机"(&s);
}
test "Fn判断当前行类别 表格行"{
var s=try @"Fn新建状态机"("");
s.in="#";
@"Fn判断当前行类别"(&s);
try expect(s.@"当前行类别"==.@"格式正文行");
try expect(s.index==0);
s.in="#advbdafs";
@"Fn判断当前行类别"(&s);
try expect(s.@"当前行类别"==.@"格式正文行");
try expect(s.index==0);
s.in=" #|";
@"Fn判断当前行类别"(&s);
try expect(s.@"当前行类别"==.@"格式正文行");
try expect(s.index==0);
s.in="#|";
@"Fn判断当前行类别"(&s);
try expect(s.@"当前行类别"==.@"单行表格行");
try expect(s.index==2);
s.in="#|\r\n";
@"Fn判断当前行类别"(&s);
try expect(s.@"当前行类别"==.@"单行表格行");
try expect(s.index==2);
s.in="#| adfsad ";
@"Fn判断当前行类别"(&s);
try expect(s.@"当前行类别"==.@"单行表格行");
try expect(s.index==2);
s.in="#|-";
@"Fn判断当前行类别"(&s);
try expect(s.@"当前行类别"==.@"多行表格行SE");
try expect(s.index==3);
s.in="#|----dd#|---";
@"Fn判断当前行类别"(&s);
try expect(s.@"当前行类别"==.@"多行表格行SE");
try expect(s.index==3);
s.in="0123\n#";
s.@"当前行开始索引"=5;
@"Fn判断当前行类别"(&s);
try expect(s.@"当前行类别"==.@"格式正文行");
try expect(s.index==5);
s.in="0123\n#advbdafs";
s.@"当前行开始索引"=5;
@"Fn判断当前行类别"(&s);
try expect(s.@"当前行类别"==.@"格式正文行");
try expect(s.index==5);
s.in="0123\n #|";
s.@"当前行开始索引"=5;
@"Fn判断当前行类别"(&s);
try expect(s.@"当前行类别"==.@"格式正文行");
try expect(s.index==5);
s.in="0123\n#|\r\n";
s.@"当前行开始索引"=5;
@"Fn判断当前行类别"(&s);
try expect(s.@"当前行类别"==.@"单行表格行");
try expect(s.index==7);
s.in="0123\n#| adfsad ";
s.@"当前行开始索引"=5;
@"Fn判断当前行类别"(&s);
try expect(s.@"当前行类别"==.@"单行表格行");
try expect(s.index==7);
s.in="0123\n#|-";
s.@"当前行开始索引"=5;
@"Fn判断当前行类别"(&s);
try expect(s.@"当前行类别"==.@"多行表格行SE");
try expect(s.index==8);
s.in="0123\n#|----dd#|---";
s.@"当前行开始索引"=5;
@"Fn判断当前行类别"(&s);
try expect(s.@"当前行类别"==.@"多行表格行SE");
try expect(s.index==8);
@"Fn清空状态机"(&s);
}
test "Fn判断当前行类别 列表行"{
var s=try @"Fn新建状态机"("");
s.in="#-";
@"Fn判断当前行类别"(&s);
try expect(s.@"当前行类别"==.@"单行列表行");
try expect(s.index==2);
s.in=" abc\r\n";
@"Fn判断当前行类别"(&s);
try expect(s.@"当前行类别"==.@"格式正文行");
try expect(s.index==0);
s.in="#-\r\n";
@"Fn判断当前行类别"(&s);
try expect(s.@"当前行类别"==.@"单行列表行");
try expect(s.index==2);
s.in="#- ";
@"Fn判断当前行类别"(&s);
try expect(s.@"当前行类别"==.@"单行列表行");
try expect(s.index==2);
s.in="#- aadf\n";
@"Fn判断当前行类别"(&s);
try expect(s.@"当前行类别"==.@"单行列表行");
try expect(s.index==2);
s.in="# -";
@"Fn判断当前行类别"(&s);
try expect(s.@"当前行类别"==.@"格式正文行");
try expect(s.index==0);
s.in=" \t \t\t #- adfsaf";
@"Fn判断当前行类别"(&s);
try expect(s.@"当前行类别"==.@"单行列表行");
try expect(s.index==10);
s.in="#--";
@"Fn判断当前行类别"(&s);
try expect(s.@"当前行类别"==.@"多行列表行E");
try expect(s.index==3);
s.in="#--\r\n adfaf";
@"Fn判断当前行类别"(&s);
try expect(s.@"当前行类别"==.@"多行列表行E");
try expect(s.index==3);
s.in=" \t#--\r\n adfaf";
@"Fn判断当前行类别"(&s);
try expect(s.@"当前行类别"==.@"多行列表行E");
try expect(s.index==6);
s.in="#-- adfaf";
@"Fn判断当前行类别"(&s);
try expect(s.@"当前行类别"==.@"多行列表行S");
try expect(s.index==3);
s.in=" #-- adfaf";
@"Fn判断当前行类别"(&s);
try expect(s.@"当前行类别"==.@"多行列表行S");
try expect(s.index==5);
s.in="0123\n#-\r\n";
s.@"当前行开始索引"=5;
@"Fn判断当前行类别"(&s);
try expect(s.@"当前行类别"==.@"单行列表行");
try expect(s.index==7);
s.in="0123\n#- ";
s.@"当前行开始索引"=5;
@"Fn判断当前行类别"(&s);
try expect(s.@"当前行类别"==.@"单行列表行");
try expect(s.index==7);
s.in="0123\n#- aadf\n";
s.@"当前行开始索引"=5;
@"Fn判断当前行类别"(&s);
try expect(s.@"当前行类别"==.@"单行列表行");
try expect(s.index==7);
s.in="0123\n# -";
s.@"当前行开始索引"=5;
@"Fn判断当前行类别"(&s);
try expect(s.@"当前行类别"==.@"格式正文行");
try expect(s.index==5);
s.in="0123\n \t \t\t #- adfsaf";
s.@"当前行开始索引"=5;
@"Fn判断当前行类别"(&s);
try expect(s.@"当前行类别"==.@"单行列表行");
try expect(s.index==15);
s.in="0123\n#--";
s.@"当前行开始索引"=5;
@"Fn判断当前行类别"(&s);
try expect(s.@"当前行类别"==.@"多行列表行E");
try expect(s.index==8);
s.in="0123\n#--\r\n adfaf";
s.@"当前行开始索引"=5;
@"Fn判断当前行类别"(&s);
try expect(s.@"当前行类别"==.@"多行列表行E");
try expect(s.index==8);
s.in="0123\n \t#--\r\n adfaf";
s.@"当前行开始索引"=5;
@"Fn判断当前行类别"(&s);
try expect(s.@"当前行类别"==.@"多行列表行E");
try expect(s.index==11);
s.in="0123\n#-- adfaf";
s.@"当前行开始索引"=5;
@"Fn判断当前行类别"(&s);
try expect(s.@"当前行类别"==.@"多行列表行S");
try expect(s.index==8);
s.in="0123\n #-- adfaf";
s.@"当前行开始索引"=5;
@"Fn判断当前行类别"(&s);
try expect(s.@"当前行类别"==.@"多行列表行S");
try expect(s.index==10);
@"Fn清空状态机"(&s);
}
// 仅对< > 进行实体替换,其它字符原样输出。用于多行代码行。
fn @"Fn无格式正文字符"(ptr:*@"Tprogdoc格式转换状态机") !void{
switch(ptr.*.in[ptr.*.index]){
'<' => {
try ptr.*.out.appendSlice("<");
},
'>' => {
try ptr.*.out.appendSlice(">");
},
else => |v|{
try ptr.*.out.append(v);
},
}
ptr.*.index+=1;
}
test "Fn无格式正文字符" {
var s=try @"Fn新建状态机"("a<b>c#`d\n");
try @"Fn无格式正文字符"(&s);
try expectEqualSlices(u8,s.out.items,"a");
try @"Fn无格式正文字符"(&s);
try expectEqualSlices(u8,s.out.items,"a<");
try @"Fn无格式正文字符"(&s);
try expectEqualSlices(u8,s.out.items,"a<b");
try @"Fn无格式正文字符"(&s);
try expectEqualSlices(u8,s.out.items,"a<b>");
try @"Fn无格式正文字符"(&s);
try expectEqualSlices(u8,s.out.items,"a<b>c");
try @"Fn无格式正文字符"(&s);
try @"Fn无格式正文字符"(&s);
try @"Fn无格式正文字符"(&s);
try expectEqualSlices(u8,s.out.items,"a<b>c#`d");
@"Fn清空状态机"(&s);
}
// < > 实体替换,#` 替换为 ` ,用于普通正文字符输出。
fn @"Fn普通正文字符"(ptr:*@"Tprogdoc格式转换状态机") !void{
switch(ptr.*.in[ptr.*.index]){
'<' => {
try ptr.*.out.appendSlice("<");
},
'>' => {
try ptr.*.out.appendSlice(">");
},
'#' => {
const j=@"Fnis包含字符串"(ptr.*.in,&ptr.*.index,.{"#`"});
if(j==0){
try ptr.*.out.append('`');
ptr.*.index-=1;
}else{
try ptr.*.out.append('#');
}
},
else => |v|{
try ptr.*.out.append(v);
},
}
ptr.*.index+=1;
}
test "Fn普通正文字符" {
var s=try @"Fn新建状态机"("t<>#`#e");
try @"Fn普通正文字符"(&s);
try expectEqualSlices(u8,s.out.items,"t");
try expect(s.index==1);
try @"Fn普通正文字符"(&s);
try expectEqualSlices(u8,s.out.items,"t<");
try expect(s.index==2);
try @"Fn普通正文字符"(&s);
try expectEqualSlices(u8,s.out.items,"t<>");
try expect(s.index==3);
try @"Fn普通正文字符"(&s);
try expectEqualSlices(u8,s.out.items,"t<>`");
try expect(s.index==5);
try @"Fn普通正文字符"(&s);
try expectEqualSlices(u8,s.out.items,"t<>`#");
try expect(s.index==6);
s.in="#`";
s.out.items.len=0;
s.index=0;
try @"Fn普通正文字符"(&s);
try expectEqualSlices(u8,s.out.items,"`");
try expect(s.index==2);
@"Fn清空状态机"(&s);
}
// BB`` AAA\r\n
// <code>AAA</code>
//从``后到行尾的所有字符原样输出,仅 < > 字符实体替换和 #` 替换为 `
//在表格中或正文中间慎重使用,容易出错。
fn @"Fn行代码"(ptr:*@"Tprogdoc格式转换状态机") !void{
try ptr.*.out.appendSlice("<code>");
while(ptr.*.index<ptr.*.in.len){
if(ptr.*.in[ptr.*.index]=='\n' or ptr.*.in[ptr.*.index]=='\r'){
try ptr.*.out.appendSlice("</code>");
return;
}
try @"Fn普通正文字符"(ptr);
}
try ptr.*.out.appendSlice("</code>");
}
test "Fn行代码" {
var s=try @"Fn新建状态机"("this ``");
s.index=7;
try @"Fn行代码"(&s);
try expectEqualSlices(u8,s.out.items,"<code></code>");
try expect(s.index==7);
s.in="`` t<h#` #|";
s.index=2;
s.out.items.len=0;
try @"Fn行代码"(&s);
try expectEqualSlices(u8,s.out.items,"<code> t<h` #|</code>");
try expect(s.index==11);
s.in="``test#[#]\r\nthis is";
s.index=2;
s.out.items.len=0;
try @"Fn行代码"(&s);
try expectEqualSlices(u8,s.out.items,"<code>test#[#]</code>");
try expect(s.index==10);
@"Fn清空状态机"(&s);
}
// `AAA`
// <code>AAA</code>
// 主要用于正常输出 [[ 等格式控制用的字符(类似于 \ 逃逸字符,或者用于程序短代码。
fn @"Fn短代码"(ptr:*@"Tprogdoc格式转换状态机") !void{
try ptr.*.out.appendSlice("<code>");
while(ptr.*.index<ptr.*.in.len){
if(ptr.*.in[ptr.*.index]=='\n' or ptr.*.in[ptr.*.index]=='\r'){
return progdoc_err.@"短代码无结束`字符";
}
if(ptr.*.in[ptr.*.index]=='`'){
try ptr.*.out.appendSlice("</code>");
ptr.*.index+=1;
return;
}
try @"Fn普通正文字符"(ptr);
}
return progdoc_err.@"短代码无结束`字符";
}
test "Fn短代码" {
var s=try @"Fn新建状态机"("");
try expectError(progdoc_err.@"短代码无结束`字符",@"Fn短代码"(&s));
s.in="test\na`";
try expectError(progdoc_err.@"短代码无结束`字符",@"Fn短代码"(&s));
s.in="test";
try expectError(progdoc_err.@"短代码无结束`字符",@"Fn短代码"(&s));
s.in="a`test<#`>b`ccc\r\n";
s.index=2;
s.out.items.len=0;
try @"Fn短代码"(&s);
try expectEqualSlices(u8,s.out.items,"<code>test<`>b</code>");
try expect(s.index==12);
@"Fn清空状态机"(&s);
}
// AA`BB`CC
// AA<code>BB</code>
// 该函数主要用于内链、外链说明等处。
fn @"Fn普通正文字符和短代码"(ptr:*@"Tprogdoc格式转换状态机") !void{
if(ptr.in[ptr.*.index]=='`'){
const j=@"Fnis包含字符串"(ptr.*.in,&ptr.*.index,.{"`\n","`\r","``"});
if(j>0 or ptr.*.index==ptr.*.in.len-1){
return progdoc_err.@"短代码无结束`字符";
}
ptr.*.index+=1;
try @"Fn短代码"(ptr);
}else{
try @"Fn普通正文字符"(ptr);
}
}
test "Fn普通正文字符和短代码" {
var s=try @"Fn新建状态机"("a`\r\n``b<`te`b#`c");
s.index=1;
try expectError(progdoc_err.@"短代码无结束`字符",@"Fn普通正文字符和短代码"(&s));
s.index=4;
try expectError(progdoc_err.@"短代码无结束`字符",@"Fn普通正文字符和短代码"(&s));
s.index=6;
s.out.items.len=0;