/
locator.go
67 lines (56 loc) · 1.46 KB
/
locator.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
/*
Wait for the new filesystem API planned by a Go proposal (from the core team).
This is currentyl just a stand-in for a real implementation.
That means: it's a quick hack!
It grows whenever I add some functionality needed for tests. Everything here
is quick and dirty right now.
*/
package locate
import (
"fmt"
"os"
"path/filepath"
"github.com/npillmayer/tyse/core/hyphenation"
)
func gtrootdir() string {
gtroot := os.Getenv("TYSEROOT")
if gtroot == "" {
gopath := os.Getenv("GOPATH")
if gopath == "" {
gtroot = os.Getenv("HOME") + "/tyse"
} else {
gtroot = gopath + "/src/github.com/npillmayer/gotype/GTROOT"
}
}
return gtroot
}
// Return path for a resource file
func FileResource(item string, typ string) string {
gtroot := gtrootdir()
var path string
switch typ {
case "lua":
path = gtroot + "/lib/lua/" + item + ".lua"
case "font":
path = filepath.Join(os.Getenv("HOME"), "Library", "Fonts", item)
case "pattern":
path = filepath.Join(gtroot, "pattern", item)
//path = "/Users/npi/prg/go/gotype/etc/" + item
}
return path
}
var dicts map[string]*hyphenation.Dictionary
func Dictionary(loc string) *hyphenation.Dictionary {
if dicts == nil {
dicts = make(map[string]*hyphenation.Dictionary)
}
if dicts[loc] == nil {
pname := "hyph-en-us.tex"
d := hyphenation.LoadPatterns(FileResource(pname, "pattern"))
dicts[loc] = d
}
if dicts[loc] == nil {
panic(fmt.Sprintf("No dictionnary found for %s", loc))
}
return dicts[loc]
}