Compare commits

...

5 Commits

Author SHA1 Message Date
Arsen Musayelyan 8a00e40402 Simplify code 6 months ago
Arsen Musayelyan d9a139f1ea Add test for search/web/web.go 6 months ago
Arsen Musayelyan 967bbd888b Add dark mode using Dark Reader 6 months ago
Arsen Musayelyan 1361c9d28b Remove unused imports 6 months ago
Arsen Musayelyan f45802f372 Add comments 6 months ago
  1. 2
      .gitignore
  2. 20
      Makefile
  3. 3
      README.md
  4. 18
      internal/cards/calc.go
  5. 4
      internal/cards/plot.go
  6. 1
      scope.toml
  7. 0
      search/web/web.go
  8. 198
      search/web/web_test.go
  9. 5311
      static/DarkReader-scope.css
  10. 7
      templates/base.html

2
.gitignore

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

20
Makefile

@ -6,18 +6,18 @@ build:
.PHONY: build
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.js -O static/katex.min.js
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/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/Algebra.js -O static/Algebra.js
wget https://cdn.jsdelivr.net/npm/nerdamer@latest/Calculus.js -O static/Calculus.js
wget https://cdn.jsdelivr.net/npm/nerdamer@latest/Solve.js -O static/Solve.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/ext/Algebra.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/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

3
README.md

@ -93,4 +93,5 @@ This project uses many other projects. Those projects and the reasons for using
- [Nerdamer](https://nerdamer.com/): Equation solver
- [Function Plot](https://mauriciopoppe.github.io/function-plot/): Equation grapher
- [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

18
internal/cards/calc.go

@ -26,19 +26,23 @@ import (
)
const calcExtraHead = `
<link rel="stylesheet" href="/static/katex.min.css">
<script defer src="/static/katex.min.js"></script>
<!-- Import KaTeX for math rendering -->
<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>
<script src="/static/Algebra.js"></script>
<script src="/static/Calculus.js"></script>
<script src="/static/Solve.js"></script>`
<!-- Import Nerdamer for equation evaluator -->
<script src="/static/ext/nerdamer.core.js"></script>
<script src="/static/ext/Algebra.js"></script>
<script src="/static/ext/Calculus.js"></script>
<script src="/static/ext/Solve.js"></script>`
const solveRenderScript = `
<div id="calc-content" class="subtitle mx-2 my-0"></div>
<script>
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'))
}
</script>`

4
internal/cards/plot.go

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

1
scope.toml

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

0
search/web/search.go → search/web/web.go

198
search/web/web_test.go

@ -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

File diff suppressed because it is too large

7
templates/base.html

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

Loading…
Cancel
Save