57 lines
1.7 KiB
Go
57 lines
1.7 KiB
Go
package braille
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"sort"
|
|
"strings"
|
|
)
|
|
|
|
const tableDirectory = "/usr/share/liblouis/tables"
|
|
|
|
type TableOption struct {
|
|
Label string
|
|
File string
|
|
}
|
|
|
|
// AvailableTables lists installed literary and computer tables for the
|
|
// current message locale. English US also offers UEB tables.
|
|
func AvailableTables() []TableOption {
|
|
locale := ""
|
|
for _, key := range []string{"LC_ALL", "LC_MESSAGES", "LANG"} {
|
|
if value := os.Getenv(key); value != "" {
|
|
locale = value
|
|
break
|
|
}
|
|
}
|
|
return tablesForLocale(tableDirectory, locale)
|
|
}
|
|
|
|
func tablesForLocale(dir, locale string) []TableOption {
|
|
locale = strings.ToLower(strings.ReplaceAll(strings.Split(strings.Split(locale, ".")[0], "@")[0], "_", "-"))
|
|
if locale == "" || locale == "c" || locale == "posix" {
|
|
locale = "en-us"
|
|
}
|
|
entries, err := os.ReadDir(dir)
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
var result []TableOption
|
|
for _, entry := range entries {
|
|
name := entry.Name()
|
|
lower := strings.ToLower(name)
|
|
if entry.IsDir() || !(strings.HasSuffix(lower, ".ctb") || strings.HasSuffix(lower, ".utb")) ||
|
|
strings.Contains(lower, "interline") || strings.Contains(lower, "mathtext") ||
|
|
!(strings.HasPrefix(lower, locale+"-") || locale == "en-us" && strings.HasPrefix(lower, "en-ueb-")) {
|
|
continue
|
|
}
|
|
label := strings.TrimSuffix(strings.TrimSuffix(name, ".ctb"), ".utb")
|
|
label = strings.ReplaceAll(label, "-g1", " Grade 1")
|
|
label = strings.ReplaceAll(label, "-g2", " Grade 2")
|
|
label = strings.ReplaceAll(label, "-g3", " Grade 3")
|
|
result = append(result, TableOption{Label: label, File: filepath.Join(dir, name)})
|
|
}
|
|
sort.Slice(result, func(i, j int) bool { return result[i].Label < result[j].Label })
|
|
return result
|
|
}
|