41 Commits

Author SHA1 Message Date
Alex Schroeder
b64d56a648 Document a curious bug with the Apache configuration 2024-01-27 21:27:17 +01:00
Alex Schroeder
ce64d04dde Switch to 70ch max-width for all 2024-01-17 22:09:11 +01:00
Alex Schroeder
de5bd2d23e Remove restore and simplify cleanup
The test setups are now simplified, since no files in the source
directory are modified.
2024-01-17 14:15:07 +01:00
Alex Schroeder
29842fe685 Major change in how notifications work 2024-01-17 14:10:07 +01:00
Alex Schroeder
4042be68f3 Reformatting of documentation, man page generation
Fill column is 80, no double spaces after sentence endings.
2024-01-17 14:06:05 +01:00
Christopher Brannon
87846e15b9 Support socket activation.
This also removes the Unix socket support added yesterday.  To use
Unix sockets, use socket activation and let systemd manage the socket.
2024-01-17 13:56:05 +01:00
Alex Schroeder
1390d82e29 Update man page 2024-01-16 14:02:32 +01:00
Alex Schroeder
bb5bd1c629 Small comment fix
Reformatted comment for 120 characters. Note that the presence of a
forward-slash anywhere in ODDMU_ADDRESS makes this a Unix-domain
socket.
2024-01-16 13:46:09 +01:00
Christopher Brannon
777c498700 Allow specifying the listen address.
This patch also adds support for HTTP over a Unix-domain socket.
2024-01-16 13:43:01 +01:00
Alex Schroeder
803025f56a Fix test for new year 2024-01-16 13:43:01 +01:00
Alex Schroeder
dce66ec5a1 Silence output of static command for tests 2024-01-16 13:43:01 +01:00
Alex Schroeder
b6d596cb08 Improve man page for static subcommand
Discuss what it means to have some files hard linked.
Discuss the use of ODDMU_LANGUAGES to speed site generation up.
2024-01-10 07:37:33 +01:00
Alex Schroeder
3be26b9af1 Load languages before generating static site
Without it, all HTML files have lang="".

Also added a progress indication because generating the site takes a
lot longer, now.
2024-01-10 07:20:47 +01:00
Alex Schroeder
197f9b78f1 Add more links to man pages to the README 2024-01-07 11:47:49 +01:00
Alex Schroeder
0ebcd1a4ef Only load templates if they are required 2024-01-07 00:33:56 +01:00
Alex Schroeder
9591913acc Improve the README 2024-01-07 00:32:52 +01:00
Alex Schroeder
2a82435b92 Add oddmu-list to the README 2023-12-20 22:56:13 +01:00
Alex Schroeder
844f623f26 Update man pages for list and search subcommands 2023-12-20 08:22:25 +01:00
Alex Schroeder
e9b128d98c Return relative links from search
When search is limited to a directory, return relative links. This
makes it possible to paste the search result on to an "index.md" file
in that same directory.
2023-12-20 08:20:52 +01:00
Alex Schroeder
1ce8182571 Search CLI with -dir
Allow limiting search to a subdirectory from the command-line.
2023-12-20 08:11:17 +01:00
Alex Schroeder
fefc00e2a2 Lower case doc string for options 2023-12-20 08:09:25 +01:00
Alex Schroeder
b44821d6de Add man page for oddmu-list 2023-12-17 23:08:11 +01:00
Alex Schroeder
1174369e8a Deleted TODO file 2023-12-17 22:55:35 +01:00
Alex Schroeder
b31745a5e4 Rewrote HEIC support to use bashdrew's code 2023-12-17 22:55:35 +01:00
Alex Schroeder
ce70c97b6a Add new list subcommand argument and list test 2023-12-17 22:55:35 +01:00
Alex Schroeder
0f2dd71449 Update all dependencies 2023-12-09 21:26:17 +01:00
Alex Schroeder
7074995d9a Add HEIC decoding 2023-12-09 21:23:05 +01:00
Alex Schroeder
d2552b2f68 Better image upload instructions 2023-12-09 09:36:48 +01:00
Alex Schroeder
e231412bdb Add list command 2023-12-09 09:21:24 +01:00
Alex Schroeder
0481d9003c Do not overwrite fresh backups
If the backup file is less than an hour old, do not overwrite it.
2023-11-24 13:49:41 +01:00
Alex Schroeder
7060f8a027 Fix handling of directories
For subdirectories, the redirect my be to an absolute URL starting
with /view.
2023-11-19 12:36:25 +01:00
Alex Schroeder
1cc5fcb823 Show index page for directory URLs 2023-11-19 12:21:51 +01:00
Alex Schroeder
f392d18dc9 Ensure empty line when adding 2023-11-18 23:48:13 +01:00
Alex Schroeder
6ab51afa30 Document the fact that HTML is not sanitized 2023-11-14 15:34:29 +01:00
Alex Schroeder
a73328ca2e Remove all HTML sanitization *again*!!
The previous revision is the one with HTML and SVG sanitization.
I'm removing it because I think the authors are all trusted users
and so they produce trusted HTML.
2023-11-14 14:07:32 +01:00
Alex Schroeder
ee3afc3384 Follow dependabot recommendations
Upgraded markdown, net, and image libraries.
2023-11-12 12:51:25 +01:00
Alex Schroeder
160ebd71e2 go fmt 2023-11-12 12:39:48 +01:00
Alex Schroeder
8900725737 go mod tidy 2023-11-12 12:38:55 +01:00
Alex Schroeder
f58476bba5 Merge branch 'svg-sanitization' 2023-11-12 12:22:45 +01:00
Alex Schroeder
dcb0cc7f51 Revert "No longer sanitize the HTML output!"
This reverts commit eb44880e8e.
2023-11-12 12:12:19 +01:00
Alex Schroeder
eb44880e8e No longer sanitize the HTML output! 2023-11-09 07:23:51 +01:00
53 changed files with 1276 additions and 495 deletions

View File

@@ -42,3 +42,6 @@ install:
for n in 1 5 7; do install -D -t $$HOME/.local/share/man/man$$n man/*.$$n; done
go build
install -D -t $$HOME/.local/bin oddmu
missing:
for f in man/*.txt; do grep --quiet "$$f" README.md || echo $$f is not in the README; done

View File

@@ -1,31 +1,34 @@
# 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. 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'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 program helps you run 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.
If you're the only user and it just runs on your laptop, then you can
think of it as a [memex](https://en.wikipedia.org/wiki/Memex), a
memory extender.
This wiki uses a [Markdown
library](https://github.com/gomarkdown/markdown) to generate the web
pages from Markdown. There are two extensions Oddmu adds to the
library: local links `[[like this]]`, hashtags `#Like_This` and
fediverse account links like @alex@alexschroeder.ch.
Oddµ can be used as a web server behind a reverse proxy such as Apache
or it can be used as a static site generator.
This wiki uses the [lingua](https://github.com/pemistahl/lingua-go)
library to detect languages in order to get hyphenation right.
When Oddµ runs as a web server, it serves all the Markdown files
(ending in `.md`) as web pages and allows you to edit them.
This wiki uses the standard
[html/template](https://pkg.go.dev/html/template) library to generate
HTML.
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.
Oddµ uses a [Markdown library](https://github.com/gomarkdown/markdown)
to generate the web pages from Markdown. Oddmu adds the following
extensions: local links `[[like this]]`, hashtags `#Like_This` and
fediverse account links like `@alex@alexschroeder.ch`.
The [lingua](https://github.com/pemistahl/lingua-go) library detects
languages in order to get hyphenation right.
The standard [html/template](https://pkg.go.dev/html/template) library
is used to generate HTML.
## Documentation
@@ -45,13 +48,20 @@ links. Local links must use percent encoding for page names so there
is a section about percent encoding. The man page also explains how
feeds are generated.
[oddmu-list(1)](/oddmu.git/blob/main/man/oddmu-list.1.txt): This man
page documents the "list" subcommand which you can use to get page
names and page titles.
[oddmu-search(1)](/oddmu.git/blob/main/man/oddmu-search.1.txt): This
man page documents the "search" subcommand which you can use to build
indexes lists of page links. These are important for feeds.
[oddmu-search(7)](/oddmu.git/blob/main/man/oddmu-search.7.txt): This
man page documents how search and scoring work.
[oddmu-replace(1)](/oddmu.git/blob/main/man/oddmu-replace.1.txt): This
man page documents the "replace" subcommand to make mass changes to
the files much like find(1), grep(1) and sed(1) or perl (1).
the files much like find(1), grep(1) and sed(1) or perl(1).
[oddmu-missing(1)](/oddmu.git/blob/main/man/oddmu-missing.1.txt): This
man page documents the "missing" subcommand to list local links that
@@ -66,6 +76,11 @@ man page documents the "static" subcommand to generate an entire
static website from the command line, avoiding the need to run Oddmu
as a server. Also great for archiving.
[oddmu-notify(1)](/oddmu.git/blob/main/man/oddmu-notify.1.txt): This
man page documents the "notify" subcommand to add links to hashtag
pages, index and changes for a given page. This is useful when you
edit the Markdown files locally.
[oddmu-templates(5)](/oddmu.git/blob/main/man/oddmu-templates.5.txt):
This man page documents how the templates can be changed (how they
*must* be changed) and lists the attributes available for the various
@@ -77,7 +92,7 @@ tasks such as using logins to limit what visitors can edit.
[oddmu.service(5)](/oddmu.git/blob/main/man/oddmu.service.5.txt): This
man page documents how to setup a systemd unit and have it manage
Oddmu. “Great configurability brings brings great burdens.”
Oddmu. “Great configurability brings great burdens.”
## Building

View File

@@ -1,6 +0,0 @@
Upload files should use path info so that we can use Apache to
restrict access to directories.
Automatically scale or process files.
Post by Delta Chat? That is, allow certain encrypted emails to post.

View File

@@ -10,7 +10,7 @@ import (
// initAccounts()
// p := &Page{Body: []byte(`@alex, @alex@alexschroeder.ch said`)}
// p.renderHtml()
// r := `<p>@alex, <a href="https://alexschroeder.ch/users/alex" rel="nofollow">@alex</a> said</p>
// r := `<p>@alex, <a href="https://alexschroeder.ch/users/alex">@alex</a> said</p>
// `
// assert.Equal(t, r, string(p.Html))
// }

View File

@@ -1,6 +1,7 @@
package main
import (
"bytes"
"log"
"net/http"
)
@@ -26,7 +27,7 @@ func appendHandler(w http.ResponseWriter, r *http.Request, name string) {
if err != nil {
p = &Page{Name: name, Body: []byte(body)}
} else {
p.Body = append(p.Body, []byte(body)...)
p.append([]byte(body))
}
p.handleTitle(false)
err = p.save()
@@ -42,3 +43,14 @@ func appendHandler(w http.ResponseWriter, r *http.Request, name string) {
}
http.Redirect(w, r, "/view/"+name, http.StatusFound)
}
func (p *Page) append(body []byte) {
// ensure an empty line at the end
if bytes.HasSuffix(p.Body, []byte("\n\n")) {
} else if bytes.HasSuffix(p.Body, []byte("\n")) {
p.Body = append(p.Body, '\n')
} else {
p.Body = append(p.Body, '\n', '\n')
}
p.Body = append(p.Body, body...)
}

View File

@@ -9,6 +9,18 @@ import (
"time"
)
func TestEmptyLineAdd(t *testing.T) {
p := &Page{Name: "testdata/add/fire", Body: []byte(`# Coal
Black rocks light as foam
Shaking, puring, shoveling`)}
p.append([]byte("Into the oven"))
assert.Equal(t, string(p.Body), `# Coal
Black rocks light as foam
Shaking, puring, shoveling
Into the oven`)
}
func TestAddAppend(t *testing.T) {
cleanup(t, "testdata/add")
index.load()
@@ -30,36 +42,32 @@ It's not `)}
"GET", "/add/testdata/add/fire", nil))
HTTPRedirectTo(t, makeHandler(appendHandler, true),
"POST", "/append/testdata/add/fire", data, "/view/testdata/add/fire")
assert.Regexp(t, regexp.MustCompile("Its not barbecue"),
assert.Regexp(t, regexp.MustCompile(`not</p>\s*<p>barbecue`),
assert.HTTPBody(makeHandler(viewHandler, true),
"GET", "/view/testdata/add/fire", nil))
}
func TestAddAppendChanges(t *testing.T) {
cleanup(t, "testdata/notification2", "changes.md", "changes.md~")
restore(t, "index.md")
os.Remove("changes.md")
cleanup(t, "testdata/append")
today := time.Now().Format(time.DateOnly)
p := &Page{Name: "testdata/notification2/" + today + "-water", Body: []byte(`# Water
p := &Page{Name: "testdata/append/" + today + "-water", Body: []byte(`# Water
Sunlight dancing fast
Blue and green and pebbles gray
`)}
p.save()
data := url.Values{}
data.Set("body", "Stand in cold water")
data.Add("notify", "on")
HTTPRedirectTo(t, makeHandler(appendHandler, true),
"POST", "/append/testdata/notification2/" + today + "-water",
data, "/view/testdata/notification2/" + today + "-water")
"POST", "/append/testdata/append/"+today+"-water",
data, "/view/testdata/append/"+today+"-water")
// The changes.md file was created
s, err := os.ReadFile("changes.md")
s, err := os.ReadFile("testdata/append/changes.md")
assert.NoError(t, err)
d := time.Now().Format(time.DateOnly)
assert.Equal(t, "# Changes\n\n## " + d + "\n* [Water](testdata/notification2/" + today + "-water)\n", string(s))
assert.Equal(t, "# Changes\n\n## "+today+"\n* [Water]("+today+"-water)\n", string(s))
// Link added to index.md file
s, err = os.ReadFile("index.md")
s, err = os.ReadFile("testdata/append/index.md")
assert.NoError(t, err)
assert.Contains(t, string(s), "\n* [Water](testdata/notification2/" + today + "-water)\n")
// New index contains just the link
assert.Equal(t, string(s), "* [Water]("+today+"-water)\n")
}

View File

@@ -1,42 +1,54 @@
package main
import (
"log"
"os"
"path"
"regexp"
"strings"
"time"
)
// notify adds a link to the "changes" page, as well as to all the existing hashtag pages. If the "changes" page does
// not exist, it is created. If the hashtag page does not exist, it is not. Hashtag pages are considered optional.
// notify adds a link to the "changes" page, the "index" page, as well as to all the existing hashtag pages. The link to
// the "index" page is only added if the page being edited is a blog page for the current year. The link to existing
// hashtag pages is only added for blog pages. If the "changes" page does not exist, it is created. If the hashtag page
// does not exist, it is not. Hashtag pages are considered optional. If the page that's being edited is in a
// subdirectory, then the "changes", "index" and hashtag pages of that particular subdirectory are affected. Every
// subdirectory is treated like a potentially independent wiki.
func (p *Page) notify() error {
p.handleTitle(false)
if p.Title == "" {
p.Title = p.Name
}
esc := nameEscape(p.Name)
esc := nameEscape(path.Base(p.Name))
link := "* [" + p.Title + "](" + esc + ")\n"
re := regexp.MustCompile(`(?m)^\* \[[^\]]+\]\(` + esc + `\)\n`)
dir := path.Dir(p.Name)
// Recent changes for all pages
err := addLinkWithDate("changes", link, re)
if dir != "." {
err := os.MkdirAll(dir, 0755)
if err != nil {
log.Printf("Creating directory %s failed: %s", dir, err)
return err
}
}
err := addLinkWithDate(path.Join(dir, "changes"), link, re)
if err != nil {
return err
}
// For blog pages only…
if p.isBlog() {
// Add to the index only if the blog post is for the current year
if strings.HasPrefix(path.Base(p.Name), time.Now().Format("2006")) {
err := addLink("index", link, re)
err := addLink(path.Join(dir, "index"), true, link, re)
if err != nil {
log.Printf("Updating index in %s failed: %s", dir, err)
return err
}
}
// Update hashtag pages
p.renderHtml() // to set hashtags
for _, hashtag := range p.Hashtags {
err := addLink(path.Join(dir, hashtag), link, re)
err := addLink(path.Join(dir, hashtag), false, link, re)
if err != nil {
log.Printf("Updating hashtag %s in %s failed: %s", hashtag, dir, err)
return err
}
}
@@ -53,7 +65,7 @@ func addLinkWithDate(name, link string, re *regexp.Regexp) error {
c, err := loadPage(name)
if err != nil {
// create a new page
c = &Page{Name: "changes", Body: []byte("# Changes\n\n## " + date + "\n" + link)}
c = &Page{Name: name, Body: []byte("# Changes\n\n## " + date + "\n" + link)}
} else {
org = string(c.Body)
// remove the old match, if one exists
@@ -68,7 +80,7 @@ func addLinkWithDate(name, link string, re *regexp.Regexp) error {
// remove the preceding date if there are now two dates following each other
re := regexp.MustCompile(`(?m)^## (\d\d\d\d-\d\d-\d\d)\n\n## (\d\d\d\d-\d\d-\d\d)\n`)
if re.Match(c.Body[loc[0]-14 : loc[0]+15]) {
c.Body = append(c.Body[0 : loc[0]-14], c.Body[loc[0]+1 : ]...)
c.Body = append(c.Body[0:loc[0]-14], c.Body[loc[0]+1:]...)
}
} else if len(c.Body) == loc[0] {
// remove a trailing date
@@ -94,7 +106,7 @@ func addLinkWithDate(name, link string, re *regexp.Regexp) error {
m := re.Find(c.Body[loc[0]-14 : loc[0]])
if m == nil {
// not a date: insert date, don't move insertion point
} else if string(c.Body[loc[0]-11 : loc[0]-1]) == date {
} else if string(c.Body[loc[0]-11:loc[0]-1]) == date {
// if the date is our date, don't add it, don't move insertion point
addDate = false
} else {
@@ -136,11 +148,16 @@ func addLinkWithDate(name, link string, re *regexp.Regexp) error {
// addLink adds a link to a named page, if the page exists and doesn't contain the link. If the link exists but with a
// different title, the title is fixed.
func addLink(name, link string, re *regexp.Regexp) error {
func addLink(name string, mandatory bool, link string, re *regexp.Regexp) error {
c, err := loadPage(name)
if err != nil {
// Skip non-existing files: no error
return nil
if (mandatory) {
c = &Page{Name: name, Body: []byte(link)}
return c.save()
} else {
// Skip non-existing files: no error
return nil
}
}
org := string(c.Body)
// if a link exists, that's the place to insert the new link (in which case loc[0] and loc[1] differ)

View File

@@ -10,32 +10,29 @@ import (
// Note TestEditSaveChanges and TestAddAppendChanges.
func TestChanges(t *testing.T) {
cleanup(t, "changes.md", "changes.md~")
restore(t, "index.md")
os.Remove("changes.md")
cleanup(t, "testdata/washing")
today := time.Now().Format(time.DateOnly)
p := &Page{Name: "testdata/" + today + "-machine",
p := &Page{Name: "testdata/washing/" + today + "-machine",
Body: []byte(`# Washing machine
Churning growling thing
Water spraying in a box
Out of sight and dark`)}
p.notify()
// Link added to changes.md file
s, err := os.ReadFile("changes.md")
s, err := os.ReadFile("testdata/washing/changes.md")
assert.NoError(t, err)
assert.Contains(t, string(s), "[Washing machine](testdata/" + today + "-machine)")
assert.Contains(t, string(s), "[Washing machine]("+today+"-machine)")
// Link added to index.md file
s, err = os.ReadFile("index.md")
s, err = os.ReadFile("testdata/washing/index.md")
assert.NoError(t, err)
assert.Contains(t, string(s), "\n* [Washing machine](testdata/" + today + "-machine)\n")
// New index contains just the link
assert.Equal(t, string(s), "* [Washing machine]("+today+"-machine)\n")
}
func TestChangesWithHashtag(t *testing.T) {
cleanup(t, "changes.md", "changes.md~")
restore(t, "index.md")
os.Remove("changes.md")
cleanup(t, "testdata/changes")
intro := "# Haiku\n"
line := "* [Hotel room](testdata/changes/2023-10-27-hotel)\n"
line := "* [Hotel room](2023-10-27-hotel)\n"
h := &Page{Name: "testdata/changes/Haiku", Body: []byte(intro)}
h.save()
p := &Page{Name: "testdata/changes/2023-10-27-hotel",
@@ -46,154 +43,164 @@ Home away from home
#Haiku #Poetry`)}
p.notify()
s, err := os.ReadFile("changes.md")
s, err := os.ReadFile("testdata/changes/changes.md")
assert.NoError(t, err)
assert.Contains(t, string(s), line)
s, err = os.ReadFile("testdata/changes/Haiku.md")
assert.NoError(t, err)
assert.Equal(t, intro + line, string(s))
assert.Equal(t, intro+line, string(s))
assert.NoFileExists(t, "testdata/changes/Poetry.md")
}
func TestChangesWithList(t *testing.T) {
cleanup(t, "changes.md", "changes.md~")
cleanup(t, "testdata/changes")
intro := "# Changes\n\nThis is a paragraph.\n\n"
line := "* [a change](change)\n"
d := "## " + time.Now().Format(time.DateOnly) + "\n"
os.WriteFile("changes.md", []byte(intro+d+line), 0644)
line := "* [a change](change)\n"
assert.NoError(t, os.MkdirAll("testdata/changes", 0755))
assert.NoError(t, os.WriteFile("testdata/changes/changes.md", []byte(intro+d+line), 0644))
p := &Page{Name: "testdata/changes/alex", Body: []byte(`Hallo!`)}
p.notify()
s, err := os.ReadFile("changes.md")
s, err := os.ReadFile("testdata/changes/changes.md")
assert.NoError(t, err)
new_line := "* [testdata/changes/alex](testdata/changes/alex)\n"
new_line := "* [testdata/changes/alex](alex)\n"
// new line was added at the beginning of the list
assert.Equal(t, intro+d+new_line+line, string(s))
}
func TestChangesWithOldList(t *testing.T) {
cleanup(t, "changes.md", "changes.md~")
cleanup(t, "testdata/changes")
intro := "# Changes\n\nThis is a paragraph.\n\n"
line := "* [a change](change)\n"
y := "## " + time.Now().Add(-24*time.Hour).Format(time.DateOnly) + "\n"
d := "## " + time.Now().Format(time.DateOnly) + "\n"
os.WriteFile("changes.md", []byte(intro+y+line), 0644)
assert.NoError(t, os.MkdirAll("testdata/changes", 0755))
assert.NoError(t, os.WriteFile("testdata/changes/changes.md", []byte(intro+y+line), 0644))
p := &Page{Name: "testdata/changes/alex", Body: []byte(`Hallo!`)}
p.notify()
s, err := os.ReadFile("changes.md")
s, err := os.ReadFile("testdata/changes/changes.md")
assert.NoError(t, err)
new_line := "* [testdata/changes/alex](testdata/changes/alex)\n"
new_line := "* [testdata/changes/alex](alex)\n"
// new line was added at the beginning of the list
assert.Equal(t, intro+d+new_line+"\n"+y+line, string(s))
}
func TestChangesWithOldDisappearingListAtTheEnd(t *testing.T) {
cleanup(t, "changes.md", "changes.md~")
cleanup(t, "testdata/changes")
intro := "# Changes\n\nThis is a paragraph.\n\n"
line := "* [a change](testdata/changes/alex)\n"
line := "* [a change](alex)\n"
y := "## " + time.Now().Add(-24*time.Hour).Format(time.DateOnly) + "\n"
d := "## " + time.Now().Format(time.DateOnly) + "\n"
os.WriteFile("changes.md", []byte(intro+y+line), 0644)
assert.NoError(t, os.MkdirAll("testdata/changes", 0755))
assert.NoError(t, os.WriteFile("testdata/changes/changes.md", []byte(intro+y+line), 0644))
p := &Page{Name: "testdata/changes/alex", Body: []byte(`Hallo!`)}
p.notify()
s, err := os.ReadFile("changes.md")
s, err := os.ReadFile("testdata/changes/changes.md")
assert.NoError(t, err)
new_line := "* [testdata/changes/alex](testdata/changes/alex)\n"
new_line := "* [testdata/changes/alex](alex)\n"
// new line was added at the beginning of the list, with the new date, and the old date disappeared
assert.Equal(t, intro+d+new_line, string(s))
}
func TestChangesWithOldDisappearingListInTheMiddle(t *testing.T) {
cleanup(t, "changes.md", "changes.md~")
cleanup(t, "testdata/changes")
intro := "# Changes\n\nThis is a paragraph.\n\n"
line := "* [a change](testdata/changes/alex)\n"
other := "* [other change](testdata/changes/whatever)\n"
line := "* [a change](alex)\n"
other := "* [other change](whatever)\n"
yy := "## " + time.Now().Add(-48*time.Hour).Format(time.DateOnly) + "\n"
y := "## " + time.Now().Add(-24*time.Hour).Format(time.DateOnly) + "\n"
d := "## " + time.Now().Format(time.DateOnly) + "\n"
os.WriteFile("changes.md", []byte(intro+y+line+"\n"+yy+other), 0644)
assert.NoError(t, os.MkdirAll("testdata/changes", 0755))
assert.NoError(t, os.WriteFile("testdata/changes/changes.md", []byte(intro+y+line+"\n"+yy+other), 0644))
p := &Page{Name: "testdata/changes/alex", Body: []byte(`Hallo!`)}
p.notify()
s, err := os.ReadFile("changes.md")
s, err := os.ReadFile("testdata/changes/changes.md")
assert.NoError(t, err)
new_line := "* [testdata/changes/alex](testdata/changes/alex)\n"
new_line := "* [testdata/changes/alex](alex)\n"
// new line was added at the beginning of the list, with the new date, and the old date disappeared
assert.Equal(t, intro+d+new_line+"\n"+yy+other, string(s))
}
func TestChangesWithListAtTheTop(t *testing.T) {
cleanup(t, "testdata/changes", "changes.md", "changes.md~")
cleanup(t, "testdata/changes")
line := "* [a change](change)\n"
d := "## " + time.Now().Format(time.DateOnly) + "\n"
os.WriteFile("changes.md", []byte(line), 0644)
assert.NoError(t, os.MkdirAll("testdata/changes", 0755))
assert.NoError(t, os.WriteFile("testdata/changes/changes.md", []byte(line), 0644))
p := &Page{Name: "testdata/changes/alex", Body: []byte(`Hallo!`)}
p.notify()
s, err := os.ReadFile("changes.md")
s, err := os.ReadFile("testdata/changes/changes.md")
assert.NoError(t, err)
new_line := "* [testdata/changes/alex](testdata/changes/alex)\n"
new_line := "* [testdata/changes/alex](alex)\n"
// new line was added at the top, no error due to missing introduction
assert.Equal(t, d+new_line+line, string(s))
}
func TestChangesWithNoList(t *testing.T) {
cleanup(t, "testdata/changes", "changes.md", "changes.md~")
cleanup(t, "testdata/changes")
intro := "# Changes\n\nThis is a paragraph."
d := "## " + time.Now().Format(time.DateOnly) + "\n"
os.WriteFile("changes.md", []byte(intro), 0644)
assert.NoError(t, os.MkdirAll("testdata/changes", 0755))
assert.NoError(t, os.WriteFile("testdata/changes/changes.md", []byte(intro), 0644))
p := &Page{Name: "testdata/changes/alex", Body: []byte(`Hallo!`)}
p.notify()
s, err := os.ReadFile("changes.md")
s, err := os.ReadFile("testdata/changes/changes.md")
assert.NoError(t, err)
new_line := "* [testdata/changes/alex](testdata/changes/alex)\n"
new_line := "* [testdata/changes/alex](alex)\n"
// into is still there and a new list was started
assert.Equal(t, intro+"\n\n"+d+new_line, string(s))
}
func TestChangesWithUpdate(t *testing.T) {
cleanup(t, "testdata/changes", "changes.md", "changes.md~")
cleanup(t, "testdata/changes")
intro := "# Changes\n\nThis is a paragraph.\n\n"
other := "* [other change](testdata/changes/whatever)\n"
other := "* [other change](whatever)\n"
d := "## " + time.Now().Format(time.DateOnly) + "\n"
line := "* [a change](testdata/changes/alex)\n"
os.WriteFile("changes.md", []byte(intro+d+other+line), 0644)
line := "* [a change](alex)\n"
assert.NoError(t, os.MkdirAll("testdata/changes", 0755))
assert.NoError(t, os.WriteFile("testdata/changes/changes.md", []byte(intro+d+other+line), 0644))
p := &Page{Name: "testdata/changes/alex", Body: []byte(`Hallo!`)}
p.notify()
s, err := os.ReadFile("changes.md")
s, err := os.ReadFile("testdata/changes/changes.md")
assert.NoError(t, err)
new_line := "* [testdata/changes/alex](testdata/changes/alex)\n"
new_line := "* [testdata/changes/alex](alex)\n"
// the change was already listed, but now it moved up and has a new title
assert.Equal(t, intro+d+new_line+other, string(s))
}
func TestChangesWithNoChangeToTheOrder(t *testing.T) {
cleanup(t, "testdata/changes", "changes.md", "changes.md~")
cleanup(t, "testdata/changes")
intro := "# Changes\n\nThis is a paragraph.\n\n"
other := "* [other change](testdata/changes/whatever)\n"
line := "* [a change](testdata/changes/alex)\n"
d := "## " + time.Now().Format(time.DateOnly) + "\n"
os.WriteFile("changes.md", []byte(intro+d+line+other), 0644)
line := "* [a change](alex)\n"
other := "* [other change](whatever)\n"
assert.NoError(t, os.MkdirAll("testdata/changes", 0755))
assert.NoError(t, os.WriteFile("testdata/changes/changes.md", []byte(intro+d+line+other), 0644))
p := &Page{Name: "testdata/changes/alex", Body: []byte(`Hallo!`)}
p.notify()
s, err := os.ReadFile("changes.md")
s, err := os.ReadFile("testdata/changes/changes.md")
assert.NoError(t, err)
new_line := "* [testdata/changes/alex](testdata/changes/alex)\n"
new_line := "* [testdata/changes/alex](alex)\n"
// the change was already listed at the top, so just use the new title
assert.Equal(t, intro+d+new_line+other, string(s))
// since the file has changed, a backup was necessary
assert.FileExists(t, "testdata/changes/changes.md~")
}
func TestChangesWithNoChanges(t *testing.T) {
cleanup(t, "testdata/changes", "changes.md", "changes.md~")
cleanup(t, "testdata/changes")
intro := "# Changes\n\nThis is a paragraph.\n\n"
other := "* [other change](testdata/changes/whatever)\n"
line := "* [a change](testdata/changes/alex)\n"
d := "## " + time.Now().Format(time.DateOnly) + "\n"
os.Remove("changes.md~")
os.WriteFile("changes.md", []byte(intro+d+line+other), 0644)
line := "* [a change](alex)\n"
other := "* [other change](whatever)\n"
assert.NoError(t, os.MkdirAll("testdata/changes", 0755))
assert.NoError(t, os.WriteFile("testdata/changes/changes.md", []byte(intro+d+line+other), 0644))
p := &Page{Name: "testdata/changes/alex", Body: []byte("# a change\nHallo!")}
p.notify()
s, err := os.ReadFile("changes.md")
s, err := os.ReadFile("testdata/changes/changes.md")
assert.NoError(t, err)
// the change was already listed at the top, so no change was necessary
assert.Equal(t, intro+d+line+other, string(s))
// since the file hasn't changed, no backup was necessary
assert.NoFileExists(t, "changes.md~")
assert.NoFileExists(t, "testdata/changes/changes.md~")
}

View File

@@ -6,7 +6,7 @@
<meta name="viewport" content="width=device-width">
<title>{{.Title}}</title>
<style>
html { max-width: 65ch; padding: 1ch; margin: auto; color: #111; background-color: #ffe; }
html { max-width: 70ch; padding: 1ch; margin: auto; color: #111; background-color: #ffe; }
body { hyphens: auto; }
del { background-color: #fab }
ins { background-color: #af8 }

View File

@@ -2,7 +2,9 @@ package main
import (
"github.com/stretchr/testify/assert"
"os"
"testing"
"time"
)
func TestDiff(t *testing.T) {
@@ -50,3 +52,45 @@ Mispronouncing words`
assert.Contains(t, body, `<del>s</del>`)
assert.Contains(t, body, `<ins>ce</ins>`)
}
func TestDiffBackup(t *testing.T) {
cleanup(t, "testdata/backup")
s := `# Cold Rooms
I shiver at home
the monitor glares and moans
fear or cold, who knows?`
r := `# Cold Rooms
I shiver at home
the monitor glares and moans
I hate the machine!`
u := `# Cold Rooms
I shiver at home
the monitor glares and moans
my grey heart grows cold`
p := &Page{Name: "testdata/backup/cold", Body: []byte(s)}
p.save()
p = &Page{Name: "testdata/backup/cold", Body: []byte(r)}
p.save()
body := string(p.Diff())
// diff from s to r:
assert.Contains(t, body, `<del>fear or cold, who knows?</del>`)
assert.Contains(t, body, `<ins>I hate the machine!</ins>`)
p = &Page{Name: "testdata/backup/cold", Body: []byte(u)}
p.save()
body = string(p.Diff())
// diff from s to u since r was not 60 min or older
assert.Contains(t, body, `<del>fear or cold, who knows?</del>`)
assert.Contains(t, body, `<ins>my grey heart grows cold</ins>`)
// set timestamp 2h in the past
ts := time.Now().Add(-2 * time.Hour)
assert.NoError(t, os.Chtimes("testdata/backup/cold.md~", ts, ts))
p = &Page{Name: "testdata/backup/cold", Body: []byte(r)}
p.save()
body = string(p.Diff())
// diff from u to r:
assert.Contains(t, body, `<del>my grey heart grows cold</del>`)
assert.Contains(t, body, `<ins>I hate the machine!</ins>`)
}

View File

@@ -37,26 +37,25 @@ func TestEditSave(t *testing.T) {
}
func TestEditSaveChanges(t *testing.T) {
cleanup(t, "testdata/notification", "changes.md")
restore(t, "index.md")
os.Remove("changes.md")
cleanup(t, "testdata/notification")
data := url.Values{}
data.Set("body", "Hallo!")
data.Add("notify", "on")
today := time.Now().Format("2006-01-02")
// Posting to the save URL saves a page
HTTPRedirectTo(t, makeHandler(saveHandler, true),
"POST", "/save/testdata/notification/2023-10-28-alex",
data, "/view/testdata/notification/2023-10-28-alex")
"POST", "/save/testdata/notification/" + today,
data, "/view/testdata/notification/" + today)
// The changes.md file was created
s, err := os.ReadFile("changes.md")
s, err := os.ReadFile("testdata/notification/changes.md")
assert.NoError(t, err)
d := time.Now().Format(time.DateOnly)
assert.Equal(t, "# Changes\n\n## " + d +
"\n* [testdata/notification/2023-10-28-alex](testdata/notification/2023-10-28-alex)\n",
assert.Equal(t, "# Changes\n\n## "+d+
"\n* [testdata/notification/"+today+"]("+today+")\n",
string(s))
// Link added to index.md file
s, err = os.ReadFile("index.md")
s, err = os.ReadFile("testdata/notification/index.md")
assert.NoError(t, err)
assert.Contains(t, string(s),
"\n* [testdata/notification/2023-10-28-alex](testdata/notification/2023-10-28-alex)\n")
// New index contains just the link
assert.Equal(t, string(s), "* [testdata/notification/"+today+"]("+today+")\n")
}

28
go.mod
View File

@@ -4,35 +4,35 @@ go 1.21.0
require (
github.com/anthonynsimon/bild v0.13.0
github.com/gomarkdown/markdown v0.0.0-20230912175223-14b07df9d538
github.com/bashdrew/goheif v0.0.0-20230406184952-7a08ca9c9bdd
github.com/charmbracelet/lipgloss v0.9.1
github.com/gomarkdown/markdown v0.0.0-20231115200524-a660076da3fd
github.com/google/subcommands v1.2.0
github.com/hexops/gotextdiff v1.0.3
github.com/microcosm-cc/bluemonday v1.0.25
github.com/microcosm-cc/bluemonday v1.0.26
github.com/pemistahl/lingua-go v1.4.0
github.com/sergi/go-diff v1.3.1
github.com/stretchr/testify v1.8.4
golang.org/x/exp v0.0.0-20231206192017-f3f8817b8deb
)
require (
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/aymerick/douceur v0.2.0 // indirect
github.com/charmbracelet/lipgloss v0.8.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gorilla/css v1.0.0 // indirect
github.com/kr/pretty v0.1.0 // indirect
github.com/gorilla/css v1.0.1 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.18 // indirect
github.com/mattn/go-runewidth v0.0.14 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/muesli/reflow v0.3.0 // indirect
github.com/muesli/termenv v0.15.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/sergi/go-diff v1.3.1 // indirect
github.com/rivo/uniseg v0.4.4 // indirect
github.com/rwcarlsen/goexif v0.0.0-20190401172101-9e8deecbddbd // indirect
github.com/shopspring/decimal v1.3.1 // indirect
golang.org/x/exp v0.0.0-20221106115401-f9659909a136 // indirect
golang.org/x/image v0.0.0-20190703141733-d6a02ce849c9 // indirect
golang.org/x/net v0.12.0 // indirect
golang.org/x/sys v0.10.0 // indirect
golang.org/x/image v0.14.0 // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/sys v0.15.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

46
go.sum
View File

@@ -6,8 +6,10 @@ github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiE
github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=
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/charmbracelet/lipgloss v0.8.0 h1:IS00fk4XAHcf8uZKc3eHeMUTCxUH6NkaTrdyCQk84RU=
github.com/charmbracelet/lipgloss v0.8.0/go.mod h1:p4eYUZZJ/0oXTuCQKFF8mqyKCz0ja6y+7DniDDw5KKU=
github.com/bashdrew/goheif v0.0.0-20230406184952-7a08ca9c9bdd h1:SxkQeH4jjXT0zMgiRgkiIQjIvWfe9vXuTAmE3cfcQrU=
github.com/bashdrew/goheif v0.0.0-20230406184952-7a08ca9c9bdd/go.mod h1:p1sbxRy+MY71fEWHcfRmerC8WUYXDFCExF9A7aXwp98=
github.com/charmbracelet/lipgloss v0.9.1 h1:PNyd3jvaJbg4jRHKWXnCj1akQm4rh8dbEzN1p/u1KWg=
github.com/charmbracelet/lipgloss v0.9.1/go.mod h1:1mPmG4cxScwUQALAAnacHaigiiHB9Pmr+v1VEawJl6I=
github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE=
github.com/coreos/go-etcd v2.0.0+incompatible/go.mod h1:Jez6KQU2B/sWsbdaef3ED8NzMklzPG4d5KIOhIy30Tk=
github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
@@ -17,15 +19,15 @@ 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/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/gomarkdown/markdown v0.0.0-20230912175223-14b07df9d538 h1:ePDpFu7l0QUV46/9A7icfL2wvIOzTJLCWh4RO2NECzE=
github.com/gomarkdown/markdown v0.0.0-20230912175223-14b07df9d538/go.mod h1:JDGcbDT52eL4fju3sZ4TeHGsQwhG9nbDV21aMyhwPoA=
github.com/gomarkdown/markdown v0.0.0-20231115200524-a660076da3fd h1:PppHBegd3uPZ3Y/Iax/2mlCFJm1w4Qf/zP1MdW4ju2o=
github.com/gomarkdown/markdown v0.0.0-20231115200524-a660076da3fd/go.mod h1:JDGcbDT52eL4fju3sZ4TeHGsQwhG9nbDV21aMyhwPoA=
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/google/subcommands v1.2.0 h1:vWQspBTo2nEqTUFita5/KeEWlUL8kQObDFbub/EN9oE=
github.com/google/subcommands v1.2.0/go.mod h1:ZjhPrFU+Olkh9WazFPsl27BQ4UPiG37m3yTrtFlrHVk=
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/gorilla/css v1.0.1 h1:ntNaBIghp6JmvWnxbZKANoLyuXTPZ4cAMlo6RyhlbO8=
github.com/gorilla/css v1.0.1/go.mod h1:BvnYkspnSzMmwRK+b8/xgNPLiIuNZr6vbZBTPQ2A3b0=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
@@ -38,13 +40,13 @@ github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
github.com/mattn/go-isatty v0.0.18 h1:DOKFKCQ7FNG2L1rbrmstDN4QVRdS89Nkh85u68Uwp98=
github.com/mattn/go-isatty v0.0.18/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU=
github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
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/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/microcosm-cc/bluemonday v1.0.26 h1:xbqSvqzQMeEHCqMi64VAs4d8uy6Mequs3rQ0k/Khz58=
github.com/microcosm-cc/bluemonday v1.0.26/go.mod h1:JyzOCs9gkyQyjs+6h10UEVSe02CGwkhd72Xdqh78TWs=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s=
@@ -57,9 +59,12 @@ github.com/pemistahl/lingua-go v1.4.0/go.mod h1:ECuM1Hp/3hvyh7k8aWSqNCPlTxLemFZs
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/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis=
github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
github.com/rwcarlsen/goexif v0.0.0-20190401172101-9e8deecbddbd h1:CmH9+J6ZSsIjUK3dcGsnCnO41eRBOnY12zwkn5qVwgc=
github.com/rwcarlsen/goexif v0.0.0-20190401172101-9e8deecbddbd/go.mod h1:hPqNNc0+uJM6H+SuU8sEs5K5IQeKccPqeSjfgcKGgPk=
github.com/sergi/go-diff v1.3.1 h1:xkr+Oxo4BOQKmkn/B9eMK0g5Kg/983T9DqqPHwYqD+8=
github.com/sergi/go-diff v1.3.1/go.mod h1:aMJSSKb2lpPvRNec0+w3fl7LP9IOFzdc9Pa4NFbPK1I=
github.com/shopspring/decimal v1.3.1 h1:2Usl1nmF/WZucqkFZhnfFYxxxu8LG21F6nPQBE5gKV8=
@@ -78,16 +83,17 @@ github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXl
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
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/image v0.0.0-20190703141733-d6a02ce849c9 h1:uc17S921SPw5F2gJo7slQ3aqvr2RwpL7eb3+DZncu3s=
golang.org/x/exp v0.0.0-20231206192017-f3f8817b8deb h1:c0vyKkb6yr3KR7jEfJaOSv4lG7xPkbN6r52aJz1d8a8=
golang.org/x/exp v0.0.0-20231206192017-f3f8817b8deb/go.mod h1:iRJReGqOEeBhDZGkGbynYwcHlctCvnjTYIamk7uXpHI=
golang.org/x/image v0.0.0-20190703141733-d6a02ce849c9/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
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/image v0.14.0 h1:tNgSxAFe3jC4uYqvZdTr84SZoM1KfwdC9SKIFrLjFn4=
golang.org/x/image v0.14.0/go.mod h1:HUYqC05R2ZcZ3ejNQsIHQDQiwWM4JBqmm6MKANTp4LE=
golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c=
golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U=
golang.org/x/sys v0.0.0-20181205085412-a5c9d58dba9a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA=
golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
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=

View File

@@ -22,7 +22,7 @@ func (*htmlCmd) Usage() string {
}
func (cmd *htmlCmd) SetFlags(f *flag.FlagSet) {
f.BoolVar(&cmd.useTemplate, "view", false, "Use the 'view.html' template.")
f.BoolVar(&cmd.useTemplate, "view", false, "use the 'view.html' template.")
}
func (cmd *htmlCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
@@ -41,6 +41,7 @@ func htmlCli(w io.Writer, useTemplate bool, args []string) subcommands.ExitStatu
p.handleTitle(true)
p.renderHtml()
t := "view.html"
templates := loadTemplates()
err := templates.ExecuteTemplate(w, t, p)
if err != nil {
fmt.Fprintf(os.Stderr, "Cannot execute %s template for %s: %s\n", t, arg, err)

View File

@@ -15,9 +15,9 @@ func TestHtmlCmd(t *testing.T) {
<p>Hello! 🙃</p>
<p>Check out the <a href="README" rel="nofollow">README</a>.</p>
<p>Check out the <a href="README">README</a>.</p>
<p>Or <a href="test" rel="nofollow">create a new page</a>.</p>
<p>Or <a href="test">create a new page</a>.</p>
`
assert.Equal(t, r, b.String())

71
list_cmd.go Normal file
View File

@@ -0,0 +1,71 @@
package main
import (
"context"
"flag"
"fmt"
"github.com/google/subcommands"
"io"
"os"
"path/filepath"
"strings"
)
type listCmd struct {
dir string
}
func (cmd *listCmd) SetFlags(f *flag.FlagSet) {
f.StringVar(&cmd.dir, "dir", "", "list only pages within this sub-directory")
}
func (*listCmd) Name() string { return "list" }
func (*listCmd) Synopsis() string { return "List pages with name and title." }
func (*listCmd) Usage() string {
return `list [-dir string]:
List all pages with name and title, separated by a tabulator.
`
}
func (cmd *listCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
return listCli(os.Stdout, cmd.dir, f.Args())
}
// listCli runs the list command on the command line. It is used
// here with an io.Writer for easy testing.
func listCli(w io.Writer, dir string, args []string) subcommands.ExitStatus {
dir, err := checkDir(dir)
if err != nil {
return subcommands.ExitFailure
}
index.load()
index.RLock()
defer index.RUnlock()
for name, title := range index.titles {
if strings.HasPrefix(name, dir) {
name = strings.Replace(name, dir, "", 1)
fmt.Fprintf(w, "%s\t%s\n", name, title)
}
}
return subcommands.ExitSuccess
}
// checkDir returns an error if the directory doesn't exist. If if exists, it returns a copy ending in a slash.
func checkDir (dir string) (string, error) {
if dir != "" {
fi, err := os.Stat(dir)
if err != nil {
fmt.Println(err)
return "", err
}
if !fi.IsDir() {
fmt.Println("This is not a sub-directory:", dir)
return "", err
}
dir = filepath.ToSlash(dir);
if (!strings.HasSuffix(dir, "/")) {
dir += "/"
}
}
return dir, nil
}

31
list_cmd_test.go Normal file
View File

@@ -0,0 +1,31 @@
package main
import (
"bytes"
"github.com/google/subcommands"
"github.com/stretchr/testify/assert"
"testing"
)
func TestListCmd(t *testing.T) {
b := new(bytes.Buffer)
s := listCli(b, "", nil)
assert.Equal(t, subcommands.ExitSuccess, s)
x := b.String()
assert.Contains(t, x, "README\tOddµ: A minimal wiki\n")
assert.Contains(t, x, "index\tWelcome to Oddµ\n")
}
func TestListSubdirCmd(t *testing.T) {
cleanup(t, "testdata/list")
p := &Page{Name: "testdata/list/red", Body: []byte(`# Red
Shifting darkness waits
I open my eyes in fear
And see the red dot`)}
p.save()
b := new(bytes.Buffer)
s := listCli(b, "testdata/list", nil)
assert.Equal(t, subcommands.ExitSuccess, s)
x := b.String()
assert.Contains(t, x, "red\tRed\n")
}

View File

@@ -1,6 +1,6 @@
docs: oddmu-apache.5 oddmu-html.1 oddmu-missing.1 oddmu-notify.1 \
oddmu-replace.1 oddmu-search.1 oddmu-search.7 oddmu-static.1 \
oddmu-templates.5 oddmu.1 oddmu.5 oddmu.service.5
oddmu-list.1 oddmu-templates.5 oddmu.1 oddmu.5 oddmu.service.5
oddmu%: oddmu%.txt
scdoc < $< > $@

View File

@@ -5,7 +5,7 @@
.nh
.ad l
.\" Begin generated content:
.TH "ODDMU-APACHE" "5" "2023-11-05"
.TH "ODDMU-APACHE" "5" "2024-01-27"
.PP
.SH NAME
.PP
@@ -15,6 +15,8 @@ oddmu-apache - how to setup Apache as a reverse proxy for Oddmu
.PP
The oddmu program serves the current working directory as a wiki on port 8080.\&
This is an unpriviledged port so an ordinary user account can do this.\&
Alternatively, you can reverse proxy HTTP over a Unix-domain socket,
as shown later.\&
.PP
The best way to protect the wiki against vandalism and spam is to use a regular
web server as reverse proxy.\& This page explains how to setup Apache on Debian to
@@ -38,13 +40,14 @@ MDCertificateAgreement accepted
<VirtualHost *:80>
ServerName transjovian\&.org
RewriteEngine on
RewriteRule ^/(\&.*) https://%{HTTP_HOST}/$1 [redirect]
RewriteRule "^/(\&.*)" "https://%{HTTP_HOST}/$1" [redirect]
</VirtualHost>
<VirtualHost *:443>
ServerAdmin alex@alexschroeder\&.ch
ServerName transjovian\&.org
SSLEngine on
ProxyPassMatch "^/((view|diff|edit|save|add|append|upload|drop|search)/(\&.*))?$" "http://localhost:8080/$1"
ProxyPassMatch "^/((view|diff|edit|save|add|append|upload|drop|search)/(\&.*))?$"
"http://localhost:8080/$1"
</VirtualHost>
.fi
.RE
@@ -81,6 +84,99 @@ second instead just proxy to the wiki like you did for the second virtual
host: use a copy of the "ProxyPassMatch" directive instead of "RewriteEngine on"
and "RewriteRule".\&
.PP
.SS Allow HTTP for viewing
.PP
When looking at pages, you might want to allow HTTP since no password is
required.\&
.PP
.nf
.RS 4
MDomain transjovian\&.org
MDCertificateAgreement accepted
<VirtualHost *:80>
ServerName transjovian\&.org
RewriteEngine on
ProxyPassMatch "^/((view|diff|search)/(\&.*))?$"
"http://localhost:8080/$1"
RewriteRule "^/((edit|save|add|append|upload|drop)/(\&.*))?$"
"https://%{HTTP_HOST}/$1" [redirect]
</VirtualHost>
<VirtualHost *:443>
ServerAdmin alex@alexschroeder\&.ch
ServerName transjovian\&.org
SSLEngine on
ProxyPassMatch "^/((view|diff|edit|save|add|append|upload|drop|search)/(\&.*))?$"
"http://localhost:8080/$1"
</VirtualHost>
.fi
.RE
.PP
.SS Using a Unix-domain Socket
.PP
Instead of having Oddmu listen on a TCP port, you can have it listen on a
Unix-domain socket.\& This requires socket activation.\& An example of configuring
the service is given in \fIoddmu.\&service(5)\fR.\&
.PP
To test just the unix domain socket, use \fIncat(1)\fR:
.PP
.nf
.RS 4
echo -e "GET /view/index HTTP/1\&.1rnHost: localhostrnrn"
| ncat --unixsock /run/oddmu/oddmu\&.sock
.fi
.RE
.PP
On the Apache side, you can proxy to the socket directly.\& This sends all
requests to the socket:
.PP
.nf
.RS 4
ProxyPass "/" "unix:/run/oddmu/oddmu\&.sock|http://localhost/"
.fi
.RE
.PP
Now, all traffic between the web server and the wiki goes over the socket at
"/run/oddmu/oddmu.\&sock".\&
.PP
To test it on the command-line, use a tool like \fIcurl(1)\fR.\& Make sure to provide
the correct servername!\&
.PP
.nf
.RS 4
curl http://transjovian\&.org/view/index
.fi
.RE
.PP
You probably want to serve some static files as well (see \fBServe static files\fR).\&
In that case, you need to use the ProxyPassMatch directive.\&
.PP
.nf
.RS 4
ProxyPassMatch "^/((view|diff|edit|save|add|append|upload|drop|search)/(\&.*))?$"
"unix:/run/oddmu/oddmu\&.sock|http://localhost/$1"
.fi
.RE
.PP
There'\&s a curious problem with this expression, however.\& If you use \fIcurl(1)\fR to
get the root path, Apache hangs:
.PP
.nf
.RS 4
curl http://transjovian\&.org/
.fi
.RE
.PP
A workaround is to add the redirect manually and drop the question-mark:
.PP
.nf
.RS 4
RedirectMatch "^/$" "/view/index"
ProxyPassMatch "^/((view|diff|edit|save|add|append|upload|drop|search)/(\&.*))$"
"unix:/run/oddmu/oddmu\&.sock|http://localhost/$1"
.fi
.RE
.PP
.SS Access
.PP
Access control is not part of Oddmu.\& By default, the wiki is editable by all.\&

View File

@@ -8,6 +8,8 @@ oddmu-apache - how to setup Apache as a reverse proxy for Oddmu
The oddmu program serves the current working directory as a wiki on port 8080.
This is an unpriviledged port so an ordinary user account can do this.
Alternatively, you can reverse proxy HTTP over a Unix-domain socket,
as shown later.
The best way to protect the wiki against vandalism and spam is to use a regular
web server as reverse proxy. This page explains how to setup Apache on Debian to
@@ -30,13 +32,14 @@ MDCertificateAgreement accepted
<VirtualHost *:80>
ServerName transjovian.org
RewriteEngine on
RewriteRule ^/(.*) https://%{HTTP_HOST}/$1 [redirect]
RewriteRule "^/(.*)" "https://%{HTTP_HOST}/$1" [redirect]
</VirtualHost>
<VirtualHost *:443>
ServerAdmin alex@alexschroeder.ch
ServerName transjovian.org
SSLEngine on
ProxyPassMatch "^/((view|diff|edit|save|add|append|upload|drop|search)/(.*))?$" "http://localhost:8080/$1"
ProxyPassMatch "^/((view|diff|edit|save|add|append|upload|drop|search)/(.*))?$" \
"http://localhost:8080/$1"
</VirtualHost>
```
@@ -64,6 +67,85 @@ second instead just proxy to the wiki like you did for the second virtual
host: use a copy of the "ProxyPassMatch" directive instead of "RewriteEngine on"
and "RewriteRule".
## Allow HTTP for viewing
When looking at pages, you might want to allow HTTP since no password is
required.
```
MDomain transjovian.org
MDCertificateAgreement accepted
<VirtualHost *:80>
ServerName transjovian.org
RewriteEngine on
ProxyPassMatch "^/((view|diff|search)/(.*))?$" \
"http://localhost:8080/$1"
RewriteRule "^/((edit|save|add|append|upload|drop)/(.*))?$" \
"https://%{HTTP_HOST}/$1" [redirect]
</VirtualHost>
<VirtualHost *:443>
ServerAdmin alex@alexschroeder.ch
ServerName transjovian.org
SSLEngine on
ProxyPassMatch "^/((view|diff|edit|save|add|append|upload|drop|search)/(.*))?$" \
"http://localhost:8080/$1"
</VirtualHost>
```
## Using a Unix-domain Socket
Instead of having Oddmu listen on a TCP port, you can have it listen on a
Unix-domain socket. This requires socket activation. An example of configuring
the service is given in _oddmu.service(5)_.
To test just the unix domain socket, use _ncat(1)_:
```
echo -e "GET /view/index HTTP/1.1\r\nHost: localhost\r\n\r\n" \
| ncat --unixsock /run/oddmu/oddmu.sock
```
On the Apache side, you can proxy to the socket directly. This sends all
requests to the socket:
```
ProxyPass "/" "unix:/run/oddmu/oddmu.sock|http://localhost/"
```
Now, all traffic between the web server and the wiki goes over the socket at
"/run/oddmu/oddmu.sock".
To test it on the command-line, use a tool like _curl(1)_. Make sure to provide
the correct servername!
```
curl http://transjovian.org/view/index
```
You probably want to serve some static files as well (see *Serve static files*).
In that case, you need to use the ProxyPassMatch directive.
```
ProxyPassMatch "^/((view|diff|edit|save|add|append|upload|drop|search)/(.*))?$" \
"unix:/run/oddmu/oddmu.sock|http://localhost/$1"
```
There's a curious problem with this expression, however. If you use _curl(1)_ to
get the root path, Apache hangs:
```
curl http://transjovian.org/
```
A workaround is to add the redirect manually and drop the question-mark:
```
RedirectMatch "^/$" "/view/index"
ProxyPassMatch "^/((view|diff|edit|save|add|append|upload|drop|search)/(.*))$" \
"unix:/run/oddmu/oddmu.sock|http://localhost/$1"
```
## Access
Access control is not part of Oddmu. By default, the wiki is editable by all.

56
man/oddmu-list.1 Normal file
View File

@@ -0,0 +1,56 @@
.\" Generated by scdoc 1.11.2
.\" Complete documentation for this program is not available as a GNU info page
.ie \n(.g .ds Aq \(aq
.el .ds Aq '
.nh
.ad l
.\" Begin generated content:
.TH "ODDMU-LIST" "1" "2023-12-20"
.PP
.SH NAME
.PP
oddmu-list - list page names and titles from the command-line
.PP
.SH SYNOPSIS
.PP
\fBoddmu list\fR [-dir string]
.PP
.SH DESCRIPTION
.PP
The "list" subcommand lists page names and their titles, separated by a TAB
character.\& This saves you from opening and parsing all the files yourself if you
need the page titles.\&
.PP
If a directory is provided, only files from the tree starting at that
subdirectory are listed, and the directory is stripped from the page name.\&
.PP
.SH OPTIONS
.PP
\fB-dir\fR \fIstring\fR
.RS 4
Limit the list to a particular directory.\&
.PP
.RE
.SH EXAMPLE
.PP
Create list of links to pages in the "dad" directory, filter it for date pages
(starting with "2"), format it as a list of links and sort in reverse order.\&
This is a list of links you could append to "dad/index.\&md" if it doesn'\&t already
have a list of links.\&
.PP
.nf
.RS 4
oddmu list -dir dad
| grep \&'^2\&'
| awk -F "t" -e \&'{ print "* [" $2 "](" $1 ")" }\&'
| sort -r
.fi
.RE
.PP
.SH SEE ALSO
.PP
\fIoddmu\fR(1), \fIoddmu-search\fR(1)
.PP
.SH AUTHORS
.PP
Maintained by Alex Schroeder <alex@gnu.\&org>.\&

45
man/oddmu-list.1.txt Normal file
View File

@@ -0,0 +1,45 @@
ODDMU-LIST(1)
# NAME
oddmu-list - list page names and titles from the command-line
# SYNOPSIS
*oddmu list* [-dir string]
# DESCRIPTION
The "list" subcommand lists page names and their titles, separated by a TAB
character. This saves you from opening and parsing all the files yourself if you
need the page titles.
If a directory is provided, only files from the tree starting at that
subdirectory are listed, and the directory is stripped from the page name.
# OPTIONS
*-dir* _string_
Limit the list to a particular directory.
# EXAMPLE
Create list of links to pages in the "dad" directory, filter it for date pages
(starting with "2"), format it as a list of links and sort in reverse order.
This is a list of links you could append to "dad/index.md" if it doesn't already
have a list of links.
```
oddmu list -dir dad \
| grep '^2' \
| awk -F "\t" -e '{ print "* [" $2 "](" $1 ")" }' \
| sort -r
```
# SEE ALSO
_oddmu_(1), _oddmu-search_(1)
# AUTHORS
Maintained by Alex Schroeder <alex@gnu.org>.

View File

@@ -5,7 +5,7 @@
.nh
.ad l
.\" Begin generated content:
.TH "ODDMU-NOTIFY" "1" "2023-11-06"
.TH "ODDMU-NOTIFY" "1" "2024-01-17"
.PP
.SH NAME
.PP
@@ -20,20 +20,22 @@ oddmu-notify - add links to changes.\&md, index.\&md, and hashtag pages
The "notify" subcommand takes all the page names provided (without the ".\&md"
extension) and adds links to it from other pages.\&
.PP
A new link is added to the \fBchanges\fR page if it doesn'\&t exist.\& The current date
of the machine Oddmu is running on is used as the heading.\& If the requested link
already exists on the changes page, it is moved up to the current date.\& If that
leaves an old date without any links, that date heading is removed.\&
A new link is added to the \fBchanges\fR page in the current directory if it doesn'\&t
exist.\& The current date of the machine Oddmu is running on is used as the
heading.\& If the requested link already exists on the changes page, it is moved
up to the current date.\& If that leaves an old date without any links, that date
heading is removed.\&
.PP
A page whose name starts with an ISO date (YYYY-MM-DD, e.\&g.\& "2023-10-28") is
called a \fBblog\fR page.\&
.PP
A link is created from the \fBindex\fR page to blog pages if and only if the blog
pages are from the current year.\& The idea is that the front page contains a lot
of links to blog posts but eventually the blog post links are moved onto archive
pages (one per year, for example), or simply deleted.\& As when editing older
pages, links to those pages should not get added to the index as if those older
pages were new again.\& A link on the changes page is enough.\&
A link is created from the \fBindex\fR page in the current directory to blog pages
if and only if the blog pages are from the current year.\& The idea is that the
front page contains a lot of links to blog posts but eventually the blog post
links are moved onto archive pages (one per year, for example), or simply
deleted.\& As when editing older pages, links to those pages should not get added
to the index as if those older pages were new again.\& A link on the changes page
is enough.\&
.PP
For every \fBhashtag\fR used on the pages named, another link might be created.\& If a
page named like the hashtag exists, a backlink is added to it.\& A hashtag
@@ -59,6 +61,44 @@ oddmu notify 2023-11-05-climate
.fi
.RE
.PP
The changes file might look as follows:
.PP
.nf
.RS 4
# Changes
This page lists all the changes made to the wiki\&.
## 2023-11-05
* [Global warming](2023-11-05-climate)
.fi
.RE
.PP
The index file might look as follows:
.PP
.nf
.RS 4
# Blog
This page links to all the blog posts\&.
* [Global warming](2023-11-05-climate)
.fi
.RE
.PP
The hashtag file might look as follows:
.PP
.nf
.RS 4
# Climate
This page links to all the blog posts tagged #Climate\&.
* [Global warming](2023-11-05-climate)
.fi
.RE
.PP
.SH SEE ALSO
.PP
\fIoddmu\fR(1)

View File

@@ -13,20 +13,22 @@ oddmu-notify - add links to changes.md, index.md, and hashtag pages
The "notify" subcommand takes all the page names provided (without the ".md"
extension) and adds links to it from other pages.
A new link is added to the *changes* page if it doesn't exist. The current date
of the machine Oddmu is running on is used as the heading. If the requested link
already exists on the changes page, it is moved up to the current date. If that
leaves an old date without any links, that date heading is removed.
A new link is added to the *changes* page in the current directory if it doesn't
exist. The current date of the machine Oddmu is running on is used as the
heading. If the requested link already exists on the changes page, it is moved
up to the current date. If that leaves an old date without any links, that date
heading is removed.
A page whose name starts with an ISO date (YYYY-MM-DD, e.g. "2023-10-28") is
called a *blog* page.
A link is created from the *index* page to blog pages if and only if the blog
pages are from the current year. The idea is that the front page contains a lot
of links to blog posts but eventually the blog post links are moved onto archive
pages (one per year, for example), or simply deleted. As when editing older
pages, links to those pages should not get added to the index as if those older
pages were new again. A link on the changes page is enough.
A link is created from the *index* page in the current directory to blog pages
if and only if the blog pages are from the current year. The idea is that the
front page contains a lot of links to blog posts but eventually the blog post
links are moved onto archive pages (one per year, for example), or simply
deleted. As when editing older pages, links to those pages should not get added
to the index as if those older pages were new again. A link on the changes page
is enough.
For every *hashtag* used on the pages named, another link might be created. If a
page named like the hashtag exists, a backlink is added to it. A hashtag
@@ -50,6 +52,38 @@ it exists):
oddmu notify 2023-11-05-climate
```
The changes file might look as follows:
```
# Changes
This page lists all the changes made to the wiki.
## 2023-11-05
* [Global warming](2023-11-05-climate)
```
The index file might look as follows:
```
# Blog
This page links to all the blog posts.
* [Global warming](2023-11-05-climate)
```
The hashtag file might look as follows:
```
# Climate
This page links to all the blog posts tagged #Climate.
* [Global warming](2023-11-05-climate)
```
# SEE ALSO
_oddmu_(1)

View File

@@ -5,7 +5,7 @@
.nh
.ad l
.\" Begin generated content:
.TH "ODDMU-REPLACE" "1" "2023-10-09"
.TH "ODDMU-REPLACE" "1" "2023-11-24"
.PP
.SH NAME
.PP
@@ -70,6 +70,32 @@ instead of making any changes and the regexp rules differ slightly.\&
The search is case-sensitive.\& To make it case-insensitive, search for a regular
expression that sets the case-insensitive flag, e.\&g.\& "(?\&i)oddmu".\&
.PP
.SH SECURITY
.PP
Consider creating a backup before doing replacements!\&
.PP
The following Bash script creates a copy of the current directory using hard
links.\& If you'\&re in a directory called "wiki", it creates a sibling directory
called "wiki-2023-11-24" (using the current date) full of links.\& This takes
little space and time.\& It works as a backup as long as you don'\&t use an
application that edits files in place.\& Most programs overwrite old files by
creating new files with the same name, so you should be safe.\&
.PP
.nf
.RS 4
#!/usr/bin/bash
d=$(basename $(pwd))
t=$(date --iso-8601)
echo Creating a snapshot of $d in \&.\&./$d-$t
rsync --link-dest "\&.\&./$d" --archive \&. "\&.\&./$d-$t/"
.fi
.RE
.PP
The above wouldn'\&t work for database files, for example.\& There, the database
changes the file in place thus the file is changed in the backup directory as
well.\& For Oddmu and the usual text editors, it works.\& If you use Emacs, don'\&t
set \fIbackup-by-copying\fR, \fIbackup-by-copying-when-linked\fR and related variables.\&
.PP
.SH SEE ALSO
.PP
\fIoddmu\fR(1), \fIoddmu-search\fR(7)

View File

@@ -55,6 +55,30 @@ instead of making any changes and the regexp rules differ slightly.
The search is case-sensitive. To make it case-insensitive, search for a regular
expression that sets the case-insensitive flag, e.g. "(?i)oddmu".
# SECURITY
Consider creating a backup before doing replacements!
The following Bash script creates a copy of the current directory using hard
links. If you're in a directory called "wiki", it creates a sibling directory
called "wiki-2023-11-24" (using the current date) full of links. This takes
little space and time. It works as a backup as long as you don't use an
application that edits files in place. Most programs overwrite old files by
creating new files with the same name, so you should be safe.
```
#!/usr/bin/bash
d=$(basename $(pwd))
t=$(date --iso-8601)
echo Creating a snapshot of $d in ../$d-$t
rsync --link-dest "../$d" --archive . "../$d-$t/"
```
The above wouldn't work for database files, for example. There, the database
changes the file in place thus the file is changed in the backup directory as
well. For Oddmu and the usual text editors, it works. If you use Emacs, don't
set _backup-by-copying_, _backup-by-copying-when-linked_ and related variables.
# SEE ALSO
_oddmu_(1), _oddmu-search_(7)

View File

@@ -5,7 +5,7 @@
.nh
.ad l
.\" Begin generated content:
.TH "ODDMU-SEARCH" "1" "2023-10-10"
.TH "ODDMU-SEARCH" "1" "2023-12-20"
.PP
.SH NAME
.PP
@@ -23,11 +23,18 @@ directory.\&
Be default, this returns a Markdown-formatted list suitable for pasting into
Oddmu pages.\&
.PP
If a directory is provided, only files from the tree starting at that
subdirectory are listed, and the directory is stripped from the page name.\&
.PP
See \fIoddmu-search\fR(7) for more information of how pages are searched, sorted and
scored.\&
.PP
.SH OPTIONS
.PP
\fB-dir\fR \fIstring\fR
.RS 4
Limit search to a particular directory.\&
.RE
\fB-extract\fR
.RS 4
Print search extracts for interactive use from the command-line.\&
@@ -36,6 +43,10 @@ Print search extracts for interactive use from the command-line.\&
.RS 4
Search results are paginated and by default only the first page is
shown.\& This option allows you to view other pages.\&
.RE
\fB-all\fR
.RS 4
Ignore pagination and just print a long list of results.\&
.PP
.RE
.SH EXAMPLE

View File

@@ -16,16 +16,23 @@ directory.
Be default, this returns a Markdown-formatted list suitable for pasting into
Oddmu pages.
If a directory is provided, only files from the tree starting at that
subdirectory are listed, and the directory is stripped from the page name.
See _oddmu-search_(7) for more information of how pages are searched, sorted and
scored.
# OPTIONS
*-dir* _string_
Limit search to a particular directory.
*-extract*
Print search extracts for interactive use from the command-line.
*-page* _n_
Search results are paginated and by default only the first page is
shown. This option allows you to view other pages.
*-all*
Ignore pagination and just print a long list of results.
# EXAMPLE

View File

@@ -5,7 +5,7 @@
.nh
.ad l
.\" Begin generated content:
.TH "ODDMU-STATIC" "1" "2023-11-05"
.TH "ODDMU-STATIC" "1" "2024-01-17"
.PP
.SH NAME
.PP
@@ -18,8 +18,8 @@ oddmu-static - create a static copy of the site
.SH DESCRIPTION
.PP
The "static" subcommand generates a static copy of the pages in the current
directory and saves them in the given target directory.\& The target directory
must not exist to unser no existing files are clobbered.\&
directory and saves them in the given destination directory.\& The destination
directory must not exist.\&
.PP
All pages (files with the ".\&md" extension) are turned into HTML files (with the
".\&html" extension) using the "static.\&html" template.\& Links pointing to existing
@@ -28,7 +28,18 @@ pages get ".\&html" appended.\&
Hidden files and directories (starting with a ".\&") and backup files (ending with
a "~") are skipped.\&
.PP
All other files are \fIlinked\fR into the same directory.\&
All other files are \fIhard linked\fR.\& This is done to save space: on a typical blog
the images take a lot more space than the text.\& On my blog in 2023 I had 2.\&62
GiB of JPG files and 0.\&02 GiB of Markdown files.\& There is no point in copying
all those images, most of the time.\&
.PP
Note, however: Hard links cannot span filesystems.\& A hard link is just an extra
name for the same file.\& This is why the destination directory for the static
site has to be on same filesystem as the current directory, if it contains any
other place.\&
.PP
Furthermore, in-place editing changes the file for all names.\& Avoid editing the
image files in the destination directory, just to be on the safe side.\&
.PP
.SH EXAMPLE
.PP
@@ -51,7 +62,14 @@ you to migrate static folders and applications.\&
.SH ENVIRONMENT
.PP
The ODDMU_WEBFINGER environment variable has no effect in this situation.\&
Fediverse accounts are not linked to their profile pages.\&
Fediverse accounts are not linked to their profile pages.\& Since the data isn'\&t
cached, every run of this command would trigger a webfinger request for every
fediverse account mentioned.\&
.PP
If the site is large, determining the language of a page slows things down.\& Set
the ODDMU_LANGUAGES environment variable to a comma-separated list of ISO 639-1
codes, e.\&g.\& "en" or "en,de,fr,pt" to limit the languages loaded and thereby
speed language determination up.\&
.PP
.SH SEE ALSO
.PP

View File

@@ -11,8 +11,8 @@ oddmu-static - create a static copy of the site
# DESCRIPTION
The "static" subcommand generates a static copy of the pages in the current
directory and saves them in the given target directory. The target directory
must not exist to unser no existing files are clobbered.
directory and saves them in the given destination directory. The destination
directory must not exist.
All pages (files with the ".md" extension) are turned into HTML files (with the
".html" extension) using the "static.html" template. Links pointing to existing
@@ -21,7 +21,18 @@ pages get ".html" appended.
Hidden files and directories (starting with a ".") and backup files (ending with
a "~") are skipped.
All other files are _linked_ into the same directory.
All other files are _hard linked_. This is done to save space: on a typical blog
the images take a lot more space than the text. On my blog in 2023 I had 2.62
GiB of JPG files and 0.02 GiB of Markdown files. There is no point in copying
all those images, most of the time.
Note, however: Hard links cannot span filesystems. A hard link is just an extra
name for the same file. This is why the destination directory for the static
site has to be on same filesystem as the current directory, if it contains any
other place.
Furthermore, in-place editing changes the file for all names. Avoid editing the
image files in the destination directory, just to be on the safe side.
# EXAMPLE
@@ -42,7 +53,14 @@ you to migrate static folders and applications.
# ENVIRONMENT
The ODDMU_WEBFINGER environment variable has no effect in this situation.
Fediverse accounts are not linked to their profile pages.
Fediverse accounts are not linked to their profile pages. Since the data isn't
cached, every run of this command would trigger a webfinger request for every
fediverse account mentioned.
If the site is large, determining the language of a page slows things down. Set
the ODDMU_LANGUAGES environment variable to a comma-separated list of ISO 639-1
codes, e.g. "en" or "en,de,fr,pt" to limit the languages loaded and thereby
speed language determination up.
# SEE ALSO

View File

@@ -5,7 +5,7 @@
.nh
.ad l
.\" Begin generated content:
.TH "ODDMU" "1" "2023-11-06"
.TH "ODDMU" "1" "2024-01-17"
.PP
.SH NAME
.PP
@@ -60,6 +60,17 @@ See \fIoddmu-templates\fR(5) for more.\&
.PP
You can change the port served by setting the ODDMU_PORT environment variable.\&
.PP
You can change the address served by setting the ODDMU_ADDRESS environment
variable to either an IPv4 address or an IPv6 address.\& If ODDMU_ADDRESS is
unset, then the program listens on all available unicast addresses, both IPv4
and IPv6.\& Here are a few example addresses:
.PP
ODDMU_ADDRESS=127.\&0.\&0.\&1 # The loopback IPv4 address.\&
ODDMU_ADDRESS=2001:db8::3:1 # An IPv6 address.\&
.PP
See the Socket Activation section for an alternative method of listening which
supports Unix-domain sockets.\&
.PP
In order to limit language-detection to the languages you actually use, set the
environment variable ODDMU_LANGUAGES to a comma-separated list of ISO 639-1
codes, e.\&g.\& "en" or "en,de,fr,pt".\&
@@ -67,6 +78,15 @@ codes, e.\&g.\& "en" or "en,de,fr,pt".\&
You can enable webfinger to link fediverse accounts to their correct profile
pages by setting ODDMU_WEBFINGER to "1".\& See \fIoddmu\fR(5).\&
.PP
.SH Socket Activation
.PP
Instead of specifying ODDMU_ADDRESS or ODDMU_PORT, you can start the service
through socket activation.\& The advantage of this method is that you can use a
Unix-domain socket instead of a TCP socket, and the permissions and ownership of
the socket are set before the program starts.\& See \fIoddmu.\&service\fR(5) and
\fIoddmu-apache\fR(5) for an example of how to use socket activation with a
Unix-domain socket under systemd and Apache.\&
.PP
.SH SECURITY
.PP
If the machine you are running Oddmu on is accessible from the Internet, you
@@ -75,6 +95,12 @@ server as a reverse proxy.\&
.PP
See \fIoddmu-apache\fR(5) for an example.\&
.PP
Oddmu assumes that all the users that can edit pages or upload files are trusted
users and therefore their content is trusted.\& Therefore, Oddmu doesn'\&t perform
HTML sanitization!\&
.PP
For an extra dose of security, consider using a Unix-domain socket.\&
.PP
.SH OPTIONS
.PP
The oddmu program can be run on the command-line using various subcommands.\&
@@ -131,7 +157,10 @@ Page names are filenames with ".\&md" appended.\& If your filesystem cannot hand
it, it can'\&t be a page name.\& Filenames can contain slashes and oddmu creates
subdirectories as necessary.\&
.PP
Files may not end with a tilde ('\&~'\&) these are backup files.\&
Files may not end with a tilde ('\&~'\&) these are backup files.\& When saving pages
and file uploads, the old file renamed to the backup file unless the backup file
is less than an hour old, thus collapsing all edits made in an hour into a
single diff when comparing backup and current version.\&
.PP
The \fBindex\fR page is the default page.\& People visiting the "root" of the site are
redirected to "/view/index".\&
@@ -204,6 +233,8 @@ pages by saving an empty page.\&
.IP \(bu 4
\fIoddmu-html\fR(1), on how to render a page from the command-line
.IP \(bu 4
\fIoddmu-list\fR(1), on how to list pages and titles from the command-line
.IP \(bu 4
\fIoddmu-missing\fR(1), on how to find broken local links from the command-line
.IP \(bu 4
\fIoddmu-replace\fR(1), on how to search and replace text from the command-line

View File

@@ -53,6 +53,17 @@ See _oddmu-templates_(5) for more.
You can change the port served by setting the ODDMU_PORT environment variable.
You can change the address served by setting the ODDMU_ADDRESS environment
variable to either an IPv4 address or an IPv6 address. If ODDMU_ADDRESS is
unset, then the program listens on all available unicast addresses, both IPv4
and IPv6. Here are a few example addresses:
ODDMU_ADDRESS=127.0.0.1 # The loopback IPv4 address.
ODDMU_ADDRESS=2001:db8::3:1 # An IPv6 address.
See the Socket Activation section for an alternative method of listening which
supports Unix-domain sockets.
In order to limit language-detection to the languages you actually use, set the
environment variable ODDMU_LANGUAGES to a comma-separated list of ISO 639-1
codes, e.g. "en" or "en,de,fr,pt".
@@ -60,6 +71,15 @@ codes, e.g. "en" or "en,de,fr,pt".
You can enable webfinger to link fediverse accounts to their correct profile
pages by setting ODDMU_WEBFINGER to "1". See _oddmu_(5).
# Socket Activation
Instead of specifying ODDMU_ADDRESS or ODDMU_PORT, you can start the service
through socket activation. The advantage of this method is that you can use a
Unix-domain socket instead of a TCP socket, and the permissions and ownership of
the socket are set before the program starts. See _oddmu.service_(5) and
_oddmu-apache_(5) for an example of how to use socket activation with a
Unix-domain socket under systemd and Apache.
# SECURITY
If the machine you are running Oddmu on is accessible from the Internet, you
@@ -68,6 +88,12 @@ server as a reverse proxy.
See _oddmu-apache_(5) for an example.
Oddmu assumes that all the users that can edit pages or upload files are trusted
users and therefore their content is trusted. Therefore, Oddmu doesn't perform
HTML sanitization!
For an extra dose of security, consider using a Unix-domain socket.
# OPTIONS
The oddmu program can be run on the command-line using various subcommands.
@@ -114,7 +140,10 @@ Page names are filenames with ".md" appended. If your filesystem cannot handle
it, it can't be a page name. Filenames can contain slashes and oddmu creates
subdirectories as necessary.
Files may not end with a tilde ('~') these are backup files.
Files may not end with a tilde ('~') these are backup files. When saving pages
and file uploads, the old file renamed to the backup file unless the backup file
is less than an hour old, thus collapsing all edits made in an hour into a
single diff when comparing backup and current version.
The *index* page is the default page. People visiting the "root" of the site are
redirected to "/view/index".
@@ -181,6 +210,7 @@ pages by saving an empty page.
- _oddmu.service_(5), on how to run the service under systemd
- _oddmu-apache_(5), on how to set up a web server such as Apache
- _oddmu-html_(1), on how to render a page from the command-line
- _oddmu-list_(1), on how to list pages and titles from the command-line
- _oddmu-missing_(1), on how to find broken local links from the command-line
- _oddmu-replace_(1), on how to search and replace text from the command-line
- _oddmu-search_(1), on how to run a search from the command-line

View File

@@ -5,7 +5,7 @@
.nh
.ad l
.\" Begin generated content:
.TH "ODDMU.SERVICE" "5" "2023-10-28"
.TH "ODDMU.SERVICE" "5" "2024-01-17"
.PP
.SH NAME
.PP
@@ -80,9 +80,30 @@ sudo ln -sf /home/oddmu/oddmu\&.service
.fi
.RE
.PP
.SH Socket Activation
.PP
Alternatively, you can let systemd handle the creation of the listening socket,
passing it to Oddmu.\& See "oddmu-unix-domain.\&service" and
"oddmu-unix-domain.\&socket" for a fully worked example of how to do this with a
Unix domain socket.\& Take note of "Accept=no" in the .\&socket file and
"StandardInput=socket" in the .\&service file.\& The option "StandardInput=socket"
tells systemd to pass the socket to the service as its standard input.\&
"Accept=no" tells systemd to pass a listening socket, rather than to try calling
Oddmu for each connection.\&
.PP
The instructions for starting and enabling the systemd service are almost
exactly the same as those in the previous section, with "oddmu.\&service" replaced
by "oddmu-unix-domain.\&service".\& You'\&ll also need to run the following:
.PP
.nf
.RS 4
ln -s /home/oddmu/oddmu-unix-domain\&.socket /etc/systemd/system
.fi
.RE
.PP
.SH SEE ALSO
.PP
\fIoddmu\fR(1), \fIsystemd.\&exec\fR(5), \fIcapabilities\fR(7)
\fIoddmu\fR(1), \fIsystemd.\&exec\fR(5), \fIsystemd.\&socket(5), \fRcapabilities_(7)
.PP
.SH AUTHORS
.PP

View File

@@ -61,9 +61,28 @@ sudo ln -sf /home/oddmu/oddmu.service \
/etc/systemd/system/multi-user.target.wants/
```
# Socket Activation
Alternatively, you can let systemd handle the creation of the listening socket,
passing it to Oddmu. See "oddmu-unix-domain.service" and
"oddmu-unix-domain.socket" for a fully worked example of how to do this with a
Unix domain socket. Take note of "Accept=no" in the .socket file and
"StandardInput=socket" in the .service file. The option "StandardInput=socket"
tells systemd to pass the socket to the service as its standard input.
"Accept=no" tells systemd to pass a listening socket, rather than to try calling
Oddmu for each connection.
The instructions for starting and enabling the systemd service are almost
exactly the same as those in the previous section, with "oddmu.service" replaced
by "oddmu-unix-domain.service". You'll also need to run the following:
```
ln -s /home/oddmu/oddmu-unix-domain.socket /etc/systemd/system
```
# SEE ALSO
_oddmu_(1), _systemd.exec_(5), _capabilities_(7)
_oddmu_(1), _systemd.exec_(5), _systemd.socket(5), _capabilities_(7)
# AUTHORS

53
oddmu-unix-domain.service Normal file
View File

@@ -0,0 +1,53 @@
[Unit]
Description=Oddmu
After=network.target
Requires=oddmu-unix-domain.socket
[Install]
WantedBy=multi-user.target
[Service]
Type=simple
Restart=always
StandardInput=socket
StandardOutput=journal
StandardError=journal
DynamicUser=true
MemoryMax=256M
MemoryHigh=128M
ExecStart=/home/oddmu/oddmu
WorkingDirectory=/home/oddmu
Environment="ODDMU_PORT=8080"
Environment="ODDMU_WEBFINGER=1"
# (man "systemd.exec")
ReadWritePaths=/home/oddmu
ProtectHostname=yes
RestrictSUIDSGID=yes
RemoveIPC=yes
MemoryDenyWriteExecute=yes
# Sandboxing options to harden security
NoNewPrivileges=yes
PrivateTmp=yes
PrivateDevices=yes
RestrictAddressFamilies=AF_UNIX AF_INET AF_INET6
RestrictNamespaces=yes
RestrictRealtime=yes
DevicePolicy=closed
ProtectSystem=full
ProtectControlGroups=yes
ProtectKernelModules=yes
ProtectKernelTunables=yes
LockPersonality=yes
SystemCallFilter=~@clock @debug @module @mount @obsolete @reboot @setuid @swap
# Denying access to capabilities that should not be relevant
# (man "capabilities")
CapabilityBoundingSet=~CAP_RAWIO CAP_MKNOD
CapabilityBoundingSet=~CAP_AUDIT_CONTROL CAP_AUDIT_READ CAP_AUDIT_WRITE
CapabilityBoundingSet=~CAP_SYS_BOOT CAP_SYS_TIME CAP_SYS_MODULE CAP_SYS_PACCT
CapabilityBoundingSet=~CAP_LEASE CAP_LINUX_IMMUTABLE CAP_IPC_LOCK
CapabilityBoundingSet=~CAP_BLOCK_SUSPEND CAP_WAKE_ALARM
CapabilityBoundingSet=~CAP_SYS_TTY_CONFIG
CapabilityBoundingSet=~CAP_MAC_ADMIN CAP_MAC_OVERRIDE
CapabilityBoundingSet=~CAP_NET_ADMIN CAP_NET_BROADCAST CAP_NET_RAW
CapabilityBoundingSet=~CAP_SYS_ADMIN CAP_SYS_PTRACE CAP_SYSLOG

14
oddmu-unix-domain.socket Normal file
View File

@@ -0,0 +1,14 @@
[Unit]
Description=Oddmu server socket
[Socket]
ListenStream=/run/oddmu/oddmu.sock
SocketGroup=www-data
# Systemd manages the socket, so may as well let it be owned by root.
SocketUser=root
# But it needs to be readable and writable by the web server.
SocketMode=0660
Accept=no
[Install]
WantedBy=sockets.target

144
page.go
View File

@@ -40,131 +40,11 @@ func sanitizeStrict(s string) template.HTML {
return template.HTML(policy.Sanitize(s))
}
// santizeBytes uses bluemonday to sanitize the HTML used for pages. This is where you make changes if you want to be
// more lenient.
func sanitizeBytes(bytes []byte) template.HTML {
policy := bluemonday.UGCPolicy()
policy.AllowURLSchemes("gemini", "gopher")
policy.AllowAttrs("title", "class", "style").Globally()
policy.AllowAttrs("loading").OnElements("img") // for lazy loading
// SVG, based on https://svgwg.org/svg2-draft/attindex.html transformed using
// (while (zerop (forward-line 1))
// (when (looking-at "\\([^\t]+\\)\t\\([^\t]+\\).*")
// (let ((attribute (match-string 1))
// (elements (split-string (match-string 2) ", ")))
// (delete-region (point) (line-end-position))
// (insert "policy.AllowAttrs(\"" attribute "\").OnElements("
// (mapconcat (lambda (elem) (concat "\"" elem "\"")) elements ", ")
// ")"))))
// Manually delete "script", "crossorigin", all attributes starting with "on", "ping"
// and add elements without attributes allowed
// (while (re-search-forward "\tpolicy.AllowAttrs(\\(.*\\)).OnElements(\\(.*\\))\n\tpolicy.AllowAttrs(\\1).OnElements(\\(.*\\))" nil t)
// (replace-match "\tpolicy.AllowAttrs(\\1).OnElements(\\2, \\3)"))
// (while (re-search-forward "\tpolicy.AllowAttrs(\\(.*\\)).OnElements(\\(.*\\))\n\tpolicy.AllowAttrs(\\(.*\\)).OnElements(\\2)" nil t)
// (replace-match "\tpolicy.AllowAttrs(\\1, \\2).OnElements(\\3)"))
policy.AllowNoAttrs().OnElements("defs")
policy.AllowAttrs("alignment-baseline", "baseline-shift", "clip-path", "clip-rule", "color", "color-interpolation", "color-interpolation-filters", "cursor", "direction", "display", "dominant-baseline", "fill-opacity", "fill-rule", "filter", "flood-color", "flood-opacity", "font-family", "font-size", "font-size-adjust", "font-stretch", "font-style", "font-variant", "font-weight", "glyph-orientation-horizontal", "glyph-orientation-vertical", "image-rendering", "letter-spacing", "lighting-color", "marker-end", "marker-mid", "marker-start", "mask", "mask-type", "opacity", "overflow", "paint-order", "pointer-events", "shape-rendering", "stop-color", "stop-opacity", "stroke", "stroke-dasharray", "stroke-dashoffset", "stroke-linecap", "stroke-linejoin", "stroke-miterlimit", "stroke-opacity", "stroke-width", "text-anchor", "text-decoration", "text-overflow", "text-rendering", "transform-origin", "unicode-bidi", "vector-effect", "visibility", "white-space", "word-spacing", "writing-mode").Globally() // SVG elements
policy.AllowAttrs("accumulate", "additive", "by", "calcMode", "from", "keySplines", "keyTimes", "values").OnElements("animate", "animateMotion", "animateTransform")
policy.AllowAttrs("amplitude").OnElements("feFuncA", "feFuncB", "feFuncG", "feFuncR")
policy.AllowAttrs("aria-activedescendant", "aria-atomic", "aria-autocomplete", "aria-busy", "aria-checked", "aria-colcount", "aria-colindex", "aria-colspan", "aria-controls", "aria-current", "aria-describedby", "aria-details", "aria-disabled", "aria-dropeffect", "aria-errormessage", "aria-expanded", "aria-flowto", "aria-grabbed", "aria-haspopup", "aria-hidden", "aria-invalid", "aria-keyshortcuts", "aria-label", "aria-labelledby", "aria-level", "aria-live", "aria-modal", "aria-multiline", "aria-multiselectable", "aria-orientation", "aria-owns", "aria-placeholder", "aria-posinset", "aria-pressed", "aria-readonly", "aria-relevant", "aria-required", "aria-roledescription", "aria-rowcount", "aria-rowindex", "aria-rowspan", "aria-selected", "aria-setsize", "aria-sort", "aria-valuemax", "aria-valuemin", "aria-valuenow", "aria-valuetext", "role").OnElements("a", "circle", "discard", "ellipse", "foreignObject", "g", "image", "line", "path", "polygon", "polyline", "rect", "svg", "switch", "symbol", "text", "textPath", "tspan", "use", "view")
policy.AllowAttrs("attributeName").OnElements("animate", "animateTransform", "set")
policy.AllowAttrs("autofocus").OnElements("a", "animate", "animateMotion", "animateTransform", "circle", "clipPath", "defs", "desc", "discard", "ellipse", "feBlend", "feColorMatrix", "feComponentTransfer", "feComposite", "feConvolveMatrix", "feDiffuseLighting", "feDisplacementMap", "feDistantLight", "feDropShadow", "feFlood", "feFuncA", "feFuncB", "feFuncG", "feFuncR", "feGaussianBlur", "feImage", "feMerge", "feMergeNode", "feMorphology", "feOffset", "fePointLight", "feSpecularLighting", "feSpotLight", "feTile", "feTurbulence", "filter", "foreignObject", "g", "image", "line", "linearGradient", "marker", "mask", "metadata", "mpath", "path", "pattern", "polygon", "polyline", "radialGradient", "rect", "set", "stop", "style", "svg", "switch", "symbol", "text", "textPath", "title", "tspan", "use", "view")
policy.AllowAttrs("azimuth", "elevation").OnElements("feDistantLight")
policy.AllowAttrs("baseFrequency", "numOctaves", "seed", "stitchTiles").OnElements("feTurbulence")
policy.AllowAttrs("begin").OnElements("animate", "animateMotion", "animateTransform", "set", "discard")
policy.AllowAttrs("bias", "divisor", "kernelMatrix", "order", "preserveAlpha", "targetX", "targetY").OnElements("feConvolveMatrix")
policy.AllowAttrs("class").OnElements("a", "animate", "animateMotion", "animateTransform", "circle", "clipPath", "defs", "desc", "discard", "ellipse", "feBlend", "feColorMatrix", "feComponentTransfer", "feComposite", "feConvolveMatrix", "feDiffuseLighting", "feDisplacementMap", "feDistantLight", "feDropShadow", "feFlood", "feFuncA", "feFuncB", "feFuncG", "feFuncR", "feGaussianBlur", "feImage", "feMerge", "feMergeNode", "feMorphology", "feOffset", "fePointLight", "feSpecularLighting", "feSpotLight", "feTile", "feTurbulence", "filter", "foreignObject", "g", "image", "line", "linearGradient", "marker", "mask", "metadata", "mpath", "path", "pattern", "polygon", "polyline", "radialGradient", "rect", "set", "stop", "style", "svg", "switch", "symbol", "text", "textPath", "title", "tspan", "use", "view")
policy.AllowAttrs("clipPathUnits").OnElements("clipPath")
policy.AllowAttrs("cx", "cy").OnElements("circle", "ellipse", "radialGradient")
policy.AllowAttrs("d").OnElements("path")
policy.AllowAttrs("diffuseConstant").OnElements("feDiffuseLighting")
policy.AllowAttrs("download").OnElements("a")
policy.AllowAttrs("dur").OnElements("animate", "animateMotion", "animateTransform", "set")
policy.AllowAttrs("dx", "dy").OnElements("feDropShadow", "feOffset", "text", "tspan")
policy.AllowAttrs("edgeMode").OnElements("feConvolveMatrix", "feGaussianBlur")
policy.AllowAttrs("end").OnElements("animate", "animateMotion", "animateTransform", "set")
policy.AllowAttrs("exponent").OnElements("feFuncA", "feFuncB", "feFuncG", "feFuncR")
policy.AllowAttrs("fill").Globally() // at least for all SVG elements
policy.AllowAttrs("filterUnits").OnElements("filter")
policy.AllowAttrs("fr", "fx", "fy").OnElements("radialGradient")
policy.AllowAttrs("gradientTransform", "gradientUnits").OnElements("linearGradient", "radialGradient")
policy.AllowAttrs("height").OnElements("feBlend", "feColorMatrix", "feComponentTransfer", "feComposite", "feConvolveMatrix", "feDiffuseLighting", "feDisplacementMap", "feDropShadow", "feFlood", "feGaussianBlur", "feImage", "feMerge", "feMorphology", "feOffset", "feSpecularLighting", "feTile", "feTurbulence", "filter", "mask", "pattern", "foreignObject", "image", "rect", "svg", "symbol", "use")
policy.AllowAttrs("href").OnElements("a", "animate", "animateMotion", "animateTransform", "set", "discard", "feImage", "image", "linearGradient", "mpath", "pattern", "radialGradient", "textPath", "use")
policy.AllowAttrs("hreflang").OnElements("a")
policy.AllowAttrs("id").OnElements("a", "animate", "animateMotion", "animateTransform", "circle", "clipPath", "defs", "desc", "discard", "ellipse", "feBlend", "feColorMatrix", "feComponentTransfer", "feComposite", "feConvolveMatrix", "feDiffuseLighting", "feDisplacementMap", "feDistantLight", "feDropShadow", "feFlood", "feFuncA", "feFuncB", "feFuncG", "feFuncR", "feGaussianBlur", "feImage", "feMerge", "feMergeNode", "feMorphology", "feOffset", "fePointLight", "feSpecularLighting", "feSpotLight", "feTile", "feTurbulence", "filter", "foreignObject", "g", "image", "line", "linearGradient", "marker", "mask", "metadata", "mpath", "path", "pattern", "polygon", "polyline", "radialGradient", "rect", "set", "stop", "style", "svg", "switch", "symbol", "text", "textPath", "title", "tspan", "use", "view")
policy.AllowAttrs("in").OnElements("feBlend", "feColorMatrix", "feComponentTransfer", "feComposite", "feConvolveMatrix", "feDiffuseLighting", "feDisplacementMap", "feDropShadow", "feGaussianBlur", "feMergeNode", "feMorphology", "feOffset", "feSpecularLighting", "feTile")
policy.AllowAttrs("in2").OnElements("feBlend", "feComposite", "feDisplacementMap")
policy.AllowAttrs("intercept").OnElements("feFuncA", "feFuncB", "feFuncG", "feFuncR")
policy.AllowAttrs("k1", "k2", "k3", "k4").OnElements("feComposite")
policy.AllowAttrs("kernelUnitLength").OnElements("feConvolveMatrix", "feDiffuseLighting", "feSpecularLighting")
policy.AllowAttrs("keyPoints").OnElements("animateMotion")
policy.AllowAttrs("lang").OnElements("a", "animate", "animateMotion", "animateTransform", "circle", "clipPath", "defs", "desc", "discard", "ellipse", "feBlend", "feColorMatrix", "feComponentTransfer", "feComposite", "feConvolveMatrix", "feDiffuseLighting", "feDisplacementMap", "feDistantLight", "feDropShadow", "feFlood", "feFuncA", "feFuncB", "feFuncG", "feFuncR", "feGaussianBlur", "feImage", "feMerge", "feMergeNode", "feMorphology", "feOffset", "fePointLight", "feSpecularLighting", "feSpotLight", "feTile", "feTurbulence", "filter", "foreignObject", "g", "image", "line", "linearGradient", "marker", "mask", "metadata", "mpath", "path", "pattern", "polygon", "polyline", "radialGradient", "rect", "set", "stop", "style", "svg", "switch", "symbol", "text", "textPath", "title", "tspan", "use", "view")
policy.AllowAttrs("lengthAdjust").OnElements("text", "textPath", "tspan")
policy.AllowAttrs("limitingConeAngle").OnElements("feSpotLight")
policy.AllowAttrs("markerHeight", "markerUnits", "markerWidth").OnElements("marker")
policy.AllowAttrs("maskContentUnits", "mask").OnElements("maskUnits")
policy.AllowAttrs("max").OnElements("animate", "animateMotion", "animateTransform", "set")
policy.AllowAttrs("media").OnElements("style")
policy.AllowAttrs("method").OnElements("textPath")
policy.AllowAttrs("min").OnElements("animate", "animateMotion", "animateTransform", "set")
policy.AllowAttrs("mode").OnElements("feBlend")
policy.AllowAttrs("offset").OnElements("feFuncA", "feFuncB", "feFuncG", "feFuncR", "stop")
policy.AllowAttrs("operator").OnElements("feComposite", "feMorphology")
policy.AllowAttrs("orient").OnElements("marker")
policy.AllowAttrs("origin").OnElements("animateMotion")
policy.AllowAttrs("path").OnElements("animateMotion", "textPath")
policy.AllowAttrs("pathLength").OnElements("circle", "ellipse", "line", "path", "polygon", "polyline", "rect")
policy.AllowAttrs("patternContentUnits", "pattern").OnElements("patternTransform")
policy.AllowAttrs("patternUnits").OnElements("pattern")
policy.AllowAttrs("playbackorder", "timelinebegin", "transform").OnElements("svg")
policy.AllowAttrs("points").OnElements("polygon", "polyline")
policy.AllowAttrs("pointsAtX", "feSpotLight").OnElements("pointsAtY")
policy.AllowAttrs("pointsAtZ").OnElements("feSpotLight")
policy.AllowAttrs("preserveAspectRatio").OnElements("feImage", "image", "marker", "pattern", "svg", "symbol", "view")
policy.AllowAttrs("primitiveUnits").OnElements("filter")
policy.AllowAttrs("r").OnElements("circle", "radialGradient")
policy.AllowAttrs("rx", "ry").OnElements("ellipse", "rect")
policy.AllowAttrs("radius").OnElements("feMorphology")
policy.AllowAttrs("refX", "marker", "symbol").OnElements("refY")
policy.AllowAttrs("referrerpolicy", "a").OnElements("rel")
policy.AllowAttrs("repeatCount", "animate", "animateMotion", "animateTransform", "set").OnElements("repeatDur")
policy.AllowAttrs("requiredExtensions").OnElements("a", "animate", "animateMotion", "animateTransform", "circle", "clipPath", "discard", "ellipse", "foreignObject", "g", "image", "line", "mask", "path", "polygon", "polyline", "rect", "set", "svg", "switch", "text", "textPath", "tspan", "use")
policy.AllowAttrs("restart").OnElements("animate", "animateMotion", "animateTransform", "set")
policy.AllowAttrs("result").OnElements("feBlend", "feColorMatrix", "feComponentTransfer", "feComposite", "feConvolveMatrix", "feDiffuseLighting", "feDisplacementMap", "feDropShadow", "feFlood", "feGaussianBlur", "feImage", "feMerge", "feMorphology", "feOffset", "feSpecularLighting", "feTile", "feTurbulence")
policy.AllowAttrs("rotate").OnElements("animateMotion", "text", "tspan")
policy.AllowAttrs("scale").OnElements("feDisplacementMap")
policy.AllowAttrs("side").OnElements("textPath")
policy.AllowAttrs("slope").OnElements("feFuncA", "feFuncB", "feFuncG", "feFuncR")
policy.AllowAttrs("spacing").OnElements("textPath")
policy.AllowAttrs("specularConstant").OnElements("feSpecularLighting")
policy.AllowAttrs("specularExponent").OnElements("feSpecularLighting", "feSpotLight")
policy.AllowAttrs("spreadMethod").OnElements("linearGradient", "radialGradient")
policy.AllowAttrs("startOffset").OnElements("textPath")
policy.AllowAttrs("stdDeviation").OnElements("feDropShadow", "feGaussianBlur")
policy.AllowAttrs("style").OnElements("a", "animate", "animateMotion", "animateTransform", "circle", "clipPath", "defs", "desc", "discard", "ellipse", "feBlend", "feColorMatrix", "feComponentTransfer", "feComposite", "feConvolveMatrix", "feDiffuseLighting", "feDisplacementMap", "feDistantLight", "feDropShadow", "feFlood", "feFuncA", "feFuncB", "feFuncG", "feFuncR", "feGaussianBlur", "feImage", "feMerge", "feMergeNode", "feMorphology", "feOffset", "fePointLight", "feSpecularLighting", "feSpotLight", "feTile", "feTurbulence", "filter", "foreignObject", "g", "image", "line", "linearGradient", "marker", "mask", "metadata", "mpath", "path", "pattern", "polygon", "polyline", "radialGradient", "rect", "set", "stop", "style", "svg", "switch", "symbol", "text", "textPath", "title", "tspan", "use", "view")
policy.AllowAttrs("surfaceScale").OnElements("feDiffuseLighting", "feSpecularLighting")
policy.AllowAttrs("systemLanguage").OnElements("a", "animate", "animateMotion", "animateTransform", "circle", "clipPath", "discard", "ellipse", "foreignObject", "g", "image", "line", "mask", "path", "polygon", "polyline", "rect", "set", "svg", "switch", "text", "textPath", "tspan", "use")
policy.AllowAttrs("tabindex").OnElements("a", "animate", "animateMotion", "animateTransform", "circle", "clipPath", "defs", "desc", "discard", "ellipse", "feBlend", "feColorMatrix", "feComponentTransfer", "feComposite", "feConvolveMatrix", "feDiffuseLighting", "feDisplacementMap", "feDistantLight", "feDropShadow", "feFlood", "feFuncA", "feFuncB", "feFuncG", "feFuncR", "feGaussianBlur", "feImage", "feMerge", "feMergeNode", "feMorphology", "feOffset", "fePointLight", "feSpecularLighting", "feSpotLight", "feTile", "feTurbulence", "filter", "foreignObject", "g", "image", "line", "linearGradient", "marker", "mask", "metadata", "mpath", "path", "pattern", "polygon", "polyline", "radialGradient", "rect", "set", "stop", "style", "svg", "switch", "symbol", "text", "textPath", "title", "tspan", "use", "view")
policy.AllowAttrs("tableValues").OnElements("feFuncA", "feFuncB", "feFuncG", "feFuncR")
policy.AllowAttrs("target").OnElements("a")
policy.AllowAttrs("textLength").OnElements("text", "textPath", "tspan")
policy.AllowAttrs("title").OnElements("style")
policy.AllowAttrs("to").OnElements("animate", "animateMotion", "animateTransform", "set")
policy.AllowAttrs("transform").Globally() // for almost all SVG elements (with the exception of the pattern, linearGradient and radialGradient elements)
policy.AllowAttrs("type").OnElements("a", "animateTransform", "feColorMatrix", "feFuncA", "feFuncB", "feFuncG", "feFuncR", "feTurbulence", "style")
policy.AllowAttrs("values").OnElements("feColorMatrix")
policy.AllowAttrs("viewBox").OnElements("marker", "pattern", "svg", "symbol", "view")
policy.AllowAttrs("width").OnElements("feBlend", "feColorMatrix", "feComponentTransfer", "feComposite", "feConvolveMatrix", "feDiffuseLighting", "feDisplacementMap", "feDropShadow", "feFlood", "feGaussianBlur", "feImage", "feMerge", "feMorphology", "feOffset", "feSpecularLighting", "feTile", "feTurbulence", "filter", "mask", "pattern", "foreignObject", "image", "rect", "svg", "symbol", "use")
policy.AllowAttrs("x").OnElements("feBlend", "feColorMatrix", "feComponentTransfer", "feComposite", "feConvolveMatrix", "feDiffuseLighting", "feDisplacementMap", "feDropShadow", "feFlood", "feGaussianBlur", "feImage", "feMerge", "feMorphology", "feOffset", "feSpecularLighting", "feTile", "feTurbulence", "fePointLight", "feSpotLight", "filter", "mask", "pattern", "text", "tspan", "foreignObject", "image", "rect", "svg", "symbol", "use")
policy.AllowAttrs("x1", "x2", "y1", "y2").OnElements("line", "linearGradient")
policy.AllowAttrs("xChannelSelector").OnElements("feDisplacementMap")
policy.AllowAttrs("xlink:href").OnElements("a", "image", "linearGradient", "pattern", "radialGradient", "textPath", "use", "feImage")
policy.AllowAttrs("xlink:title").OnElements("a", "image", "linearGradient", "pattern", "radialGradient", "textPath", "use")
policy.AllowAttrs("xml:space").OnElements("a", "animate", "animateMotion", "animateTransform", "circle", "clipPath", "defs", "desc", "discard", "ellipse", "feBlend", "feColorMatrix", "feComponentTransfer", "feComposite", "feConvolveMatrix", "feDiffuseLighting", "feDisplacementMap", "feDistantLight", "feDropShadow", "feFlood", "feFuncA", "feFuncB", "feFuncG", "feFuncR", "feGaussianBlur", "feImage", "feMerge", "feMergeNode", "feMorphology", "feOffset", "fePointLight", "feSpecularLighting", "feSpotLight", "feTile", "feTurbulence", "filter", "foreignObject", "g", "image", "line", "linearGradient", "marker", "mask", "metadata", "mpath", "path", "pattern", "polygon", "polyline", "radialGradient", "rect", "set", "stop", "style", "svg", "switch", "symbol", "text", "textPath", "title", "tspan", "use", "view")
policy.AllowAttrs("y").OnElements("feBlend", "feColorMatrix", "feComponentTransfer", "feComposite", "feConvolveMatrix", "feDiffuseLighting", "feDisplacementMap", "feDropShadow", "feFlood", "feGaussianBlur", "feImage", "feMerge", "feMorphology", "feOffset", "feSpecularLighting", "feTile", "feTurbulence", "fePointLight", "feSpotLight", "filter", "mask", "pattern", "text", "tspan", "foreignObject", "image", "rect", "svg", "symbol", "use")
policy.AllowAttrs("yChannelSelector").OnElements("feDisplacementMap")
policy.AllowAttrs("z").OnElements("fePointLight", "feSpotLight")
return template.HTML(policy.SanitizeBytes(bytes))
// unsafeBytes does not use bluemonday to sanitize the HTML used for pages. This is where you make changes if you want
// to be more lenient. If you look at the git repository, there are older versions containing the function sanitizeBytes
// which would do elaborate checking.
func unsafeBytes(bytes []byte) template.HTML {
return template.HTML(bytes)
}
// nameEscape returns the page name safe for use in URLs. That is,
@@ -200,10 +80,22 @@ func (p *Page) save() error {
return err
}
}
_ = os.Rename(filename, filename+"~")
backup(filename)
return os.WriteFile(filename, s, 0644)
}
// backup a file by renaming (!) it unless the existing backup is less than an hour old. A backup gets a tilde appended
// to it ("~"). This is true even if the file refers to a binary file like "image.png" and most applications don't know
// what to do with a file called "image.png~".
func backup(filename string) error {
backup := filename + "~"
fi, err := os.Stat(backup)
if err != nil || time.Since(fi.ModTime()).Minutes() >= 60 {
return os.Rename(filename, backup)
}
return nil
}
// loadPage loads a Page given a name. The filename loaded is that
// Page.Name with the ".md" extension. The Page.Title is set to the
// Page.Name (and possibly changed, later). The Page.Body is set to

View File

@@ -4,8 +4,8 @@ import (
"bytes"
"github.com/gomarkdown/markdown"
"github.com/gomarkdown/markdown/ast"
"github.com/gomarkdown/markdown/parser"
"github.com/gomarkdown/markdown/html"
"github.com/gomarkdown/markdown/parser"
"net/url"
)
@@ -81,7 +81,7 @@ func wikiRenderer() *html.Renderer {
renderer := html.NewRenderer(opts)
return renderer
}
// renderHtml renders the Page.Body to HTML and sets Page.Html, Page.Language, Page.Hashtags, and escapes Page.Name.
// Note: If the rendered HTML doesn't contain the attributes or elements you expect it to contain, check sanitizeBytes!
func (p *Page) renderHtml() {
@@ -89,7 +89,7 @@ func (p *Page) renderHtml() {
renderer := wikiRenderer()
maybeUnsafeHTML := markdown.ToHTML(p.Body, parser, renderer)
p.Name = nameEscape(p.Name)
p.Html = sanitizeBytes(maybeUnsafeHTML)
p.Html = unsafeBytes(maybeUnsafeHTML)
p.Language = language(p.plainText())
p.Hashtags = *hashtags
}

View File

@@ -43,7 +43,7 @@ I am cold, alone
Too faint to focus, so far
I am cold, alone</p>
<p><a class="tag" href="/search/?q=%23Haiku" rel="nofollow">#Haiku</a> <a class="tag" href="/search/?q=%23Cold_Poets" rel="nofollow">#Cold Poets</a></p>
<p><a class="tag" href="/search/?q=%23Haiku">#Haiku</a> <a class="tag" href="/search/?q=%23Cold_Poets">#Cold Poets</a></p>
`
assert.Equal(t, r, string(p.Html))
}
@@ -57,8 +57,8 @@ Our [[time together]]`)}
r := `<h1>Photos and Books</h1>
<p>Blue and green and black
Sky and grass and <a href="cliffs" rel="nofollow">ragged cliffs</a>
Our <a href="time%20together" rel="nofollow">time together</a></p>
Sky and grass and <a href="cliffs">ragged cliffs</a>
Our <a href="time%20together">time together</a></p>
`
assert.Equal(t, r, string(p.Html))
}

View File

@@ -200,7 +200,7 @@ NameLoop:
// prependQueryPage prepends the query itself, if a matching page name exists. This helps if people remember the name
// exactly, or if searching for a hashtag. This function assumes that q is not the empty string. Return wether a page
// was prepended or not.
func prependQueryPage (names []string, dir, q string) ([]string, bool) {
func prependQueryPage(names []string, dir, q string) ([]string, bool) {
index.RLock()
defer index.RUnlock()
if q[0] == '#' && !strings.Contains(q[1:], "#") {

View File

@@ -6,7 +6,7 @@
<meta name="viewport" content="width=device-width">
<title>Search for {{.Query}}</title>
<style>
html { max-width: 65ch; padding: 2ch; margin: auto; color: #111; background-color: #ffe; }
html { max-width: 70ch; padding: 2ch; margin: auto; color: #111; background-color: #ffe; }
body { hyphens: auto; }
header a { margin-right: 1ch; }
form { display: inline-block; }

View File

@@ -13,12 +13,14 @@ import (
)
type searchCmd struct {
page int
all bool
dir string
page int
all bool
extract bool
}
func (cmd *searchCmd) SetFlags(f *flag.FlagSet) {
f.StringVar(&cmd.dir, "dir", "", "search only pages within this sub-directory")
f.IntVar(&cmd.page, "page", 1, "the page in the search result set, default 1")
f.BoolVar(&cmd.all, "all", false, "show all the pages and ignore -page")
f.BoolVar(&cmd.extract, "extract", false, "print page extract instead of link list")
@@ -27,7 +29,7 @@ func (cmd *searchCmd) SetFlags(f *flag.FlagSet) {
func (*searchCmd) Name() string { return "search" }
func (*searchCmd) Synopsis() string { return "Search pages and print a list of links." }
func (*searchCmd) Usage() string {
return `search [-page <n>] <terms>:
return `search [-dir string] [-page <n>|-all] [-extract] <terms>:
Search for pages matching terms and print the result set as a
Markdown list. Before searching, all the pages are indexed. Thus,
startup is slow. The benefit is that the page order is exactly as
@@ -36,32 +38,40 @@ func (*searchCmd) Usage() string {
}
func (cmd *searchCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
return searchCli(os.Stdout, cmd.page, cmd.all, cmd.extract, false, f.Args())
return searchCli(os.Stdout, cmd.dir, cmd.page, cmd.all, cmd.extract, false, f.Args())
}
// searchCli runs the search command on the command line. It is used
// here with an io.Writer for easy testing.
func searchCli(w io.Writer, n int, all, extract bool, quiet bool, args []string) subcommands.ExitStatus {
func searchCli(w io.Writer, dir string, n int, all, extract bool, quiet bool, args []string) subcommands.ExitStatus {
dir, err := checkDir(dir)
if err != nil {
return subcommands.ExitFailure
}
index.load()
q := strings.Join(args, " ")
items, more := search(q, ".", n, true)
items, more := search(q, dir, n, true)
if !quiet {
fmt.Fprint(os.Stderr, "Search for ", q)
if !all {
fmt.Fprint(os.Stderr, ", page ", n)
}
fmt.Fprint(os.Stderr, ": ", len(items))
if len(items) == 1 {
fmt.Fprint(os.Stderr, " result\n")
} else {
fmt.Fprint(os.Stderr, " results\n")
}
fmt.Fprint(os.Stderr, "Search for ", q)
if !all {
fmt.Fprint(os.Stderr, ", page ", n)
}
fmt.Fprint(os.Stderr, ": ", len(items))
if len(items) == 1 {
fmt.Fprint(os.Stderr, " result\n")
} else {
fmt.Fprint(os.Stderr, " results\n")
}
}
if extract {
searchExtract(w, items)
} else {
for _, p := range items {
fmt.Fprintf(w, "* [%s](%s)\n", p.Title, p.Name)
name := p.Name
if strings.HasPrefix(name, dir) {
name = strings.Replace(name, dir, "", 1)
}
fmt.Fprintf(w, "* [%s](%s)\n", p.Title, name)
}
}
if more {

View File

@@ -9,10 +9,25 @@ import (
func TestSearchCmd(t *testing.T) {
b := new(bytes.Buffer)
s := searchCli(b, 1, false, false, true, []string{"oddµ"})
s := searchCli(b, "", 1, false, false, true, []string{"oddµ"})
assert.Equal(t, subcommands.ExitSuccess, s)
r := `* [Oddµ: A minimal wiki](README)
* [Welcome to Oddµ](index)
`
assert.Equal(t, r, b.String())
}
func TestSearchSubdirCmd(t *testing.T) {
cleanup(t, "testdata/search")
p := &Page{Name: "testdata/search/wait", Body: []byte(`# Wait
We should make it so
that before we type and speak
we hear that moment`)}
p.save()
b := new(bytes.Buffer)
s := searchCli(b, "testdata/search", 1, false, false, true, []string{"speak"})
assert.Equal(t, subcommands.ExitSuccess, s)
r := `* [Wait](wait)
`
assert.Equal(t, r, b.String())
}

View File

@@ -29,14 +29,13 @@ func TestSortNames(t *testing.T) {
assert.True(t, slices.IsSorted(names), fmt.Sprintf("Sorted: %v", names))
}
func TestPrependMatches(t *testing.T) {
index.Lock()
for _, s := range []string{"Alex", "Berta", "Chris"} {
index.titles[s] = s
}
index.Unlock()
r := []string{"Berta", "Chris"} // does not prepend
r := []string{"Berta", "Chris"} // does not prepend
u := []string{"Alex", "Berta", "Chris"} // does prepend
v, _ := prependQueryPage(r, "", "Alex")
assert.Equal(t, u, v, "prepend q")
@@ -130,10 +129,10 @@ We met in the park?`)}
func TestHashtagSearch(t *testing.T) {
cleanup(t, "testdata/hashtag")
p := &Page{Name: "testdata/hashtag/Haiku", Body: []byte("# Haikus\n")}
p.save()
p = &Page{Name: "testdata/hashtag/2023-10-28", Body: []byte(`# Tea
My tongue is on fire

View File

@@ -9,6 +9,7 @@ import (
"github.com/gomarkdown/markdown/ast"
"github.com/gomarkdown/markdown/html"
"github.com/google/subcommands"
"html/template"
"io/fs"
"net/url"
"os"
@@ -36,19 +37,33 @@ func (cmd *staticCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface
fmt.Println("Exactly one target directory is required")
return subcommands.ExitFailure
}
return staticCli(filepath.Clean(args[0]))
return staticCli(filepath.Clean(args[0]), false)
}
func staticCli(dir string) subcommands.ExitStatus {
// staticCli generates a static site in the designated directory. The quiet flag is used to suppress output when running
// tests.
func staticCli(dir string, quiet bool) subcommands.ExitStatus {
err := os.Mkdir(dir, 0755)
if err != nil {
fmt.Println(err)
return subcommands.ExitFailure
}
initAccounts()
if (!quiet) {
fmt.Printf("Loaded %d languages\n", loadLanguages())
}
templates := loadTemplates()
n := 0;
err = filepath.Walk(".", func(path string, info fs.FileInfo, err error) error {
return staticFile(path, dir, info, err)
n++
if (!quiet && (n < 100 || n < 1000 && n % 10 == 0 || n % 100 == 0)) {
fmt.Fprintf(os.Stdout, "\r%d", n)
}
return staticFile(path, dir, info, templates, err)
})
if (!quiet) {
fmt.Printf("\r%d\n", n)
}
if err != nil {
fmt.Println(err)
return subcommands.ExitFailure
@@ -58,7 +73,7 @@ func staticCli(dir string) subcommands.ExitStatus {
// staticFile is used to walk the file trees and do the right thing for the destination directory: create
// subdirectories, link files, render HTML files.
func staticFile(path, dir string, info fs.FileInfo, err error) error {
func staticFile(path, dir string, info fs.FileInfo, templates *template.Template, err error) error {
if err != nil {
return err
}
@@ -75,14 +90,14 @@ func staticFile(path, dir string, info fs.FileInfo, err error) error {
}
// render pages
if strings.HasSuffix(filename, ".md") {
return staticPage(filename, dir)
return staticPage(filename, dir, templates)
}
// remaining files are linked
return os.Link(filename, filepath.Join(dir, filename))
}
// staticPage takes the filename of a page (ending in ".md") and generates a static HTML page.
func staticPage(filename, dir string) error {
func staticPage(filename, dir string, templates *template.Template) error {
name := strings.TrimSuffix(filename, ".md")
p, err := loadPage(name)
if err != nil {
@@ -100,10 +115,10 @@ func staticPage(filename, dir string) error {
renderer := html.NewRenderer(opts)
maybeUnsafeHTML := markdown.Render(doc, renderer)
p.Name = nameEscape(p.Name)
p.Html = sanitizeBytes(maybeUnsafeHTML)
p.Html = unsafeBytes(maybeUnsafeHTML)
p.Language = language(p.plainText())
p.Hashtags = *hashtags
return p.write(filepath.Join(dir, name+".html"))
return p.write(filepath.Join(dir, name+".html"), templates)
}
// staticLinks checks a node and if it is a link to a local page, it appends ".html" to the link destination.
@@ -131,7 +146,7 @@ func staticLinks(node ast.Node, entering bool) ast.WalkStatus {
return ast.GoToNext
}
func (p *Page) write(destination string) error {
func (p *Page) write(destination string, templates *template.Template) error {
t := "static.html"
f, err := os.Create(destination)
if err != nil {

View File

@@ -8,7 +8,7 @@ import (
func TestStatusCmd(t *testing.T) {
cleanup(t, "testdata/static")
s := staticCli("testdata/static")
s := staticCli("testdata/static", true)
assert.Equal(t, subcommands.ExitSuccess, s)
// pages
assert.FileExists(t, "testdata/static/index.html")

View File

@@ -23,9 +23,9 @@ label { display: inline-block; width: 20ch }
{{end}}
<form action="/drop/{{.Dir}}" method="POST" enctype="multipart/form-data">
<p>When uploading pictures from a phone, its filename is going to be something cryptic like IMG_1234.JPG.
Please provide your own filename.
Please provide your own filename. End the filename with "-1" to auto-increment.
<p><label for="text">Filename to use:</label>
<input id="text" name="name" value="{{.Name}}" type="text" placeholder="image.jpg" autofocus required>
<input id="text" name="name" value="{{.Name}}" type="text" placeholder="image-1.jpg" autofocus required>
<p>If the uploaded file is a picture from a phone, it is going to be too big for your site.
Sadly, resizing only works for JPG and PNG files. Luckily, most pictures from a phone camera are JPG images.
Feel free to specify a max width of 1200 pixels, for example.

View File

@@ -3,7 +3,9 @@ package main
import (
"github.com/anthonynsimon/bild/imgio"
"github.com/anthonynsimon/bild/transform"
"github.com/bashdrew/goheif"
"image/jpeg"
"image/png"
"io"
"net/http"
"net/url"
@@ -26,11 +28,9 @@ type Upload struct {
var lastRe = regexp.MustCompile(`^(.*)([0-9]+)(.*)$`)
// uploadHandler uses the "upload.html" template to enable uploads.
// The file is saved using the saveUploadHandler. URL parameter are
// used to copy name, maxwidth and quality from the previous upload.
// If the previous name contains a number, this is incremented by
// one.
// uploadHandler uses the "upload.html" template to enable uploads. The file is saved using the dropHandler. URL
// parameters are used to copy name, maxwidth and quality from the previous upload. If the previous name contains a
// number, this is incremented by one.
func uploadHandler(w http.ResponseWriter, r *http.Request, dir string) {
data := &Upload{Dir: dir}
maxwidth := r.FormValue("maxwidth")
@@ -60,9 +60,8 @@ func uploadHandler(w http.ResponseWriter, r *http.Request, dir string) {
renderTemplate(w, "upload", data)
}
// dropHandler takes the "name" form field and the "file" form
// file and saves the file under the given name. The browser is
// redirected to the view of that file.
// dropHandler takes the "name" form field and the "file" form file and saves the file under the given name. The browser
// is redirected to the view of that file.
func dropHandler(w http.ResponseWriter, r *http.Request, dir string) {
d := path.Dir(dir)
// ensure the directory exists
@@ -89,11 +88,7 @@ func dropHandler(w http.ResponseWriter, r *http.Request, dir string) {
return
}
defer file.Close()
// backup an existing file with the same name
_, err = os.Stat(filename)
if err != nil {
os.Rename(filename, filename+"~")
}
backup(filename)
// create the new file
path := d + "/" + filename
dst, err := os.Create(path)
@@ -102,10 +97,6 @@ func dropHandler(w http.ResponseWriter, r *http.Request, dir string) {
return
}
defer dst.Close()
if _, err := io.Copy(dst, file); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// if a resize was requested
maxwidth := r.FormValue("maxwidth")
if len(maxwidth) > 0 {
@@ -115,6 +106,7 @@ func dropHandler(w http.ResponseWriter, r *http.Request, dir string) {
return
}
data.Add("maxwidth", maxwidth)
// determine how the file will be written
ext := strings.ToLower(filepath.Ext(path))
var encoder imgio.Encoder
switch ext {
@@ -133,13 +125,19 @@ func dropHandler(w http.ResponseWriter, r *http.Request, dir string) {
}
encoder = imgio.JPEGEncoder(q)
default:
http.Error(w, "only .png, .jpg, or .jpeg files are supported", http.StatusInternalServerError)
http.Error(w, "Resizing images requires a .png, .jpg or .jpeg extension for the filename", http.StatusInternalServerError)
return
}
img, err := imgio.Open(path)
// try and decode the data in various formats
img, err := jpeg.Decode(file)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
img, err = png.Decode(file)
}
if err != nil {
img, err = goheif.Decode(file)
}
if err != nil {
http.Error(w, "The image could not be decoded (only PNG, JPG and HEIC formats are supported for resizing)", http.StatusInternalServerError)
}
rect := img.Bounds()
width := rect.Max.X - rect.Min.X
@@ -150,8 +148,16 @@ func dropHandler(w http.ResponseWriter, r *http.Request, dir string) {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
} else {
http.Error(w, "The file is too small for this", http.StatusInternalServerError)
return
}
} else {
// just copy the bytes
if _, err := io.Copy(dst, file); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
http.Redirect(w, r, "/upload/"+d+"/?"+data.Encode(), http.StatusFound)
}

27
view.go
View File

@@ -3,6 +3,7 @@ package main
import (
"net/http"
"os"
"path"
"strings"
"time"
)
@@ -12,19 +13,22 @@ func rootHandler(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/view/index", http.StatusFound)
}
// viewHandler serves existing files (including markdown files with
// the .md extension). If the requested file does not exist, a page
// with the same name is loaded. This means adding the .md extension
// and using the "view.html" template to render the HTML. Both
// attempts fail, the browser is redirected to an edit page. As far as
// caching goes: we respond with a 304 NOT MODIFIED if the request has
// an If-Modified-Since header that matches the file's modification
// time, truncated to one second, because the file's modtime has
// sub-second precision and the HTTP timestamp for the Last-Modified
// header has not.
// viewHandler serves pages. If the requested URL maps to an existing file, it is served. If the requested URL maps to a
// directory, the browser is redirected to the index page. If the requested URL ends in ".rss" and the corresponding
// file ending with ".md" exists, a feed is generated and the "feed.html" template is used (it is used to generate a RSS
// 2.0 feed, no matter what the template's extension is). If the requested URL maps to a page name, the corresponding
// file (ending in ".md") is loaded and served using the "view.html" template. If none of the above, the browser is
// redirected to an edit page.
//
// Caching: a 304 NOT MODIFIED is returned if the request has an If-Modified-Since header that matches the file's
// modification time, truncated to one second. Truncation is required because the file's modtime has sub-second
// precision and the HTTP timestamp for the Last-Modified header has not.
func viewHandler(w http.ResponseWriter, r *http.Request, name string) {
file := true
rss := false
if name == "" {
name = "."
}
fn := name
fi, err := os.Stat(fn)
if err != nil {
@@ -36,6 +40,9 @@ func viewHandler(w http.ResponseWriter, r *http.Request, name string) {
}
fn += ".md"
fi, err = os.Stat(fn)
} else if fi.IsDir() {
http.Redirect(w, r, path.Join("/view", name, "index"), http.StatusFound)
return
}
if err == nil {
h, ok := r.Header["If-Modified-Since"]

View File

@@ -6,7 +6,7 @@
<meta name="viewport" content="width=device-width">
<title>{{.Title}}</title>
<style>
html { max-width: 65ch; padding: 1ch; margin: auto; color: #111; background-color: #ffe; }
html { max-width: 70ch; padding: 1ch; margin: auto; color: #111; background-color: #ffe; }
body { hyphens: auto; }
header a { margin-right: 1ch; }
form { display: inline-block; }

View File

@@ -3,10 +3,10 @@ package main
import (
"github.com/stretchr/testify/assert"
"net/http"
"net/url"
"os"
"regexp"
"testing"
"net/url"
)
func TestRootHandler(t *testing.T) {
@@ -19,6 +19,12 @@ func TestViewHandler(t *testing.T) {
assert.HTTPBody(makeHandler(viewHandler, true), "GET", "/view/index", nil))
}
func TestViewHandlerDir(t *testing.T) {
HTTPRedirectTo(t, makeHandler(viewHandler, true), "GET", "/view/", nil, "/view/index")
HTTPRedirectTo(t, makeHandler(viewHandler, true), "GET", "/view/man", nil, "/view/man/index")
HTTPRedirectTo(t, makeHandler(viewHandler, true), "GET", "/view/man/", nil, "/view/man/index")
}
// relies on index.md in the current directory!
func TestViewHandlerWithId(t *testing.T) {
data := make(url.Values)

103
wiki.go
View File

@@ -3,35 +3,32 @@ package main
import (
"context"
"flag"
"fmt"
"github.com/google/subcommands"
"html/template"
"io/fs"
"log"
"net"
"net/http"
"os"
"regexp"
"strings"
)
// Templates are parsed at startup.
var templates = template.Must(
template.ParseFiles("edit.html", "add.html", "view.html", "diff.html",
"search.html", "static.html", "upload.html", "feed.html"))
// validPath is a regular expression where the second group matches a
// page, so when the editHandler is called, a URL path of "/edit/foo"
// results in the editHandler being called with title "foo". The
// regular expression doesn't define the handlers (this happens in the
// main function).
// validPath is a regular expression where the second group matches a page, so when the editHandler is called, a URL
// path of "/edit/foo" results in the editHandler being called with title "foo". The regular expression doesn't define
// the handlers (this happens in the main function).
var validPath = regexp.MustCompile("^/([^/]+)/(.*)$")
// titleRegexp is a regular expression matching a level 1 header line
// in a Markdown document. The first group matches the actual text and
// is used to provide an title for pages. If no title exists in the
// document, the page name is used instead.
// titleRegexp is a regular expression matching a level 1 header line in a Markdown document. The first group matches
// the actual text and is used to provide an title for pages. If no title exists in the document, the page name is used
// instead.
var titleRegexp = regexp.MustCompile("(?m)^#\\s*(.*)\n+")
// renderTemplate is the helper that is used render the templates with
// data.
// renderTemplate is the helper that is used render the templates with data. If the templates cannot be found, that's
// fatal.
func renderTemplate(w http.ResponseWriter, tmpl string, data any) {
templates := loadTemplates()
err := templates.ExecuteTemplate(w, tmpl+".html", data)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
@@ -63,8 +60,7 @@ func makeHandler(fn func(http.ResponseWriter, *http.Request, string), required b
}
}
// getPort returns the environment variable ODDMU_PORT or the default
// port, "8080".
// getPort returns the environment variable ODDMU_PORT or the default port, "8080".
func getPort() string {
port := os.Getenv("ODDMU_PORT")
if port == "" {
@@ -73,9 +69,39 @@ func getPort() string {
return port
}
// scheduleLoadIndex calls index.load and prints some messages before
// and after. For testing, call index.load directly and skip the
// messages.
// When stdin is a socket, getListener returns a listener that listens
// on the socket passed as stdin. This allows systemd-style socket
// activation.
// Otherwise, getListener returns a net.Listener listening on the address from
// ODDMU_ADDRESS and the port from ODDMU_PORT.
// ODDMU_ADDRESS may be either an IPV4 address or an IPv6 address.
// If ODDMU_ADDRESS is unspecified, then the
// listener listens on all available unicast addresses, both IPv4 and IPv6.
func getListener() (net.Listener, error) {
address := os.Getenv("ODDMU_ADDRESS")
port := getPort()
stat, err := os.Stdin.Stat()
if stat == nil {
return nil, err
}
if stat.Mode().Type() == fs.ModeSocket {
// Listening socket passed on stdin, through systemd socket
// activation or similar:
log.Println("Serving a wiki on a listening socket passed by systemd.")
return net.FileListener(os.Stdin)
}
if strings.ContainsRune(address, ':') {
address = fmt.Sprintf("[%s]:%s", address, port)
} else {
address = fmt.Sprintf("%s:%s", address, port)
}
log.Printf("Serving a wiki at address %s", address)
return net.Listen("tcp", address)
}
// scheduleLoadIndex calls index.load and prints some messages before and after. For testing, call index.load directly
// and skip the messages.
func scheduleLoadIndex() {
log.Print("Indexing pages")
n, err := index.load()
@@ -86,15 +112,26 @@ func scheduleLoadIndex() {
}
}
// scheduleLoadLanguages calls loadLanguages and prints some messages before
// and after. For testing, call loadLanguages directly and skip the
// messages.
// scheduleLoadLanguages calls loadLanguages and prints some messages before and after. For testing, call loadLanguages
// directly and skip the messages.
func scheduleLoadLanguages() {
log.Print("Loading languages")
n := loadLanguages()
log.Printf("Loaded %d languages", n)
}
// loadTemplates loads the templates. These aren't always required. If the templates are required and cannot be loaded,
// this a fatal error and the program exits.
func loadTemplates() *template.Template {
templates, err := template.ParseFiles("edit.html", "add.html", "view.html",
"diff.html", "search.html", "static.html", "upload.html", "feed.html")
if err != nil {
log.Println("Templates:", err)
os.Exit(1)
}
return templates
}
func serve() {
http.HandleFunc("/", rootHandler)
http.HandleFunc("/view/", makeHandler(viewHandler, true))
@@ -109,23 +146,25 @@ func serve() {
go scheduleLoadIndex()
go scheduleLoadLanguages()
initAccounts()
port := getPort()
log.Printf("Serving a wiki on port %s", port)
err := http.ListenAndServe(":"+port, nil)
if err != nil {
listener, err := getListener()
if listener == nil {
log.Println(err)
} else {
err := http.Serve(listener, nil)
if err != nil {
log.Println(err)
}
}
}
// commands does the command line parsing in case Oddmu is called with
// some arguments. Without any arguments, the wiki server is started.
// At this point we already know that there is at least one
// subcommand.
// commands does the command line parsing in case Oddmu is called with some arguments. Without any arguments, the wiki
// server is started. At this point we already know that there is at least one subcommand.
func commands() {
subcommands.Register(subcommands.HelpCommand(), "")
subcommands.Register(subcommands.FlagsCommand(), "")
subcommands.Register(subcommands.CommandsCommand(), "")
subcommands.Register(&htmlCmd{}, "")
subcommands.Register(&listCmd{}, "")
subcommands.Register(&staticCmd{}, "")
subcommands.Register(&searchCmd{}, "")
subcommands.Register(&replaceCmd{}, "")

View File

@@ -86,63 +86,22 @@ func HTTPStatusCodeIfModifiedSince(t *testing.T, handler http.HandlerFunc, url s
assert.Equal(t, http.StatusNotModified, w.Code)
}
// restore remembers the file content before the test starts and restores the file at the end. Important for files such
// as "index.md".
func restore(t *testing.T, files ...string) {
data := make(map[string][]byte)
stat := make(map[string]os.FileInfo)
for _, file := range files {
s, err := os.Stat(file)
if err != nil {
t.Log("Could not stat ", file, ": ", err)
continue;
}
c, err := os.ReadFile(file)
if err != nil {
t.Log("Could not read ", file, ": ", err)
continue;
}
stat[file] = s
data[file] = c
}
// cleanup deletes a directory mentioned and removes all pages in that directory from the index.
func cleanup(t *testing.T, dir string) {
t.Cleanup(func() {
for file, c := range data {
m := stat[file].Mode()
err := os.WriteFile(file, c, m)
if err != nil {
t.Log("Could not restore ", file, ": ", err)
}
t := stat[file].ModTime()
os.Chtimes(file, t, t)
}
})
}
// cleanup deletes any directories mentioned and removes all pages in those directories from the index. Incidentally, if
// a filename such as "changes.md" or "changes.md~" is provided instead of a directory, then that page file is removed
// and any mention of it is removed from the index.
func cleanup(t *testing.T, dirs ...string) {
t.Cleanup(func() {
for _, dir := range dirs {
_ = os.RemoveAll(dir)
}
_ = os.RemoveAll(dir)
index.Lock()
defer index.Unlock()
for name := range index.titles {
for _, dir := range dirs {
if strings.HasPrefix(name, dir) {
delete(index.titles, name)
}
if strings.HasPrefix(name, dir) {
delete(index.titles, name)
}
}
ids := []docid{}
for id, name := range index.documents {
for _, dir := range dirs {
if strings.HasPrefix(name, dir) {
delete(index.documents, id)
ids = append(ids, id)
}
if strings.HasPrefix(name, dir) {
delete(index.documents, id)
ids = append(ids, id)
}
}
for hashtag, docs := range index.token {