25 Commits

Author SHA1 Message Date
Alex Schroeder
a093fc5378 Switch to blackfriday, remove Gemtext instructions 2023-09-11 15:42:06 +02:00
Alex Schroeder
471cd3c6ec Add appending to pages 2023-09-11 00:27:11 +02:00
Alex Schroeder
da361284e8 Removed some TODO items 2023-09-11 00:06:53 +02:00
Alex Schroeder
6215d2a842 More details about trigrams 2023-09-11 00:05:15 +02:00
Alex Schroeder
47c727c00d Fix language detection code
https://github.com/pemistahl/lingua-go/discussions/50
2023-09-10 23:56:55 +02:00
Alex Schroeder
91381e474c Whitespace 2023-09-10 23:48:24 +02:00
Alex Schroeder
4e5aa70529 Add language detection 2023-09-10 15:58:31 +02:00
Alex Schroeder
b7048bd5a9 Pass regular expression to highlighter 2023-09-10 15:57:50 +02:00
Alex Schroeder
41be47dc03 Scoring
Improved description in the README. Score the raw body of the page and
not the plain text (so that invisible text such as URLs are part of
the score). Page sorting is such that with equal score pages that
start with a number get sorted descending (in the hopes of putting
newer pages at the top, if ISO dates are part of page names).
2023-09-10 11:31:18 +02:00
Alex Schroeder
44b92cc3e0 Add CSS for header to search.html 2023-09-10 10:56:54 +02:00
Alex Schroeder
025d993eb7 Add search box to search itself for iteration 2023-09-10 10:45:00 +02:00
Alex Schroeder
1209c2b209 Moved a score test to the right file 2023-09-10 10:34:10 +02:00
Alex Schroeder
5d3aa45ddb Split score and highlight into 2 files 2023-09-10 10:31:01 +02:00
Alex Schroeder
f93177def5 Split highlighting and scoring 2023-09-10 00:51:20 +02:00
Alex Schroeder
aeb53148e7 Consider using a full text search engine 2023-09-09 22:11:35 +02:00
Alex Schroeder
4bce6fcb38 Split search phrase into words
Trigrams are then merged but word boundaries don't get their own
trigrams. The result is that the word order no longer matters.
2023-09-09 21:46:33 +02:00
Alex Schroeder
92cc1ad883 Document scoring of the search results 2023-09-09 21:46:22 +02:00
Alex Schroeder
378330cbce Add case-insensitive indexing 2023-09-09 20:45:36 +02:00
Alex Schroeder
ad472f9db1 Thoughts on multi-lingual wikis. 2023-09-09 20:07:36 +02:00
Alex Schroeder
b4f861a24e Add todo list. 2023-09-09 20:01:08 +02:00
Alex Schroeder
e97e5c7e6c Add back the missing rocket link translation 2023-08-25 21:23:22 +02:00
Alex Schroeder
0a4eabee3d New intro 2023-08-25 21:19:56 +02:00
Alex Schroeder
fcd4d9136d Move sanitization into separate functions
Add score for title matches but discard the markup.
2023-08-25 19:08:47 +02:00
Alex Schroeder
103007be48 Fix regexp quoting and title searching 2023-08-25 18:32:15 +02:00
Alex Schroeder
4afffbc409 Add hyphenation to the templates 2023-08-25 18:15:31 +02:00
19 changed files with 610 additions and 247 deletions

182
README.md
View File

@@ -1,20 +1,75 @@
# Oddµ: A minimal wiki
This program runs a wiki. It serves all the Markdown files (ending in
`.md`) into web pages and allows you to edit them.
`.md`) into web pages and allows you to edit them. If your files don't
provide their own title (`# title`), the file name (without `.md`) is
used for the title. Subdirectories are created as necessary.
This is a minimal wiki. There is no version history. It probably makes
sense to only use it as one person or in very small groups.
This is a minimal wiki. There is no version history. It's well suited
as a *secondary* medium: collaboration and conversation happens
elsewhere, in chat, on social media. The wiki serves as the text
repository that results from these discussions.
This wiki only uses Markdown. There is no additional wiki markup, most
The wiki lists no recent changes. The expectation is that the people
that care were involved in the discussions beforehand.
The wiki also produces no feed. The assumption is that announcements
are made on social media: blogs, news aggregators, discussion forums,
the fediverse, but humans. There is no need for bots.
As you'll see below, the idea is that the webserver handles as many
tasks as possible. It logs requests, does rate limiting, handles
encryption, gets the certificates, and so on. The web server acts as a
reverse proxy and the wiki ends up being a content management system
with almost no structure or endless malleability, depending on your
point of view.
And last but not least: µ is the letter mu, so Oddµ is usually written
Oddmu. 🙃
## Markdown
This wiki uses Markdown. There is no additional wiki markup, most
importantly double square brackets are not a link. If you're used to
that, it'll be strange as you need to repeat the name: `[like
this](like this)`.
this](like this)`. The Markdown processor comes with a few extensions,
some of which are enable by default:
If your files don't provide their own title (`# title`), the file name
is used for the title.
* emphasis markers inside words are ignored
* tables are supported
* fenced code blocks are supported
* autolinking of "naked" URLs are supported
* strikethrough using two tildes is supported (`~~like this~~`)
* it is strict about prefix heading rules
* you can specify an id for headings (`{#id}`)
* trailing backslashes turn into line breaks
* definition lists are supported
* MathJax is supported (but needs a separte setup)
µ is the letter mu, so Oddµ is usually written Oddmu. 🙃
See the section on
[extensions](https://github.com/russross/blackfriday#extensions) in
the Blackfriday library for information on the various extensions.
A table with footers and a columnspan:
```text
Name | Age
--------|------
Bob ||
Alice | 23
========|======
Total | 23
```
A definition list:
```text
Cat
: Fluffy animal everyone likes
Internet
: Vector of transmission for pictures of cats
```
## Templates
@@ -47,6 +102,21 @@ curl --form body="Did you bring a towel?" \
http://localhost:8080/save/welcome
```
## Non-English hyphenation
Automatic hyphenation by the browser requires two things: The style
sheet must indicate `hyphen: auto` for an HTML element such as `body`,
and that element must have a `lang` set (usually a two letter language
code such as `de` for German). This happens in the template files,
such as `view.html` and `search.html`.
If have pages in different languages, the problem is that they all use
the same template and that's not good. In such cases, it might be
better to not specificy the `lang` attribute in the template. This
also disables hyphenation by the browser, unfortunately. It might
still be better than using English hyphenation patterns for
non-English languages.
## Building
```sh
@@ -147,8 +217,9 @@ MDCertificateAgreement accepted
First, it manages the domain, getting the necessary certificates. It
redirects regular HTTP traffic from port 80 to port 443. It turns on
the SSL engine for port 443. It redirects `/` to `/view/index` and any
path that starts with `/view/`, `/edit/`, `/save/` or `/search/` is
proxied to port 8080 where the Oddmu program can handle it.
path that starts with `/view/`, `/edit/`, `/save/`, `/add/`,
`/append/` or `/search/` is proxied to port 8080 where the Oddmu
program can handle it.
Thus, this is what happens:
@@ -195,12 +266,12 @@ To delete remove a user:
htpasswd -D .htpasswd berta
```
Modify your site configuration and protect the `/edit/` and `/save/`
URLs with a password by adding the following to your `<VirtualHost
*:443>` section:
Modify your site configuration and protect the `/edit/`, `/save/`,
`/add/` and `/append/` URLs with a password by adding the following to
your `<VirtualHost *:443>` section:
```apache
<LocationMatch "^/(edit|save)/">
<LocationMatch "^/(edit|save|add|append)/">
AuthType Basic
AuthName "Password Required"
AuthUserFile /home/oddmu/.htpasswd
@@ -225,10 +296,9 @@ webserver can read (world readable file, world readable and executable
directory). Populate it with files.
Make sure that none of the static files look like the wiki paths
`/view/`, `/edit/`, `/save/` or `/search/`.
For example, create a file called `robots.txt` containing the
following, tellin all robots that they're not welcome.
`/view/`, `/edit/`, `/save/`, `/add/`, `/append/` or `/search/`. For
example, create a file called `robots.txt` containing the following,
tellin all robots that they're not welcome.
```text
User-agent: *
@@ -276,59 +346,57 @@ that matches everything:
## Customization (with recompilation)
The Markdown parser can be customized and
[extensions](https://pkg.go.dev/github.com/gomarkdown/markdown/parser#Extensions)
can be added. There's an example in the
[usage](https://github.com/gomarkdown/markdown#usage) section. You'll
need to make changes to the `viewHandler` yourself.
[extensions](https://github.com/russross/blackfriday#extensions) can
be added. You'll need to make changes to `renderHtml` yourself.
### Render Gemtext
## Understanding search
In a first approximation, Gemtext is valid Markdown except for the
rocket links (`=>`). Here's how to modify the `loadPage` so that a
`.gmi` file is loaded if no `.md` is found, and the rocket links are
translated into Markdown:
The index indexes trigrams. Each group of three characters is a
trigram. A document with content "This is a test" is turned to lower
case and indexed under the trigrams "thi", "his", "is ", "s i", " is",
"is ", "s a", " a ", "a t", " te", "tes", "est".
```go
func loadPage(name string) (*Page, error) {
filename := name + ".md"
body, err := os.ReadFile(filename)
if err == nil {
return &Page{Title: name, Name: name, Body: body}, nil
}
filename = name + ".gmi"
body, err = os.ReadFile(filename)
if err == nil {
return &Page{Title: name, Name: name, Body: body}, nil
}
return nil, err
}
```
Each query is split into words and then processed the same way. A
query with the words "this test" is turned to lower case and produces
the trigrams "thi", "his", "tes", "est". This means that the word
order is not considered when searching for documents.
There is a small problem, however: By default, Markdown expects an
empty line before a list begins. The following change to `renderHtml`
uses the `NoEmptyLineBeforeBlock` extension for the parser:
This also means that there is no stemming. Searching for "testing"
won't find "This is a test" because there are no matches for the
trigrams "sti", "tin", "ing".
```go
func (p* Page) renderHtml() {
// Here is where a new extension is added!
extensions := parser.CommonExtensions | parser.NoEmptyLineBeforeBlock
markdownParser := parser.NewWithExtensions(extensions)
maybeUnsafeHTML := markdown.ToHTML(p.Body, markdownParser, nil)
html := bluemonday.UGCPolicy().SanitizeBytes(maybeUnsafeHTML)
p.Html = template.HTML(html);
}
```
These trigrams are looked up in the index, resulting in the list of
documents. Each document found is then scored. Each of the following
increases the score by one point:
- the entire phrase matches
- a word matches
- a word matches at the beginning of a word
- a word matches at the end of a word
- a word matches as a whole word
A document with content "This is a test" when searched with the phrase
"this test" therefore gets a score of 8: the entire phrase does not
match but each word gets four points.
Trigrams are sometimes strange: In a text containing the words "main"
and "rail", a search for "mail" returns a match because the trigrams
"mai" and "ail" are found. In this situation, the result has a score
of 0.
## Limitations
Page titles are filenames with `.md` appended. If your filesystem
cannot handle it, it can't be a page title. Specifically, *no slashes*
in filenames.
cannot handle it, it can't be a page name.
The pages are indexed as the server starts and the index is kept in
memory. If you have a ton of pages, this surely wastes a lot of
memory.
## Bugs
If you spot any, [contact](https://alexschroeder.ch/wiki/Contact) me.
## References
[Writing Web Applications](https://golang.org/doc/articles/wiki/)

12
TODO.md Normal file
View File

@@ -0,0 +1,12 @@
Upload files.
Automatically scale or process files.
Post by Delta Chat? That is, allow certain encrypted emails to post.
Convert the existing wiki.
Investigate how to run a multi-linugual wiki where an appropriate
version of a page is served based on language preferences of the user.
This is a low priority issue since it's probably only of interest for
corporate or governmental sites.

21
add.html Normal file
View File

@@ -0,0 +1,21 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="format-detection" content="telephone=no">
<meta name="viewport" content="width=device-width">
<title>Add to {{.Title}}</title>
<style>
html { max-width: 70ch; padding: 2ch; margin: auto; color: #111; background: #ffe; }
form, textarea { width: 100%; }
</style>
</head>
<body>
<h1>Adding to {{.Title}}</h1>
<form action="/append/{{.Name}}" method="POST">
<div><textarea name="body" rows="20" cols="80" placeholder="Text" autofocus></textarea></div>
<p><input type="submit" value="Add">
<a href="/view/{{.Name}}"><button>Cancel</button></a></p>
</form>
</body>
</html>

View File

@@ -12,9 +12,10 @@ form, textarea { width: 100%; }
</head>
<body>
<h1>Editing {{.Title}}</h1>
<form action="/save/{{.Name}}" method="POST">
<div><textarea name="body" rows="20" cols="80">{{printf "%s" .Body}}</textarea></div>
<div><textarea name="body" rows="20" cols="80" placeholder="# Title
Text" autofocus>{{printf "%s" .Body}}</textarea></div>
<p><input type="submit" value="Save">
<a href="/view/{{.Name}}"><button>Cancel</button></a></p>
</form>

10
go.mod
View File

@@ -4,12 +4,20 @@ go 1.21.0
require (
github.com/dgryski/go-trigram v0.0.0-20160407183937-79ec494e1ad0
github.com/gomarkdown/markdown v0.0.0-20230716120725-531d2d74bc12
github.com/microcosm-cc/bluemonday v1.0.25
github.com/pemistahl/lingua-go v1.4.0
github.com/russross/blackfriday/v2 v2.1.0
)
require (
github.com/aymerick/douceur v0.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gorilla/css v1.0.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/shopspring/decimal v1.3.1 // indirect
github.com/stretchr/testify v1.8.4 // indirect
golang.org/x/exp v0.0.0-20221106115401-f9659909a136 // indirect
golang.org/x/net v0.12.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

27
go.sum
View File

@@ -1,12 +1,35 @@
github.com/aymerick/douceur v0.2.0 h1:Mv+mAeH1Q+n9Fr+oyamOlAkUNPWPlA8PPGR0QAaYuPk=
github.com/aymerick/douceur v0.2.0/go.mod h1:wlT5vV2O3h55X9m7iVYN0TBM0NH/MmbLnd30/FjWUq4=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgryski/go-trigram v0.0.0-20160407183937-79ec494e1ad0 h1:b+7JSiBM+hnLQjP/lXztks5hnLt1PS46hktG9VOJgzo=
github.com/dgryski/go-trigram v0.0.0-20160407183937-79ec494e1ad0/go.mod h1:qzKC/DpcxK67zaSHdCmIv3L9WJViHVinYXN2S7l3RM8=
github.com/gomarkdown/markdown v0.0.0-20230716120725-531d2d74bc12 h1:uK3X/2mt4tbSGoHvbLBHUny7CKiuwUip3MArtukol4E=
github.com/gomarkdown/markdown v0.0.0-20230716120725-531d2d74bc12/go.mod h1:JDGcbDT52eL4fju3sZ4TeHGsQwhG9nbDV21aMyhwPoA=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/gorilla/css v1.0.0 h1:BQqNyPTi50JCFMTw/b67hByjMVXZRwGha6wxVGkeihY=
github.com/gorilla/css v1.0.0/go.mod h1:Dn721qIggHpt4+EFCcTLTU/vk5ySda2ReITrtgBl60c=
github.com/microcosm-cc/bluemonday v1.0.25 h1:4NEwSfiJ+Wva0VxN5B8OwMicaJvD8r9tlJWm9rtloEg=
github.com/microcosm-cc/bluemonday v1.0.25/go.mod h1:ZIOjCQp1OrzBBPIJmfX4qDYFuhU02nx4bn030ixfHLE=
github.com/pemistahl/lingua-go v1.4.0 h1:ifYhthrlW7iO4icdubwlduYnmwU37V1sbNrwhKBR4rM=
github.com/pemistahl/lingua-go v1.4.0/go.mod h1:ECuM1Hp/3hvyh7k8aWSqNCPlTxLemFZsRjocUf3KgME=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/shopspring/decimal v1.3.1 h1:2Usl1nmF/WZucqkFZhnfFYxxxu8LG21F6nPQBE5gKV8=
github.com/shopspring/decimal v1.3.1/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
golang.org/x/exp v0.0.0-20221106115401-f9659909a136 h1:Fq7F/w7MAa1KJ5bt2aJ62ihqp9HDcRuyILskkpIAurw=
golang.org/x/exp v0.0.0-20221106115401-f9659909a136/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
golang.org/x/net v0.12.0 h1:cfawfvKITfUsFCeJIHJrbSxpeu/E81khclypR0GVT50=
golang.org/x/net v0.12.0/go.mod h1:zEVYFnQC7m/vmpQFELhcD1EWkZlX69l4oqgmer6hfKA=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

View File

@@ -2,50 +2,12 @@ package main
import (
"regexp"
"strings"
)
// highlight splits the query string q into terms and highlights them
// using the bold tag. Return the highlighted string and a score.
func highlight(q string, s string) (string, int) {
c := 0
re, err := regexp.Compile("(?i)" + q)
if err == nil {
m := re.FindAllString(s, -1)
if m != nil {
// Score increases for each full match of q.
c += len(m)
}
}
for _, v := range strings.Split(q, " ") {
if len(v) == 0 {
continue
}
re, err := regexp.Compile(`(?is)(\pL?)(` + v + `)(\pL?)`)
if err != nil {
continue
}
r := make(map[string]string)
for _, m := range re.FindAllStringSubmatch(s, -1) {
// Term matched increases the score.
c++
// Terms matching at the beginning and
// end of words and matching entire
// words increase the score further.
if len(m[1]) == 0 {
c++
}
if len(m[3]) == 0 {
c++
}
if len(m[1]) == 0 && len(m[3]) == 0 {
c++
}
r[m[2]] = "<b>" + m[2] + "</b>"
}
for old, new := range r {
s = strings.ReplaceAll(s, old, new)
}
}
return s, c
// using the bold tag. Return the highlighted string.
// This assumes that q already has all its meta characters quoted.
func highlight(q string, re *regexp.Regexp, s string) string {
s = re.ReplaceAllString(s, "<b>$1</b>")
return s
}

View File

@@ -15,49 +15,29 @@ A wave of car noise hits me
No birds to be heard.`
q := "window"
r, c := highlight(q, s)
re, _ := re(q)
r := highlight(q, re, s)
if r != h {
t.Logf("The highlighting is wrong in 「%s」", r)
t.Fail()
}
// Score:
// - q itself
// - the single token
// - the beginning of a word
if c != 3 {
t.Logf("%s score is %d", q, c)
t.Fail()
}
q = "windows"
_, c = highlight(q, s)
// Score:
// - q itself
// - the single token
// - the beginning of a word
// - the end of a word
// - the whole word
if c != 5 {
t.Logf("%s score is %d", q, c)
t.Fail()
}
q = "car noise"
_, c = highlight(q, s)
// Score:
// - car noise (+1)
// - car, with beginning, end, whole word (+4)
// - noise, with beginning, end, whole word (+4)
if c != 9 {
t.Logf("%s score is %d", q, c)
t.Fail()
}
q = "noise car"
_, c = highlight(q, s)
// Score:
// - the car token
// - the noise token
// - each with beginning, end and whole token (3 each)
if c != 8 {
t.Logf("%s score is %d", q, c)
}
func TestOverlap(t *testing.T) {
s := `Sit with me my love
Kids shout and so do parents
I hear the fountain`
h := `Sit with me my love
Kids <b>shout</b> and so do parents
I hear the fountain`
q := "shout out"
re, _ := re(q)
r := highlight(q, re, s)
if r != h {
t.Logf("The highlighting is wrong in 「%s」", r)
t.Fail()
}
}

101
page.go
View File

@@ -3,16 +3,30 @@ package main
import (
"bytes"
"fmt"
"github.com/gomarkdown/markdown"
"github.com/gomarkdown/markdown/ast"
"github.com/gomarkdown/markdown/parser"
"github.com/microcosm-cc/bluemonday"
"github.com/pemistahl/lingua-go"
"github.com/russross/blackfriday/v2"
"html/template"
"os"
"path/filepath"
"strings"
)
// languages is the list of languages the wiki understands. This is
// passed along to the template so that it can be added to the
// template which allows browsers to (maybe) do hyphenation correctly.
var languages = []lingua.Language{
lingua.English,
lingua.German,
}
// detector is built once based on the list languages.
var detector = lingua.NewLanguageDetectorBuilder().
FromLanguages(languages...).
WithPreloadedLanguageModels().
WithLowAccuracyMode().
Build()
// Page is a struct containing information about a single page. Title
// is the title extracted from the page content using titleRegexp.
// Name is the filename without extension (so a filename of "foo.md"
@@ -20,11 +34,20 @@ import (
// page and Html is the rendered HTML for that Markdown. Score is a
// number indicating how well the page matched for a search query.
type Page struct {
Title string
Name string
Body []byte
Html template.HTML
Score int
Title string
Name string
Language string
Body []byte
Html template.HTML
Score int
}
func sanitize(s string) template.HTML {
return template.HTML(bluemonday.UGCPolicy().Sanitize(s))
}
func sanitizeBytes(bytes []byte) template.HTML {
return template.HTML(bluemonday.UGCPolicy().SanitizeBytes(bytes))
}
// save saves a Page. The filename is based on the Page.Name and gets
@@ -59,7 +82,7 @@ func loadPage(name string) (*Page, error) {
if err != nil {
return nil, err
}
return &Page{Title: name, Name: name, Body: body}, nil
return &Page{Title: name, Name: name, Body: body, Language: ""}, nil
}
// handleTitle extracts the title from a Page and sets Page.Title, if
@@ -78,44 +101,54 @@ func (p *Page) handleTitle(replace bool) {
// renderHtml renders the Page.Body to HTML and sets Page.Html.
func (p *Page) renderHtml() {
maybeUnsafeHTML := markdown.ToHTML(p.Body, nil, nil)
html := bluemonday.UGCPolicy().SanitizeBytes(maybeUnsafeHTML)
p.Html = template.HTML(html)
maybeUnsafeHTML := blackfriday.Run(p.Body)
p.Html = sanitizeBytes(maybeUnsafeHTML)
p.Language = language(p.plainText())
}
// plainText renders the Page.Body to plain text and returns it,
// ignoring all the Markdown and all the newlines. The result is one
// long single line of text.
func (p *Page) plainText() string {
parser := parser.New()
doc := markdown.Parse(p.Body, parser)
optList := []blackfriday.Option{blackfriday.WithExtensions(blackfriday.CommonExtensions)}
parser := blackfriday.New(optList...)
ast := parser.Parse(p.Body)
text := []byte("")
ast.WalkFunc(doc, func(node ast.Node, entering bool) ast.WalkStatus {
if entering && node.AsLeaf() != nil {
text = append(text, node.AsLeaf().Literal...)
text = append(text, []byte(" ")...)
ast.Walk(func(node *blackfriday.Node, entering bool) blackfriday.WalkStatus {
if entering && node.Type == blackfriday.Text {
s := node.Literal
if len(s) == 0 {
return blackfriday.GoToNext
}
// Some Markdown still contains newlines
for i, c := range s {
if c == '\n' {
s[i] = ' '
}
}
if len(text) > 0 && text[len(text)-1] != ' ' && s[0] != ' ' {
text = append(text, ' ')
}
text = append(text, s...)
}
return ast.GoToNext
return blackfriday.GoToNext
})
// Some Markdown still contains newlines
for i, c := range text {
if c == '\n' {
text[i] = ' '
}
}
// Remove trailing space
for text[len(text)-1] == ' ' {
text = text[0 : len(text)-1]
}
return string(text)
}
// summarize for query string q sets Page.Html to an extract.
func (p *Page) summarize(q string) {
p.handleTitle(true)
s, c := snippets(q, p.plainText())
p.Score = c
extract := []byte(s)
html := bluemonday.UGCPolicy().SanitizeBytes(extract)
p.Html = template.HTML(html)
p.Score = score(q, string(p.Body)) + score(q, p.Title)
t := p.plainText()
p.Html = sanitize(snippets(q, t))
p.Language = language(t)
}
func language(s string) string {
if language, ok := detector.DetectLanguageOf(s); ok {
return strings.ToLower(language.IsoCode639_1().String())
}
return ""
}

View File

@@ -2,8 +2,9 @@ package main
import (
"os"
"strings"
"regexp"
"testing"
"github.com/stretchr/testify/assert"
)
func TestPageTitle(t *testing.T) {
@@ -12,19 +13,10 @@ My back aches for you
I sit, stare and type for hours
But yearn for blue sky`)}
p.handleTitle(false)
if p.Title != "Ache" {
t.Logf("The page title was not extracted correctly: %s", p.Title)
t.Fail()
}
if !strings.HasPrefix(string(p.Body), "# Ache") {
t.Logf("The page title was removed: %s", p.Body)
t.Fail()
}
assert.Equal(t, "Ache", p.Title)
assert.Regexp(t, regexp.MustCompile("^# Ache"), string(p.Body))
p.handleTitle(true)
if !strings.HasPrefix(string(p.Body), "My back") {
t.Logf("The page title was not removed: %s", p.Body)
t.Fail()
}
assert.Regexp(t, regexp.MustCompile("^My back"), string(p.Body))
}
func TestPagePlainText(t *testing.T) {
@@ -32,12 +24,8 @@ func TestPagePlainText(t *testing.T) {
The air will not come
To inhale is an effort
The summer heat kills`)}
s := p.plainText()
r := "Water The air will not come To inhale is an effort The summer heat kills"
if s != r {
t.Logf("The plain text version is wrong: %s", s)
t.Fail()
}
assert.Equal(t, r, p.plainText())
}
func TestPageHtml(t *testing.T) {
@@ -46,17 +34,13 @@ Silver leaves shine bright
They droop, boneless, weak and sad
A cruel sun stares down`)}
p.renderHtml()
s := string(p.Html)
r := `<h1>Sun</h1>
<p>Silver leaves shine bright
They droop, boneless, weak and sad
A cruel sun stares down</p>
`
if s != r {
t.Logf("The HTML is wrong: %s", s)
t.Fail()
}
assert.Equal(t, r, string(p.Html))
}
func TestPageDir(t *testing.T) {
@@ -68,11 +52,17 @@ A slow shuffle in the dark
Moonlight floods the aisle`)}
p.save()
o, err := loadPage("testdata/moon")
if err != nil || string(o.Body) != string(p.Body) {
t.Logf("File in subdirectory not loaded: %s", p.Name)
t.Fail()
}
assert.NoError(t, err, "load page")
assert.Equal(t, p.Body, o.Body)
t.Cleanup(func() {
_ = os.RemoveAll("testdata")
})
}
func TestLanguage(t *testing.T) {
l := language(`
My back hurts at night
My shoulders won't budge today
Winter bones I say`)
assert.Equal(t, "en", l)
}

47
score.go Normal file
View File

@@ -0,0 +1,47 @@
package main
import (
"regexp"
"strings"
)
// score splits the query string q into terms and scores the text
// based on those terms. This assumes that q already has all its meta
// characters quoted.
func score(q string, s string) int {
score := 0
re, err := regexp.Compile("(?i)" + q)
if err == nil {
m := re.FindAllString(s, -1)
if m != nil {
// Score increases for each full match of q.
score += len(m)
}
}
for _, v := range strings.Split(q, " ") {
if len(v) == 0 {
continue
}
re, err := regexp.Compile(`(?is)(\pL?)(` + v + `)(\pL?)`)
if err != nil {
continue
}
for _, m := range re.FindAllStringSubmatch(s, -1) {
// Term matched increases the score.
score++
// Terms matching at the beginning and
// end of words and matching entire
// words increase the score further.
if len(m[1]) == 0 {
score++
}
if len(m[3]) == 0 {
score++
}
if len(m[1]) == 0 && len(m[3]) == 0 {
score++
}
}
}
return score
}

104
score_test.go Normal file
View File

@@ -0,0 +1,104 @@
package main
import (
"testing"
)
func TestScore(t *testing.T) {
s := `The windows opens
A wave of car noise hits me
No birds to be heard.`
q := "window"
// Score:
// - q itself
// - the single token
// - the beginning of a word
c := score(q, s)
if c != 3 {
t.Logf("%s score is %d", q, c)
t.Fail()
}
q = "windows"
c = score(q, s)
// Score:
// - q itself
// - the single token
// - the beginning of a word
// - the end of a word
// - the whole word
if c != 5 {
t.Logf("%s score is %d", q, c)
t.Fail()
}
q = "car noise"
c = score(q, s)
// Score:
// - car noise (+1)
// - car, with beginning, end, whole word (+4)
// - noise, with beginning, end, whole word (+4)
if c != 9 {
t.Logf("%s score is %d", q, c)
t.Fail()
}
q = "noise car"
c = score(q, s)
// Score:
// - the car token
// - the noise token
// - each with beginning, end and whole token (3 each)
if c != 8 {
t.Logf("%s score is %d", q, c)
t.Fail()
}
}
func TestScoreLong(t *testing.T) {
s := `We are immersed in a sea of dead people. All the dead that have gone before us, silent now, just staring, gaping. As we move and talk and fret, never once stopping to ask ourselves or them! what it was all about. Instead we drown ourselves in noise. Incessantly we babble, surrounded by false friends claiming that all is well. And look at us! Yes, we are well. Patting our backs and expecting a pat and we do! we smugly do enjoy.`
q := "all is well"
c := score(q, s)
// Score:
// - all is well (1)
// - all, beginning, end, whole word (+4 × 3 = 12)
// - is, beginning, end, whole word (+4 × 1 = 4), and as a substring (1)
// - well, beginning, end, whole word (+4 × 2 = 8)
if c != 26 {
t.Logf("%s score is %d", q, c)
t.Fail()
}
}
func TestScoreSubstring(t *testing.T) {
s := `The loneliness of space means that receiving messages means knowledge that other people are out there. Not satellites pinging forever. Not bots searching and probing. Instead, humans. People who care. Curious and cautious.`
q := "search probe"
c := score(q, s)
// Score:
// - search, beginning (2)
// - probe (0)
if c != 2 {
t.Logf("%s score is %d", q, c)
t.Fail()
}
q = "ear"
c = score(q, s)
// Score:
// - ear, all (2)
if c != 2 {
t.Logf("%s score is %d", q, c)
t.Fail()
}
}
func TestScorePageAndMarkup(t *testing.T) {
s := `The Transjovian Council accepts new members. If you think we'd be a good fit, apply for an account. Contact [Alex Schroeder](https://alexschroeder.ch/wiki/Contact). Mail is best. Encrypted mail is best. [Delta Chat](https://delta.chat/de/) is a messenger app that uses encrypted mail. It's the bestest best.`
p := &Page{Title: "Test", Name: "Test", Body: []byte(s)}
q := "wiki"
p.summarize(q)
// "wiki" is not visible in the plain text but the score is no affected:
// - wiki, all, whole, beginning, end (5)
if p.Score != 5 {
t.Logf("%s score is %d", q, p.Score)
t.Fail()
}
}

View File

@@ -7,6 +7,8 @@ import (
"path/filepath"
"slices"
"strings"
"unicode"
"unicode/utf8"
)
// Search is a struct containing the result of a search. Query is the
@@ -20,7 +22,8 @@ type Search struct {
}
// index is a struct containing the trigram index for search. It is
// generated at startup and updated after every page edit.
// generated at startup and updated after every page edit. The index
// is case-insensitive.
var index trigram.Index
// documents is a map, mapping document ids of the index to page
@@ -40,7 +43,7 @@ func indexAdd(path string, info fs.FileInfo, err error) error {
if err != nil {
return err
}
id := index.Add(string(p.Body))
id := index.Add(strings.ToLower(string(p.Body)))
documents[id] = p.Name
return nil
}
@@ -66,21 +69,29 @@ func (p *Page) updateIndex() {
}
}
if id == 0 {
id = index.Add(string(p.Body))
id = index.Add(strings.ToLower(string(p.Body)))
documents[id] = p.Name
} else {
o, err := loadPage(p.Name)
if err == nil {
index.Delete(string(o.Body), id)
index.Delete(strings.ToLower(string(o.Body)), id)
}
index.Insert(string(p.Body), id)
index.Insert(strings.ToLower(string(p.Body)), id)
}
}
// search returns a sorted []Page where each page contains an extract
// of the actual Page.Body in its Page.Html.
func search(q string) []Page {
ids := index.Query(q)
if len(q) == 0 {
return make([]Page, 0)
}
words := strings.Split(strings.ToLower(q), " ")
var trigrams []trigram.T
for _, word := range words {
trigrams = trigram.Extract(word, trigrams)
}
ids := index.QueryTrigrams(trigrams)
items := make([]Page, len(ids))
for i, id := range ids {
name := documents[id]
@@ -93,11 +104,27 @@ func search(q string) []Page {
}
}
fn := func(a, b Page) int {
// Sort by score
if a.Score < b.Score {
return 1
} else if a.Score > b.Score {
return -1
} else if a.Title < b.Title {
}
// If the score is the same and both page names start
// with a number (like an ISO date), sort descending.
ra, _ := utf8.DecodeRuneInString(a.Title)
rb, _ := utf8.DecodeRuneInString(b.Title)
if unicode.IsNumber(ra) && unicode.IsNumber(rb) {
if a.Title < b.Title {
return 1
} else if a.Title > b.Title {
return -1
} else {
return 0
}
}
// Otherwise sort ascending.
if a.Title < b.Title {
return -1
} else if a.Title > b.Title {
return 1

View File

@@ -6,23 +6,37 @@
<meta name="viewport" content="width=device-width">
<title>Search for {{.Query}}</title>
<style>
html { max-width: 70ch; padding: 2ch; margin: auto; color: #111; background: #ffe; }
html { max-width: 65ch; padding: 2ch; margin: auto; color: #111; background: #ffe; }
body { hyphens: auto; }
header a { margin-right: 1ch; }
form { display: inline-block; }
img { max-width: 20%; }
.result { font-size: larger }
.score { font-size: smaller; opacity: 0.8; }
</style>
</head>
<body>
<h1>Search for {{.Query}}</h1>
<div>
<body lang="en">
<header>
<a href="#main">Skip navigation</a>
<a href="/view/index">Home</a>
<form role="search" action="/search" method="GET">
<input type="text" value="{{.Query}}" spellcheck="false" name="q" required>
<button>Search</button>
</form>
</header>
<main id="main">
<h1>Search for {{.Query}}</h1>
{{if .Results}}
{{range .Items}}
<p><a class="result" href="/view/{{.Name}}">{{.Title}}</a> <span class="score">{{.Score}}</span></p>
<blockquote>{{.Html}}</blockquote>
{{end}}
{{range .Items}}
<article lang="{{.Language}}">
<p><a class="result" href="/view/{{.Name}}">{{.Title}}</a>
<span class="score">{{.Score}}</span></p>
<blockquote>{{.Html}}</blockquote>
</article>
{{end}}
{{else}}
<p>No results.</p>
{{end}}
</div>
</main>
</body>
</html>

View File

@@ -19,7 +19,11 @@ func TestIndex(t *testing.T) {
t.Fail()
}
for _, p := range pages {
if !strings.Contains(string(p.Body), q) {
if strings.Contains(p.Title, "<b>") {
t.Logf("Page %s contains <b>", p.Name)
t.Fail()
}
if !strings.Contains(string(p.Body), q) && !strings.Contains(string(p.Title), q) {
t.Logf("Page %s does not contain %s", p.Name, q)
t.Fail()
}
@@ -42,6 +46,30 @@ func TestIndex(t *testing.T) {
t.Logf("Page '%s' was not found", name)
t.Fail()
}
pages = search("this is a test")
found = false
for _, p := range pages {
if p.Name == name {
found = true
break
}
}
if !found {
t.Logf("Page '%s' was not found using the lower case text", name)
t.Fail()
}
pages = search("this test")
found = false
for _, p := range pages {
if p.Name == name {
found = true
break
}
}
if !found {
t.Logf("Page '%s' was not found using a query missing some words", name)
t.Fail()
}
p = &Page{Name: name, Body: []byte("Guvf vf n grfg.")}
p.save()
pages = search("This is a test")

View File

@@ -5,18 +5,32 @@ import (
"strings"
)
func snippets(q string, s string) (string, int) {
// re returns a regular expression matching any word in q.
func re(q string) (*regexp.Regexp, error) {
q = regexp.QuoteMeta(q)
re, err := regexp.Compile(`\s+`)
if err != nil {
return nil, err
}
words := re.ReplaceAllString(q, "|")
re, err = regexp.Compile(`(?i)(` + words + `)`)
if err != nil {
return nil, err
}
return re, nil
}
func snippets(q string, s string) string {
// Look for Snippets
snippetlen := 100
maxsnippets := 4
// Compile the query as a regular expression
re, err := regexp.Compile("(?i)(" + strings.Join(strings.Split(q, " "), "|") + ")")
// If the compilation didn't work, truncate
re, err := re(q)
// If the compilation didn't work, truncate and return
if err != nil || len(s) <= snippetlen {
if len(s) > 400 {
s = s[0:400]
s = s[0:400] + " …"
}
return highlight(q, s)
return s
}
// show a snippet from the beginning of the document
j := strings.LastIndex(s[:snippetlen], " ")
@@ -26,9 +40,9 @@ func snippets(q string, s string) (string, int) {
if j == -1 {
// Or just truncate the body.
if len(s) > 400 {
s = s[0:400]
s = s[0:400] + " …"
}
return highlight(q, s)
return highlight(q, re, s)
}
}
t := s[0:j]
@@ -75,5 +89,5 @@ func snippets(q string, s string) (string, int) {
s = s[end:]
}
}
return highlight(q, res)
return highlight(q, re, res)
}

View File

@@ -10,18 +10,9 @@ func TestSnippets(t *testing.T) {
h := `We are immersed in a sea of dead people. <b>All</b> the dead that have gone before us, silent now, just … to ask ourselves or them! what it was <b>all</b> about. Instead we drown ourselves in no<b>is</b>e. … surrounded by false friends claiming that <b>all</b> <b>is</b> <b>well</b>. And look at us! Yes, we are <b>well</b>. …`
q := "all is well"
r, c := snippets(q, s)
r := snippets(q, s)
if r != h {
t.Logf("The snippets are wrong in 「%s」", r)
t.Fail()
}
// Score 12:
// - all is well (1)
// - all, beginning, end, whole word (+4 × 3 = 12)
// - is, beginning, end, whole word (+4 × 1 = 4), and as a substring (1)
// - well, beginning, end, whole word (+4 × 2 = 8)
if c != 26 {
t.Logf("%s score is %d", q, c)
t.Fail()
}
}

View File

@@ -6,17 +6,20 @@
<meta name="viewport" content="width=device-width">
<title>{{.Title}}</title>
<style>
html { max-width: 70ch; padding: 2ch; margin: auto; color: #111; background: #ffe; }
form { display: inline-block; padding-left: 1em; }
html { max-width: 65ch; padding: 1ch; margin: auto; color: #111; background: #ffe; }
body { hyphens: auto; }
header a { margin-right: 1ch; }
form { display: inline-block; }
footer { border-top: 1px solid #888 }
img { max-width: 100%; }
</style>
</head>
<body>
<body lang="{{.Language}}">
<header>
<a href="#main">Skip navigation</a>
<a href="/view/index">Home</a>
<a href="/edit/{{.Name}}">Edit this page</a>
<a href="/edit/{{.Name}}">Edit</a>
<a href="/add/{{.Name}}">Add</a>
<form role="search" action="/search" method="GET">
<input type="text" spellcheck="false" name="q" required>
<button>Search</button>

41
wiki.go
View File

@@ -10,7 +10,8 @@ import (
)
// Templates are parsed at startup.
var templates = template.Must(template.ParseFiles("edit.html", "view.html", "search.html"))
var templates = template.Must(
template.ParseFiles("edit.html", "add.html", "view.html", "search.html"))
// validPath is a regular expression where the second group matches a
// page, so when the handler for "/edit/" is called, a URL path of
@@ -65,7 +66,8 @@ func viewHandler(w http.ResponseWriter, r *http.Request, name string) {
// editHandler uses the "edit.html" template to present an edit page.
// When editing, the page title is not overriden by a title in the
// text. Instead, the page name is used.
// text. Instead, the page name is used. The edit is saved using the
// saveHandler.
func editHandler(w http.ResponseWriter, r *http.Request, name string) {
p, err := loadPage(name)
if err != nil {
@@ -89,6 +91,38 @@ func saveHandler(w http.ResponseWriter, r *http.Request, name string) {
http.Redirect(w, r, "/view/"+name, http.StatusFound)
}
// addHandler uses the "add.html" template to present an empty edit
// page. What you type there is appended to the page using the
// appendHandler.
func addHandler(w http.ResponseWriter, r *http.Request, name string) {
p, err := loadPage(name)
if err != nil {
p = &Page{Title: name, Name: name}
} else {
p.handleTitle(false)
}
renderTemplate(w, "add", p)
}
// appendHandler takes the "body" form parameter and appends it. The
// browser is redirected to the page view.
func appendHandler(w http.ResponseWriter, r *http.Request, name string) {
body := r.FormValue("body")
p, err := loadPage(name)
if err != nil {
p = &Page{Title: name, Name: name, Body: []byte(body)}
} else {
p.handleTitle(false)
p.Body = append(p.Body, []byte(body)...)
}
err = p.save()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
http.Redirect(w, r, "/view/"+name, http.StatusFound)
}
// makeHandler returns a handler that uses the URL path without the
// first path element as its argument, e.g. if the URL path is
// /edit/foo/bar, the editHandler is called with "foo/bar" as its
@@ -130,7 +164,10 @@ func main() {
http.HandleFunc("/view/", makeHandler(viewHandler))
http.HandleFunc("/edit/", makeHandler(editHandler))
http.HandleFunc("/save/", makeHandler(saveHandler))
http.HandleFunc("/add/", makeHandler(addHandler))
http.HandleFunc("/append/", makeHandler(appendHandler))
http.HandleFunc("/search", searchHandler)
fmt.Print("Indexing all pages\n")
loadIndex()
port := getPort()
fmt.Printf("Serving a wiki on port %s\n", port)