Skip to content

Commit

Permalink
rename canon to pali text
Browse files Browse the repository at this point in the history
  • Loading branch information
siongui committed Oct 25, 2023
1 parent 4f994b8 commit ca8c7ec
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 35 deletions.
45 changes: 24 additions & 21 deletions lib/tipitaka/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,27 @@ import (

// The url path of Pali tipitaka website will be
//
// [rootPath]/[locale]/[canonPath]
// [rootPath]/[locale]/[paliTextPath]

var actionUrlMap map[string]string
var urlActionMap map[string]string
var actionToPaliTextPathMap map[string]string
var paliTextPathToActionMap map[string]string

func traverse(tree lib.Tree, indent int) {
//print(strings.Repeat(" ", indent))
//println(TrimTreeText(tree.Text))
if tree.Action != "" {
//println(tree.Action)
actionUrlMap[tree.Action] = ActionToCanonPath(tree.Action)
urlActionMap[ActionToCanonPath(tree.Action)] = tree.Action
actionToPaliTextPathMap[tree.Action] = ActionToPaliTextPath(tree.Action)
paliTextPathToActionMap[ActionToPaliTextPath(tree.Action)] = tree.Action
}
for _, subtree := range tree.SubTrees {
traverse(subtree, indent+2)
}
}

func init() {
actionUrlMap = make(map[string]string)
urlActionMap = make(map[string]string)
actionToPaliTextPathMap = make(map[string]string)
paliTextPathToActionMap = make(map[string]string)

b, _ := toc.ReadFile("tpktoc.json")
//println(string(b))
Expand All @@ -41,21 +41,24 @@ func init() {
traverse(tree, 0)
}

// ActionToCanonPath converts action string to canon path in URL.
func ActionToCanonPath(action string) string {
// ActionToPaliTextPath converts action string to pali text path in URL.
// For example, "cscd/vin01m.mul2.xml" to "/romn/cscd/vin01m/mul2/".
// TODO FIXME: add *script* and *edition* parameters in the future.
func ActionToPaliTextPath(action string) string {
noext := strings.TrimSuffix(action, filepath.Ext(action))
// TODO: FIXME: elegant way to support different script and edition.
// TODO FIXME: find elegant way to support different script and edition.
return "/romn/" + strings.Replace(noext, ".", "/", -1) + "/"
}

// GetAllCanonPath returns all canon paths according to given script.
func GetAllCanonPath(script string) []string {
// GetAllPaliTextPath returns all canon paths according to given script.
// TODO FIXME: add *edition* parameter in the future.
func GetAllPaliTextPath(script string) []string {
// FIXME TODO: script param is not respected right now. return only romn

// https://stackoverflow.com/a/27848197
keys := make([]string, len(urlActionMap))
keys := make([]string, len(paliTextPathToActionMap))
i := 0
for k := range urlActionMap {
for k := range paliTextPathToActionMap {
keys[i] = k
i++
}
Expand All @@ -70,7 +73,7 @@ type PageType int

const (
RootPage PageType = iota
CanonPage
PaliTextPage
NoSuchPage
)

Expand All @@ -94,23 +97,23 @@ func DeterminePageType(urlpath string) PageType {
if urlpath == "/" {
return RootPage
}
if IsValidCanonUrlPath(urlpath) {
return CanonPage
if IsValidPaliTextUrlPath(urlpath) {
return PaliTextPage
}

return NoSuchPage
}

// IsValidCanonUrlPath will return true if the path of the url is a possible
// IsValidPaliTextUrlPath will return true if the path of the url is a possible
// canon page.
func IsValidCanonUrlPath(urlpath string) bool {
func IsValidPaliTextUrlPath(urlpath string) bool {
urlpath, _ = dictionary.GetNormalizedUrlPath(urlpath)

_, ok := urlActionMap[urlpath]
_, ok := paliTextPathToActionMap[urlpath]
return ok
}

// ActionToUrlPath converts action string to url path.
func ActionToUrlPath(action string) string {
return dictionary.AddRootPathAndCurrentLocaleToUrlPath(ActionToCanonPath(action))
return dictionary.AddRootPathAndCurrentLocaleToUrlPath(ActionToPaliTextPath(action))
}
26 changes: 13 additions & 13 deletions lib/tipitaka/url_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ import (
"testing"
)

func TestActionToCanonPath(t *testing.T) {
s := ActionToCanonPath("cscd/vin01m.mul2.xml")
func TestActionToPaliTextPath(t *testing.T) {
s := ActionToPaliTextPath("cscd/vin01m.mul2.xml")
if s != "/romn/cscd/vin01m/mul2/" {
t.Error(s)
return
}
}

func TestUrlActionMap(t *testing.T) {
for k, v := range actionUrlMap {
if k2, ok := urlActionMap[v]; !ok && k2 != k {
func TestPaliTextPathToActionMap(t *testing.T) {
for k, v := range actionToPaliTextPathMap {
if k2, ok := paliTextPathToActionMap[v]; !ok && k2 != k {
t.Error(k, v, k2)
return
}
Expand All @@ -31,7 +31,7 @@ func TestDeterminePageType(t *testing.T) {
if DeterminePageType("/pali-dictionary/") != NoSuchPage {
t.Error("/pali-dictionary/")
}
if DeterminePageType("/romn/cscd/vin01m/mul2/") != CanonPage {
if DeterminePageType("/romn/cscd/vin01m/mul2/") != PaliTextPage {
t.Error("/romn/cscd/vin01m/mul2/")
}
if DeterminePageType("/pali-dictionary/cscd/vin01m/mul2/") != NoSuchPage {
Expand All @@ -46,7 +46,7 @@ func TestDeterminePageType(t *testing.T) {
if DeterminePageType("/pali-dictionary/") != RootPage {
t.Error("/pali-dictionary/")
}
if DeterminePageType("/pali-dictionary/romn/cscd/vin01m/mul2/") != CanonPage {
if DeterminePageType("/pali-dictionary/romn/cscd/vin01m/mul2/") != PaliTextPage {
t.Error("/pali-dictionary/romn/cscd/vin01m/mul2/")
}
if DeterminePageType("/pali-dictionary/zh_TW/cscd/vin01m/mul2/") != NoSuchPage {
Expand All @@ -61,33 +61,33 @@ func TestDeterminePageType(t *testing.T) {
//if DeterminePageType("/pali-dictionary/cscd/vin01m/mul2/") != NoSuchPage {
// t.Error("/pali-dictionary/cscd/vin01m/mul2/")
//}
if DeterminePageType("/pali-dictionary/zh_TW/romn/cscd/vin01m/mul2/") != CanonPage {
if DeterminePageType("/pali-dictionary/zh_TW/romn/cscd/vin01m/mul2/") != PaliTextPage {
t.Error("/pali-dictionary/zh_TW/romn/cscd/vin01m/mul2/")
}
if DeterminePageType("/pali-dictionary/zh_TW/abc/cscd/vin01m/mul2/") != NoSuchPage {
t.Error("/pali-dictionary/zh_TW/abc/cscd/vin01m/mul2/")
}
}

func TestIsValidCanonUrlPath(t *testing.T) {
func TestIsValidPaliTextUrlPath(t *testing.T) {
SetSiteUrl("")
SetCurrentLocale("")

if IsValidCanonUrlPath("/romn/cscd/vin01m/mul2/") != true {
if IsValidPaliTextUrlPath("/romn/cscd/vin01m/mul2/") != true {
t.Error("/romn/cscd/vin01m/mul2/")
}

if IsValidCanonUrlPath("/abc/cscd/vin01m/mul2/") != false {
if IsValidPaliTextUrlPath("/abc/cscd/vin01m/mul2/") != false {
t.Error("/abc/cscd/vin01m/mul2/")
}

SetSiteUrl("https://siongui.gitlab.io/pali-dictionary/")
if IsValidCanonUrlPath("/pali-dictionary/romn/cscd/vin01m/mul2/") != true {
if IsValidPaliTextUrlPath("/pali-dictionary/romn/cscd/vin01m/mul2/") != true {
t.Error("/pali-dictionary/romn/cscd/vin01m/mul2/")
}

SetCurrentLocale("zh_TW")
if IsValidCanonUrlPath("/pali-dictionary/zh_TW/romn/cscd/vin01m/mul2/") != true {
if IsValidPaliTextUrlPath("/pali-dictionary/zh_TW/romn/cscd/vin01m/mul2/") != true {
t.Error("/pali-dictionary/zh_TW/romn/cscd/vin01m/mul2/")
}
}
Expand Down
2 changes: 1 addition & 1 deletion tpkutil/symlink.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func SymlinkToRootIndexHtml(websiteroot string, script string) (err error) {
//fmt.Println(websiteroot)
//return

paths := tpk.GetAllCanonPath("romn")
paths := tpk.GetAllPaliTextPath("romn")
for _, p := range paths {
err = CreatePageSymlink(p, websiteroot)
if err != nil {
Expand Down

0 comments on commit ca8c7ec

Please sign in to comment.