Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MDX adapted CSS loading supported #5

Merged
merged 7 commits into from
Dec 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions .github/workflows/github-actions-demo.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: GitHub Actions Demo
run-name: ${{ github.actor }} is testing out GitHub Actions 🚀
on: [push]
jobs:
Explore-GitHub-Actions:
runs-on: ubuntu-latest
steps:
- run: echo "🎉 The job was automatically triggered by a ${{ github.event_name }} event."
- run: echo "🐧 This job is now running on a ${{ runner.os }} server hosted by GitHub!"
- run: echo "🔎 The name of your branch is ${{ github.ref }} and your repository is ${{ github.repository }}."
- name: Check out repository code
uses: actions/checkout@v4
- run: echo "💡 The ${{ github.repository }} repository has been cloned to the runner."
- run: echo "🖥️ The workflow is now ready to test your code on the runner."
- name: List files in the repository
run: |
ls ${{ github.workspace }}
- run: echo "🍏 This job's status is ${{ job.status }}."

28 changes: 28 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# This workflow will build a golang project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go

name: Go

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

jobs:

build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.20'

- name: Build
run: go build -v ./...

- name: Test
run: go test -v ./...
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/tmp
mdict_parser
.DS_Store
.luarc.json
5 changes: 3 additions & 2 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ func loadConfig() error { // TODO: more configurations, such as default engine.
if len(c.Dicts) == 0 {
return nil
}
ldoceMdx = c.Dicts[0] + ".json"
log.Printf("get dicts: %v", ldoceMdx)
globalDict.mdxFile = c.Dicts[0] + ".json"
globalDict.mdxCss = c.Dicts[0] + ".css"
log.Printf("get global dicts: %v", globalDict)
return nil
}
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</style>
<body>
<h1>
<form action="/dict?format=html" method="get">
<form action="/d/dict?format=html" method="get">
<label for="word">Query:</label>
<input type="text" id="name" name="query" placeholder="doctor" required/><br>
<label for="word">Engine:</label>
Expand Down
6 changes: 3 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func main() {
}

if *interactive {
ldoceDict = loadDecodedMdx(filepath.Join(dataPath, "dicts", ldoceMdx)) // TODO(ch): lazy loading for performance?
globalDict.Load() // TODO(ch): lazy loading for performance?
startLoop()
return
}
Expand All @@ -106,7 +106,7 @@ func main() {
}
}
log.Printf("start a new server: %s/%s/%s/%s", network, addr, *render, *engine)
ldoceDict = loadDecodedMdx(filepath.Join(dataPath, "dicts", ldoceMdx)) // TODO(ch): lazy loading for performance?
globalDict.Load()
l, err := net.Listen(network, addr)
if err != nil {
log.Fatal("bad Listen: ", err)
Expand Down Expand Up @@ -208,7 +208,7 @@ func main() {

if *engine == "mdx" {
// io.Copy(os.Stdout, fd)
ldoceDict = loadDecodedMdx(filepath.Join(dataPath, "dicts", ldoceMdx)) // TODO(ch): lazy loading for performance?
globalDict.Load()
fmt.Println(queryMDX(*word, *render))
return
}
Expand Down
32 changes: 24 additions & 8 deletions mdx.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,42 @@ import (
"io"
"log"
"os"
"path/filepath"
"strings"

"golang.org/x/net/html"
)

var (
// ldoceMdx = "Longman Dictionary of Contemporary English" + ".json"
// ldoceMdx = "ODE_zh" + ".json"
ldoceMdx = "oald9" + ".json"
ldoceDict map[string]string
)
type MdxDict struct {
// For personal usage, "oald9.json", or "Longman Dictionary of Contemporary English"
mdxFile string
// Only match the mdx with the same mdxFile name
mdxCss string
mdxDict map[string]string
}

func (g *MdxDict) Load() error {
g.mdxDict = loadDecodedMdx(filepath.Join(dataPath, "dicts", g.mdxFile))
if contents, err := os.ReadFile((filepath.Join(dataPath, "dicts", g.mdxCss))); err == nil {
g.mdxCss = string(contents)
} else {
g.mdxCss = ""
log.Printf("load dicts[%v] css err: %v", g.mdxFile, err)
}
return nil
}

// TODO: support multiple mdx lib at the same time.
var globalDict MdxDict

var gbold = "**"
var gitalic = "*"

func queryMDX(word string, f string) string {
if f == "html" {
return ldoceDict[word]
return globalDict.mdxDict[word]
}
fd := strings.NewReader(ldoceDict[word]) // TODO: find a "close" one when missing?
fd := strings.NewReader(globalDict.mdxDict[word]) // TODO: find a "close" one when missing?
return parseMDX(fd, f)
}

Expand Down
8 changes: 4 additions & 4 deletions mdx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func Test_GetWords(t *testing.T) {
dfs(c, level+1, n)
}
}
fd, err := os.Open("./tmp/ldoce5.html")
fd, err := os.Open("./testdata/ldoce5.html")
if err != nil {
log.Fatal(err)
}
Expand All @@ -41,8 +41,8 @@ func Test_GetWords(t *testing.T) {
}

func Test_MDXParser(t *testing.T) {
// fd, err := os.Open("./tmp/test.html")
fd, err := os.Open("./tmp/doctor_mdx.html")
// fd, err := os.Open("./testdata/test.html")
fd, err := os.Open("./testdata/doctor_mdx.html")
if err != nil {
log.Fatal(err)
}
Expand All @@ -52,5 +52,5 @@ func Test_MDXParser(t *testing.T) {
log.Fatal(err)
}
// log.Printf("result: %v", readText(doc))
t.Logf("res: %v", format([]string{f(doc, 0, nil)}))
t.Logf("res: %v", format([]string{f(doc, 0, nil, "md")}))
}
Loading