2018-11-24 12:34:51 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
2019-08-25 10:30:00 +00:00
|
|
|
"encoding/json"
|
2019-02-19 06:34:52 +00:00
|
|
|
"fmt"
|
2018-11-24 12:34:51 +00:00
|
|
|
"html"
|
|
|
|
"html/template"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2019-02-18 19:47:31 +00:00
|
|
|
"time"
|
2019-08-25 10:30:00 +00:00
|
|
|
|
|
|
|
"github.com/sergi/go-diff/diffmatchpatch"
|
2018-11-24 12:34:51 +00:00
|
|
|
)
|
|
|
|
|
2020-06-23 21:54:49 +00:00
|
|
|
const (
|
|
|
|
DocumentsFile = "_documents.json"
|
|
|
|
LinksFile = "_links.json"
|
|
|
|
)
|
|
|
|
|
2020-05-30 22:56:10 +00:00
|
|
|
type saveMessage struct {
|
|
|
|
p string
|
|
|
|
page Page
|
|
|
|
summary string
|
|
|
|
author string
|
|
|
|
}
|
|
|
|
|
2018-11-24 12:34:51 +00:00
|
|
|
type FilePages struct {
|
|
|
|
dirname string
|
2020-05-30 22:56:10 +00:00
|
|
|
saveC chan saveMessage
|
2018-11-24 12:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewFilePages(dirname string) PagesRepository {
|
2020-05-30 22:56:10 +00:00
|
|
|
fp := &FilePages{dirname, make(chan saveMessage)}
|
2020-06-23 21:54:49 +00:00
|
|
|
go func() {
|
2020-05-30 22:56:10 +00:00
|
|
|
for msg := range fp.saveC {
|
|
|
|
fp.save(msg)
|
|
|
|
}
|
|
|
|
}()
|
2018-11-24 12:34:51 +00:00
|
|
|
return fp
|
|
|
|
}
|
|
|
|
|
2020-05-07 13:20:36 +00:00
|
|
|
func (fp *FilePages) Get(title string) Page {
|
|
|
|
name := strings.Replace(title, " ", "_", -1)
|
2020-05-17 13:09:18 +00:00
|
|
|
title = strings.Replace(title, "_", " ", -1)
|
2020-05-08 15:21:39 +00:00
|
|
|
refs, err := loadBackrefs(fp, name)
|
|
|
|
if err != nil {
|
2020-05-16 19:02:19 +00:00
|
|
|
refs = nil
|
2020-05-08 15:21:39 +00:00
|
|
|
}
|
|
|
|
|
2020-05-07 13:20:36 +00:00
|
|
|
f, err := os.Open(filepath.Join(fp.dirname, name))
|
2018-11-24 12:34:51 +00:00
|
|
|
if err != nil {
|
2020-05-08 15:21:39 +00:00
|
|
|
return Page{
|
|
|
|
Title: title,
|
|
|
|
Name: name,
|
|
|
|
Content: "",
|
|
|
|
Refs: refs,
|
|
|
|
}
|
2018-11-24 12:34:51 +00:00
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
body, err := ioutil.ReadAll(f)
|
|
|
|
if err != nil {
|
2020-05-08 15:21:39 +00:00
|
|
|
return Page{
|
|
|
|
Title: title,
|
|
|
|
Name: name,
|
|
|
|
Content: "",
|
|
|
|
Refs: refs,
|
|
|
|
}
|
2020-05-07 13:20:36 +00:00
|
|
|
}
|
|
|
|
return Page{
|
|
|
|
Name: name,
|
|
|
|
Title: title,
|
|
|
|
Content: string(body),
|
|
|
|
Refs: refs,
|
|
|
|
}
|
2018-11-24 12:34:51 +00:00
|
|
|
}
|
|
|
|
|
2019-02-19 06:34:52 +00:00
|
|
|
func (fp *FilePages) Save(p string, page Page, summary, author string) error {
|
2020-05-30 22:56:10 +00:00
|
|
|
fp.saveC <- saveMessage{p, page, summary, author}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fp *FilePages) save(msg saveMessage) error {
|
|
|
|
p := msg.p
|
|
|
|
page := msg.page
|
|
|
|
summary := msg.summary
|
|
|
|
author := msg.author
|
|
|
|
|
2018-11-24 12:34:51 +00:00
|
|
|
f, err := os.Create(filepath.Join(fp.dirname, strings.Replace(p, " ", "_", -1)))
|
|
|
|
if err != nil {
|
2019-02-19 06:34:52 +00:00
|
|
|
return err
|
2018-11-24 12:34:51 +00:00
|
|
|
}
|
|
|
|
defer f.Close()
|
2020-05-20 21:07:56 +00:00
|
|
|
if page.Content[0] == '{' || page.Content[0] == '[' {
|
2019-08-25 10:30:00 +00:00
|
|
|
var buf bytes.Buffer
|
|
|
|
err = json.Indent(&buf, []byte(page.Content), "", " ")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = buf.WriteTo(f)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
f.WriteString(strings.Replace(page.Content, "\r\n", "\n", -1))
|
|
|
|
}
|
2018-11-24 12:34:51 +00:00
|
|
|
|
2020-05-07 13:20:36 +00:00
|
|
|
err = processBackrefs(fp)
|
|
|
|
if err != nil {
|
2020-05-16 19:02:19 +00:00
|
|
|
return fmt.Errorf("while processing backrefs: %s", err)
|
2020-05-07 13:20:36 +00:00
|
|
|
}
|
|
|
|
|
2020-05-30 22:56:10 +00:00
|
|
|
err = saveWithGit(fp, p, summary, author)
|
2020-06-23 21:54:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = saveDocuments(fp)
|
|
|
|
err = saveLinks(fp)
|
2020-05-30 22:56:10 +00:00
|
|
|
return err
|
2018-11-24 12:34:51 +00:00
|
|
|
}
|
|
|
|
|
2020-06-23 21:54:49 +00:00
|
|
|
func saveLinks(fp *FilePages) error {
|
|
|
|
type Document struct {
|
|
|
|
Title string `json:"title"`
|
|
|
|
}
|
|
|
|
var results []Document
|
|
|
|
pages, err := mp.(*FilePages).AllPages()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, page := range pages {
|
|
|
|
results = append(results, Document{page.Title})
|
|
|
|
}
|
|
|
|
|
2020-06-23 22:32:41 +00:00
|
|
|
f, err := os.Create(filepath.Join(fp.dirname, LinksFile))
|
2020-06-23 21:54:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
err = json.NewEncoder(f).Encode(&results)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func saveDocuments(fp *FilePages) error {
|
|
|
|
type Document struct {
|
|
|
|
Title string `json:"title"`
|
|
|
|
Body string `json:"body"`
|
|
|
|
URL string `json:"url"`
|
|
|
|
}
|
|
|
|
var results []Document
|
|
|
|
pages, err := mp.(*FilePages).AllPages()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, page := range pages {
|
|
|
|
content := strings.Builder{}
|
|
|
|
|
|
|
|
var listItems []struct {
|
|
|
|
Indented int
|
|
|
|
Text string
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.NewDecoder(strings.NewReader(page.Content)).Decode(&listItems)
|
|
|
|
if err == nil {
|
|
|
|
for _, item := range listItems {
|
|
|
|
content.WriteString(item.Text)
|
|
|
|
content.WriteByte(' ')
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
content.WriteString(page.Content)
|
|
|
|
content.WriteByte(' ')
|
|
|
|
}
|
|
|
|
|
|
|
|
for page, refs := range page.Refs {
|
|
|
|
content.WriteString(page)
|
|
|
|
content.WriteByte(' ')
|
|
|
|
for _, ref := range refs {
|
|
|
|
content.WriteString(ref.Line)
|
|
|
|
content.WriteByte(' ')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
results = append(results, Document{
|
|
|
|
Title: page.Title,
|
|
|
|
Body: content.String(),
|
|
|
|
URL: page.Name,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-06-23 22:32:41 +00:00
|
|
|
f, err := os.Create(filepath.Join(fp.dirname, DocumentsFile))
|
2020-06-23 21:54:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
err = json.NewEncoder(f).Encode(&results)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-02-19 06:34:52 +00:00
|
|
|
func saveWithGit(fp *FilePages, p string, summary, author string) error {
|
2018-11-24 12:34:51 +00:00
|
|
|
cmd := exec.Command("git", "add", ".")
|
|
|
|
cmd.Dir = fp.dirname
|
2019-02-02 10:08:50 +00:00
|
|
|
err := cmd.Run()
|
|
|
|
if err != nil {
|
2020-05-16 19:02:19 +00:00
|
|
|
return fmt.Errorf("while adding page %s: %s", p, err)
|
2019-02-02 10:08:50 +00:00
|
|
|
}
|
2018-11-24 12:34:51 +00:00
|
|
|
|
|
|
|
cmd = exec.Command("git", "commit", "-m", "Changes to "+p+" by "+author+"\n\n"+summary)
|
|
|
|
cmd.Dir = fp.dirname
|
2019-02-02 10:08:50 +00:00
|
|
|
err = cmd.Run()
|
|
|
|
if err != nil {
|
2020-05-16 19:02:19 +00:00
|
|
|
return fmt.Errorf("while commiting page %s: %s", p, err)
|
2019-02-02 10:08:50 +00:00
|
|
|
}
|
2019-02-19 06:34:52 +00:00
|
|
|
return nil
|
2018-11-24 12:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (fp *FilePages) Exist(p string) bool {
|
|
|
|
f, err := os.Open(filepath.Join(fp.dirname, strings.Replace(p, " ", "_", -1)))
|
|
|
|
if err != nil {
|
|
|
|
return os.IsExist(err)
|
|
|
|
}
|
|
|
|
f.Close()
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func DiffPrettyHtml(diffs []diffmatchpatch.Diff) string {
|
|
|
|
var buff bytes.Buffer
|
|
|
|
for _, diff := range diffs {
|
|
|
|
// text := strings.Replace(html.EscapeString(diff.Text), "\n", "<span class=\"lighter\">¶</span><br>", -1)
|
|
|
|
text := html.EscapeString(diff.Text)
|
|
|
|
switch diff.Type {
|
|
|
|
case diffmatchpatch.DiffInsert:
|
|
|
|
_, _ = buff.WriteString("<ins style=\"background:#e6ffe6;\">")
|
|
|
|
_, _ = buff.WriteString(text)
|
|
|
|
_, _ = buff.WriteString("</ins>")
|
|
|
|
case diffmatchpatch.DiffDelete:
|
|
|
|
_, _ = buff.WriteString("<del style=\"background:#ffe6e6;\">")
|
|
|
|
_, _ = buff.WriteString(text)
|
|
|
|
_, _ = buff.WriteString("</del>")
|
|
|
|
case diffmatchpatch.DiffEqual:
|
|
|
|
_, _ = buff.WriteString("<span>")
|
|
|
|
_, _ = buff.WriteString(text)
|
|
|
|
_, _ = buff.WriteString("</span>")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return buff.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fp *FilePages) PageHistory(p string) ([]Revision, error) {
|
|
|
|
page := strings.Replace(p, " ", "_", -1)
|
|
|
|
cmd := exec.Command("git", "log", "--pretty=oneline", "--no-decorate", "--color=never", page)
|
|
|
|
cmd.Dir = fp.dirname
|
|
|
|
output, err := cmd.StdoutPipe()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer output.Close()
|
|
|
|
|
|
|
|
err = cmd.Start()
|
|
|
|
if err != nil {
|
2020-05-16 19:25:07 +00:00
|
|
|
return nil, fmt.Errorf("while starting: %s", err)
|
2018-11-24 12:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
buf := bufio.NewScanner(output)
|
|
|
|
|
|
|
|
var revisions []Revision
|
|
|
|
|
|
|
|
for buf.Scan() {
|
|
|
|
line := buf.Text()
|
|
|
|
start := strings.Index(line, " ")
|
|
|
|
commitId := line[0:start]
|
|
|
|
rest := line[start+1:]
|
|
|
|
pageText := gitRevision(fp.dirname, page, commitId)
|
|
|
|
revisions = append(revisions, Revision{
|
|
|
|
Version: commitId,
|
|
|
|
Page: DiffPage{Content: pageText},
|
|
|
|
Summary: rest,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
dmp := diffmatchpatch.New()
|
|
|
|
prevText := ""
|
|
|
|
|
|
|
|
for i := len(revisions) - 1; i >= 0; i-- {
|
|
|
|
diffs := dmp.DiffMain(prevText, revisions[i].Page.Content, false)
|
|
|
|
revisions[i].Page.Diff = template.HTML(DiffPrettyHtml(diffs))
|
|
|
|
prevText = revisions[i].Page.Content
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := cmd.Wait(); err != nil {
|
2020-05-16 19:25:07 +00:00
|
|
|
return nil, fmt.Errorf("while waiting: %s", err)
|
2018-11-24 12:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return revisions, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func gitRevision(dirname, page, version string) string {
|
|
|
|
cmd := exec.Command("git", "show", version+":"+page)
|
|
|
|
cmd.Dir = dirname
|
|
|
|
buf := bytes.Buffer{}
|
|
|
|
cmd.Stdout = &buf
|
|
|
|
cmd.Start()
|
|
|
|
cmd.Wait()
|
|
|
|
return buf.String()
|
|
|
|
}
|
2019-02-18 19:47:31 +00:00
|
|
|
|
|
|
|
func (fp *FilePages) RecentChanges() ([]Change, error) {
|
2019-03-02 13:06:17 +00:00
|
|
|
cmd := exec.Command("git", "log", "--format=--1--%nDate: %aI%n--2--%n%b%n--3--", "--name-only")
|
2019-02-18 19:47:31 +00:00
|
|
|
cmd.Dir = fp.dirname
|
|
|
|
buf := bytes.Buffer{}
|
|
|
|
cmd.Stdout = &buf
|
|
|
|
err := cmd.Start()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = cmd.Wait()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
scanner := bufio.NewScanner(&buf)
|
|
|
|
|
2019-03-02 13:06:17 +00:00
|
|
|
state := 0
|
2019-02-18 19:47:31 +00:00
|
|
|
|
|
|
|
var changes []Change
|
|
|
|
var change Change
|
|
|
|
|
2019-03-02 13:06:17 +00:00
|
|
|
body := ""
|
|
|
|
|
2019-02-18 19:47:31 +00:00
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
|
|
|
|
|
|
|
if line == "--1--" {
|
2019-03-02 13:06:17 +00:00
|
|
|
state = 1
|
|
|
|
|
|
|
|
body = ""
|
2019-02-18 19:47:31 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if line == "--2--" {
|
2019-03-02 13:06:17 +00:00
|
|
|
state = 2
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if line == "--3--" {
|
|
|
|
state = 3
|
2019-02-18 19:47:31 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-03-02 13:06:17 +00:00
|
|
|
if state == 1 && strings.HasPrefix(line, "Date: ") {
|
2019-02-18 19:47:31 +00:00
|
|
|
line = line[6:]
|
|
|
|
changeTime, err := time.Parse(time.RFC3339, line)
|
|
|
|
if err != nil {
|
|
|
|
return changes, err
|
|
|
|
}
|
|
|
|
change.Date = changeTime
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-03-02 13:06:17 +00:00
|
|
|
if state == 2 {
|
|
|
|
if line == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
body = body + line
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if state == 3 {
|
2019-02-18 19:47:31 +00:00
|
|
|
if line == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
change.Page = line
|
|
|
|
}
|
|
|
|
|
2019-03-02 13:06:17 +00:00
|
|
|
change.Body = body
|
2019-02-18 19:47:31 +00:00
|
|
|
changes = append(changes, change)
|
|
|
|
}
|
|
|
|
|
|
|
|
return changes, nil
|
|
|
|
}
|
2020-05-07 13:20:36 +00:00
|
|
|
|
|
|
|
func (fp *FilePages) AllPages() ([]Page, error) {
|
|
|
|
files, err := ioutil.ReadDir(fp.dirname)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var pages []Page
|
|
|
|
for _, file := range files {
|
2020-05-17 13:09:18 +00:00
|
|
|
if file.Name()[0] == '.' {
|
|
|
|
continue
|
|
|
|
}
|
2020-05-16 19:02:19 +00:00
|
|
|
if file.Name() == "backrefs.json" {
|
|
|
|
continue
|
|
|
|
}
|
2020-05-07 13:20:36 +00:00
|
|
|
pages = append(pages, fp.Get(file.Name()))
|
|
|
|
}
|
|
|
|
|
|
|
|
return pages, nil
|
|
|
|
}
|