Compare commits

...

5 Commits

Author SHA1 Message Date
Elara 8a00e40402 Simplify code 2021-12-10 09:52:36 -08:00
Elara d9a139f1ea Add test for search/web/web.go 2021-12-10 02:16:12 -08:00
Elara 967bbd888b Add dark mode using Dark Reader 2021-12-09 19:23:03 -08:00
Elara 1361c9d28b Remove unused imports 2021-12-09 18:10:32 -08:00
Elara f45802f372 Add comments 2021-12-09 18:10:08 -08:00
10 changed files with 5542 additions and 22 deletions

2
.gitignore vendored
View File

@ -1,2 +1,2 @@
/static/ /static/ext/
/scope /scope

View File

@ -6,18 +6,18 @@ build:
.PHONY: build .PHONY: build
static: static:
mkdir -p static mkdir -p static/ext
wget https://cdn.jsdelivr.net/npm/bulma@0.9.3/css/bulma.min.css -O static/bulma.min.css wget https://cdn.jsdelivr.net/npm/bulma@0.9.3/css/bulma.min.css -O static/ext/bulma.min.css
wget https://code.iconify.design/2/2.1.0/iconify.min.js -O static/iconify.min.js wget https://code.iconify.design/2/2.1.0/iconify.min.js -O static/ext/iconify.min.js
wget https://cdn.jsdelivr.net/npm/katex@0.15.1/dist/katex.min.css -O static/katex.min.css wget https://cdn.jsdelivr.net/npm/katex@0.15.1/dist/katex.min.css -O static/ext/katex.min.css
wget https://cdn.jsdelivr.net/npm/katex@0.15.1/dist/katex.min.js -O static/katex.min.js wget https://cdn.jsdelivr.net/npm/katex@0.15.1/dist/katex.min.js -O static/ext/katex.min.js
wget https://cdn.jsdelivr.net/npm/nerdamer@latest/nerdamer.core.js -O static/nerdamer.core.js wget https://cdn.jsdelivr.net/npm/nerdamer@latest/nerdamer.core.js -O static/ext/nerdamer.core.js
wget https://cdn.jsdelivr.net/npm/nerdamer@latest/Algebra.js -O static/Algebra.js wget https://cdn.jsdelivr.net/npm/nerdamer@latest/Algebra.js -O static/ext/Algebra.js
wget https://cdn.jsdelivr.net/npm/nerdamer@latest/Calculus.js -O static/Calculus.js wget https://cdn.jsdelivr.net/npm/nerdamer@latest/Calculus.js -O static/ext/Calculus.js
wget https://cdn.jsdelivr.net/npm/nerdamer@latest/Solve.js -O static/Solve.js wget https://cdn.jsdelivr.net/npm/nerdamer@latest/Solve.js -O static/ext/Solve.js
wget https://unpkg.com/function-plot/dist/function-plot.js -O static/function-plot.js wget https://unpkg.com/function-plot/dist/function-plot.js -O static/ext/function-plot.js

View File

@ -93,4 +93,5 @@ This project uses many other projects. Those projects and the reasons for using
- [Nerdamer](https://nerdamer.com/): Equation solver - [Nerdamer](https://nerdamer.com/): Equation solver
- [Function Plot](https://mauriciopoppe.github.io/function-plot/): Equation grapher - [Function Plot](https://mauriciopoppe.github.io/function-plot/): Equation grapher
- [Metaweather](https://www.metaweather.com/): Weather API - [Metaweather](https://www.metaweather.com/): Weather API
- [DuckDuckGo](https://duckduckgo.com/): Provides instant answers - [DuckDuckGo](https://duckduckgo.com/): Provides instant answers
- [Dark Reader](https://darkreader.org/): Dark mode CSS

View File

@ -26,19 +26,23 @@ import (
) )
const calcExtraHead = ` const calcExtraHead = `
<link rel="stylesheet" href="/static/katex.min.css"> <!-- Import KaTeX for math rendering -->
<script defer src="/static/katex.min.js"></script> <link rel="stylesheet" href="/static/ext/katex.min.css">
<script defer src="/static/ext/katex.min.js"></script>
<script src="/static/nerdamer.core.js"></script> <!-- Import Nerdamer for equation evaluator -->
<script src="/static/Algebra.js"></script> <script src="/static/ext/nerdamer.core.js"></script>
<script src="/static/Calculus.js"></script> <script src="/static/ext/Algebra.js"></script>
<script src="/static/Solve.js"></script>` <script src="/static/ext/Calculus.js"></script>
<script src="/static/ext/Solve.js"></script>`
const solveRenderScript = ` const solveRenderScript = `
<div id="calc-content" class="subtitle mx-2 my-0"></div> <div id="calc-content" class="subtitle mx-2 my-0"></div>
<script> <script>
window.onload = () => { window.onload = () => {
latex = nerdamer.convertToLaTeX(nerdamer('%s').toString()) // Execute input and get output as LaTeX
latex = nerdamer('%s').latex()
// Use KaTeX to render output to #calc-content div
katex.render(latex, document.getElementById('calc-content')) katex.render(latex, document.getElementById('calc-content'))
} }
</script>` </script>`

View File

@ -25,7 +25,7 @@ import (
) )
const plotExtraHead = ` const plotExtraHead = `
<script src="/static/function-plot.js"></script> <script src="/static/ext/function-plot.js"></script>
<style> <style>
.top-right-legend { .top-right-legend {
display: none; display: none;
@ -35,6 +35,7 @@ const plotExtraHead = `
const plotScript = ` const plotScript = `
<div id="plot-content" class="container"></div> <div id="plot-content" class="container"></div>
<script> <script>
// Create function to draw plot in #plot-content
plotFn = () => functionPlot({ plotFn = () => functionPlot({
target: '#plot-content', target: '#plot-content',
grid: true, grid: true,
@ -43,6 +44,7 @@ plotFn = () => functionPlot({
fn: '%s' fn: '%s'
}] }]
}) })
// Create resize observer that runs plot function
new ResizeObserver(plotFn).observe(document.getElementById('plot-content')) new ResizeObserver(plotFn).observe(document.getElementById('plot-content'))
</script>` </script>`

View File

@ -6,6 +6,7 @@
name = "Scope" name = "Scope"
baseURL = "http://localhost:8080" baseURL = "http://localhost:8080"
sourceURL = "https://gitea.arsenm.dev/Arsen6331/scope" sourceURL = "https://gitea.arsenm.dev/Arsen6331/scope"
theme = "dark"
[search] [search]
engines = ["google", "ddg", "bing"] engines = ["google", "ddg", "bing"]

198
search/web/web_test.go Normal file
View File

@ -0,0 +1,198 @@
package web_test
import (
"errors"
"testing"
"go.arsenm.dev/scope/search/web"
)
const (
ErrInNone = iota
ErrInInit
ErrInLink
ErrInTitle
ErrInDesc
)
var (
ErrInit = errors.New("error in init")
ErrLink = errors.New("error in link")
ErrTitle = errors.New("error in title")
ErrDesc = errors.New("error in description")
)
type TestEngine struct {
errIn int
name string
query string
userAgent string
page int
results []TestResult
}
type TestResult struct {
title string
link string
desc string
}
func (te *TestEngine) SetKeyword(query string) {
te.query = query
}
func (te *TestEngine) SetUserAgent(ua string) {
te.userAgent = ua
}
func (te *TestEngine) SetPage(page int) {
te.page = page
}
func (te *TestEngine) Init() error {
if te.errIn == ErrInInit {
return ErrInit
}
te.results = append(te.results,
TestResult{
"Google",
"https://www.google.com",
"Google search engine",
},
TestResult{
"Wikipedia",
"https://wikipedia.org",
"Online wiki encyclopedia",
},
TestResult{
"Reddit",
"https://reddit.com",
"600 chars 600 chars 600 chars 600 chars 600 chars 600 chars 600 chars 600 chars 600 chars 600 chars 600 chars 600 chars 600 chars 600 chars 600 chars 600 chars 600 chars 600 chars 600 chars 600 chars 600 chars 600 chars 600 chars 600 chars 600 chars 600 chars 600 chars 600 chars 600 chars 600 chars 600 chars 600 chars 600 chars 600 chars 600 chars 600 chars 600 chars 600 chars 600 chars 600 chars 600 chars 600 chars 600 chars 600 chars 600 chars 600 chars 600 chars 600 chars 600 chars 600 chars 600 chars 600 chars 600 chars 600 chars 600 chars 600 chars 600 chars 600 chars 600 chars 600 chars ",
},
TestResult{
"Example",
"https://example.com",
"",
},
)
return nil
}
func (te *TestEngine) Each(eachCb func(int) error) error {
for index := range te.results {
if err := eachCb(index); err != nil {
return err
}
}
return nil
}
func (te *TestEngine) Title(i int) (string, error) {
if te.errIn == ErrInTitle {
return "", ErrTitle
}
return te.results[i].title, nil
}
func (te *TestEngine) Link(i int) (string, error) {
if te.errIn == ErrInLink {
return "", ErrLink
}
return te.results[i].link, nil
}
func (te *TestEngine) Desc(i int) (string, error) {
if te.errIn == ErrInDesc {
return "", ErrDesc
}
return te.results[i].desc, nil
}
func (te *TestEngine) Name() string {
return te.name
}
func TestSearch(t *testing.T) {
engine := &TestEngine{name: "one"}
results, err := web.Search(
web.Options{
Keyword: "test keyword",
UserAgent: "TestEngine/0.0.0",
Page: 0,
},
engine,
&TestEngine{name: "two"},
)
if err != nil {
t.Fatalf("Error in Search(): %s", err)
}
if len(results) < len(engine.results)-1 {
t.Fatalf(
"Expected %d results, got %d",
len(engine.results),
len(results),
)
}
for index, result := range results {
if engine.results[index].desc == "" {
continue
}
if result.Title != engine.results[index].title {
t.Fatalf(
"Result %d: expected title %s, got %s",
index,
engine.results[index].title,
result.Title,
)
} else if result.Link != engine.results[index].link {
t.Fatalf(
"Result %d: expected link %s, got %s",
index,
engine.results[index].link,
result.Link,
)
} else if result.Desc != engine.results[index].desc &&
len(engine.results[index].desc) <= 500 {
t.Fatalf(
"Result %d: expected description %s, got %s",
index,
engine.results[index].desc,
result.Desc,
)
}
}
}
func TestSearchError(t *testing.T) {
engines := []*TestEngine{
{errIn: ErrInInit},
{errIn: ErrInTitle},
{errIn: ErrInLink},
{errIn: ErrInDesc},
}
for index, engine := range engines {
_, err := web.Search(
web.Options{
Keyword: "test keyword",
UserAgent: "TestEngine/0.0.0",
Page: 0,
},
engine,
)
if err == nil {
t.Fatalf("Expected error in engine %d, received nil", index)
} else if err != ErrTitle && engines[index].errIn == ErrInTitle {
t.Fatalf("Expected error in title (index %d), received %s", index, err)
} else if err != ErrLink && engines[index].errIn == ErrInLink {
t.Fatalf("Expected error in link (index %d), received %s", index, err)
} else if err != ErrDesc && engines[index].errIn == ErrInDesc {
t.Fatalf("Expected error in description (index %d), received %s", index, err)
} else if err != ErrInit && engines[index].errIn == ErrInInit {
t.Fatalf("Expected error in init (index %d), received %s", index, err)
}
}
}

5311
static/DarkReader-scope.css Normal file

File diff suppressed because it is too large Load Diff

View File

@ -3,8 +3,11 @@
<head> <head>
<title>{{template "page" .}} - {{.Config "site.name"}}</title> <title>{{template "page" .}} - {{.Config "site.name"}}</title>
<link rel="search" type="application/opensearchdescription+xml" title='{{.Config "site.name"}}' href="/opensearch"> <link rel="search" type="application/opensearchdescription+xml" title='{{.Config "site.name"}}' href="/opensearch">
<link rel="stylesheet" href="/static/bulma.min.css"> <link rel="stylesheet" href="/static/ext/bulma.min.css">
<script async src="/static/iconify.min.js"></script> <script async src="/static/ext/iconify.min.js"></script>
{{if .Config "site.theme" | ne "light"}}
<link rel="stylesheet" href="/static/DarkReader-scope.css">
{{end}}
{{if .Card}} {{if .Card}}
{{.Card.Head}} {{.Card.Head}}
{{end}} {{end}}