Skip to content

Commit

Permalink
create symlinks for Pāli Tipiṭaka pages
Browse files Browse the repository at this point in the history
  • Loading branch information
siongui committed Oct 24, 2023
1 parent 09d78c2 commit 4f994b8
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 1 deletion.
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ test_extract_one_dic: fmt
############################
# Bootstrap/Setup Tipiṭaka #
############################
test_tipitaka: test_build_tpk_tree
test_tipitaka: test_build_tpk_tree test_tipitaka_symlink

test_download_tpk: fmt
@echo "\033[92mTesting download Tipiṭaka xml from https://tipitaka.org/romn/ ...\033[0m"
Expand All @@ -167,6 +167,10 @@ test_embed_tpk_toc: fmt clone_tpk_xml
@echo "\033[92mTesting embedding Tipiṭaka Table of content data into Go code ...\033[0m"
@cd tpkutil; go test -v buildtpktree.go embedtpktoc_test.go -args -tpkXmlDir=$(TIPITAKA_XML_DIR) -outputGoFilePath=$(OUTPUT_TIPITAKA_TOC_GO_FILE)
@make fmt

test_tipitaka_symlink: fmt
@echo "\033[92mTesting making Pāli Tipiṭaka symlinks for GitHub Pages...\033[0m"
@cd tpkutil; go test -v symlink.go symlink_test.go -args -outputDir=$(OUTPUT_DIR)
###################################
# End of Bootstrap/Setup Tipiṭaka #
###################################
Expand Down
2 changes: 2 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ This repository consist of:

- test_download_tpk
- test_build_tpk_tree
- test_embed_tpk_toc
- test_tipitaka_symlink

4. Utility methods for offline data processing.
The code is located at `util <util/>`_.
Expand Down
15 changes: 15 additions & 0 deletions lib/tipitaka/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,21 @@ func ActionToCanonPath(action string) string {
return "/romn/" + strings.Replace(noext, ".", "/", -1) + "/"
}

// GetAllCanonPath returns all canon paths according to given script.
func GetAllCanonPath(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))
i := 0
for k := range urlActionMap {
keys[i] = k
i++
}

return keys
}

// PageType represents the type of the webpage, determined according to path of
// URL.
//go:generate stringer -type=PageType
Expand Down
90 changes: 90 additions & 0 deletions tpkutil/symlink.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package tpkutil

// This file make symlinks for Pāli Tipiṭaka SPA (Single Page Application)
// hosted on GitHub Pages.

import (
"os"
"path/filepath"

tpk "github.com/siongui/gopalilib/lib/tipitaka"
"github.com/siongui/gopalilib/util"
)

// CreatePageSymlink create symbolic links for Pali pages to the root index.html
// of the website root directory. This is for deploying single page application
// (SPA) on GitHub Pages or GitLab Pages, which serve only static website
// content.
//
// The URL path of a Pali page looks like:
//
// /romn/cscd/vin01m/mul0/
//
// The page contains the content of the Pali texts.
func CreatePageSymlink(pagePath, root string) (err error) {
// create dir of the page
pageIndexAbs := filepath.Join(root, pagePath, "index.html")
util.CreateDirIfNotExist(pageIndexAbs)
//fmt.Println("pageIndexAbs:", pageIndexAbs)

err = os.Chdir(root)
if err != nil {
return
}

pageIndex := filepath.Join(pagePath, "index.html")
// remove heading /
pageIndex = pageIndex[1:]
//fmt.Println("page index.html path:", pageIndex)
err = os.Symlink("../../../../index.html", pageIndex)
if os.IsExist(err) {
// If the symlink we want to create already exist, error will be
// raised. Remove the existing symlink and create new symlink.
os.Remove(pageIndex)
err = os.Symlink("../../../../index.html", pageIndex)
if err != nil {
return
}
}
if err != nil {
return
}

util.LocalPrintln(pagePath)
return
}

// SymlinkToRootIndexHtml creates symbolic links which points all pages of the
// website to the root index.html in the root directory of the website. The
// purpose is to deploy single page application (SPA) on GitHub Pages or GitLab
// Pages, which serves only static website contents.
//
// Only one page: ``/index.html``
//
// All other webpages are symlinks to ``/index.html``
func SymlinkToRootIndexHtml(websiteroot string, script string) (err error) {
// FIXME TODO: script param is not respected right now. return only romn

wd, err := os.Getwd()
if err != nil {
return
}
websiteroot, err = filepath.Abs(websiteroot)
if err != nil {
return
}
//fmt.Println(websiteroot)
//return

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

// change back to original directory to prevent causing unwanted results
// if users call this methods multiple times in program
return os.Chdir(wd)
}
15 changes: 15 additions & 0 deletions tpkutil/symlink_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package tpkutil

import (
"flag"
"testing"
)

var outputDir = flag.String("outputDir", ".", "output dir")

func TestSymlinkToRootIndexHtml(t *testing.T) {
err := SymlinkToRootIndexHtml(*outputDir, "romn")
if err != nil {
t.Error(err)
}
}

0 comments on commit 4f994b8

Please sign in to comment.