-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.go
124 lines (99 loc) · 2.92 KB
/
cli.go
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
package main
import (
"embed"
"flag"
"fmt"
"io/fs"
"log"
"os"
"path"
"strings"
"payyans"
)
//go:embed unicode-conversion-maps/maps/*.map
var fontMapsFs embed.FS
//go:embed normalizer/libindic/normalizer/*.rules
var normalizerRulesFs embed.FS
func main() {
asciiToUnicode := flag.Bool("ascii-to-unicode", true, "ASCII to Unicode conversion")
unicodeToAscii := flag.Bool("unicode-to-ascii", false, "Unicode to ASCII conversion")
font := flag.String("font", "", "Font name")
fonts := flag.Bool("fonts", false, "List all available fonts")
fontMapFilePath := flag.String("font-map-file", "", "Path to font map file")
normalizerRulesFilePath := flag.String("normalizer-rules-file", "", "Path to normalizer rules file")
flag.Parse()
if *fonts {
files, err := fs.ReadDir(fontMapsFs, "unicode-conversion-maps/maps")
if err != nil {
log.Fatal(err)
}
for _, file := range files {
fmt.Println(strings.Replace(file.Name(), ".map", "", 1))
}
return
}
if *font == "" && *fontMapFilePath == "" {
fmt.Println("Please specify font with -font or path to font map file with -font-map-file.\n\nUse -fonts to list all available fonts.\n\nUse --help for all available commands.")
return
}
args := flag.Args()
if len(args) < 1 {
log.Fatal("Please provide input filename")
}
inputFilename := args[0]
outputFilename := ""
if len(args) > 1 {
outputFilename = args[1]
}
if *unicodeToAscii {
*asciiToUnicode = false
}
if *asciiToUnicode {
bytes, err := os.ReadFile(inputFilename)
if err != nil {
log.Print("Unable to read file")
log.Fatal(err.Error())
}
var fontMapFileBytes []byte
if *fontMapFilePath != "" {
fontMapFileBytes, err = os.ReadFile(*fontMapFilePath)
if err != nil {
log.Print("Unable to read font map file from filesystem")
log.Fatal(err.Error())
}
} else {
fontMapFileBytes, err = fs.ReadFile(fontMapsFs, path.Join("unicode-conversion-maps", "maps", *font+".map"))
if err != nil {
log.Print("Unable to find font. See list of available fonts with -font")
log.Fatal(err.Error())
}
}
var normalizerMapFileBytes []byte
if *normalizerRulesFilePath != "" {
normalizerMapFileBytes, err = os.ReadFile(*normalizerRulesFilePath)
if err != nil {
log.Print("Unable to read normalizer map file")
log.Fatal(err.Error())
}
} else {
normalizerMapFileBytes, err = fs.ReadFile(normalizerRulesFs, path.Join("normalizer", "libindic", "normalizer", "normalizer_ml.rules"))
if err != nil {
log.Print("Unable to find normalizer map file from binary")
log.Fatal(err.Error())
}
}
outputString, err := payyans.AsciiToUnicodeByMapString(string(bytes), string(fontMapFileBytes), string(normalizerMapFileBytes))
if err != nil {
log.Fatal(err)
}
if outputFilename != "" {
err = os.WriteFile(outputFilename, []byte(outputString), 0644)
if err != nil {
log.Fatal(err)
}
} else {
fmt.Println(outputString)
}
} else {
}
}