-
Notifications
You must be signed in to change notification settings - Fork 11
/
format_source_block.awk
executable file
·68 lines (61 loc) · 1.43 KB
/
format_source_block.awk
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
#! /usr/bin/awk -f
BEGIN{
IGNORECASE=1;
# LINT=1;
source_block_syntax_start="```"
source_block_syntax_end="```"
}
function min(n,m)
{
if (n<m){
return n;
}
return m;
}
function is_blank(content)
{
return content ~ "^[[:blank:]]*$"
}
function rtrim(content)
{
# match(content, "[[:space:]]*$")
# return substr(content,)
sub("[[:space:]]*$", "", content)
return content
}
# _ARGVEND_ 后面的参数是用来定义局部变量的,不做真正的参数用,详情请参见[[https://www.ibm.com/developerworks/cn/linux/l-cn-awkf/index.html]]
function collect_src_block(_ARGVEND_, src_block, src_lines,idx,i,min_blank_num)
{
idx = 0;
min_blank_num = 1000;
# 跳过代码快中最开始的空行
getline
while(is_blank($0)){
getline
}
while($0 !~ source_block_syntax_end){
src_lines[idx] = $0;
idx++;
if(! is_blank($0)){ # skip blank lines
match($0,"^ *"); # We should only delete the space, since the TAB is import in the makefile
min_blank_num = min(RLENGTH,min_blank_num);
}
getline;
}
for(i=0; i<idx; i++)
{
src_block = src_block substr(src_lines[i], min_blank_num+1) ORS;
}
return rtrim(src_block);
}
{
if(match($0, source_block_syntax_start)){
print $0;
print collect_src_block();
print $0;
}
else
{
print $0;
}
}