forked from mirror/oddmu
Compare commits
30 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
da361284e8 | ||
|
|
6215d2a842 | ||
|
|
47c727c00d | ||
|
|
91381e474c | ||
|
|
4e5aa70529 | ||
|
|
b7048bd5a9 | ||
|
|
41be47dc03 | ||
|
|
44b92cc3e0 | ||
|
|
025d993eb7 | ||
|
|
1209c2b209 | ||
|
|
5d3aa45ddb | ||
|
|
f93177def5 | ||
|
|
aeb53148e7 | ||
|
|
4bce6fcb38 | ||
|
|
92cc1ad883 | ||
|
|
378330cbce | ||
|
|
ad472f9db1 | ||
|
|
b4f861a24e | ||
|
|
e97e5c7e6c | ||
|
|
0a4eabee3d | ||
|
|
fcd4d9136d | ||
|
|
103007be48 | ||
|
|
4afffbc409 | ||
|
|
9e6d59cefa | ||
|
|
2a4902b1b4 | ||
|
|
efc54f1524 | ||
|
|
8fc5bd30e3 | ||
|
|
40855ea442 | ||
|
|
29af9a4cfa | ||
|
|
146f4c9f57 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,2 +1,3 @@
|
||||
/oddmu
|
||||
test.md
|
||||
/testdata/
|
||||
|
||||
171
README.md
171
README.md
@@ -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/gomarkdown/markdown#extensions) in the
|
||||
Markdown 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
|
||||
|
||||
@@ -22,6 +77,9 @@ Feel free to change the templates `view.html` and `edit.html` and
|
||||
restart the server. Modifying the styles in the templates would be a
|
||||
good start to get a feel for it.
|
||||
|
||||
The first change you should make is to replace the email address in
|
||||
`view.html`. 😄
|
||||
|
||||
The templates can refer to the following properties of a page:
|
||||
|
||||
`{{.Title}}` is the page title. If the page doesn't provide its own
|
||||
@@ -44,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
|
||||
@@ -77,9 +150,11 @@ As root, on your server:
|
||||
adduser --system --home /home/oddmu oddmu
|
||||
```
|
||||
|
||||
Copy all the files into `/home/oddmu` to your server: `oddmu`, `oddmu.service`, `view.html` and `edit.html`.
|
||||
Copy all the files into `/home/oddmu` to your server: `oddmu`,
|
||||
`oddmu.service`, `view.html` and `edit.html`.
|
||||
|
||||
Edit the `oddmu.service` file. These are the three lines you most likely have to take care of:
|
||||
Edit the `oddmu.service` file. These are the three lines you most
|
||||
likely have to take care of:
|
||||
|
||||
```
|
||||
ExecStart=/home/oddmu/oddmu
|
||||
@@ -236,6 +311,38 @@ and without needing a wiki page.
|
||||
[Wikipedia](https://en.wikipedia.org/wiki/Robot_exclusion_standard)
|
||||
has more information.
|
||||
|
||||
## Different logins for different access rights
|
||||
|
||||
What if you have a site with various subdirectories and each
|
||||
subdirectory is for a different group of friends? You can set this up
|
||||
using your webserver. One way to do this is to require specific
|
||||
usernames (which must have a password in the password file mentioned
|
||||
above.
|
||||
|
||||
This requires a valid login by the user "alex" or "berta":
|
||||
|
||||
```apache
|
||||
<LocationMatch "^/(edit|save)/intetebi/">
|
||||
Require user alex berta
|
||||
</LocationMatch>
|
||||
```
|
||||
|
||||
## Private wikis
|
||||
|
||||
Based on the above, you can prevent people from reading the wiki, too.
|
||||
The `LocationMatch` must cover the `/view/` URLs. In order to protect
|
||||
*everything*, use a [Location directive](https://httpd.apache.org/docs/current/mod/core.html#location)
|
||||
that matches everything:
|
||||
|
||||
```apache
|
||||
<Location />
|
||||
AuthType Basic
|
||||
AuthName "Password Required"
|
||||
AuthUserFile /home/oddmu/.htpasswd
|
||||
Require valid-user
|
||||
</Location>
|
||||
```
|
||||
|
||||
## Customization (with recompilation)
|
||||
|
||||
The Markdown parser can be customized and
|
||||
@@ -261,6 +368,8 @@ func loadPage(name string) (*Page, error) {
|
||||
filename = name + ".gmi"
|
||||
body, err = os.ReadFile(filename)
|
||||
if err == nil {
|
||||
re := regexp.MustCompile(`(?m)^=>\s*(\S+)\s+(.+)`)
|
||||
body = []byte(re.ReplaceAllString(string(body), `* [$2]($1)`))
|
||||
return &Page{Title: name, Name: name, Body: body}, nil
|
||||
}
|
||||
return nil, err
|
||||
@@ -282,16 +391,54 @@ func (p* Page) renderHtml() {
|
||||
}
|
||||
```
|
||||
|
||||
## Understanding search
|
||||
|
||||
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".
|
||||
|
||||
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.
|
||||
|
||||
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".
|
||||
|
||||
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/)
|
||||
|
||||
15
TODO.md
Normal file
15
TODO.md
Normal file
@@ -0,0 +1,15 @@
|
||||
Easily prepend or append text for use with a mobile browser. Like
|
||||
comments.
|
||||
|
||||
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.
|
||||
4
go.mod
4
go.mod
@@ -11,5 +11,9 @@ require (
|
||||
require (
|
||||
github.com/aymerick/douceur v0.2.0 // indirect
|
||||
github.com/gorilla/css v1.0.0 // indirect
|
||||
github.com/pemistahl/lingua-go v1.4.0 // indirect
|
||||
github.com/shopspring/decimal v1.3.1 // 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
|
||||
)
|
||||
|
||||
12
go.sum
12
go.sum
@@ -2,11 +2,23 @@ github.com/aymerick/douceur v0.2.0 h1:Mv+mAeH1Q+n9Fr+oyamOlAkUNPWPlA8PPGR0QAaYuP
|
||||
github.com/aymerick/douceur v0.2.0/go.mod h1:wlT5vV2O3h55X9m7iVYN0TBM0NH/MmbLnd30/FjWUq4=
|
||||
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/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
|
||||
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/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||
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/shopspring/decimal v1.3.1 h1:2Usl1nmF/WZucqkFZhnfFYxxxu8LG21F6nPQBE5gKV8=
|
||||
github.com/shopspring/decimal v1.3.1/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
|
||||
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=
|
||||
|
||||
42
highlight.go
42
highlight.go
@@ -1,45 +1,13 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
}
|
||||
|
||||
83
page.go
83
page.go
@@ -1,16 +1,34 @@
|
||||
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"
|
||||
"html/template"
|
||||
"strings"
|
||||
"bytes"
|
||||
"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"
|
||||
@@ -18,11 +36,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
|
||||
@@ -35,6 +62,14 @@ func (p *Page) save() error {
|
||||
s := bytes.ReplaceAll(p.Body, []byte{'\r'}, []byte{})
|
||||
p.Body = s
|
||||
p.updateIndex()
|
||||
d := filepath.Dir(filename)
|
||||
if d != "." {
|
||||
err := os.MkdirAll(d, 0700)
|
||||
if err != nil {
|
||||
fmt.Printf("Creating directory %s failed", d)
|
||||
return err
|
||||
}
|
||||
}
|
||||
return os.WriteFile(filename, s, 0600)
|
||||
}
|
||||
|
||||
@@ -49,13 +84,13 @@ 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
|
||||
// any. If replace is true, the page title is also removed from
|
||||
// Page.Body. Make sure not to save this! This is only for rendering.
|
||||
func (p* Page) handleTitle(replace bool) {
|
||||
func (p *Page) handleTitle(replace bool) {
|
||||
s := string(p.Body)
|
||||
m := titleRegexp.FindStringSubmatch(s)
|
||||
if m != nil {
|
||||
@@ -67,16 +102,16 @@ func (p* Page) handleTitle(replace bool) {
|
||||
}
|
||||
|
||||
// renderHtml renders the Page.Body to HTML and sets Page.Html.
|
||||
func (p* Page) renderHtml() {
|
||||
func (p *Page) renderHtml() {
|
||||
maybeUnsafeHTML := markdown.ToHTML(p.Body, nil, nil)
|
||||
html := bluemonday.UGCPolicy().SanitizeBytes(maybeUnsafeHTML)
|
||||
p.Html = template.HTML(html);
|
||||
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 {
|
||||
func (p *Page) plainText() string {
|
||||
parser := parser.New()
|
||||
doc := markdown.Parse(p.Body, parser)
|
||||
text := []byte("")
|
||||
@@ -94,18 +129,24 @@ func (p* Page) plainText() string {
|
||||
}
|
||||
}
|
||||
// Remove trailing space
|
||||
for text[len(text)-1] == ' ' {
|
||||
text = text[0:len(text)-1]
|
||||
for len(text) > 0 && 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) {
|
||||
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 ""
|
||||
}
|
||||
|
||||
36
page_test.go
36
page_test.go
@@ -1,11 +1,12 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestPageTitle (t *testing.T) {
|
||||
func TestPageTitle(t *testing.T) {
|
||||
p := &Page{Body: []byte(`# Ache
|
||||
My back aches for you
|
||||
I sit, stare and type for hours
|
||||
@@ -26,7 +27,7 @@ But yearn for blue sky`)}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPagePlainText (t *testing.T) {
|
||||
func TestPagePlainText(t *testing.T) {
|
||||
p := &Page{Body: []byte(`# Water
|
||||
The air will not come
|
||||
To inhale is an effort
|
||||
@@ -39,7 +40,7 @@ The summer heat kills`)}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPageHtml (t *testing.T) {
|
||||
func TestPageHtml(t *testing.T) {
|
||||
p := &Page{Body: []byte(`# Sun
|
||||
Silver leaves shine bright
|
||||
They droop, boneless, weak and sad
|
||||
@@ -57,3 +58,32 @@ A cruel sun stares down</p>
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
|
||||
func TestPageDir(t *testing.T) {
|
||||
_ = os.RemoveAll("testdata")
|
||||
loadIndex()
|
||||
p := &Page{Name: "testdata/moon", Body: []byte(`# Moon
|
||||
From bed to bathroom
|
||||
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()
|
||||
}
|
||||
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`)
|
||||
if l != "en" {
|
||||
t.Logf("Language detected: %s", l)
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
|
||||
47
score.go
Normal file
47
score.go
Normal 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
104
score_test.go
Normal 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()
|
||||
}
|
||||
}
|
||||
51
search.go
51
search.go
@@ -1,12 +1,14 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
trigram "github.com/dgryski/go-trigram"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"slices"
|
||||
"io/fs"
|
||||
"fmt"
|
||||
trigram "github.com/dgryski/go-trigram"
|
||||
"io/fs"
|
||||
"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
|
||||
|
||||
32
search.html
32
search.html
@@ -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>
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"strings"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var name string = "test"
|
||||
|
||||
// TestIndex relies on README.md being indexed
|
||||
func TestIndex (t *testing.T) {
|
||||
func TestIndex(t *testing.T) {
|
||||
_ = os.Remove(name + ".md")
|
||||
loadIndex()
|
||||
q := "Oddµ"
|
||||
@@ -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")
|
||||
@@ -68,4 +96,7 @@ func TestIndex (t *testing.T) {
|
||||
t.Logf("Page '%s' not found using the new content: %s", name, p.Body)
|
||||
t.Fail()
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
_ = os.Remove(name + ".md")
|
||||
})
|
||||
}
|
||||
|
||||
42
snippets.go
42
snippets.go
@@ -1,22 +1,36 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"regexp"
|
||||
"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]
|
||||
@@ -45,7 +59,7 @@ func snippets (q string, s string) (string, int) {
|
||||
if j > -1 {
|
||||
// get the substring containing the start of
|
||||
// the match, ending on word boundaries
|
||||
from := j - snippetlen / 2
|
||||
from := j - snippetlen/2
|
||||
if from < 0 {
|
||||
from = 0
|
||||
}
|
||||
@@ -55,7 +69,7 @@ func snippets (q string, s string) (string, int) {
|
||||
} else {
|
||||
start += from
|
||||
}
|
||||
to := j + snippetlen / 2
|
||||
to := j + snippetlen/2
|
||||
if to > len(s) {
|
||||
to = len(s)
|
||||
}
|
||||
@@ -69,11 +83,11 @@ func snippets (q string, s string) (string, int) {
|
||||
end += to
|
||||
}
|
||||
}
|
||||
t = s[start : end];
|
||||
res = res + t + " …";
|
||||
t = s[start:end]
|
||||
res = res + t + " …"
|
||||
// truncate text to avoid rematching the same string.
|
||||
s = s[end:]
|
||||
}
|
||||
}
|
||||
return highlight(q, res)
|
||||
return highlight(q, re, res)
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
}
|
||||
|
||||
28
view.html
28
view.html
@@ -6,22 +6,32 @@
|
||||
<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>
|
||||
<h1>{{.Title}}</h1>
|
||||
<div>
|
||||
<body lang="{{.Language}}">
|
||||
<header>
|
||||
<a href="#main">Skip navigation</a>
|
||||
<a href="/view/index">Home</a>
|
||||
<a href="/edit/{{.Name}}">Edit this page</a>
|
||||
<form role="search" action="/search" method="GET">
|
||||
<input type="text" spellcheck="false" name="q" required>
|
||||
<button>Search</button>
|
||||
</form>
|
||||
</div>
|
||||
<div>
|
||||
{{.Html}}
|
||||
</div>
|
||||
</header>
|
||||
<main id="main">
|
||||
<h1>{{.Title}}</h1>
|
||||
{{.Html}}
|
||||
</main>
|
||||
<footer>
|
||||
<address>
|
||||
Comments? Send mail to Alex Schroeder <<a href="mailto:alex@alexschroeder.ch">alex@alexschroeder.ch</a>>
|
||||
</address>
|
||||
</footer>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
13
wiki.go
13
wiki.go
@@ -1,12 +1,12 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"html/template"
|
||||
"net/http"
|
||||
"strings"
|
||||
"regexp"
|
||||
"fmt"
|
||||
"os"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Templates are parsed at startup.
|
||||
@@ -45,7 +45,7 @@ func rootHandler(w http.ResponseWriter, r *http.Request) {
|
||||
// "view.html" template is used to show the rendered HTML.
|
||||
func viewHandler(w http.ResponseWriter, r *http.Request, name string) {
|
||||
// Short cut for text files
|
||||
if (strings.HasSuffix(name, ".txt")) {
|
||||
if strings.HasSuffix(name, ".txt") {
|
||||
body, err := os.ReadFile(name)
|
||||
if err == nil {
|
||||
w.Write(body)
|
||||
@@ -94,7 +94,7 @@ func saveHandler(w http.ResponseWriter, r *http.Request, name string) {
|
||||
// /edit/foo/bar, the editHandler is called with "foo/bar" as its
|
||||
// argument. This uses the second group from the validPath regular
|
||||
// expression.
|
||||
func makeHandler(fn func (http.ResponseWriter, *http.Request, string)) http.HandlerFunc {
|
||||
func makeHandler(fn func(http.ResponseWriter, *http.Request, string)) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
m := validPath.FindStringSubmatch(r.URL.Path)
|
||||
if m != nil {
|
||||
@@ -131,8 +131,9 @@ func main() {
|
||||
http.HandleFunc("/edit/", makeHandler(editHandler))
|
||||
http.HandleFunc("/save/", makeHandler(saveHandler))
|
||||
http.HandleFunc("/search", searchHandler)
|
||||
fmt.Print("Indexing all pages\n")
|
||||
loadIndex()
|
||||
port := getPort()
|
||||
fmt.Printf("Serving a wiki on port %s\n", port)
|
||||
http.ListenAndServe(":" + port, nil)
|
||||
http.ListenAndServe(":"+port, nil)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user