forked from Qayisoqho/rime_latinextended
-
Notifications
You must be signed in to change notification settings - Fork 0
/
u.nu
87 lines (76 loc) · 2.12 KB
/
u.nu
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
const ROOT_PATH = ('.' | path expand)
const SOURCE_PATH = ('./src/' | path expand)
const SCHEMA_NAME = 'occidental'
const DICT_FILE = 'dict.tsv'
const LEADING_KEY = '\'
alias open = open -r
alias save = save -f
# Free movement after imported
def --env ensure-pwd [] {
if (pwd) != $ROOT_PATH {
cd $ROOT_PATH
}
}
# For repository `rime-occidental`
export def main [] {
ensure-pwd
build-dict
build-readme
}
export def move-to-rimecfg [] {
ensure-pwd
let rimedir = open "rimedir" | str trim
cp -fv *.yaml $rimedir
}
export alias m = move-to-rimecfg
export def build-and-move [] {
build-dict
move-to-rimecfg
}
export alias bm = build-and-move
export def build-dict [] {
cd $SOURCE_PATH
let dest_file = [$SCHEMA_NAME dict yaml] | str join '.'
let header_file = [$SCHEMA_NAME dict header] | str join '.'
let header = open $header_file
| lines | each {|l| ["#" $l] | str join " "}
| str join "\n"
let meta_file = [$SCHEMA_NAME dict meta yaml] | str join '.'
let meta = open $meta_file
| from yaml | to yaml | str trim # validates yaml by the way
| ['---' $in '...'] | str join "\n"
let dict = open $DICT_FILE
| read-tsv
| add-leading-key $LEADING_KEY
| to-tsv
cd $ROOT_PATH
[$header $meta $dict] | str join "\n\n"
| save $dest_file
}
export def build-readme [] {
cd $SOURCE_PATH
[
(open README.main.md)
(open $DICT_FILE | read-tsv | add-leading-key $LEADING_KEY
| update value {|col| ['``` ' $col.value ' ```'] | str join}
| to md -p)
] | str join "\n"
| save ([$ROOT_PATH README.md] | path join)
}
export def read-tsv [] {
$in | (from tsv
--comment "#"
--trim all
--quote (char us)) # make `"` no more special
}
# wheeled because normal `to tsv` have no option for quotes and escapes
export def to-tsv []: table -> string {
$in | reduce -f "" {
|it acc|
[$it.key $it.value] | str join "\t"
| [$acc $in] | str join "\n"
} | [$in "\n"] | str join # UNIX EOF
}
def add-leading-key [lkey: string] {
$in | update value {|col| [$lkey $col.value] | str join}
}