Compare commits
103 commits
process_ma
...
master
Author | SHA1 | Date | |
---|---|---|---|
c3e02a0d1a | |||
|
b034dcf61a | ||
|
03227d6aa9 | ||
|
0521f0ce5a | ||
|
25dcb85c24 | ||
|
bf34e2923b | ||
|
95a73b9471 | ||
|
9320d14d89 | ||
|
e0f52cd031 | ||
|
203a701517 | ||
|
9f80ced3ec | ||
|
26fe2b64da | ||
|
3f71040aec | ||
|
f675896054 | ||
|
3b16598444 | ||
|
fef213f9f3 | ||
|
f96cc8dac5 | ||
|
a20117a42c | ||
|
58a859b014 | ||
|
318273a75d | ||
|
0a64a274ac | ||
|
ccbabb86f8 | ||
|
09c68c5993 | ||
|
7373af0417 | ||
|
65ad262bd8 | ||
|
cffcc93b15 | ||
|
575c18f915 | ||
|
6d1c9967aa | ||
|
748459483e | ||
|
01ceda8015 | ||
|
16384c9f83 | ||
|
d5410f2a22 | ||
|
17c23da9bf | ||
|
619ba14b3f | ||
|
58283a601c | ||
|
c6dd37ed9e | ||
|
85014d2789 | ||
|
a81a568ee2 | ||
|
e92c9695be | ||
|
0439ca0d8e | ||
|
62378007b1 | ||
|
3bbe5840ee | ||
|
baaab6ebc8 | ||
|
fe0011e757 | ||
|
828490df3b | ||
|
c1fcc80cf0 | ||
|
8f1e95bdde | ||
|
c82228f019 | ||
|
b777a92a48 | ||
|
ca1e072d9b | ||
|
df066c611d | ||
|
f961699f0f | ||
|
6118f14bb0 | ||
|
c1dcb1de64 | ||
|
d4e8277f2a | ||
|
a65e85f242 | ||
|
ecf4c1e98e | ||
|
534dffdfe3 | ||
|
999253a778 | ||
|
a00cdf6394 | ||
|
8d86e8937a | ||
|
830f641167 | ||
|
d6e1015197 | ||
|
9c67333b87 | ||
|
7b1a0256e1 | ||
|
94db59c44c | ||
|
42e7857fcd | ||
|
0847cb5c4d | ||
|
b8da0ff753 | ||
|
096da6ef38 | ||
|
c94bf91fc2 | ||
|
b93c79c479 | ||
|
b8a4368219 | ||
|
8500075357 | ||
|
53e983bee9 | ||
|
c18b8f45d1 | ||
|
d3a50b0bc0 | ||
|
d905c1aa62 | ||
|
5a2be1d0a8 | ||
|
c2c0bae335 | ||
|
e26e60ce2c | ||
|
d5bb94dfb6 | ||
|
05b12501a3 | ||
|
c201bb4bc4 | ||
|
963d70ff7a | ||
|
8b0e58c24c | ||
|
862632335b | ||
|
c0ce03973a | ||
|
0b5bff6356 | ||
|
77210a9692 | ||
|
e4fa7ed89a | ||
|
a85abf1ccb | ||
|
bddf4c0225 | ||
|
f7227bf3d4 | ||
|
38c70f7b25 | ||
|
31ace5d4c2 | ||
|
e4629d8edb | ||
|
2b27e27a9b | ||
|
0a48ff2a54 | ||
|
7e6fe36ea0 | ||
|
4516534b39 | ||
|
67ac61ee42 | ||
|
a40d45b197 |
64 changed files with 4531 additions and 2270 deletions
15
.travis.yml
15
.travis.yml
|
@ -19,21 +19,6 @@ script:
|
||||||
- strip -s target/x86_64-unknown-linux-musl/release/sausagewiki
|
- strip -s target/x86_64-unknown-linux-musl/release/sausagewiki
|
||||||
- XZ_OPT=-9 tar Jcf sausagewiki.tar.xz -C target/x86_64-unknown-linux-musl/release/ sausagewiki
|
- XZ_OPT=-9 tar Jcf sausagewiki.tar.xz -C target/x86_64-unknown-linux-musl/release/ sausagewiki
|
||||||
|
|
||||||
deploy:
|
|
||||||
provider: releases
|
|
||||||
api_key:
|
|
||||||
secure: NmM+uk4ijbv5wFF3O7w9KLTrGYbe1mxWAzJDR8cD9rimgORWNQKlHOZtthAQxVgtvmhKAMkzwglgQSX3p0w4yGK5oaV3oO1aA21dzlf0BXL7/BOxgYSTjV+x8O1uIu57ERnf4k2WATCpLSx4r4LpfxMgjdEtIl6LDAnV/zX+HKu7pZAzXvmkS22m5CJbEY4M6DpCAIfpP99dolnyU7h5/AR1njMmzSqGB/naVe5O2j0sBveInsjC+4gPSh9QT/VHZBxbOctcy+kSzwN4iDktkFdYIGe9Z2sDjXsiI39ihXntyOHXA2iVpdkgpIGeLIYBOo+DobgMdS45CzZQ2y9zLnwXwODCgrh8qexxnRpC8RG7uKuVe50R6v4HDPgkjwCJoHicxaEUDiPIsg5qCxEfMYd5qUt21OwEwBN9N8K/RZD0fmgKLE5lQiyxubufeSB4wjpWrXct2M46t25qPFobbZ0kzLCXtZHtKk1mkkk+EWv8UOhRvJ8ih0Fb9ivSOrN6YA1/eRd9/SRntkJriMYmfAW50W3DnyFnPHqdV+x+jHJgcB+DnaDvQnPamk93ZDF/UyUDjVuPJFd0BAFxoRUy6HGaF/yajH4r9g3EdlfSu2IrGDo4vIA9qawBYpHyaSGvYwdCDx4/oUPIAf8sLBS01WOaDJgcmmFey7A/OqSEt6Q=
|
|
||||||
file: sausagewiki.tar.xz
|
|
||||||
skip_cleanup: true
|
|
||||||
on:
|
|
||||||
repo: maghoff/sausagewiki
|
|
||||||
branch: master
|
|
||||||
rust: stable
|
|
||||||
|
|
||||||
cache: cargo
|
|
||||||
before_cache:
|
|
||||||
- chmod -R a+r $HOME/.cargo
|
|
||||||
|
|
||||||
branches:
|
branches:
|
||||||
except:
|
except:
|
||||||
- "/^untagged-/"
|
- "/^untagged-/"
|
||||||
|
|
1566
Cargo.lock
generated
1566
Cargo.lock
generated
File diff suppressed because it is too large
Load diff
81
Cargo.toml
81
Cargo.toml
|
@ -1,84 +1,93 @@
|
||||||
[package]
|
[package]
|
||||||
|
authors = ["Magnus Hoff <maghoff@gmail.com>"]
|
||||||
|
description = "A wiki engine"
|
||||||
|
license = "GPL-3.0"
|
||||||
name = "sausagewiki"
|
name = "sausagewiki"
|
||||||
version = "0.1.0-dev"
|
version = "0.1.0-dev"
|
||||||
description = "A wiki engine"
|
edition = "2018"
|
||||||
authors = ["Magnus Hoff <maghoff@gmail.com>"]
|
|
||||||
license = "GPL-3.0"
|
|
||||||
|
|
||||||
[profile.release]
|
[build-dependencies]
|
||||||
panic = "abort"
|
quote = "1.0.17"
|
||||||
|
walkdir = "1"
|
||||||
|
|
||||||
[dev-dependencies]
|
[build-dependencies.diesel]
|
||||||
indoc = "0.2"
|
default-features = false
|
||||||
matches = "0.1"
|
features = ["sqlite", "chrono"]
|
||||||
|
version = "1.4.8"
|
||||||
|
|
||||||
|
[build-dependencies.diesel_migrations]
|
||||||
|
default-features = false
|
||||||
|
features = ["sqlite"]
|
||||||
|
version = "1.4.0"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
bart = "0.1.4"
|
bart = "0.1.6"
|
||||||
bart_derive = "0.1.4"
|
bart_derive = "0.1.6"
|
||||||
chrono = "0.4"
|
chrono = "0.4"
|
||||||
clap = "2.31"
|
clap = "2.31"
|
||||||
diff = "0.1"
|
diff = "0.1"
|
||||||
futures = "0.1"
|
futures = "0.1"
|
||||||
futures-cpupool = "0.1"
|
futures-cpupool = "0.1"
|
||||||
hyper = "0.11"
|
hyper = "0.11"
|
||||||
lazy_static = "0.2"
|
lazy_static = "1.4.0"
|
||||||
maplit = "1"
|
maplit = "1"
|
||||||
percent-encoding = "1.0"
|
percent-encoding = "1.0"
|
||||||
r2d2 = "0.8"
|
r2d2 = "0.8"
|
||||||
r2d2-diesel = "1.0.0"
|
r2d2-diesel = "1.0.0"
|
||||||
regex = "0.2"
|
regex = "0.2"
|
||||||
|
seahash = "3.0.5"
|
||||||
serde = "1.0.0"
|
serde = "1.0.0"
|
||||||
serde_derive = "1.0.0"
|
serde_derive = "1.0.0"
|
||||||
serde_json = "1.0"
|
serde_json = "1.0"
|
||||||
serde_urlencoded = "0.5"
|
serde_urlencoded = "0.5.3"
|
||||||
slug = "0.1"
|
slug = "0.1"
|
||||||
titlecase = "0.10"
|
titlecase = "0.10"
|
||||||
tokio-io = "0.1"
|
tokio-io = "0.1"
|
||||||
tokio-proto = "0.1"
|
tokio-proto = "0.1"
|
||||||
tokio-service = "0.1"
|
tokio-service = "0.1"
|
||||||
|
serde_plain = "0.3.0"
|
||||||
|
rand = "0.5.5"
|
||||||
|
|
||||||
[dependencies.libsqlite3-sys]
|
[dependencies.codegen]
|
||||||
features = ["bundled"]
|
path = "libs/codegen"
|
||||||
version = "0.9.1"
|
|
||||||
|
|
||||||
[dependencies.diesel]
|
[dependencies.diesel]
|
||||||
default-features = false
|
default-features = false
|
||||||
features = ["sqlite", "chrono"]
|
features = ["sqlite", "chrono"]
|
||||||
version = "1.3.0"
|
version = "1.4.8"
|
||||||
|
|
||||||
[dependencies.diesel_migrations]
|
|
||||||
default-features = false
|
|
||||||
features = ["sqlite"]
|
|
||||||
version = "1.3.0"
|
|
||||||
|
|
||||||
[dependencies.diesel_infer_schema]
|
[dependencies.diesel_infer_schema]
|
||||||
default-features = false
|
default-features = false
|
||||||
features = ["sqlite"]
|
features = ["sqlite"]
|
||||||
version = "1.3.0"
|
version = "1.4.0"
|
||||||
|
|
||||||
|
[dependencies.diesel_migrations]
|
||||||
|
default-features = false
|
||||||
|
features = ["sqlite"]
|
||||||
|
version = "1.4.0"
|
||||||
|
|
||||||
|
[dependencies.libsqlite3-sys]
|
||||||
|
features = ["bundled"]
|
||||||
|
version = "<0.23.0"
|
||||||
|
|
||||||
[dependencies.num]
|
[dependencies.num]
|
||||||
default-features = false
|
default-features = false
|
||||||
version = "0.1"
|
version = "0.1"
|
||||||
|
|
||||||
[dependencies.pulldown-cmark]
|
[dependencies.pulldown-cmark]
|
||||||
|
default-features = false
|
||||||
git = "https://github.com/maghoff/pulldown-cmark.git"
|
git = "https://github.com/maghoff/pulldown-cmark.git"
|
||||||
default-features = false
|
|
||||||
|
|
||||||
[dependencies.codegen]
|
[dev-dependencies]
|
||||||
path = "libs/codegen"
|
indoc = "1.0.4"
|
||||||
|
matches = "0.1"
|
||||||
|
|
||||||
[build-dependencies]
|
[features]
|
||||||
quote = "0.3.10"
|
dynamic-assets = []
|
||||||
walkdir = "1"
|
|
||||||
|
|
||||||
[build-dependencies.diesel]
|
[profile]
|
||||||
default-features = false
|
|
||||||
features = ["sqlite", "chrono"]
|
|
||||||
version = "1.3.0"
|
|
||||||
|
|
||||||
[build-dependencies.diesel_migrations]
|
[profile.release]
|
||||||
default-features = false
|
panic = "abort"
|
||||||
features = ["sqlite"]
|
|
||||||
version = "1.3.0"
|
|
||||||
|
|
||||||
[workspace]
|
[workspace]
|
||||||
|
|
157
assets/script.js
157
assets/script.js
|
@ -1,3 +1,5 @@
|
||||||
|
"use strict";
|
||||||
|
|
||||||
function autosizeTextarea(textarea, shadow) {
|
function autosizeTextarea(textarea, shadow) {
|
||||||
shadow.style.width = textarea.clientWidth + "px";
|
shadow.style.width = textarea.clientWidth + "px";
|
||||||
shadow.value = textarea.value;
|
shadow.value = textarea.value;
|
||||||
|
@ -6,16 +8,17 @@ function autosizeTextarea(textarea, shadow) {
|
||||||
|
|
||||||
function queryArgsFromForm(form) {
|
function queryArgsFromForm(form) {
|
||||||
const items = [];
|
const items = [];
|
||||||
for (const {name, value} of form.elements) {
|
for (const {name, value, type, checked} of form.elements) {
|
||||||
if (!name) continue;
|
if (!name) continue;
|
||||||
|
if (type === "radio" && !checked) continue;
|
||||||
items.push(encodeURIComponent(name) + '=' + encodeURIComponent(value));
|
items.push(encodeURIComponent(name) + '=' + encodeURIComponent(value));
|
||||||
}
|
}
|
||||||
return items.join('&');
|
return items.join('&');
|
||||||
}
|
}
|
||||||
|
|
||||||
function isEdited(form) {
|
function isEdited(form) {
|
||||||
for (const {name, value, defaultValue} of form.elements) {
|
for (const {name, value, defaultValue, checked, defaultChecked} of form.elements) {
|
||||||
if (name && (value !== defaultValue)) return true;
|
if (name && ((value !== defaultValue) || (checked !== defaultChecked))) return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -56,40 +59,77 @@ function confirmDiscard() {
|
||||||
return popup(instantiate("confirm-discard"));
|
return popup(instantiate("confirm-discard"));
|
||||||
}
|
}
|
||||||
|
|
||||||
let hasBeenOpen = false;
|
const state = {
|
||||||
|
hasBeenOpen: false,
|
||||||
|
saving: false,
|
||||||
|
editing: function () { return document.querySelector(".container").classList.contains('edit'); },
|
||||||
|
hasCancelUrl: function () { return document.querySelector("a.button-cancel").getAttribute('href') !== ""; }
|
||||||
|
};
|
||||||
|
|
||||||
function openEditor() {
|
function openEditor() {
|
||||||
|
const bodyElement = document.querySelector("body");
|
||||||
const container = document.querySelector(".container");
|
const container = document.querySelector(".container");
|
||||||
const rendered = container.querySelector(".rendered");
|
const rendered = container.querySelector(".rendered");
|
||||||
const editor = container.querySelector(".editor");
|
const editor = container.querySelector(".editor");
|
||||||
const textarea = editor.querySelector('textarea[name="body"]');
|
const textarea = editor.querySelector('textarea[name="body"]');
|
||||||
const shadow = editor.querySelector('textarea.shadow-control');
|
const shadow = editor.querySelector('textarea.shadow-control');
|
||||||
const form = editor.querySelector("form");
|
const form = document.getElementById('article-editor');
|
||||||
const cancel = editor.querySelector('.cancel');
|
const cancel = form.querySelector('.cancel');
|
||||||
|
const cancelButton = form.querySelector('button.button-cancel');
|
||||||
|
const cancelInteractionGroup = form.querySelector(".cancel-interaction-group");
|
||||||
|
|
||||||
const footer = document.querySelector("footer");
|
const footer = document.querySelector("footer");
|
||||||
const lastUpdated = footer.querySelector(".last-updated");
|
const lastUpdated = footer.querySelector(".last-updated");
|
||||||
|
|
||||||
textarea.style.height = rendered.clientHeight + "px";
|
textarea.style.height = rendered.clientHeight + "px";
|
||||||
|
|
||||||
|
retainScrollRatio(() => {
|
||||||
container.classList.add('edit');
|
container.classList.add('edit');
|
||||||
|
|
||||||
autosizeTextarea(textarea, shadow);
|
autosizeTextarea(textarea, shadow);
|
||||||
|
});
|
||||||
|
updateFormEnabledState();
|
||||||
|
|
||||||
textarea.focus();
|
if (state.hasBeenOpen) return;
|
||||||
|
state.hasBeenOpen = true;
|
||||||
if (hasBeenOpen) return;
|
|
||||||
hasBeenOpen = true;
|
|
||||||
|
|
||||||
textarea.addEventListener('input', () => autosizeTextarea(textarea, shadow));
|
textarea.addEventListener('input', () => autosizeTextarea(textarea, shadow));
|
||||||
window.addEventListener('resize', () => autosizeTextarea(textarea, shadow));
|
window.addEventListener('resize', () => autosizeTextarea(textarea, shadow));
|
||||||
|
|
||||||
form.addEventListener("submit", function (ev) {
|
function updateFormEnabledState() {
|
||||||
ev.preventDefault();
|
const baseEnabled = !state.saving && state.editing();
|
||||||
ev.stopPropagation();
|
const enabled = {
|
||||||
|
cancel: baseEnabled && state.hasCancelUrl(),
|
||||||
|
};
|
||||||
|
|
||||||
|
cancelInteractionGroup.classList.remove(!enabled.cancel ? "interaction-group--root--enabled" : "interaction-group--root--disabled");
|
||||||
|
cancelInteractionGroup.classList.add(enabled.cancel ? "interaction-group--root--enabled" : "interaction-group--root--disabled");
|
||||||
|
|
||||||
|
for (const el of form.elements) {
|
||||||
|
el.disabled = !baseEnabled;
|
||||||
|
}
|
||||||
|
|
||||||
|
cancelButton.disabled = true;
|
||||||
|
|
||||||
|
// TODO: edit-link in footer?
|
||||||
|
}
|
||||||
|
|
||||||
|
function retainScrollRatio(innerFunction) {
|
||||||
|
const scrollElement = document.body.parentElement;
|
||||||
|
const savedScrollRatio = scrollElement.scrollTop / (scrollElement.scrollHeight - scrollElement.clientHeight);
|
||||||
|
innerFunction();
|
||||||
|
scrollElement.scrollTop = (scrollElement.scrollHeight - scrollElement.clientHeight) * savedScrollRatio;
|
||||||
|
}
|
||||||
|
|
||||||
|
function closeEditor() {
|
||||||
|
retainScrollRatio(() => container.classList.remove('edit'));
|
||||||
|
document.activeElement && document.activeElement.blur();
|
||||||
|
}
|
||||||
|
|
||||||
|
function doSave() {
|
||||||
|
state.saving = true;
|
||||||
|
updateFormEnabledState();
|
||||||
|
|
||||||
const body = queryArgsFromForm(form);
|
const body = queryArgsFromForm(form);
|
||||||
textarea.disabled = true;
|
|
||||||
// TODO Disable other interaction as well: title editor, cancel and OK buttons
|
|
||||||
|
|
||||||
fetch(
|
fetch(
|
||||||
form.getAttribute("action"),
|
form.getAttribute("action"),
|
||||||
|
@ -109,7 +149,8 @@ function openEditor() {
|
||||||
if (probablyLoginRedirect) {
|
if (probablyLoginRedirect) {
|
||||||
return loginDialog(response.url)
|
return loginDialog(response.url)
|
||||||
.then(() => {
|
.then(() => {
|
||||||
textarea.disabled = false;
|
state.saving = false;
|
||||||
|
updateFormEnabledState();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -117,8 +158,10 @@ function openEditor() {
|
||||||
|
|
||||||
return response.json()
|
return response.json()
|
||||||
.then(result => {
|
.then(result => {
|
||||||
// Update url-bar, page title and footer
|
// Update url-bar, page title, footer and cancel link
|
||||||
window.history.replaceState(null, result.title, result.slug == "" ? "." : result.slug);
|
const url = result.slug == "" ? "." : result.slug;
|
||||||
|
window.history.replaceState(null, result.title, url);
|
||||||
|
cancel.setAttribute("href", url);
|
||||||
document.querySelector("title").textContent = result.title;
|
document.querySelector("title").textContent = result.title;
|
||||||
lastUpdated.innerHTML = result.last_updated;
|
lastUpdated.innerHTML = result.last_updated;
|
||||||
lastUpdated.classList.remove("missing");
|
lastUpdated.classList.remove("missing");
|
||||||
|
@ -129,17 +172,22 @@ function openEditor() {
|
||||||
form.elements.title.value = result.title;
|
form.elements.title.value = result.title;
|
||||||
shadow.value = textarea.value = result.body;
|
shadow.value = textarea.value = result.body;
|
||||||
|
|
||||||
|
form.querySelector(`.theme-picker--option[value=${JSON.stringify(result.theme)}]`).checked = true;
|
||||||
|
bodyElement.className = `theme-${result.theme}`;
|
||||||
|
|
||||||
// Update form:
|
// Update form:
|
||||||
form.elements.base_revision.value = result.revision;
|
form.elements.base_revision.value = result.revision;
|
||||||
for (const element of form.elements) {
|
for (const element of form.elements) {
|
||||||
element.defaultValue = element.value;
|
element.defaultValue = element.value;
|
||||||
|
element.defaultChecked = element.checked;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!result.conflict) {
|
if (!result.conflict) {
|
||||||
container.classList.remove('edit');
|
closeEditor();
|
||||||
}
|
}
|
||||||
|
|
||||||
textarea.disabled = false;
|
state.saving = false;
|
||||||
|
updateFormEnabledState();
|
||||||
autosizeTextarea(textarea, shadow);
|
autosizeTextarea(textarea, shadow);
|
||||||
|
|
||||||
if (result.conflict) {
|
if (result.conflict) {
|
||||||
|
@ -149,23 +197,37 @@ function openEditor() {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}).catch(err => {
|
}).catch(err => {
|
||||||
textarea.disabled = false;
|
state.saving = false;
|
||||||
|
updateFormEnabledState();
|
||||||
console.error(err);
|
console.error(err);
|
||||||
return alertAsync(err.toString());
|
return alertAsync(err.toString());
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function doCancel() {
|
||||||
|
Promise.resolve(!isEdited(form) || confirmDiscard())
|
||||||
|
.then(doReset => {
|
||||||
|
if (doReset) {
|
||||||
|
closeEditor();
|
||||||
|
updateFormEnabledState();
|
||||||
|
form.reset();
|
||||||
|
|
||||||
|
let selectedTheme = form.querySelector(`.theme-picker--option[checked]`).value;
|
||||||
|
bodyElement.className = `theme-${selectedTheme}`;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
form.addEventListener("submit", function (ev) {
|
||||||
|
ev.preventDefault();
|
||||||
|
ev.stopPropagation();
|
||||||
|
doSave();
|
||||||
});
|
});
|
||||||
|
|
||||||
cancel.addEventListener('click', function (ev) {
|
cancel.addEventListener('click', function (ev) {
|
||||||
ev.preventDefault();
|
ev.preventDefault();
|
||||||
ev.stopPropagation();
|
ev.stopPropagation();
|
||||||
|
doCancel();
|
||||||
Promise.resolve(!isEdited(form) || confirmDiscard())
|
|
||||||
.then(doReset => {
|
|
||||||
if (doReset) {
|
|
||||||
container.classList.remove('edit');
|
|
||||||
form.reset();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
window.addEventListener("beforeunload", function (ev) {
|
window.addEventListener("beforeunload", function (ev) {
|
||||||
|
@ -174,8 +236,43 @@ function openEditor() {
|
||||||
return ev.returnValue = "Discard changes?";
|
return ev.returnValue = "Discard changes?";
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
document.addEventListener("keypress", function (ev) {
|
||||||
|
const accel = ev.ctrlKey || ev.metaKey; // Imprecise, but works cross platform
|
||||||
|
if (ev.key === "Enter" && accel) {
|
||||||
|
if (!state.editing()) return;
|
||||||
|
|
||||||
|
ev.stopPropagation();
|
||||||
|
ev.preventDefault();
|
||||||
|
|
||||||
|
doSave();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
const themeOptions = form.querySelectorAll(".theme-picker--option");
|
||||||
|
for (let themeOption of themeOptions) {
|
||||||
|
themeOption.addEventListener("click", function (ev) {
|
||||||
|
bodyElement.className = `theme-${ev.target.value}`;
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function initializeTheme() {
|
||||||
|
const form = document.getElementById('article-editor');
|
||||||
|
|
||||||
|
let preSelectedTheme = form.querySelector(`.theme-picker--option[checked]`);
|
||||||
|
if (preSelectedTheme) return;
|
||||||
|
|
||||||
|
let themes = form.querySelectorAll(`.theme-picker--option`);
|
||||||
|
let randomThemeId = (Math.random() * themes.length) | 0;
|
||||||
|
|
||||||
|
let theme = themes[randomThemeId];
|
||||||
|
theme.defaultChecked = theme.checked = true;
|
||||||
|
document.querySelector("body").className = `theme-${theme.value}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
initializeTheme();
|
||||||
|
|
||||||
document
|
document
|
||||||
.getElementById("openEditor")
|
.getElementById("openEditor")
|
||||||
.addEventListener("click", function (ev) {
|
.addEventListener("click", function (ev) {
|
||||||
|
|
320
assets/style.css
320
assets/style.css
|
@ -1,15 +1,11 @@
|
||||||
@font-face {
|
|
||||||
font-family: 'Amatic SC';
|
|
||||||
font-style: normal;
|
|
||||||
font-weight: 400;
|
|
||||||
src: local('Amatic SC Regular'), local('AmaticSC-Regular'),
|
|
||||||
url('amatic-sc-v9-latin-regular.woff') format('woff');
|
|
||||||
}
|
|
||||||
|
|
||||||
.prototype {
|
.prototype {
|
||||||
display: none;
|
display: none;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
input {
|
||||||
|
margin: 0; /* reset for Safari */
|
||||||
|
}
|
||||||
|
|
||||||
html {
|
html {
|
||||||
font-family: "Apple Garamond", "Baskerville",
|
font-family: "Apple Garamond", "Baskerville",
|
||||||
"Times New Roman", "Droid Serif", "Times",
|
"Times New Roman", "Droid Serif", "Times",
|
||||||
|
@ -17,8 +13,6 @@ html {
|
||||||
}
|
}
|
||||||
|
|
||||||
h1 {
|
h1 {
|
||||||
font-family: 'Amatic SC', sans-serif;
|
|
||||||
|
|
||||||
font-weight: normal;
|
font-weight: normal;
|
||||||
font-style: normal;
|
font-style: normal;
|
||||||
|
|
||||||
|
@ -64,15 +58,16 @@ h1+*, h2+*, h3+*, h4+*, h5+*, h6+* {
|
||||||
|
|
||||||
article>hr {
|
article>hr {
|
||||||
border: none;
|
border: none;
|
||||||
border-top: 1px solid black;
|
border-top: 6px solid var(--theme-main);
|
||||||
max-width: 400px;
|
width: 40px;
|
||||||
width: 70%;
|
|
||||||
margin: 20px auto;
|
margin: 20px auto;
|
||||||
}
|
}
|
||||||
|
|
||||||
.notice {
|
.notice {
|
||||||
background: lightyellow;
|
background: var(--theme-main);
|
||||||
padding: 16px 48px;
|
color: var(--theme-text);
|
||||||
|
|
||||||
|
padding: 1px 24px;
|
||||||
font-size: 18px;
|
font-size: 18px;
|
||||||
line-height: 32px;
|
line-height: 32px;
|
||||||
|
|
||||||
|
@ -81,6 +76,22 @@ article>hr {
|
||||||
width: 100%;
|
width: 100%;
|
||||||
margin: 30px auto;
|
margin: 30px auto;
|
||||||
}
|
}
|
||||||
|
.notice a {
|
||||||
|
color: var(--theme-link);
|
||||||
|
}
|
||||||
|
|
||||||
|
.hero {
|
||||||
|
background: var(--theme-main);
|
||||||
|
color: var(--theme-text);
|
||||||
|
|
||||||
|
/* Hack to force containing the children instead of collapsing marigins */
|
||||||
|
border: 1px solid var(--theme-main);
|
||||||
|
}
|
||||||
|
|
||||||
|
.search-container {
|
||||||
|
background: var(--theme-main);
|
||||||
|
color: var(--theme-text);
|
||||||
|
}
|
||||||
|
|
||||||
header, article>*, .search {
|
header, article>*, .search {
|
||||||
box-sizing: border-box;
|
box-sizing: border-box;
|
||||||
|
@ -97,7 +108,7 @@ header {
|
||||||
}
|
}
|
||||||
|
|
||||||
article {
|
article {
|
||||||
margin: 0 auto 120px auto;
|
margin: 50px auto 120px auto;
|
||||||
|
|
||||||
font-size: 18px;
|
font-size: 18px;
|
||||||
line-height: 32px;
|
line-height: 32px;
|
||||||
|
@ -148,7 +159,19 @@ pre {
|
||||||
}
|
}
|
||||||
|
|
||||||
a {
|
a {
|
||||||
color: #457796;
|
color: #1976D2;
|
||||||
|
text-decoration: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
a[href^="http"]::after {
|
||||||
|
display: inline-block;
|
||||||
|
padding: 0 0.1rem;
|
||||||
|
font-size: 75%;
|
||||||
|
content: "🔗";
|
||||||
|
line-height: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
a[href^="http"]:hover::after {
|
||||||
text-decoration: none;
|
text-decoration: none;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -182,10 +205,10 @@ body {
|
||||||
|
|
||||||
|
|
||||||
footer {
|
footer {
|
||||||
padding: 0 8px;
|
padding: 16px 8px 16px 8px;
|
||||||
|
|
||||||
background: #f8f8f8;
|
background: var(--theme-main);
|
||||||
color: #444;
|
color: var(--theme-text);
|
||||||
text-align: center;
|
text-align: center;
|
||||||
font-family: -apple-system, BlinkMacSystemFont,
|
font-family: -apple-system, BlinkMacSystemFont,
|
||||||
"Segoe UI", "Roboto", "Oxygen",
|
"Segoe UI", "Roboto", "Oxygen",
|
||||||
|
@ -193,6 +216,10 @@ footer {
|
||||||
"Droid Sans", "Helvetica Neue", sans-serif;
|
"Droid Sans", "Helvetica Neue", sans-serif;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
footer a {
|
||||||
|
color: var(--theme-link);
|
||||||
|
}
|
||||||
|
|
||||||
ul.dense {
|
ul.dense {
|
||||||
list-style: none;
|
list-style: none;
|
||||||
padding: 0;
|
padding: 0;
|
||||||
|
@ -248,6 +275,11 @@ h1>input {
|
||||||
width: 100%;
|
width: 100%;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.hero input {
|
||||||
|
background: var(--theme-input);
|
||||||
|
color: var(--theme-text);
|
||||||
|
}
|
||||||
|
|
||||||
.shadow-control {
|
.shadow-control {
|
||||||
visibility: hidden;
|
visibility: hidden;
|
||||||
position: fixed;
|
position: fixed;
|
||||||
|
@ -277,18 +309,163 @@ h1>input {
|
||||||
bottom: 0;
|
bottom: 0;
|
||||||
left: 0;
|
left: 0;
|
||||||
|
|
||||||
background: #91A238;
|
box-sizing: border-box;
|
||||||
padding: 10px 20px;
|
|
||||||
|
text-align: right;
|
||||||
|
|
||||||
|
box-shadow: 0px 5px 20px rgba(0,0,0, 0.2);
|
||||||
|
|
||||||
|
background: white;
|
||||||
|
color: var(--theme-text);
|
||||||
|
padding: 10px 10px;
|
||||||
|
|
||||||
|
transform: translate(0, 65px);
|
||||||
|
transition: transform 100ms;
|
||||||
|
transition-timing-function: linear;
|
||||||
|
|
||||||
|
pointer-events: none;
|
||||||
}
|
}
|
||||||
|
|
||||||
@media (min-width: 630px) {
|
.edit .editor-controls {
|
||||||
.editor-controls {
|
transform: translate(0, 0);
|
||||||
position: fixed;
|
transition-timing-function: cubic-bezier(.17,.84,.44,1);
|
||||||
left: auto;
|
|
||||||
right: 20px;
|
|
||||||
bottom: 20px;
|
|
||||||
|
|
||||||
box-shadow: 2px 2px 8px rgba(0,0,0, 0.25);
|
pointer-events: unset;
|
||||||
|
}
|
||||||
|
|
||||||
|
.theme-picker {
|
||||||
|
position: absolute;
|
||||||
|
top: 0;
|
||||||
|
left: 0;
|
||||||
|
right: 0;
|
||||||
|
|
||||||
|
display: flex;
|
||||||
|
}
|
||||||
|
|
||||||
|
.theme-picker--option {
|
||||||
|
/* reset */
|
||||||
|
-webkit-appearance: none;
|
||||||
|
-moz-appearance: none;
|
||||||
|
-o-appearance: none;
|
||||||
|
-ms-appearance: none;
|
||||||
|
appearance: none;
|
||||||
|
border: none;
|
||||||
|
border-radius: 0;
|
||||||
|
margin: 0;
|
||||||
|
padding: 0;
|
||||||
|
|
||||||
|
height: 20px;
|
||||||
|
background: var(--theme-main);
|
||||||
|
color: var(--theme-text);
|
||||||
|
|
||||||
|
flex-grow: 1;
|
||||||
|
|
||||||
|
position: relative;
|
||||||
|
}
|
||||||
|
|
||||||
|
.theme-picker--option:checked::after {
|
||||||
|
content: " ";
|
||||||
|
display: block;
|
||||||
|
background: white;
|
||||||
|
border-radius: 5px;
|
||||||
|
width: 10px;
|
||||||
|
height: 10px;
|
||||||
|
|
||||||
|
position: absolute;
|
||||||
|
top: calc(50% - 5px);
|
||||||
|
left: calc(50% - 5px);
|
||||||
|
}
|
||||||
|
|
||||||
|
.button {
|
||||||
|
border-radius: 2px;
|
||||||
|
|
||||||
|
display: inline-block;
|
||||||
|
width: 120px;
|
||||||
|
text-align: center;
|
||||||
|
border: none;
|
||||||
|
|
||||||
|
cursor: pointer;
|
||||||
|
|
||||||
|
font-family: -apple-system, BlinkMacSystemFont,
|
||||||
|
"Segoe UI", "Roboto", "Oxygen",
|
||||||
|
"Ubuntu", "Cantarell", "Fira Sans",
|
||||||
|
"Droid Sans", "Helvetica Neue", sans-serif;
|
||||||
|
font-size: 16px;
|
||||||
|
line-height: 20px;
|
||||||
|
|
||||||
|
padding: 10px 0px;
|
||||||
|
margin-left: 10px;
|
||||||
|
}
|
||||||
|
.button[disabled] {
|
||||||
|
opacity: 0.5;
|
||||||
|
cursor: default;
|
||||||
|
}
|
||||||
|
.button:hover {
|
||||||
|
text-decoration: none;
|
||||||
|
}
|
||||||
|
.button:not([disabled]):hover, .button:not([disabled]):active {
|
||||||
|
background: var(--button-alt);
|
||||||
|
}
|
||||||
|
|
||||||
|
.button-cancel {
|
||||||
|
background: white;
|
||||||
|
color: var(--theme-main);
|
||||||
|
--button-alt: #f0f0f0;
|
||||||
|
}
|
||||||
|
.button-default {
|
||||||
|
background: var(--theme-main);
|
||||||
|
color: var(--theme-text);
|
||||||
|
--button-alt: var(--theme-input);
|
||||||
|
}
|
||||||
|
|
||||||
|
.cancel-interaction-group {
|
||||||
|
display: inline;
|
||||||
|
}
|
||||||
|
|
||||||
|
.interaction-group--root--enabled .interaction-group--disabled {
|
||||||
|
display: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
.interaction-group--root--disabled .interaction-group--enabled {
|
||||||
|
display: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
@media (min-width: 960px) {
|
||||||
|
/* min-width is calculated like this:
|
||||||
|
|
||||||
|
body-width = width of the main text column
|
||||||
|
controls-width = width of .editor-controls element, including drop-shadow
|
||||||
|
|
||||||
|
min-width = body-width + 2*controls-width = 600 + 2 * 180 = 960
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
.editor-controls {
|
||||||
|
border-radius: 2px;
|
||||||
|
|
||||||
|
position: fixed;
|
||||||
|
left: calc(50% + 320px);
|
||||||
|
width: 140px;
|
||||||
|
top: calc(50% - 55px);
|
||||||
|
height: 110px;
|
||||||
|
padding: 10px;
|
||||||
|
|
||||||
|
transform: translate(20px, 0);
|
||||||
|
opacity: 0;
|
||||||
|
|
||||||
|
transition: transform 100ms, opacity 100ms;
|
||||||
|
transition-timing-function: linear;
|
||||||
|
}
|
||||||
|
|
||||||
|
.edit .editor-controls {
|
||||||
|
transform: translate(0, 0);
|
||||||
|
transition-timing-function: cubic-bezier(.17,.84,.44,1);
|
||||||
|
opacity: 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
.button {
|
||||||
|
margin: 0;
|
||||||
|
margin-bottom: 10px;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -313,18 +490,18 @@ article ul.search-results {
|
||||||
display: block;
|
display: block;
|
||||||
color: inherit;
|
color: inherit;
|
||||||
text-decoration: none;
|
text-decoration: none;
|
||||||
border: 1px solid #ccc;
|
padding: 8px 16px;
|
||||||
padding: 8px;
|
background: white;
|
||||||
|
color: black;
|
||||||
}
|
}
|
||||||
.search-result a:hover, .search-result a:focus {
|
.search-result a:hover, .search-result a:focus {
|
||||||
background: #0074D9;
|
background: var(--theme-main);
|
||||||
border-color: #0074D9;
|
color: var(--theme-text);
|
||||||
color: white;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
.search {
|
.search {
|
||||||
text-align: center;
|
text-align: center;
|
||||||
margin-top: 30px;
|
margin-top: 45px;
|
||||||
position: relative;
|
position: relative;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -333,16 +510,18 @@ input[type="search"]::-webkit-search-decoration {
|
||||||
}
|
}
|
||||||
|
|
||||||
input[type="search"] {
|
input[type="search"] {
|
||||||
-webkit-appearance: textfield;
|
-webkit-appearance: none;
|
||||||
|
border-radius: 0;
|
||||||
|
|
||||||
box-sizing: border-box;
|
box-sizing: border-box;
|
||||||
width: 100%;
|
width: 100%;
|
||||||
max-width: 300px;
|
max-width: 300px;
|
||||||
transition: max-width 200ms;
|
|
||||||
|
|
||||||
padding: 0 16px;
|
padding: 0 16px;
|
||||||
margin: 0;
|
margin: 0;
|
||||||
border: 1px solid #ccc;
|
border: none;
|
||||||
|
background: var(--theme-input);
|
||||||
|
color: var(--theme-text);
|
||||||
|
|
||||||
font: inherit;
|
font: inherit;
|
||||||
font-size: 18px;
|
font-size: 18px;
|
||||||
|
@ -350,13 +529,12 @@ input[type="search"] {
|
||||||
line-height: 32px;
|
line-height: 32px;
|
||||||
height: 34px;
|
height: 34px;
|
||||||
|
|
||||||
border-radius: 18px;
|
|
||||||
text-overflow: ellipsis;
|
text-overflow: ellipsis;
|
||||||
}
|
}
|
||||||
|
|
||||||
.search input:focus, .search.focus input {
|
input[type="search"]::placeholder, .hero input::placeholder {
|
||||||
max-width: 300px;
|
color: var(--theme-text);
|
||||||
border-color: #999;
|
opacity: 0.6;
|
||||||
}
|
}
|
||||||
|
|
||||||
.search .live-results {
|
.search .live-results {
|
||||||
|
@ -364,9 +542,8 @@ input[type="search"] {
|
||||||
|
|
||||||
box-sizing: border-box;
|
box-sizing: border-box;
|
||||||
width: 100%;
|
width: 100%;
|
||||||
max-width: 266px; /* 300px - padding - border */
|
max-width: 300px;
|
||||||
|
|
||||||
background: white;
|
|
||||||
padding: 0;
|
padding: 0;
|
||||||
margin: 0 auto;
|
margin: 0 auto;
|
||||||
|
|
||||||
|
@ -376,6 +553,15 @@ input[type="search"] {
|
||||||
max-height: 0px;
|
max-height: 0px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.search-widget-container {
|
||||||
|
border-radius: 2px;
|
||||||
|
overflow: hidden;
|
||||||
|
|
||||||
|
width: 100%;
|
||||||
|
max-width: 300px;
|
||||||
|
display: inline-block;
|
||||||
|
}
|
||||||
|
|
||||||
.live-results.show {
|
.live-results.show {
|
||||||
max-height: 500px;
|
max-height: 500px;
|
||||||
}
|
}
|
||||||
|
@ -383,13 +569,8 @@ input[type="search"] {
|
||||||
.live-results .search-result {
|
.live-results .search-result {
|
||||||
margin: 0;
|
margin: 0;
|
||||||
}
|
}
|
||||||
.live-results a {
|
|
||||||
border-top: none;
|
|
||||||
}
|
|
||||||
|
|
||||||
.live-results .search-result.error {
|
.live-results .search-result.error {
|
||||||
border: 1px solid #ccc;
|
|
||||||
border-top: none;
|
|
||||||
padding: 8px;
|
padding: 8px;
|
||||||
color: #888;
|
color: #888;
|
||||||
}
|
}
|
||||||
|
@ -397,16 +578,24 @@ input[type="search"] {
|
||||||
@media (min-width: 630px) {
|
@media (min-width: 630px) {
|
||||||
.search {
|
.search {
|
||||||
text-align: right;
|
text-align: right;
|
||||||
|
height: 38px;
|
||||||
|
position: relative;
|
||||||
}
|
}
|
||||||
|
|
||||||
.search input {
|
.search-widget-container {
|
||||||
max-width: 125px;
|
position: absolute;
|
||||||
|
right: 8px;
|
||||||
|
width: 300px;
|
||||||
|
box-shadow: 0 0 0 rgba(0,0,0,0.2);
|
||||||
|
transition: all 0.2s ease-in-out;
|
||||||
|
}
|
||||||
|
|
||||||
|
.focus .search-widget-container {
|
||||||
|
box-shadow: 0 5px 20px rgba(0,0,0,0.2);
|
||||||
}
|
}
|
||||||
|
|
||||||
.search .live-results {
|
.search .live-results {
|
||||||
position: absolute;
|
width: 100%;
|
||||||
right: 8px;
|
|
||||||
margin: 0 16px;
|
|
||||||
max-height: 0px;
|
max-height: 0px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -447,7 +636,7 @@ input[type="search"] {
|
||||||
|
|
||||||
text-align: center;
|
text-align: center;
|
||||||
background: #eee;
|
background: #eee;
|
||||||
box-shadow: 2px 2px 8px rgba(0,0,0, 0.25);
|
box-shadow: 0px 5px 20px rgba(0,0,0, 0.2);
|
||||||
}
|
}
|
||||||
|
|
||||||
.popup>.message {
|
.popup>.message {
|
||||||
|
@ -499,6 +688,14 @@ input[type="search"] {
|
||||||
display: block;
|
display: block;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.hero {
|
||||||
|
background: none;
|
||||||
|
color: initial;
|
||||||
|
|
||||||
|
/* Disable hack to force containing the children instead of collapsing marigins */
|
||||||
|
border: none;
|
||||||
|
}
|
||||||
|
|
||||||
h1, h2, h3, h4, h5, h6 {
|
h1, h2, h3, h4, h5, h6 {
|
||||||
/* This doesn't work at all, but it might start to! */
|
/* This doesn't work at all, but it might start to! */
|
||||||
break-after: avoid;
|
break-after: avoid;
|
||||||
|
@ -511,10 +708,23 @@ input[type="search"] {
|
||||||
font-weight: normal !important;
|
font-weight: normal !important;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
a[href^="http"]::after {
|
||||||
|
display: none;
|
||||||
|
}
|
||||||
|
|
||||||
article {
|
article {
|
||||||
margin: 0 auto;
|
margin: 0 auto;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
article>hr {
|
||||||
|
border-color: black;
|
||||||
|
}
|
||||||
|
|
||||||
|
h1 {
|
||||||
|
font-size: 22pt;
|
||||||
|
line-height: 33pt;
|
||||||
|
}
|
||||||
|
|
||||||
article, h2, h3, h4, h5, h6, .notice {
|
article, h2, h3, h4, h5, h6, .notice {
|
||||||
font-size: 12pt;
|
font-size: 12pt;
|
||||||
line-height: 18pt;
|
line-height: 18pt;
|
||||||
|
|
54
assets/test-themes.html
Normal file
54
assets/test-themes.html
Normal file
|
@ -0,0 +1,54 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>Test themes – Sausagewiki</title>
|
||||||
|
<link href="themes.css" rel="stylesheet">
|
||||||
|
<link href="style.css" rel="stylesheet">
|
||||||
|
<style>
|
||||||
|
.themed {
|
||||||
|
padding: 10px;
|
||||||
|
background: var(--theme-main);
|
||||||
|
color: var(--theme-text);
|
||||||
|
}
|
||||||
|
.link {
|
||||||
|
color: var(--theme-link);
|
||||||
|
}
|
||||||
|
.proto {
|
||||||
|
display: none;
|
||||||
|
}
|
||||||
|
#bar {
|
||||||
|
display: flex;
|
||||||
|
width: 100%;
|
||||||
|
}
|
||||||
|
#bar>div {
|
||||||
|
height: 30px;
|
||||||
|
flex-grow: 1;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div id="bar"></div>
|
||||||
|
<div class="proto">
|
||||||
|
<div class="themed">The <span class="link">quick</span> brown <span class="link">dog</span> jumps over the lazy log <span class="theme-name"></span></div>
|
||||||
|
<div class="themed"><input type=search placeholder=placeholder> <input type=search value="Bacon"></div>
|
||||||
|
</div>
|
||||||
|
<script>
|
||||||
|
const themes = ["red", "pink", "purple", "deep-purple", "indigo", "blue", "light-blue", "cyan", "teal", "green", "light-green", "lime", "yellow", "amber", "orange", "deep-orange", "brown", "gray", "blue-gray"];
|
||||||
|
const body = document.querySelector("body");
|
||||||
|
const proto = document.querySelector(".proto");
|
||||||
|
for (theme of themes) {
|
||||||
|
const block = proto.cloneNode(true);
|
||||||
|
block.className = `theme-${theme}`;
|
||||||
|
block.querySelector(".theme-name").textContent = theme;
|
||||||
|
body.appendChild(block);
|
||||||
|
}
|
||||||
|
|
||||||
|
const bar = document.querySelector("#bar");
|
||||||
|
for (theme of themes) {
|
||||||
|
const block = document.createElement("div");
|
||||||
|
block.className = `theme-${theme} themed`;
|
||||||
|
bar.appendChild(block);
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
134
assets/themes.css
Normal file
134
assets/themes.css
Normal file
|
@ -0,0 +1,134 @@
|
||||||
|
.theme-red {
|
||||||
|
--theme-main: #F44336;
|
||||||
|
--theme-text: white;
|
||||||
|
--theme-input: #E57373;
|
||||||
|
--theme-link: #FFF59D;
|
||||||
|
}
|
||||||
|
|
||||||
|
.theme-pink {
|
||||||
|
--theme-main: #E91E63;
|
||||||
|
--theme-text: white;
|
||||||
|
--theme-input: #F06292;
|
||||||
|
--theme-link: #FFF59D;
|
||||||
|
}
|
||||||
|
|
||||||
|
.theme-purple {
|
||||||
|
--theme-main: #9C27B0;
|
||||||
|
--theme-text: white;
|
||||||
|
--theme-input: #BA68C8;
|
||||||
|
--theme-link: #90CAF9;
|
||||||
|
}
|
||||||
|
|
||||||
|
.theme-deep-purple {
|
||||||
|
--theme-main: #673AB7;
|
||||||
|
--theme-text: white;
|
||||||
|
--theme-input: #9575CD;
|
||||||
|
--theme-link: #90CAF9;
|
||||||
|
}
|
||||||
|
|
||||||
|
.theme-indigo {
|
||||||
|
--theme-main: #3F51B5;
|
||||||
|
--theme-text: white;
|
||||||
|
--theme-input: #7986CB;
|
||||||
|
--theme-link: #90CAF9;
|
||||||
|
}
|
||||||
|
|
||||||
|
.theme-blue {
|
||||||
|
--theme-main: #2196F3;
|
||||||
|
--theme-text: white;
|
||||||
|
--theme-input: #64B5F6;
|
||||||
|
--theme-link: #90CAF9;
|
||||||
|
}
|
||||||
|
|
||||||
|
.theme-light-blue {
|
||||||
|
--theme-main: #03A9F4;
|
||||||
|
--theme-text: white;
|
||||||
|
--theme-input: #4FC3F7;
|
||||||
|
--theme-link: #90CAF9;
|
||||||
|
}
|
||||||
|
|
||||||
|
.theme-cyan {
|
||||||
|
--theme-main: #00ACC1;
|
||||||
|
--theme-text: white;
|
||||||
|
--theme-input: #26C6DA;
|
||||||
|
--theme-link: #90CAF9;
|
||||||
|
}
|
||||||
|
|
||||||
|
.theme-teal {
|
||||||
|
--theme-main: #009688;
|
||||||
|
--theme-text: white;
|
||||||
|
--theme-input: #4DB6AC;
|
||||||
|
--theme-link: #90CAF9;
|
||||||
|
}
|
||||||
|
|
||||||
|
.theme-green {
|
||||||
|
--theme-main: #4CAF50;
|
||||||
|
--theme-text: white;
|
||||||
|
--theme-input: #81C784;
|
||||||
|
--theme-link: #90CAF9;
|
||||||
|
}
|
||||||
|
|
||||||
|
.theme-light-green {
|
||||||
|
--theme-main: #7CB342;
|
||||||
|
--theme-text: white;
|
||||||
|
--theme-input: #9CCC65;
|
||||||
|
--theme-link: #90CAF9;
|
||||||
|
}
|
||||||
|
|
||||||
|
.theme-lime {
|
||||||
|
--theme-main: #C0CA33;
|
||||||
|
--theme-text: white;
|
||||||
|
--theme-input: #AFB42B;
|
||||||
|
--theme-link: #1976D2;
|
||||||
|
}
|
||||||
|
|
||||||
|
.theme-yellow {
|
||||||
|
--theme-main: #FDD835;
|
||||||
|
--theme-text: white;
|
||||||
|
--theme-input: #FBC02D;
|
||||||
|
--theme-link: #1976D2;
|
||||||
|
}
|
||||||
|
|
||||||
|
.theme-amber {
|
||||||
|
--theme-main: #FFB300;
|
||||||
|
--theme-text: white;
|
||||||
|
--theme-input: #FFA000;
|
||||||
|
--theme-link: #1976D2;
|
||||||
|
}
|
||||||
|
|
||||||
|
.theme-orange {
|
||||||
|
--theme-main: #FB8C00;
|
||||||
|
--theme-text: white;
|
||||||
|
--theme-input: #FFA726;
|
||||||
|
--theme-link: #FFF59D;
|
||||||
|
}
|
||||||
|
|
||||||
|
.theme-deep-orange {
|
||||||
|
--theme-main: #FF5722;
|
||||||
|
--theme-text: white;
|
||||||
|
--theme-input: #FF8A65;
|
||||||
|
--theme-link: #FFF59D;
|
||||||
|
}
|
||||||
|
|
||||||
|
.theme-brown {
|
||||||
|
--theme-main: #795548;
|
||||||
|
--theme-text: white;
|
||||||
|
--theme-input: #A1887F;
|
||||||
|
--theme-link: #FFF59D;
|
||||||
|
}
|
||||||
|
|
||||||
|
.theme-gray {
|
||||||
|
--theme-main: #9E9E9E;
|
||||||
|
--theme-text: white;
|
||||||
|
--theme-input: #E0E0E0;
|
||||||
|
--theme-link: #90CAF9;
|
||||||
|
}
|
||||||
|
|
||||||
|
.theme-blue-gray {
|
||||||
|
--theme-main: #607D8B;
|
||||||
|
--theme-text: white;
|
||||||
|
--theme-input: #90A4AE;
|
||||||
|
--theme-link: #90CAF9;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
45
build.rs
45
build.rs
|
@ -1,10 +1,9 @@
|
||||||
#[macro_use] extern crate quote;
|
#[macro_use]
|
||||||
#[macro_use] extern crate diesel;
|
extern crate diesel;
|
||||||
extern crate diesel_migrations;
|
|
||||||
extern crate walkdir;
|
|
||||||
|
|
||||||
use diesel::Connection;
|
|
||||||
use diesel::prelude::*;
|
use diesel::prelude::*;
|
||||||
|
use diesel::Connection;
|
||||||
|
use quote::quote;
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::prelude::*;
|
use std::io::prelude::*;
|
||||||
|
@ -15,31 +14,40 @@ use walkdir::WalkDir;
|
||||||
mod sqlfunc {
|
mod sqlfunc {
|
||||||
use diesel::sql_types::Text;
|
use diesel::sql_types::Text;
|
||||||
sql_function!(fn markdown_to_fts(text: Text) -> Text);
|
sql_function!(fn markdown_to_fts(text: Text) -> Text);
|
||||||
|
sql_function!(fn theme_from_str_hash(text: Text) -> Text);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let out_dir = env::var("OUT_DIR").expect("cargo must set OUT_DIR");
|
let out_dir = env::var("OUT_DIR").expect("cargo must set OUT_DIR");
|
||||||
let db_path = Path::new(&out_dir).join("build.db");
|
let db_path = Path::new(&out_dir).join("build.db");
|
||||||
let db_path = db_path.to_str().expect("Will only work for Unicode-representable paths");
|
let db_path = db_path
|
||||||
|
.to_str()
|
||||||
|
.expect("Will only work for Unicode-representable paths");
|
||||||
|
|
||||||
let _ignore_failure = std::fs::remove_file(db_path);
|
let _ignore_failure = std::fs::remove_file(db_path);
|
||||||
|
|
||||||
let connection = SqliteConnection::establish(db_path)
|
let connection = SqliteConnection::establish(db_path)
|
||||||
.expect(&format!("Error esablishing a database connection to {}", db_path));
|
.unwrap_or_else(|_| panic!("Error esablishing a database connection to {}", db_path));
|
||||||
|
|
||||||
// Integer is a dummy placeholder. Compiling fails when passing ().
|
// Integer is a dummy placeholder. Compiling fails when passing ().
|
||||||
diesel::expression::sql_literal::sql::<(diesel::sql_types::Integer)>("PRAGMA foreign_keys = ON")
|
diesel::expression::sql_literal::sql::<diesel::sql_types::Integer>("PRAGMA foreign_keys = ON")
|
||||||
.execute(&connection)
|
.execute(&connection)
|
||||||
.expect("Should be able to enable foreign keys");
|
.expect("Should be able to enable foreign keys");
|
||||||
|
|
||||||
sqlfunc::markdown_to_fts::register_impl(&connection, |_: String| -> String { unreachable!() }).unwrap();
|
sqlfunc::markdown_to_fts::register_impl(&connection, |_: String| -> String { unreachable!() })
|
||||||
|
.unwrap();
|
||||||
|
sqlfunc::theme_from_str_hash::register_impl(&connection, |_: String| -> String {
|
||||||
|
unreachable!()
|
||||||
|
})
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
diesel_migrations::run_pending_migrations(&connection).unwrap();
|
diesel_migrations::run_pending_migrations(&connection).unwrap();
|
||||||
|
|
||||||
let infer_schema_path = Path::new(&out_dir).join("infer_schema.rs");
|
let infer_schema_path = Path::new(&out_dir).join("infer_schema.rs");
|
||||||
let mut file = File::create(infer_schema_path).expect("Unable to open file for writing");
|
let mut file = File::create(infer_schema_path).expect("Unable to open file for writing");
|
||||||
|
|
||||||
file.write_all(quote! {
|
file.write_all(
|
||||||
|
quote! {
|
||||||
mod __diesel_infer_schema_articles {
|
mod __diesel_infer_schema_articles {
|
||||||
infer_table_from_schema!(#db_path, "articles");
|
infer_table_from_schema!(#db_path, "articles");
|
||||||
}
|
}
|
||||||
|
@ -49,18 +57,21 @@ fn main() {
|
||||||
infer_table_from_schema!(#db_path, "article_revisions");
|
infer_table_from_schema!(#db_path, "article_revisions");
|
||||||
}
|
}
|
||||||
pub use self::__diesel_infer_schema_article_revisions::*;
|
pub use self::__diesel_infer_schema_article_revisions::*;
|
||||||
}.as_str().as_bytes()).expect("Unable to write to file");
|
}
|
||||||
|
.to_string()
|
||||||
|
.as_bytes(),
|
||||||
|
)
|
||||||
|
.expect("Unable to write to file");
|
||||||
|
|
||||||
for entry in WalkDir::new("migrations").into_iter().filter_map(|e| e.ok()) {
|
for entry in WalkDir::new("migrations")
|
||||||
|
.into_iter()
|
||||||
|
.filter_map(|e| e.ok())
|
||||||
|
{
|
||||||
println!("cargo:rerun-if-changed={}", entry.path().display());
|
println!("cargo:rerun-if-changed={}", entry.path().display());
|
||||||
}
|
}
|
||||||
|
|
||||||
// For build_config.rs
|
// For build_config.rs
|
||||||
for env_var in &[
|
for env_var in &["CONTINUOUS_INTEGRATION", "TRAVIS_BRANCH", "TRAVIS_COMMIT"] {
|
||||||
"CONTINUOUS_INTEGRATION",
|
|
||||||
"TRAVIS_BRANCH",
|
|
||||||
"TRAVIS_COMMIT",
|
|
||||||
] {
|
|
||||||
println!("cargo:rerun-if-env-changed={}", env_var);
|
println!("cargo:rerun-if-env-changed={}", env_var);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,3 +5,4 @@ This is a list of people who have contributed to this project.
|
||||||
- Magnus Hoff (maghoff@gmail.com)
|
- Magnus Hoff (maghoff@gmail.com)
|
||||||
- Johannes Hoff (johanneshoff@gmail.com)
|
- Johannes Hoff (johanneshoff@gmail.com)
|
||||||
- Konstantin Yegupov (kyegupov4@gmail.com)
|
- Konstantin Yegupov (kyegupov4@gmail.com)
|
||||||
|
- cmal (paul@cmal.info)
|
|
@ -1,11 +1,13 @@
|
||||||
#![recursion_limit="128"]
|
#![recursion_limit = "128"]
|
||||||
|
|
||||||
#[macro_use] extern crate quote;
|
#[macro_use]
|
||||||
#[macro_use] extern crate serde_derive;
|
extern crate quote;
|
||||||
|
#[macro_use]
|
||||||
|
extern crate serde_derive;
|
||||||
extern crate base64;
|
extern crate base64;
|
||||||
extern crate proc_macro;
|
extern crate proc_macro;
|
||||||
extern crate serde_json;
|
|
||||||
extern crate serde;
|
extern crate serde;
|
||||||
|
extern crate serde_json;
|
||||||
extern crate sha2;
|
extern crate sha2;
|
||||||
extern crate syn;
|
extern crate syn;
|
||||||
|
|
||||||
|
|
|
@ -2,13 +2,10 @@ use std::fs::File;
|
||||||
|
|
||||||
use proc_macro::TokenStream;
|
use proc_macro::TokenStream;
|
||||||
use quote;
|
use quote;
|
||||||
use serde_json;
|
|
||||||
use serde::de::IgnoredAny;
|
use serde::de::IgnoredAny;
|
||||||
|
use serde_json;
|
||||||
|
|
||||||
const SOURCES: &[&str] = &[
|
const SOURCES: &[&str] = &["src/licenses/license-hound.json", "src/licenses/other.json"];
|
||||||
"src/licenses/license-hound.json",
|
|
||||||
"src/licenses/other.json",
|
|
||||||
];
|
|
||||||
|
|
||||||
#[derive(Debug, Copy, Clone, Deserialize)]
|
#[derive(Debug, Copy, Clone, Deserialize)]
|
||||||
pub enum LicenseId {
|
pub enum LicenseId {
|
||||||
|
@ -22,7 +19,7 @@ impl LicenseId {
|
||||||
fn include_notice(&self) -> bool {
|
fn include_notice(&self) -> bool {
|
||||||
use self::LicenseId::*;
|
use self::LicenseId::*;
|
||||||
match self {
|
match self {
|
||||||
&Mpl2 => false,
|
Mpl2 => false,
|
||||||
_ => true,
|
_ => true,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -32,10 +29,10 @@ impl quote::ToTokens for LicenseId {
|
||||||
fn to_tokens(&self, tokens: &mut quote::Tokens) {
|
fn to_tokens(&self, tokens: &mut quote::Tokens) {
|
||||||
use self::LicenseId::*;
|
use self::LicenseId::*;
|
||||||
tokens.append(match self {
|
tokens.append(match self {
|
||||||
&Bsd3Clause => "Bsd3Clause",
|
Bsd3Clause => "Bsd3Clause",
|
||||||
&Mit => "Mit",
|
Mit => "Mit",
|
||||||
&Mpl2 => "Mpl2",
|
Mpl2 => "Mpl2",
|
||||||
&Ofl11 => "Ofl11",
|
Ofl11 => "Ofl11",
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -56,12 +53,16 @@ struct LicenseReport {
|
||||||
impl quote::ToTokens for LicenseReport {
|
impl quote::ToTokens for LicenseReport {
|
||||||
fn to_tokens(&self, tokens: &mut quote::Tokens) {
|
fn to_tokens(&self, tokens: &mut quote::Tokens) {
|
||||||
let c: &LicenseDescription = self.conclusion.as_ref().unwrap();
|
let c: &LicenseDescription = self.conclusion.as_ref().unwrap();
|
||||||
let (name, link, copyright, license) =
|
let (name, link, copyright, license) = (
|
||||||
(&self.package_name, &c.link, &c.copyright_notice, &c.chosen_license);
|
&self.package_name,
|
||||||
|
&c.link,
|
||||||
|
&c.copyright_notice,
|
||||||
|
&c.chosen_license,
|
||||||
|
);
|
||||||
|
|
||||||
let link = match link {
|
let link = match *link {
|
||||||
&Some(ref link) => quote! { Some(#link) },
|
Some(ref link) => quote! { Some(#link) },
|
||||||
&None => quote! { None },
|
None => quote! { None },
|
||||||
};
|
};
|
||||||
|
|
||||||
let copyright = match license.include_notice() {
|
let copyright = match license.include_notice() {
|
||||||
|
@ -85,7 +86,10 @@ pub fn licenses(_input: TokenStream) -> TokenStream {
|
||||||
.iter()
|
.iter()
|
||||||
.map(|x| -> Vec<LicenseReport> { serde_json::from_reader(File::open(x).unwrap()).unwrap() })
|
.map(|x| -> Vec<LicenseReport> { serde_json::from_reader(File::open(x).unwrap()).unwrap() })
|
||||||
.map(|x| x.into_iter().filter(|x| x.conclusion.is_ok()))
|
.map(|x| x.into_iter().filter(|x| x.conclusion.is_ok()))
|
||||||
.fold(vec![], |mut a, b| { a.extend(b); a });
|
.fold(vec![], |mut a, b| {
|
||||||
|
a.extend(b);
|
||||||
|
a
|
||||||
|
});
|
||||||
|
|
||||||
license_infos.sort_unstable_by_key(|x| x.package_name.to_lowercase());
|
license_infos.sort_unstable_by_key(|x| x.package_name.to_lowercase());
|
||||||
|
|
||||||
|
|
|
@ -10,30 +10,29 @@ fn user_crate_root() -> PathBuf {
|
||||||
std::env::current_dir().expect("Unable to get current directory")
|
std::env::current_dir().expect("Unable to get current directory")
|
||||||
}
|
}
|
||||||
|
|
||||||
fn find_attr<'a>(attrs: &'a Vec<syn::Attribute>, name: &str) -> Option<&'a str> {
|
fn find_attr<'a>(attrs: &'a [syn::Attribute], name: &str) -> Option<&'a str> {
|
||||||
attrs.iter()
|
attrs
|
||||||
|
.iter()
|
||||||
.find(|&x| x.name() == name)
|
.find(|&x| x.name() == name)
|
||||||
.and_then(|ref attr| match &attr.value {
|
.and_then(|attr| match attr.value {
|
||||||
&syn::MetaItem::NameValue(_, syn::Lit::Str(ref template, _)) => Some(template),
|
syn::MetaItem::NameValue(_, syn::Lit::Str(ref template, _)) => Some(template),
|
||||||
_ => None
|
_ => None,
|
||||||
})
|
})
|
||||||
.map(|x| x.as_ref())
|
.map(|x| x.as_ref())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn buf_file<P: AsRef<Path>>(filename: P) -> Vec<u8> {
|
fn buf_file<P: AsRef<Path>>(filename: P) -> Vec<u8> {
|
||||||
let mut f = File::open(filename)
|
let mut f = File::open(filename).expect("Unable to open file for reading");
|
||||||
.expect("Unable to open file for reading");
|
|
||||||
|
|
||||||
let mut buf = Vec::new();
|
let mut buf = Vec::new();
|
||||||
f.read_to_end(&mut buf)
|
f.read_to_end(&mut buf).expect("Unable to read file");
|
||||||
.expect("Unable to read file");
|
|
||||||
|
|
||||||
buf
|
buf
|
||||||
}
|
}
|
||||||
|
|
||||||
fn calculate_checksum<P: AsRef<Path>>(filename: P) -> String {
|
fn calculate_checksum<P: AsRef<Path>>(filename: P) -> String {
|
||||||
use base64::*;
|
use base64::*;
|
||||||
use sha2::{Sha256, Digest};
|
use sha2::{Digest, Sha256};
|
||||||
|
|
||||||
encode_config(&Sha256::digest(&buf_file(filename)), URL_SAFE)
|
encode_config(&Sha256::digest(&buf_file(filename)), URL_SAFE)
|
||||||
}
|
}
|
||||||
|
@ -42,15 +41,24 @@ pub fn static_resource(input: TokenStream) -> TokenStream {
|
||||||
let s = input.to_string();
|
let s = input.to_string();
|
||||||
let ast = syn::parse_macro_input(&s).unwrap();
|
let ast = syn::parse_macro_input(&s).unwrap();
|
||||||
|
|
||||||
let filename = find_attr(&ast.attrs, "filename")
|
let filename =
|
||||||
.expect("The `filename` attribute must be specified");
|
find_attr(&ast.attrs, "filename").expect("The `filename` attribute must be specified");
|
||||||
let abs_filename = user_crate_root().join(filename);
|
let abs_filename = user_crate_root().join(filename);
|
||||||
let abs_filename = abs_filename.to_str().expect("Absolute file path must be valid Unicode");
|
let abs_filename = abs_filename
|
||||||
|
.to_str()
|
||||||
|
.expect("Absolute file path must be valid Unicode");
|
||||||
|
|
||||||
let checksum = calculate_checksum(&abs_filename);
|
let checksum = calculate_checksum(&abs_filename);
|
||||||
|
|
||||||
let mime = find_attr(&ast.attrs, "mime")
|
let path: &Path = filename.as_ref();
|
||||||
.expect("The `mime` attribute must be specified");
|
let resource_name = format!(
|
||||||
|
"{}-{}.{}",
|
||||||
|
path.file_stem().unwrap().to_str().unwrap(),
|
||||||
|
checksum,
|
||||||
|
path.extension().unwrap().to_str().unwrap()
|
||||||
|
);
|
||||||
|
|
||||||
|
let mime = find_attr(&ast.attrs, "mime").expect("The `mime` attribute must be specified");
|
||||||
|
|
||||||
let name = &ast.ident;
|
let name = &ast.ident;
|
||||||
let (impl_generics, ty_generics, where_clause) = ast.generics.split_for_impl();
|
let (impl_generics, ty_generics, where_clause) = ast.generics.split_for_impl();
|
||||||
|
@ -90,12 +98,12 @@ pub fn static_resource(input: TokenStream) -> TokenStream {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl #impl_generics #name #ty_generics #where_clause {
|
impl #impl_generics #name #ty_generics #where_clause {
|
||||||
pub fn checksum() -> &'static str {
|
pub fn resource_name() -> &'static str {
|
||||||
#checksum
|
#resource_name
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn etag() -> ::hyper::header::EntityTag {
|
pub fn etag() -> ::hyper::header::EntityTag {
|
||||||
::hyper::header::EntityTag::new(false, Self::checksum().to_owned())
|
::hyper::header::EntityTag::new(false, #checksum.to_owned())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -0,0 +1,7 @@
|
||||||
|
ALTER TABLE article_revisions ADD COLUMN theme TEXT NOT NULL CHECK (theme IN (
|
||||||
|
'red', 'pink', 'purple', 'deep-purple', 'indigo', 'blue', 'light-blue',
|
||||||
|
'cyan', 'teal', 'green', 'light-green', 'lime', 'yellow', 'amber',
|
||||||
|
'orange', 'deep-orange', 'brown', 'gray', 'blue-gray'
|
||||||
|
)) DEFAULT 'red';
|
||||||
|
|
||||||
|
UPDATE article_revisions SET theme=theme_from_str_hash(title);
|
|
@ -72,3 +72,4 @@ Command line arguments
|
||||||
Sausagewiki will create an SQLite database file with the filename given in the
|
Sausagewiki will create an SQLite database file with the filename given in the
|
||||||
`DATABASE` parameter and open an HTTP server bound to the configured address,
|
`DATABASE` parameter and open an HTTP server bound to the configured address,
|
||||||
`<address>:<port>`.
|
`<address>:<port>`.
|
||||||
|
|
106
src/assets.rs
106
src/assets.rs
|
@ -1,24 +1,90 @@
|
||||||
use futures::Future;
|
#[cfg(not(feature = "dynamic-assets"))]
|
||||||
use web::{Resource, ResponseFuture};
|
mod static_assets {
|
||||||
|
use crate::web::{Resource, ResponseFuture};
|
||||||
|
use futures::Future;
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
#[derive(StaticResource)]
|
// The CSS should be built to a single CSS file at compile time
|
||||||
#[filename = "assets/style.css"]
|
#[derive(StaticResource)]
|
||||||
#[mime = "text/css"]
|
#[filename = "assets/themes.css"]
|
||||||
pub struct StyleCss;
|
#[mime = "text/css"]
|
||||||
|
pub struct ThemesCss;
|
||||||
|
|
||||||
#[derive(StaticResource)]
|
#[derive(StaticResource)]
|
||||||
#[filename = "assets/script.js"]
|
#[filename = "assets/style.css"]
|
||||||
#[mime = "application/javascript"]
|
#[mime = "text/css"]
|
||||||
pub struct ScriptJs;
|
pub struct StyleCss;
|
||||||
|
|
||||||
#[derive(StaticResource)]
|
#[derive(StaticResource)]
|
||||||
#[filename = "assets/search.js"]
|
#[filename = "assets/script.js"]
|
||||||
#[mime = "application/javascript"]
|
#[mime = "application/javascript"]
|
||||||
pub struct SearchJs;
|
pub struct ScriptJs;
|
||||||
|
|
||||||
// SIL Open Font License 1.1: http://scripts.sil.org/cms/scripts/page.php?site_id=nrsi&id=OFL
|
#[derive(StaticResource)]
|
||||||
// Copyright 2015 The Amatic SC Project Authors (contact@sansoxygen.com)
|
#[filename = "assets/search.js"]
|
||||||
#[derive(StaticResource)]
|
#[mime = "application/javascript"]
|
||||||
#[filename = "assets/amatic-sc-v9-latin-regular.woff"]
|
pub struct SearchJs;
|
||||||
#[mime = "application/font-woff"]
|
|
||||||
pub struct AmaticFont;
|
// SIL Open Font License 1.1: http://scripts.sil.org/cms/scripts/page.php?site_id=nrsi&id=OFL
|
||||||
|
// Copyright 2015 The Amatic SC Project Authors (contact@sansoxygen.com)
|
||||||
|
// #[derive(StaticResource)]
|
||||||
|
// #[filename = "assets/amatic-sc-v9-latin-regular.woff"]
|
||||||
|
// #[mime = "application/font-woff"]
|
||||||
|
// pub struct AmaticFont;
|
||||||
|
|
||||||
|
type BoxResource = Box<dyn Resource + Sync + Send>;
|
||||||
|
type ResourceFn = Box<dyn Fn() -> BoxResource + Sync + Send>;
|
||||||
|
lazy_static! {
|
||||||
|
pub static ref ASSETS_MAP: HashMap<&'static str, ResourceFn> = hashmap!{
|
||||||
|
// The CSS should be built to a single CSS file at compile time
|
||||||
|
ThemesCss::resource_name() =>
|
||||||
|
Box::new(|| Box::new(ThemesCss) as BoxResource) as ResourceFn,
|
||||||
|
|
||||||
|
StyleCss::resource_name() =>
|
||||||
|
Box::new(|| Box::new(StyleCss) as BoxResource) as ResourceFn,
|
||||||
|
|
||||||
|
ScriptJs::resource_name() =>
|
||||||
|
Box::new(|| Box::new(ScriptJs) as BoxResource) as ResourceFn,
|
||||||
|
|
||||||
|
SearchJs::resource_name() =>
|
||||||
|
Box::new(|| Box::new(SearchJs) as BoxResource) as ResourceFn,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(not(feature = "dynamic-assets"))]
|
||||||
|
pub use self::static_assets::*;
|
||||||
|
|
||||||
|
#[cfg(feature = "dynamic-assets")]
|
||||||
|
mod dynamic_assets {
|
||||||
|
pub struct ThemesCss;
|
||||||
|
impl ThemesCss {
|
||||||
|
pub fn resource_name() -> &'static str {
|
||||||
|
"themes.css"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct StyleCss;
|
||||||
|
impl StyleCss {
|
||||||
|
pub fn resource_name() -> &'static str {
|
||||||
|
"style.css"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct ScriptJs;
|
||||||
|
impl ScriptJs {
|
||||||
|
pub fn resource_name() -> &'static str {
|
||||||
|
"script.js"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct SearchJs;
|
||||||
|
impl SearchJs {
|
||||||
|
pub fn resource_name() -> &'static str {
|
||||||
|
"search.js"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "dynamic-assets")]
|
||||||
|
pub use self::dynamic_assets::*;
|
||||||
|
|
|
@ -7,9 +7,12 @@ pub const PROJECT_NAME: &str = env!("CARGO_PKG_NAME");
|
||||||
|
|
||||||
const SOFT_HYPHEN: &str = "\u{00AD}";
|
const SOFT_HYPHEN: &str = "\u{00AD}";
|
||||||
|
|
||||||
|
#[cfg(all(not(debug_assertions), feature = "dynamic-assets"))]
|
||||||
|
compile_error!("dynamic-assets must not be used for production");
|
||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
pub static ref VERSION: String = || -> String {
|
pub static ref VERSION: String = || -> String {
|
||||||
let mut components = Vec::<String>::new();
|
let mut components = vec![];
|
||||||
|
|
||||||
#[cfg(debug_assertions)]
|
#[cfg(debug_assertions)]
|
||||||
components.push("debug".into());
|
components.push("debug".into());
|
||||||
|
@ -17,7 +20,10 @@ lazy_static! {
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
components.push("test".into());
|
components.push("test".into());
|
||||||
|
|
||||||
if let None = option_env!("CONTINUOUS_INTEGRATION") {
|
#[cfg(feature = "dynamic-assets")]
|
||||||
|
components.push("dynamic-assets".into());
|
||||||
|
|
||||||
|
if option_env!("CONTINUOUS_INTEGRATION").is_none() {
|
||||||
components.push("local-build".into());
|
components.push("local-build".into());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -26,26 +32,22 @@ lazy_static! {
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(commit) = option_env!("TRAVIS_COMMIT") {
|
if let Some(commit) = option_env!("TRAVIS_COMMIT") {
|
||||||
components.push(format!("commit:{}",
|
components.push(format!(
|
||||||
|
"commit:{}",
|
||||||
commit
|
commit
|
||||||
.as_bytes()
|
.as_bytes()
|
||||||
.chunks(4)
|
.chunks(4)
|
||||||
.map(|x|
|
.map(|x| String::from_utf8(x.to_owned()).unwrap_or_else(|_| String::new()))
|
||||||
String::from_utf8(x.to_owned())
|
|
||||||
.unwrap_or_else(|_| String::new())
|
|
||||||
)
|
|
||||||
.collect::<Vec<_>>()
|
.collect::<Vec<_>>()
|
||||||
.join(SOFT_HYPHEN)
|
.join(SOFT_HYPHEN)
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
if components.len() > 0 {
|
if !components.is_empty() {
|
||||||
format!("{} ({})", env!("CARGO_PKG_VERSION"), components.join(" "))
|
format!("{} ({})", env!("CARGO_PKG_VERSION"), components.join(" "))
|
||||||
} else {
|
} else {
|
||||||
env!("CARGO_PKG_VERSION").to_string()
|
env!("CARGO_PKG_VERSION").to_string()
|
||||||
}
|
}
|
||||||
}();
|
}();
|
||||||
|
pub static ref HTTP_SERVER: String = format!("{}/{}", PROJECT_NAME, VERSION.as_str());
|
||||||
pub static ref HTTP_SERVER: String =
|
|
||||||
format!("{}/{}", PROJECT_NAME, VERSION.as_str());
|
|
||||||
}
|
}
|
||||||
|
|
74
src/db.rs
74
src/db.rs
|
@ -1,10 +1,11 @@
|
||||||
use diesel::prelude::*;
|
|
||||||
use diesel::expression::sql_literal::sql;
|
use diesel::expression::sql_literal::sql;
|
||||||
|
use diesel::prelude::*;
|
||||||
use diesel::sql_types::*;
|
use diesel::sql_types::*;
|
||||||
use r2d2::{CustomizeConnection, Pool};
|
use r2d2::{CustomizeConnection, Pool};
|
||||||
use r2d2_diesel::{self, ConnectionManager};
|
use r2d2_diesel::{self, ConnectionManager};
|
||||||
|
|
||||||
use rendering;
|
use crate::rendering;
|
||||||
|
use crate::theme;
|
||||||
|
|
||||||
embed_migrations!();
|
embed_migrations!();
|
||||||
|
|
||||||
|
@ -12,27 +13,35 @@ embed_migrations!();
|
||||||
struct SqliteInitializer;
|
struct SqliteInitializer;
|
||||||
|
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
mod sqlfunc {
|
pub mod sqlfunc {
|
||||||
use diesel::sql_types::Text;
|
use diesel::sql_types::Text;
|
||||||
sql_function!(fn markdown_to_fts(text: Text) -> Text);
|
sql_function!(fn markdown_to_fts(text: Text) -> Text);
|
||||||
|
sql_function!(fn theme_from_str_hash(text: Text) -> Text);
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CustomizeConnection<SqliteConnection, r2d2_diesel::Error> for SqliteInitializer {
|
impl CustomizeConnection<SqliteConnection, r2d2_diesel::Error> for SqliteInitializer {
|
||||||
fn on_acquire(&self, conn: &mut SqliteConnection) -> Result<(), r2d2_diesel::Error> {
|
fn on_acquire(&self, conn: &mut SqliteConnection) -> Result<(), r2d2_diesel::Error> {
|
||||||
sql::<(Integer)>("PRAGMA foreign_keys = ON")
|
sql::<Integer>("PRAGMA foreign_keys = ON")
|
||||||
.execute(conn)
|
.execute(conn)
|
||||||
.map_err(|x| r2d2_diesel::Error::QueryError(x))?;
|
.map_err(r2d2_diesel::Error::QueryError)?;
|
||||||
|
|
||||||
sqlfunc::markdown_to_fts::register_impl(
|
sqlfunc::markdown_to_fts::register_impl(conn, |text: String| {
|
||||||
conn,
|
rendering::render_markdown_for_fts(&text)
|
||||||
|text: String| rendering::render_markdown_for_fts(&text)
|
})
|
||||||
).map_err(|x| r2d2_diesel::Error::QueryError(x))?;
|
.map_err(r2d2_diesel::Error::QueryError)?;
|
||||||
|
|
||||||
|
sqlfunc::theme_from_str_hash::register_impl(conn, |title: String| {
|
||||||
|
theme::theme_from_str_hash(&title)
|
||||||
|
})
|
||||||
|
.map_err(r2d2_diesel::Error::QueryError)?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn create_pool<S: Into<String>>(connection_string: S) -> Result<Pool<ConnectionManager<SqliteConnection>>, Box<::std::error::Error>> {
|
pub fn create_pool<S: Into<String>>(
|
||||||
|
connection_string: S,
|
||||||
|
) -> Result<Pool<ConnectionManager<SqliteConnection>>, Box<dyn (::std::error::Error)>> {
|
||||||
let manager = ConnectionManager::<SqliteConnection>::new(connection_string);
|
let manager = ConnectionManager::<SqliteConnection>::new(connection_string);
|
||||||
let pool = Pool::builder()
|
let pool = Pool::builder()
|
||||||
.connection_customizer(Box::new(SqliteInitializer {}))
|
.connection_customizer(Box::new(SqliteInitializer {}))
|
||||||
|
@ -53,3 +62,48 @@ pub fn test_connection() -> SqliteConnection {
|
||||||
|
|
||||||
conn
|
conn
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
use diesel::sql_query;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn markdown_to_fts() {
|
||||||
|
let conn = test_connection();
|
||||||
|
|
||||||
|
#[derive(QueryableByName, PartialEq, Eq, Debug)]
|
||||||
|
struct Row {
|
||||||
|
#[sql_type = "Text"]
|
||||||
|
text: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
let res = sql_query("SELECT markdown_to_fts('[link](url)') as text")
|
||||||
|
.load::<Row>(&conn)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let expected = rendering::render_markdown_for_fts("[link](url)");
|
||||||
|
|
||||||
|
assert_eq!(expected, res[0].text);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn theme_from_str_hash() {
|
||||||
|
let conn = test_connection();
|
||||||
|
|
||||||
|
#[derive(QueryableByName, PartialEq, Eq, Debug)]
|
||||||
|
struct Row {
|
||||||
|
#[sql_type = "Text"]
|
||||||
|
theme: theme::Theme,
|
||||||
|
}
|
||||||
|
|
||||||
|
let res = sql_query("SELECT theme_from_str_hash('Bartefjes') as theme")
|
||||||
|
.load::<Row>(&conn)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let expected = theme::theme_from_str_hash("Bartefjes");
|
||||||
|
|
||||||
|
assert_eq!(expected, res[0].theme);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
74
src/lib.rs
74
src/lib.rs
|
@ -1,31 +1,33 @@
|
||||||
#![recursion_limit="128"] // for diesel's infer_schema!
|
#![allow(clippy::into_iter_on_ref)]
|
||||||
|
#![allow(clippy::vec_init_then_push)]
|
||||||
|
#![recursion_limit = "128"]
|
||||||
|
// for diesel's infer_schema!
|
||||||
|
|
||||||
#[cfg(test)] #[macro_use] extern crate matches;
|
#[cfg(test)]
|
||||||
#[cfg(test)] #[macro_use] extern crate indoc;
|
#[macro_use]
|
||||||
|
extern crate matches;
|
||||||
#[macro_use] extern crate bart_derive;
|
#[macro_use]
|
||||||
#[macro_use] extern crate codegen;
|
extern crate bart_derive;
|
||||||
#[macro_use] #[allow(deprecated)] extern crate diesel_infer_schema;
|
#[macro_use]
|
||||||
#[macro_use] extern crate diesel_migrations;
|
extern crate codegen;
|
||||||
#[macro_use] extern crate diesel;
|
#[macro_use]
|
||||||
#[macro_use] extern crate hyper;
|
#[allow(clippy::useless_attribute)]
|
||||||
#[macro_use] extern crate lazy_static;
|
#[allow(deprecated)]
|
||||||
#[macro_use] extern crate maplit;
|
extern crate diesel_infer_schema;
|
||||||
#[macro_use] extern crate serde_derive;
|
#[macro_use]
|
||||||
|
extern crate diesel_migrations;
|
||||||
extern crate chrono;
|
#[macro_use]
|
||||||
extern crate diff;
|
extern crate diesel;
|
||||||
extern crate futures_cpupool;
|
#[macro_use]
|
||||||
extern crate futures;
|
extern crate hyper;
|
||||||
extern crate percent_encoding;
|
#[macro_use]
|
||||||
extern crate pulldown_cmark;
|
extern crate lazy_static;
|
||||||
extern crate r2d2_diesel;
|
#[macro_use]
|
||||||
extern crate r2d2;
|
extern crate maplit;
|
||||||
extern crate serde_json;
|
#[macro_use]
|
||||||
extern crate serde_urlencoded;
|
extern crate serde_derive;
|
||||||
extern crate serde;
|
#[macro_use]
|
||||||
extern crate slug;
|
extern crate serde_plain;
|
||||||
extern crate titlecase;
|
|
||||||
|
|
||||||
use std::net::{IpAddr, SocketAddr};
|
use std::net::{IpAddr, SocketAddr};
|
||||||
|
|
||||||
|
@ -40,22 +42,26 @@ mod resources;
|
||||||
mod schema;
|
mod schema;
|
||||||
mod site;
|
mod site;
|
||||||
mod state;
|
mod state;
|
||||||
|
mod theme;
|
||||||
mod web;
|
mod web;
|
||||||
mod wiki_lookup;
|
mod wiki_lookup;
|
||||||
|
|
||||||
pub fn main(db_file: String, bind_host: IpAddr, bind_port: u16, trust_identity: bool) -> Result<(), Box<std::error::Error>> {
|
pub fn main(
|
||||||
|
db_file: String,
|
||||||
|
bind_host: IpAddr,
|
||||||
|
bind_port: u16,
|
||||||
|
trust_identity: bool,
|
||||||
|
) -> Result<(), Box<dyn std::error::Error>> {
|
||||||
let db_pool = db::create_pool(db_file)?;
|
let db_pool = db::create_pool(db_file)?;
|
||||||
let cpu_pool = futures_cpupool::CpuPool::new_num_cpus();
|
let cpu_pool = futures_cpupool::CpuPool::new_num_cpus();
|
||||||
|
|
||||||
let state = state::State::new(db_pool, cpu_pool);
|
let state = state::State::new(db_pool, cpu_pool);
|
||||||
let lookup = wiki_lookup::WikiLookup::new(state, trust_identity);
|
let lookup = wiki_lookup::WikiLookup::new(state, trust_identity);
|
||||||
|
|
||||||
let server =
|
let server = hyper::server::Http::new()
|
||||||
hyper::server::Http::new()
|
.bind(&SocketAddr::new(bind_host, bind_port), move || {
|
||||||
.bind(
|
Ok(site::Site::new(lookup.clone(), trust_identity))
|
||||||
&SocketAddr::new(bind_host, bind_port),
|
})?;
|
||||||
move || Ok(site::Site::new(lookup.clone(), trust_identity))
|
|
||||||
)?;
|
|
||||||
|
|
||||||
println!("Listening on http://{}", server.local_addr().unwrap());
|
println!("Listening on http://{}", server.local_addr().unwrap());
|
||||||
|
|
||||||
|
|
56
src/main.rs
56
src/main.rs
|
@ -1,11 +1,10 @@
|
||||||
#[macro_use] extern crate lazy_static;
|
#[macro_use]
|
||||||
extern crate clap;
|
extern crate lazy_static;
|
||||||
extern crate sausagewiki;
|
|
||||||
|
|
||||||
use std::net::IpAddr;
|
use std::net::IpAddr;
|
||||||
|
|
||||||
mod build_config;
|
mod build_config;
|
||||||
use build_config::*;
|
use crate::build_config::*;
|
||||||
|
|
||||||
const DATABASE: &str = "DATABASE";
|
const DATABASE: &str = "DATABASE";
|
||||||
const TRUST_IDENTITY: &str = "trust-identity";
|
const TRUST_IDENTITY: &str = "trust-identity";
|
||||||
|
@ -18,52 +17,61 @@ fn args<'a>() -> clap::ArgMatches<'a> {
|
||||||
App::new(PROJECT_NAME)
|
App::new(PROJECT_NAME)
|
||||||
.version(VERSION.as_str())
|
.version(VERSION.as_str())
|
||||||
.about(env!("CARGO_PKG_DESCRIPTION"))
|
.about(env!("CARGO_PKG_DESCRIPTION"))
|
||||||
.arg(Arg::with_name(DATABASE)
|
.arg(
|
||||||
|
Arg::with_name(DATABASE)
|
||||||
.help("Sets the database file to use")
|
.help("Sets the database file to use")
|
||||||
.required(true))
|
.required(true),
|
||||||
.arg(Arg::with_name(PORT)
|
)
|
||||||
|
.arg(
|
||||||
|
Arg::with_name(PORT)
|
||||||
.help("Sets the listening port")
|
.help("Sets the listening port")
|
||||||
.short("p")
|
.short("p")
|
||||||
.long(PORT)
|
.long(PORT)
|
||||||
.default_value("8080")
|
.default_value("8080")
|
||||||
.validator(|x| match x.parse::<u16>() {
|
.validator(|x| match x.parse::<u16>() {
|
||||||
Ok(_) => Ok(()),
|
Ok(_) => Ok(()),
|
||||||
Err(_) => Err("Must be an integer in the range [0, 65535]".into())
|
Err(_) => Err("Must be an integer in the range [0, 65535]".into()),
|
||||||
})
|
})
|
||||||
.takes_value(true))
|
.takes_value(true),
|
||||||
.arg(Arg::with_name(ADDRESS)
|
)
|
||||||
|
.arg(
|
||||||
|
Arg::with_name(ADDRESS)
|
||||||
.help("Sets the IP address to bind to")
|
.help("Sets the IP address to bind to")
|
||||||
.short("a")
|
.short("a")
|
||||||
.long(ADDRESS)
|
.long(ADDRESS)
|
||||||
.default_value("127.0.0.1")
|
.default_value("127.0.0.1")
|
||||||
.validator(|x| match x.parse::<IpAddr>() {
|
.validator(|x| match x.parse::<IpAddr>() {
|
||||||
Ok(_) => Ok(()),
|
Ok(_) => Ok(()),
|
||||||
Err(_) => Err("Must be a valid IP address".into())
|
Err(_) => Err("Must be a valid IP address".into()),
|
||||||
})
|
})
|
||||||
.takes_value(true))
|
.takes_value(true),
|
||||||
.arg(Arg::with_name(TRUST_IDENTITY)
|
)
|
||||||
.help("Trust the value in the X-Identity header to be an \
|
.arg(
|
||||||
|
Arg::with_name(TRUST_IDENTITY)
|
||||||
|
.help(
|
||||||
|
"Trust the value in the X-Identity header to be an \
|
||||||
authenticated username. This only makes sense when Sausagewiki \
|
authenticated username. This only makes sense when Sausagewiki \
|
||||||
runs behind a reverse proxy which sets this header.")
|
runs behind a reverse proxy which sets this header.",
|
||||||
.long(TRUST_IDENTITY))
|
)
|
||||||
|
.long(TRUST_IDENTITY),
|
||||||
|
)
|
||||||
.get_matches()
|
.get_matches()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() -> Result<(), Box<std::error::Error>> {
|
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
let args = args();
|
let args = args();
|
||||||
|
|
||||||
const CLAP: &str = "Guaranteed by clap";
|
const CLAP: &str = "Guaranteed by clap";
|
||||||
const VALIDATOR: &str = "Guaranteed by clap validator";
|
const VALIDATOR: &str = "Guaranteed by clap validator";
|
||||||
let db_file = args.value_of(DATABASE).expect(CLAP).to_owned();
|
let db_file = args.value_of(DATABASE).expect(CLAP).to_owned();
|
||||||
let bind_host = args.value_of(ADDRESS).expect(CLAP).parse().expect(VALIDATOR);
|
let bind_host = args
|
||||||
|
.value_of(ADDRESS)
|
||||||
|
.expect(CLAP)
|
||||||
|
.parse()
|
||||||
|
.expect(VALIDATOR);
|
||||||
let bind_port = args.value_of(PORT).expect(CLAP).parse().expect(VALIDATOR);
|
let bind_port = args.value_of(PORT).expect(CLAP).parse().expect(VALIDATOR);
|
||||||
|
|
||||||
let trust_identity = args.is_present(TRUST_IDENTITY);
|
let trust_identity = args.is_present(TRUST_IDENTITY);
|
||||||
|
|
||||||
sausagewiki::main(
|
sausagewiki::main(db_file, bind_host, bind_port, trust_identity)
|
||||||
db_file,
|
|
||||||
bind_host,
|
|
||||||
bind_port,
|
|
||||||
trust_identity,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,7 @@
|
||||||
use std::fmt::Debug;
|
use std::fmt::Debug;
|
||||||
use diff;
|
|
||||||
|
|
||||||
#[derive(Debug, PartialEq)]
|
#[derive(Debug, PartialEq)]
|
||||||
pub struct Chunk<'a, Item: 'a + Debug + PartialEq + Copy>(
|
pub struct Chunk<'a, Item: 'a + Debug + PartialEq + Copy>(
|
||||||
pub &'a [diff::Result<Item>],
|
pub &'a [diff::Result<Item>],
|
||||||
pub &'a [diff::Result<Item>]
|
pub &'a [diff::Result<Item>],
|
||||||
);
|
);
|
||||||
|
|
|
@ -1,13 +1,12 @@
|
||||||
use std::fmt::Debug;
|
use std::fmt::Debug;
|
||||||
|
|
||||||
use diff;
|
|
||||||
use diff::Result::*;
|
use diff::Result::*;
|
||||||
|
|
||||||
use super::chunk::Chunk;
|
use super::chunk::Chunk;
|
||||||
|
|
||||||
pub struct ChunkIterator<'a, Item>
|
pub struct ChunkIterator<'a, Item>
|
||||||
where
|
where
|
||||||
Item: 'a + Debug + PartialEq
|
Item: 'a + Debug + PartialEq,
|
||||||
{
|
{
|
||||||
left: &'a [diff::Result<Item>],
|
left: &'a [diff::Result<Item>],
|
||||||
right: &'a [diff::Result<Item>],
|
right: &'a [diff::Result<Item>],
|
||||||
|
@ -15,16 +14,19 @@ where
|
||||||
|
|
||||||
impl<'a, Item> ChunkIterator<'a, Item>
|
impl<'a, Item> ChunkIterator<'a, Item>
|
||||||
where
|
where
|
||||||
Item: 'a + Debug + PartialEq + Eq
|
Item: 'a + Debug + PartialEq + Eq,
|
||||||
{
|
{
|
||||||
pub fn new(left: &'a [diff::Result<Item>], right: &'a [diff::Result<Item>]) -> ChunkIterator<'a, Item> {
|
pub fn new(
|
||||||
|
left: &'a [diff::Result<Item>],
|
||||||
|
right: &'a [diff::Result<Item>],
|
||||||
|
) -> ChunkIterator<'a, Item> {
|
||||||
ChunkIterator { left, right }
|
ChunkIterator { left, right }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, Item> Iterator for ChunkIterator<'a, Item>
|
impl<'a, Item> Iterator for ChunkIterator<'a, Item>
|
||||||
where
|
where
|
||||||
Item: 'a + Debug + PartialEq + Copy
|
Item: 'a + Debug + PartialEq + Copy,
|
||||||
{
|
{
|
||||||
type Item = Chunk<'a, Item>;
|
type Item = Chunk<'a, Item>;
|
||||||
|
|
||||||
|
@ -46,18 +48,18 @@ where
|
||||||
match (self.left.get(li), self.right.get(ri)) {
|
match (self.left.get(li), self.right.get(ri)) {
|
||||||
(Some(&Right(_)), _) => {
|
(Some(&Right(_)), _) => {
|
||||||
li += 1;
|
li += 1;
|
||||||
},
|
}
|
||||||
(_, Some(&Right(_))) => {
|
(_, Some(&Right(_))) => {
|
||||||
ri += 1;
|
ri += 1;
|
||||||
},
|
}
|
||||||
(Some(&Left(_)), Some(_)) => {
|
(Some(&Left(_)), Some(_)) => {
|
||||||
li += 1;
|
li += 1;
|
||||||
ri += 1;
|
ri += 1;
|
||||||
},
|
}
|
||||||
(Some(_), Some(&Left(_))) => {
|
(Some(_), Some(&Left(_))) => {
|
||||||
li += 1;
|
li += 1;
|
||||||
ri += 1;
|
ri += 1;
|
||||||
},
|
}
|
||||||
(Some(&Both(..)), Some(&Both(..))) => {
|
(Some(&Both(..)), Some(&Both(..))) => {
|
||||||
let chunk = Chunk(&self.left[..li], &self.right[..ri]);
|
let chunk = Chunk(&self.left[..li], &self.right[..ri]);
|
||||||
self.left = &self.left[li..];
|
self.left = &self.left[li..];
|
||||||
|
@ -65,7 +67,7 @@ where
|
||||||
return Some(chunk);
|
return Some(chunk);
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
if self.left.len() > 0 || self.right.len() > 0 {
|
if !self.left.is_empty() || !self.right.is_empty() {
|
||||||
let chunk = Chunk(self.left, self.right);
|
let chunk = Chunk(self.left, self.right);
|
||||||
self.left = &self.left[self.left.len()..];
|
self.left = &self.left[self.left.len()..];
|
||||||
self.right = &self.right[self.right.len()..];
|
self.right = &self.right[self.right.len()..];
|
||||||
|
@ -81,7 +83,6 @@ where
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use super::*;
|
use super::*;
|
||||||
use diff;
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn simple_case() {
|
fn simple_case() {
|
||||||
|
@ -94,13 +95,16 @@ mod test {
|
||||||
|
|
||||||
let chunks = ChunkIterator::new(&oa, &ob).collect::<Vec<_>>();
|
let chunks = ChunkIterator::new(&oa, &ob).collect::<Vec<_>>();
|
||||||
|
|
||||||
assert_eq!(vec![
|
assert_eq!(
|
||||||
Chunk(&oa[0.. 3], &ob[0.. 3]),
|
vec![
|
||||||
Chunk(&oa[3.. 6], &ob[3.. 3]),
|
Chunk(&oa[0..3], &ob[0..3]),
|
||||||
Chunk(&oa[6.. 9], &ob[3.. 6]),
|
Chunk(&oa[3..6], &ob[3..3]),
|
||||||
Chunk(&oa[9.. 9], &ob[6.. 9]),
|
Chunk(&oa[6..9], &ob[3..6]),
|
||||||
|
Chunk(&oa[9..9], &ob[6..9]),
|
||||||
Chunk(&oa[9..12], &ob[9..12]),
|
Chunk(&oa[9..12], &ob[9..12]),
|
||||||
], chunks);
|
],
|
||||||
|
chunks
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -113,11 +117,14 @@ mod test {
|
||||||
let ob = diff::chars(o, b);
|
let ob = diff::chars(o, b);
|
||||||
|
|
||||||
let chunks = ChunkIterator::new(&oa, &ob).collect::<Vec<_>>();
|
let chunks = ChunkIterator::new(&oa, &ob).collect::<Vec<_>>();
|
||||||
assert_eq!(vec![
|
assert_eq!(
|
||||||
Chunk(&oa[0.. 3], &ob[0.. 3]),
|
vec![
|
||||||
Chunk(&oa[3.. 9], &ob[3.. 9]),
|
Chunk(&oa[0..3], &ob[0..3]),
|
||||||
|
Chunk(&oa[3..9], &ob[3..9]),
|
||||||
Chunk(&oa[9..12], &ob[9..12]),
|
Chunk(&oa[9..12], &ob[9..12]),
|
||||||
], chunks);
|
],
|
||||||
|
chunks
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -130,10 +137,10 @@ mod test {
|
||||||
let ob = diff::chars(o, b);
|
let ob = diff::chars(o, b);
|
||||||
|
|
||||||
let chunks = ChunkIterator::new(&oa, &ob).collect::<Vec<_>>();
|
let chunks = ChunkIterator::new(&oa, &ob).collect::<Vec<_>>();
|
||||||
assert_eq!(vec![
|
assert_eq!(
|
||||||
Chunk(&oa[0..9], &ob[0.. 9]),
|
vec![Chunk(&oa[0..9], &ob[0..9]), Chunk(&oa[9..9], &ob[9..12]),],
|
||||||
Chunk(&oa[9..9], &ob[9..12]),
|
chunks
|
||||||
], chunks);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -146,10 +153,10 @@ mod test {
|
||||||
let ob = diff::chars(o, b);
|
let ob = diff::chars(o, b);
|
||||||
|
|
||||||
let chunks = ChunkIterator::new(&oa, &ob).collect::<Vec<_>>();
|
let chunks = ChunkIterator::new(&oa, &ob).collect::<Vec<_>>();
|
||||||
assert_eq!(vec![
|
assert_eq!(
|
||||||
Chunk(&oa[0..6], &ob[0.. 6]),
|
vec![Chunk(&oa[0..6], &ob[0..6]), Chunk(&oa[6..9], &ob[6..12]),],
|
||||||
Chunk(&oa[6..9], &ob[6..12]),
|
chunks
|
||||||
], chunks);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -162,8 +169,6 @@ mod test {
|
||||||
let ob = diff::chars(o, b);
|
let ob = diff::chars(o, b);
|
||||||
|
|
||||||
let chunks = ChunkIterator::new(&oa, &ob).collect::<Vec<_>>();
|
let chunks = ChunkIterator::new(&oa, &ob).collect::<Vec<_>>();
|
||||||
assert_eq!(vec![
|
assert_eq!(vec![Chunk(&oa[0..6], &ob[0..6]),], chunks);
|
||||||
Chunk(&oa[0..6], &ob[0..6]),
|
|
||||||
], chunks);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
164
src/merge/mod.rs
164
src/merge/mod.rs
|
@ -1,14 +1,12 @@
|
||||||
mod chunk_iterator;
|
|
||||||
mod chunk;
|
mod chunk;
|
||||||
|
mod chunk_iterator;
|
||||||
mod output;
|
mod output;
|
||||||
|
|
||||||
use std::fmt::Debug;
|
use std::fmt::Debug;
|
||||||
|
|
||||||
use diff;
|
|
||||||
|
|
||||||
use self::chunk_iterator::ChunkIterator;
|
use self::chunk_iterator::ChunkIterator;
|
||||||
use self::output::*;
|
|
||||||
use self::output::Output::Resolved;
|
use self::output::Output::Resolved;
|
||||||
|
use self::output::*;
|
||||||
|
|
||||||
pub use self::output::Output;
|
pub use self::output::Output;
|
||||||
|
|
||||||
|
@ -19,12 +17,12 @@ pub enum MergeResult<Item: Debug + PartialEq> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> MergeResult<&'a str> {
|
impl<'a> MergeResult<&'a str> {
|
||||||
pub fn to_strings(self) -> MergeResult<String> {
|
pub fn into_strings(self) -> MergeResult<String> {
|
||||||
match self {
|
match self {
|
||||||
MergeResult::Clean(x) => MergeResult::Clean(x),
|
MergeResult::Clean(x) => MergeResult::Clean(x),
|
||||||
MergeResult::Conflicted(x) => MergeResult::Conflicted(
|
MergeResult::Conflicted(x) => {
|
||||||
x.into_iter().map(Output::to_strings).collect()
|
MergeResult::Conflicted(x.into_iter().map(Output::into_strings).collect())
|
||||||
)
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -33,8 +31,8 @@ impl MergeResult<String> {
|
||||||
pub fn flatten(self) -> String {
|
pub fn flatten(self) -> String {
|
||||||
match self {
|
match self {
|
||||||
MergeResult::Clean(x) => x,
|
MergeResult::Clean(x) => x,
|
||||||
MergeResult::Conflicted(x) => {
|
MergeResult::Conflicted(x) => x
|
||||||
x.into_iter()
|
.into_iter()
|
||||||
.flat_map(|out| match out {
|
.flat_map(|out| match out {
|
||||||
Output::Conflict(a, _o, b) => {
|
Output::Conflict(a, _o, b) => {
|
||||||
let mut x: Vec<String> = vec![];
|
let mut x: Vec<String> = vec![];
|
||||||
|
@ -44,12 +42,10 @@ impl MergeResult<String> {
|
||||||
x.extend(b.into_iter().map(|x| format!("{}\n", x)));
|
x.extend(b.into_iter().map(|x| format!("{}\n", x)));
|
||||||
x.push(">>>>>>> Conflict ends here\n".into());
|
x.push(">>>>>>> Conflict ends here\n".into());
|
||||||
x
|
x
|
||||||
},
|
|
||||||
Output::Resolved(x) =>
|
|
||||||
x.into_iter().map(|x| format!("{}\n", x)).collect(),
|
|
||||||
})
|
|
||||||
.collect()
|
|
||||||
}
|
}
|
||||||
|
Output::Resolved(x) => x.into_iter().map(|x| format!("{}\n", x)).collect(),
|
||||||
|
})
|
||||||
|
.collect(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -58,8 +54,8 @@ impl MergeResult<char> {
|
||||||
pub fn flatten(self) -> String {
|
pub fn flatten(self) -> String {
|
||||||
match self {
|
match self {
|
||||||
MergeResult::Clean(x) => x,
|
MergeResult::Clean(x) => x,
|
||||||
MergeResult::Conflicted(x) => {
|
MergeResult::Conflicted(x) => x
|
||||||
x.into_iter()
|
.into_iter()
|
||||||
.flat_map(|out| match out {
|
.flat_map(|out| match out {
|
||||||
Output::Conflict(a, _o, b) => {
|
Output::Conflict(a, _o, b) => {
|
||||||
let mut x: Vec<char> = vec![];
|
let mut x: Vec<char> = vec![];
|
||||||
|
@ -69,11 +65,10 @@ impl MergeResult<char> {
|
||||||
x.extend(b);
|
x.extend(b);
|
||||||
x.push('>');
|
x.push('>');
|
||||||
x
|
x
|
||||||
},
|
}
|
||||||
Output::Resolved(x) => x,
|
Output::Resolved(x) => x,
|
||||||
})
|
})
|
||||||
.collect()
|
.collect(),
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -85,7 +80,7 @@ pub fn merge_lines<'a>(a: &'a str, o: &'a str, b: &'a str) -> MergeResult<&'a st
|
||||||
let chunks = ChunkIterator::new(&oa, &ob);
|
let chunks = ChunkIterator::new(&oa, &ob);
|
||||||
let hunks: Vec<_> = chunks.map(resolve).collect();
|
let hunks: Vec<_> = chunks.map(resolve).collect();
|
||||||
|
|
||||||
let clean = hunks.iter().all(|x| match x { &Resolved(..) => true, _ => false });
|
let clean = hunks.iter().all(|x| matches!(x, Resolved(..)));
|
||||||
|
|
||||||
if clean {
|
if clean {
|
||||||
MergeResult::Clean(
|
MergeResult::Clean(
|
||||||
|
@ -93,10 +88,10 @@ pub fn merge_lines<'a>(a: &'a str, o: &'a str, b: &'a str) -> MergeResult<&'a st
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.flat_map(|x| match x {
|
.flat_map(|x| match x {
|
||||||
Resolved(y) => y.into_iter(),
|
Resolved(y) => y.into_iter(),
|
||||||
_ => unreachable!()
|
_ => unreachable!(),
|
||||||
})
|
})
|
||||||
.collect::<Vec<_>>()
|
.collect::<Vec<_>>()
|
||||||
.join("\n")
|
.join("\n"),
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
MergeResult::Conflicted(hunks)
|
MergeResult::Conflicted(hunks)
|
||||||
|
@ -110,7 +105,7 @@ pub fn merge_chars<'a>(a: &'a str, o: &'a str, b: &'a str) -> MergeResult<char>
|
||||||
let chunks = ChunkIterator::new(&oa, &ob);
|
let chunks = ChunkIterator::new(&oa, &ob);
|
||||||
let hunks: Vec<_> = chunks.map(resolve).collect();
|
let hunks: Vec<_> = chunks.map(resolve).collect();
|
||||||
|
|
||||||
let clean = hunks.iter().all(|x| match x { &Resolved(..) => true, _ => false });
|
let clean = hunks.iter().all(|x| matches!(x, Resolved(..)));
|
||||||
|
|
||||||
if clean {
|
if clean {
|
||||||
MergeResult::Clean(
|
MergeResult::Clean(
|
||||||
|
@ -118,9 +113,9 @@ pub fn merge_chars<'a>(a: &'a str, o: &'a str, b: &'a str) -> MergeResult<char>
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.flat_map(|x| match x {
|
.flat_map(|x| match x {
|
||||||
Resolved(y) => y.into_iter(),
|
Resolved(y) => y.into_iter(),
|
||||||
_ => unreachable!()
|
_ => unreachable!(),
|
||||||
})
|
})
|
||||||
.collect()
|
.collect(),
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
MergeResult::Conflicted(hunks)
|
MergeResult::Conflicted(hunks)
|
||||||
|
@ -129,11 +124,11 @@ pub fn merge_chars<'a>(a: &'a str, o: &'a str, b: &'a str) -> MergeResult<char>
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use diff;
|
use indoc::indoc;
|
||||||
|
|
||||||
use super::*;
|
|
||||||
use super::output::*;
|
|
||||||
use super::output::Output::*;
|
use super::output::Output::*;
|
||||||
|
use super::output::*;
|
||||||
|
use super::*;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn simple_case() {
|
fn simple_case() {
|
||||||
|
@ -145,106 +140,141 @@ mod test {
|
||||||
chunks.map(resolve).collect()
|
chunks.map(resolve).collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
assert_eq!(vec![
|
assert_eq!(
|
||||||
|
vec![
|
||||||
Resolved("aaa".chars().collect()),
|
Resolved("aaa".chars().collect()),
|
||||||
Resolved("xxx".chars().collect()),
|
Resolved("xxx".chars().collect()),
|
||||||
Resolved("bbb".chars().collect()),
|
Resolved("bbb".chars().collect()),
|
||||||
Resolved("yyy".chars().collect()),
|
Resolved("yyy".chars().collect()),
|
||||||
Resolved("ccc".chars().collect()),
|
Resolved("ccc".chars().collect()),
|
||||||
], merge_chars(
|
],
|
||||||
"aaaxxxbbbccc",
|
merge_chars("aaaxxxbbbccc", "aaabbbccc", "aaabbbyyyccc",)
|
||||||
"aaabbbccc",
|
);
|
||||||
"aaabbbyyyccc",
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn clean_case() {
|
fn clean_case() {
|
||||||
assert_eq!(MergeResult::Clean(indoc!("
|
assert_eq!(
|
||||||
|
MergeResult::Clean(
|
||||||
|
indoc!(
|
||||||
|
"
|
||||||
aaa
|
aaa
|
||||||
xxx
|
xxx
|
||||||
bbb
|
bbb
|
||||||
yyy
|
yyy
|
||||||
ccc
|
ccc
|
||||||
").into()), merge_lines(
|
"
|
||||||
indoc!("
|
)
|
||||||
|
.into()
|
||||||
|
),
|
||||||
|
merge_lines(
|
||||||
|
indoc!(
|
||||||
|
"
|
||||||
aaa
|
aaa
|
||||||
xxx
|
xxx
|
||||||
bbb
|
bbb
|
||||||
ccc
|
ccc
|
||||||
"),
|
"
|
||||||
indoc!("
|
),
|
||||||
|
indoc!(
|
||||||
|
"
|
||||||
aaa
|
aaa
|
||||||
bbb
|
bbb
|
||||||
ccc
|
ccc
|
||||||
"),
|
"
|
||||||
indoc!("
|
),
|
||||||
|
indoc!(
|
||||||
|
"
|
||||||
aaa
|
aaa
|
||||||
bbb
|
bbb
|
||||||
yyy
|
yyy
|
||||||
ccc
|
ccc
|
||||||
"),
|
"
|
||||||
));
|
),
|
||||||
|
)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn clean_case_chars() {
|
fn clean_case_chars() {
|
||||||
assert_eq!(MergeResult::Clean("Title".into()), merge_chars(
|
assert_eq!(
|
||||||
"Titlle",
|
MergeResult::Clean("Title".into()),
|
||||||
"titlle",
|
merge_chars("Titlle", "titlle", "title",)
|
||||||
"title",
|
);
|
||||||
));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn false_conflict() {
|
fn false_conflict() {
|
||||||
assert_eq!(MergeResult::Clean(indoc!("
|
assert_eq!(
|
||||||
|
MergeResult::Clean(
|
||||||
|
indoc!(
|
||||||
|
"
|
||||||
aaa
|
aaa
|
||||||
xxx
|
xxx
|
||||||
ccc
|
ccc
|
||||||
").into()), merge_lines(
|
"
|
||||||
indoc!("
|
)
|
||||||
|
.into()
|
||||||
|
),
|
||||||
|
merge_lines(
|
||||||
|
indoc!(
|
||||||
|
"
|
||||||
aaa
|
aaa
|
||||||
xxx
|
xxx
|
||||||
ccc
|
ccc
|
||||||
"),
|
"
|
||||||
indoc!("
|
),
|
||||||
|
indoc!(
|
||||||
|
"
|
||||||
aaa
|
aaa
|
||||||
bbb
|
bbb
|
||||||
ccc
|
ccc
|
||||||
"),
|
"
|
||||||
indoc!("
|
),
|
||||||
|
indoc!(
|
||||||
|
"
|
||||||
aaa
|
aaa
|
||||||
xxx
|
xxx
|
||||||
ccc
|
ccc
|
||||||
"),
|
"
|
||||||
));
|
),
|
||||||
|
)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn true_conflict() {
|
fn true_conflict() {
|
||||||
assert_eq!(MergeResult::Conflicted(vec![
|
assert_eq!(
|
||||||
|
MergeResult::Conflicted(vec![
|
||||||
Resolved(vec!["aaa"]),
|
Resolved(vec!["aaa"]),
|
||||||
Conflict(vec!["xxx"], vec![], vec!["yyy"]),
|
Conflict(vec!["xxx"], vec![], vec!["yyy"]),
|
||||||
Resolved(vec!["bbb", "ccc", ""]),
|
Resolved(vec!["bbb", "ccc", ""]),
|
||||||
]), merge_lines(
|
]),
|
||||||
indoc!("
|
merge_lines(
|
||||||
|
indoc!(
|
||||||
|
"
|
||||||
aaa
|
aaa
|
||||||
xxx
|
xxx
|
||||||
bbb
|
bbb
|
||||||
ccc
|
ccc
|
||||||
"),
|
"
|
||||||
indoc!("
|
),
|
||||||
|
indoc!(
|
||||||
|
"
|
||||||
aaa
|
aaa
|
||||||
bbb
|
bbb
|
||||||
ccc
|
ccc
|
||||||
"),
|
"
|
||||||
indoc!("
|
),
|
||||||
|
indoc!(
|
||||||
|
"
|
||||||
aaa
|
aaa
|
||||||
yyy
|
yyy
|
||||||
bbb
|
bbb
|
||||||
ccc
|
ccc
|
||||||
"),
|
"
|
||||||
));
|
),
|
||||||
|
)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
use std::fmt::Debug;
|
use std::fmt::Debug;
|
||||||
|
|
||||||
use diff;
|
|
||||||
use diff::Result::*;
|
use diff::Result::*;
|
||||||
|
|
||||||
use super::chunk::Chunk;
|
use super::chunk::Chunk;
|
||||||
|
@ -12,7 +11,7 @@ pub enum Output<Item: Debug + PartialEq> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Output<&'a str> {
|
impl<'a> Output<&'a str> {
|
||||||
pub fn to_strings(self) -> Output<String> {
|
pub fn into_strings(self) -> Output<String> {
|
||||||
match self {
|
match self {
|
||||||
Output::Resolved(x) => Output::Resolved(x.into_iter().map(str::to_string).collect()),
|
Output::Resolved(x) => Output::Resolved(x.into_iter().map(str::to_string).collect()),
|
||||||
Output::Conflict(a, o, b) => Output::Conflict(
|
Output::Conflict(a, o, b) => Output::Conflict(
|
||||||
|
@ -27,10 +26,10 @@ impl<'a> Output<&'a str> {
|
||||||
fn choose_left<Item: Copy>(operations: &[diff::Result<Item>]) -> Vec<Item> {
|
fn choose_left<Item: Copy>(operations: &[diff::Result<Item>]) -> Vec<Item> {
|
||||||
operations
|
operations
|
||||||
.iter()
|
.iter()
|
||||||
.filter_map(|x| match x {
|
.filter_map(|x| match *x {
|
||||||
&Both(y, _) => Some(y),
|
Both(y, _) => Some(y),
|
||||||
&Left(y) => Some(y),
|
Left(y) => Some(y),
|
||||||
&Right(_) => None,
|
Right(_) => None,
|
||||||
})
|
})
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
@ -38,21 +37,16 @@ fn choose_left<Item: Copy>(operations: &[diff::Result<Item>]) -> Vec<Item> {
|
||||||
fn choose_right<Item: Copy>(operations: &[diff::Result<Item>]) -> Vec<Item> {
|
fn choose_right<Item: Copy>(operations: &[diff::Result<Item>]) -> Vec<Item> {
|
||||||
operations
|
operations
|
||||||
.iter()
|
.iter()
|
||||||
.filter_map(|x| match x {
|
.filter_map(|x| match *x {
|
||||||
&Both(_, y) => Some(y),
|
Both(_, y) => Some(y),
|
||||||
&Left(_) => None,
|
Left(_) => None,
|
||||||
&Right(y) => Some(y),
|
Right(y) => Some(y),
|
||||||
})
|
})
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn no_change<Item>(operations: &[diff::Result<Item>]) -> bool {
|
fn no_change<Item>(operations: &[diff::Result<Item>]) -> bool {
|
||||||
operations
|
operations.iter().all(|x| matches!(x, Both(..)))
|
||||||
.iter()
|
|
||||||
.all(|x| match x {
|
|
||||||
&Both(..) => true,
|
|
||||||
_ => false,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn resolve<'a, Item: 'a + Debug + PartialEq + Copy>(chunk: Chunk<'a, Item>) -> Output<Item> {
|
pub fn resolve<'a, Item: 'a + Debug + PartialEq + Copy>(chunk: Chunk<'a, Item>) -> Output<Item> {
|
||||||
|
@ -69,92 +63,51 @@ pub fn resolve<'a, Item: 'a + Debug + PartialEq + Copy>(chunk: Chunk<'a, Item>)
|
||||||
return Output::Resolved(choose_right(chunk.0));
|
return Output::Resolved(choose_right(chunk.0));
|
||||||
}
|
}
|
||||||
|
|
||||||
return Output::Conflict(
|
Output::Conflict(
|
||||||
choose_right(chunk.0),
|
choose_right(chunk.0),
|
||||||
choose_left(chunk.0),
|
choose_left(chunk.0),
|
||||||
choose_right(chunk.1),
|
choose_right(chunk.1),
|
||||||
);
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use diff::Result::*;
|
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn empty() {
|
fn empty() {
|
||||||
assert_eq!(
|
assert_eq!(Output::Resolved(vec![]), resolve::<i32>(Chunk(&[], &[])));
|
||||||
Output::Resolved(vec![]),
|
|
||||||
resolve::<i32>(Chunk(&[], &[]))
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn same() {
|
fn same() {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
Output::Resolved(vec![
|
Output::Resolved(vec![1]),
|
||||||
1
|
resolve::<i32>(Chunk(&[Both(1, 1)], &[Both(1, 1)]))
|
||||||
]),
|
|
||||||
resolve::<i32>(Chunk(
|
|
||||||
&[Both(1, 1)],
|
|
||||||
&[Both(1, 1)]
|
|
||||||
))
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn only_left() {
|
fn only_left() {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
Output::Resolved(vec![
|
Output::Resolved(vec![2]),
|
||||||
2
|
resolve::<i32>(Chunk(&[Left(1), Right(2)], &[]))
|
||||||
]),
|
|
||||||
resolve::<i32>(Chunk(
|
|
||||||
&[
|
|
||||||
Left(1),
|
|
||||||
Right(2)
|
|
||||||
],
|
|
||||||
&[]
|
|
||||||
))
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn false_conflict() {
|
fn false_conflict() {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
Output::Resolved(vec![
|
Output::Resolved(vec![2]),
|
||||||
2
|
resolve::<i32>(Chunk(&[Left(1), Right(2)], &[Left(1), Right(2)],))
|
||||||
]),
|
|
||||||
resolve::<i32>(Chunk(
|
|
||||||
&[
|
|
||||||
Left(1),
|
|
||||||
Right(2)
|
|
||||||
],
|
|
||||||
&[
|
|
||||||
Left(1),
|
|
||||||
Right(2)
|
|
||||||
],
|
|
||||||
))
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn real_conflict() {
|
fn real_conflict() {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
Output::Conflict(
|
Output::Conflict(vec![2], vec![1], vec![3],),
|
||||||
vec![2],
|
resolve::<i32>(Chunk(&[Left(1), Right(2)], &[Left(1), Right(3)],))
|
||||||
vec![1],
|
|
||||||
vec![3],
|
|
||||||
),
|
|
||||||
resolve::<i32>(Chunk(
|
|
||||||
&[
|
|
||||||
Left(1),
|
|
||||||
Right(2)
|
|
||||||
],
|
|
||||||
&[
|
|
||||||
Left(1),
|
|
||||||
Right(3)
|
|
||||||
],
|
|
||||||
))
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use chrono;
|
use crate::theme::Theme;
|
||||||
|
|
||||||
fn slug_link(slug: &str) -> &str {
|
fn slug_link(slug: &str) -> &str {
|
||||||
if slug.is_empty() {
|
if slug.is_empty() {
|
||||||
|
@ -23,10 +23,14 @@ pub struct ArticleRevision {
|
||||||
pub latest: bool,
|
pub latest: bool,
|
||||||
|
|
||||||
pub author: Option<String>,
|
pub author: Option<String>,
|
||||||
|
|
||||||
|
pub theme: Theme,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ArticleRevision {
|
impl ArticleRevision {
|
||||||
pub fn link(&self) -> &str { slug_link(&self.slug) }
|
pub fn link(&self) -> &str {
|
||||||
|
slug_link(&self.slug)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Queryable)]
|
#[derive(Debug, PartialEq, Queryable)]
|
||||||
|
@ -43,10 +47,14 @@ pub struct ArticleRevisionStub {
|
||||||
pub latest: bool,
|
pub latest: bool,
|
||||||
|
|
||||||
pub author: Option<String>,
|
pub author: Option<String>,
|
||||||
|
|
||||||
|
pub theme: Theme,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ArticleRevisionStub {
|
impl ArticleRevisionStub {
|
||||||
pub fn link(&self) -> &str { slug_link(&self.slug) }
|
pub fn link(&self) -> &str {
|
||||||
|
slug_link(&self.slug)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
use diesel::sql_types::Text;
|
use diesel::sql_types::Text;
|
||||||
|
@ -63,5 +71,7 @@ pub struct SearchResult {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SearchResult {
|
impl SearchResult {
|
||||||
pub fn link(&self) -> &str { slug_link(&self.slug) }
|
pub fn link(&self) -> &str {
|
||||||
|
slug_link(&self.slug)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,18 @@
|
||||||
use pulldown_cmark::{Parser, Tag, html, OPTION_ENABLE_TABLES, OPTION_DISABLE_HTML};
|
use pulldown_cmark::Event::{End, Text};
|
||||||
use pulldown_cmark::Event::{Text, End};
|
use pulldown_cmark::{html, Parser, Tag, OPTION_DISABLE_HTML, OPTION_ENABLE_TABLES};
|
||||||
|
use slug::slugify;
|
||||||
|
|
||||||
|
fn slugify_link(text: &str, title: &str) -> Option<(String, String)> {
|
||||||
|
Some((slugify(text), title.to_owned()))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parser(src: &str) -> Parser {
|
||||||
|
let opts = OPTION_ENABLE_TABLES | OPTION_DISABLE_HTML;
|
||||||
|
Parser::new_with_broken_link_callback(src, opts, Some(&slugify_link))
|
||||||
|
}
|
||||||
|
|
||||||
pub fn render_markdown(src: &str) -> String {
|
pub fn render_markdown(src: &str) -> String {
|
||||||
let opts = OPTION_ENABLE_TABLES | OPTION_DISABLE_HTML;
|
let p = parser(src);
|
||||||
let p = Parser::new_ext(src, opts);
|
|
||||||
let mut buf = String::new();
|
let mut buf = String::new();
|
||||||
html::push_html(&mut buf, p);
|
html::push_html(&mut buf, p);
|
||||||
buf
|
buf
|
||||||
|
@ -14,22 +23,43 @@ fn is_html_special(c: char) -> bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn render_markdown_for_fts(src: &str) -> String {
|
pub fn render_markdown_for_fts(src: &str) -> String {
|
||||||
let opts = OPTION_ENABLE_TABLES | OPTION_DISABLE_HTML;
|
let p = parser(src);
|
||||||
let p = Parser::new_ext(src, opts);
|
|
||||||
let mut buf = String::new();
|
let mut buf = String::new();
|
||||||
|
|
||||||
for event in p {
|
for event in p {
|
||||||
match event {
|
match event {
|
||||||
Text(text) =>
|
// As far as I understand this is a basic
|
||||||
buf.push_str(&text.replace(is_html_special, " ")),
|
// sanitizing to prevent HTML from
|
||||||
|
// appearing in page.
|
||||||
|
Text(text) => buf.push_str(&text.replace(is_html_special, " ")),
|
||||||
|
// Footnote links maybe?
|
||||||
End(Tag::Link(uri, _title)) => {
|
End(Tag::Link(uri, _title)) => {
|
||||||
buf.push_str(" (");
|
buf.push_str(" (");
|
||||||
buf.push_str(&uri.replace(is_html_special, " "));
|
buf.push_str(&uri.replace(is_html_special, " "));
|
||||||
buf.push_str(") ");
|
buf.push_str(") ");
|
||||||
}
|
}
|
||||||
_ => buf.push_str(" "),
|
_ => buf.push(' '),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
buf
|
buf
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn slug_link() {
|
||||||
|
let actual = render_markdown("[Slug link]");
|
||||||
|
let expected = "<p><a href=\"slug-link\" title=\"Slug link\">Slug link</a></p>\n";
|
||||||
|
assert_eq!(actual, expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn footnote_links() {
|
||||||
|
let actual = render_markdown("[Link]\n\n[Link]: target");
|
||||||
|
let expected = "<p><a href=\"target\">Link</a></p>\n";
|
||||||
|
assert_eq!(actual, expected);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1,12 +1,12 @@
|
||||||
use futures::{self, Future};
|
use futures::{self, Future};
|
||||||
use hyper;
|
|
||||||
use hyper::header::ContentType;
|
use hyper::header::ContentType;
|
||||||
use hyper::server::*;
|
use hyper::server::*;
|
||||||
|
|
||||||
use build_config;
|
use crate::build_config;
|
||||||
use mimes::*;
|
use crate::mimes::*;
|
||||||
use site::Layout;
|
use crate::site::system_page;
|
||||||
use web::{Resource, ResponseFuture};
|
use crate::web::{Resource, ResponseFuture};
|
||||||
|
|
||||||
#[derive(Licenses)]
|
#[derive(Licenses)]
|
||||||
pub struct AboutResource;
|
pub struct AboutResource;
|
||||||
|
@ -28,20 +28,20 @@ impl License {
|
||||||
fn link(&self) -> &'static str {
|
fn link(&self) -> &'static str {
|
||||||
use self::License::*;
|
use self::License::*;
|
||||||
match self {
|
match self {
|
||||||
&Bsd3Clause => "bsd-3-clause",
|
Bsd3Clause => "bsd-3-clause",
|
||||||
&Mit => "mit",
|
Mit => "mit",
|
||||||
&Mpl2 => "mpl2",
|
Mpl2 => "mpl2",
|
||||||
&Ofl11 => "sil-ofl-1.1",
|
Ofl11 => "sil-ofl-1.1",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn name(&self) -> &'static str {
|
fn name(&self) -> &'static str {
|
||||||
use self::License::*;
|
use self::License::*;
|
||||||
match self {
|
match self {
|
||||||
&Bsd3Clause => "BSD-3-Clause",
|
Bsd3Clause => "BSD-3-Clause",
|
||||||
&Mit => "MIT",
|
Mit => "MIT",
|
||||||
&Mpl2 => "MPL2",
|
Mpl2 => "MPL2",
|
||||||
&Ofl11 => "OFL-1.1",
|
Ofl11 => "OFL-1.1",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -54,13 +54,15 @@ struct LicenseInfo {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(BartDisplay)]
|
#[derive(BartDisplay)]
|
||||||
#[template="templates/about.html"]
|
#[template = "templates/about.html"]
|
||||||
struct Template<'a> {
|
struct Template<'a> {
|
||||||
deps: &'a [LicenseInfo]
|
deps: &'a [LicenseInfo],
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Template<'a> {
|
impl<'a> Template<'a> {
|
||||||
fn version(&self) -> &str { &build_config::VERSION }
|
fn version(&self) -> &str {
|
||||||
|
&build_config::VERSION
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Resource for AboutResource {
|
impl Resource for AboutResource {
|
||||||
|
@ -70,25 +72,27 @@ impl Resource for AboutResource {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn head(&self) -> ResponseFuture {
|
fn head(&self) -> ResponseFuture {
|
||||||
Box::new(futures::finished(Response::new()
|
Box::new(futures::finished(
|
||||||
|
Response::new()
|
||||||
.with_status(hyper::StatusCode::Ok)
|
.with_status(hyper::StatusCode::Ok)
|
||||||
.with_header(ContentType(TEXT_HTML.clone()))
|
.with_header(ContentType(TEXT_HTML.clone())),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get(self: Box<Self>) -> ResponseFuture {
|
fn get(self: Box<Self>) -> ResponseFuture {
|
||||||
let head = self.head();
|
let head = self.head();
|
||||||
|
|
||||||
Box::new(head
|
Box::new(head.and_then(move |head| {
|
||||||
.and_then(move |head| {
|
Ok(head.with_body(
|
||||||
Ok(head
|
system_page(
|
||||||
.with_body(Layout {
|
None, // Hmm, should perhaps accept `base` as argument
|
||||||
base: None, // Hmm, should perhaps accept `base` as argument
|
"About Sausagewiki",
|
||||||
title: "About Sausagewiki",
|
Template {
|
||||||
body: &Template {
|
deps: *LICENSE_INFOS,
|
||||||
deps: &*LICENSE_INFOS
|
|
||||||
},
|
},
|
||||||
}.to_string()))
|
)
|
||||||
|
.to_string(),
|
||||||
|
))
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,22 +1,26 @@
|
||||||
use chrono::{TimeZone, DateTime, Local};
|
use chrono::{DateTime, Local, TimeZone};
|
||||||
use futures::{self, Future};
|
use futures::{self, Future};
|
||||||
use hyper;
|
|
||||||
use hyper::header::{ContentType, Location};
|
use hyper::header::{ContentType, Location};
|
||||||
use hyper::server::*;
|
use hyper::server::*;
|
||||||
use serde_json;
|
|
||||||
use serde_urlencoded;
|
|
||||||
|
|
||||||
use assets::ScriptJs;
|
use crate::assets::ScriptJs;
|
||||||
use mimes::*;
|
use crate::mimes::*;
|
||||||
use rendering::render_markdown;
|
use crate::rendering::render_markdown;
|
||||||
use site::Layout;
|
use crate::site::Layout;
|
||||||
use state::{State, UpdateResult, RebaseConflict};
|
use crate::state::{RebaseConflict, State, UpdateResult};
|
||||||
use web::{Resource, ResponseFuture};
|
use crate::theme::{self, Theme};
|
||||||
|
use crate::web::{Resource, ResponseFuture};
|
||||||
|
|
||||||
use super::changes_resource::QueryParameters;
|
use super::changes_resource::QueryParameters;
|
||||||
|
|
||||||
|
struct SelectableTheme {
|
||||||
|
theme: Theme,
|
||||||
|
selected: bool,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(BartDisplay)]
|
#[derive(BartDisplay)]
|
||||||
#[template="templates/article.html"]
|
#[template = "templates/article.html"]
|
||||||
struct Template<'a> {
|
struct Template<'a> {
|
||||||
revision: i32,
|
revision: i32,
|
||||||
last_updated: Option<&'a str>,
|
last_updated: Option<&'a str>,
|
||||||
|
@ -26,11 +30,12 @@ struct Template<'a> {
|
||||||
title: &'a str,
|
title: &'a str,
|
||||||
raw: &'a str,
|
raw: &'a str,
|
||||||
rendered: String,
|
rendered: String,
|
||||||
|
themes: &'a [SelectableTheme],
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Template<'a> {
|
impl<'a> Template<'a> {
|
||||||
fn script_js_checksum(&self) -> &'static str {
|
fn script_js(&self) -> &'static str {
|
||||||
ScriptJs::checksum()
|
ScriptJs::resource_name()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -39,6 +44,7 @@ struct UpdateArticle {
|
||||||
base_revision: i32,
|
base_revision: i32,
|
||||||
title: String,
|
title: String,
|
||||||
body: String,
|
body: String,
|
||||||
|
theme: Option<Theme>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct ArticleResource {
|
pub struct ArticleResource {
|
||||||
|
@ -50,7 +56,12 @@ pub struct ArticleResource {
|
||||||
|
|
||||||
impl ArticleResource {
|
impl ArticleResource {
|
||||||
pub fn new(state: State, article_id: i32, revision: i32, edit: bool) -> Self {
|
pub fn new(state: State, article_id: i32, revision: i32, edit: bool) -> Self {
|
||||||
Self { state, article_id, revision, edit }
|
Self {
|
||||||
|
state,
|
||||||
|
article_id,
|
||||||
|
revision,
|
||||||
|
edit,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -70,12 +81,23 @@ pub fn last_updated(article_id: i32, created: &DateTime<Local>, author: Option<&
|
||||||
|
|
||||||
Template {
|
Template {
|
||||||
created: &created.to_rfc2822(),
|
created: &created.to_rfc2822(),
|
||||||
article_history: &format!("_changes{}", QueryParameters::default().article_id(Some(article_id)).into_link()),
|
article_history: &format!(
|
||||||
|
"_changes{}",
|
||||||
|
QueryParameters::default()
|
||||||
|
.article_id(Some(article_id))
|
||||||
|
.into_link()
|
||||||
|
),
|
||||||
author: author.map(|author| Author {
|
author: author.map(|author| Author {
|
||||||
author: &author,
|
author,
|
||||||
history: format!("_changes{}", QueryParameters::default().author(Some(author.to_owned())).into_link()),
|
history: format!(
|
||||||
|
"_changes{}",
|
||||||
|
QueryParameters::default()
|
||||||
|
.author(Some(author.to_owned()))
|
||||||
|
.into_link()
|
||||||
|
),
|
||||||
}),
|
}),
|
||||||
}.to_string()
|
}
|
||||||
|
.to_string()
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Resource for ArticleResource {
|
impl Resource for ArticleResource {
|
||||||
|
@ -85,37 +107,49 @@ impl Resource for ArticleResource {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn head(&self) -> ResponseFuture {
|
fn head(&self) -> ResponseFuture {
|
||||||
Box::new(futures::finished(Response::new()
|
Box::new(futures::finished(
|
||||||
|
Response::new()
|
||||||
.with_status(hyper::StatusCode::Ok)
|
.with_status(hyper::StatusCode::Ok)
|
||||||
.with_header(ContentType(TEXT_HTML.clone()))
|
.with_header(ContentType(TEXT_HTML.clone())),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get(self: Box<Self>) -> ResponseFuture {
|
fn get(self: Box<Self>) -> ResponseFuture {
|
||||||
let data = self.state.get_article_revision(self.article_id, self.revision)
|
let data = self
|
||||||
|
.state
|
||||||
|
.get_article_revision(self.article_id, self.revision)
|
||||||
.map(|x| x.expect("Data model guarantees that this exists"));
|
.map(|x| x.expect("Data model guarantees that this exists"));
|
||||||
let head = self.head();
|
let head = self.head();
|
||||||
|
|
||||||
Box::new(data.join(head)
|
Box::new(data.join(head).and_then(move |(data, head)| {
|
||||||
.and_then(move |(data, head)| {
|
Ok(head.with_body(
|
||||||
Ok(head
|
Layout {
|
||||||
.with_body(Layout {
|
|
||||||
base: None, // Hmm, should perhaps accept `base` as argument
|
base: None, // Hmm, should perhaps accept `base` as argument
|
||||||
title: &data.title,
|
title: &data.title,
|
||||||
|
theme: data.theme,
|
||||||
body: &Template {
|
body: &Template {
|
||||||
revision: data.revision,
|
revision: data.revision,
|
||||||
last_updated: Some(&last_updated(
|
last_updated: Some(&last_updated(
|
||||||
data.article_id,
|
data.article_id,
|
||||||
&Local.from_utc_datetime(&data.created),
|
&Local.from_utc_datetime(&data.created),
|
||||||
data.author.as_ref().map(|x| &**x)
|
data.author.as_deref(),
|
||||||
)),
|
)),
|
||||||
edit: self.edit,
|
edit: self.edit,
|
||||||
cancel_url: Some(data.link()),
|
cancel_url: Some(data.link()),
|
||||||
title: &data.title,
|
title: &data.title,
|
||||||
raw: &data.body,
|
raw: &data.body,
|
||||||
rendered: render_markdown(&data.body),
|
rendered: render_markdown(&data.body),
|
||||||
|
themes: &theme::THEMES
|
||||||
|
.iter()
|
||||||
|
.map(|&x| SelectableTheme {
|
||||||
|
theme: x,
|
||||||
|
selected: x == data.theme,
|
||||||
|
})
|
||||||
|
.collect::<Vec<_>>(),
|
||||||
},
|
},
|
||||||
}.to_string()))
|
}
|
||||||
|
.to_string(),
|
||||||
|
))
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -125,7 +159,7 @@ impl Resource for ArticleResource {
|
||||||
use futures::Stream;
|
use futures::Stream;
|
||||||
|
|
||||||
#[derive(BartDisplay)]
|
#[derive(BartDisplay)]
|
||||||
#[template="templates/article_contents.html"]
|
#[template = "templates/article_contents.html"]
|
||||||
struct Template<'a> {
|
struct Template<'a> {
|
||||||
title: &'a str,
|
title: &'a str,
|
||||||
rendered: String,
|
rendered: String,
|
||||||
|
@ -138,69 +172,84 @@ impl Resource for ArticleResource {
|
||||||
revision: i32,
|
revision: i32,
|
||||||
title: &'a str,
|
title: &'a str,
|
||||||
body: &'a str,
|
body: &'a str,
|
||||||
|
theme: Theme,
|
||||||
rendered: &'a str,
|
rendered: &'a str,
|
||||||
last_updated: &'a str,
|
last_updated: &'a str,
|
||||||
}
|
}
|
||||||
|
|
||||||
Box::new(body
|
Box::new(
|
||||||
.concat2()
|
body.concat2()
|
||||||
.map_err(Into::into)
|
.map_err(Into::into)
|
||||||
.and_then(|body| {
|
.and_then(|body| serde_urlencoded::from_bytes(&body).map_err(Into::into))
|
||||||
serde_urlencoded::from_bytes(&body)
|
|
||||||
.map_err(Into::into)
|
|
||||||
})
|
|
||||||
.and_then(move |update: UpdateArticle| {
|
.and_then(move |update: UpdateArticle| {
|
||||||
self.state.update_article(self.article_id, update.base_revision, update.title, update.body, identity)
|
self.state.update_article(
|
||||||
|
self.article_id,
|
||||||
|
update.base_revision,
|
||||||
|
update.title,
|
||||||
|
update.body,
|
||||||
|
identity,
|
||||||
|
update.theme,
|
||||||
|
)
|
||||||
})
|
})
|
||||||
.and_then(|updated| match updated {
|
.and_then(|updated| match updated {
|
||||||
UpdateResult::Success(updated) =>
|
UpdateResult::Success(updated) => Ok(Response::new()
|
||||||
Ok(Response::new()
|
|
||||||
.with_status(hyper::StatusCode::Ok)
|
.with_status(hyper::StatusCode::Ok)
|
||||||
.with_header(ContentType(APPLICATION_JSON.clone()))
|
.with_header(ContentType(APPLICATION_JSON.clone()))
|
||||||
.with_body(serde_json::to_string(&PutResponse {
|
.with_body(
|
||||||
|
serde_json::to_string(&PutResponse {
|
||||||
conflict: false,
|
conflict: false,
|
||||||
slug: &updated.slug,
|
slug: &updated.slug,
|
||||||
revision: updated.revision,
|
revision: updated.revision,
|
||||||
title: &updated.title,
|
title: &updated.title,
|
||||||
body: &updated.body,
|
body: &updated.body,
|
||||||
|
theme: updated.theme,
|
||||||
rendered: &Template {
|
rendered: &Template {
|
||||||
title: &updated.title,
|
title: &updated.title,
|
||||||
rendered: render_markdown(&updated.body),
|
rendered: render_markdown(&updated.body),
|
||||||
}.to_string(),
|
}
|
||||||
|
.to_string(),
|
||||||
last_updated: &last_updated(
|
last_updated: &last_updated(
|
||||||
updated.article_id,
|
updated.article_id,
|
||||||
&Local.from_utc_datetime(&updated.created),
|
&Local.from_utc_datetime(&updated.created),
|
||||||
updated.author.as_ref().map(|x| &**x)
|
updated.author.as_deref(),
|
||||||
),
|
|
||||||
}).expect("Should never fail"))
|
|
||||||
),
|
),
|
||||||
|
})
|
||||||
|
.expect("Should never fail"),
|
||||||
|
)),
|
||||||
UpdateResult::RebaseConflict(RebaseConflict {
|
UpdateResult::RebaseConflict(RebaseConflict {
|
||||||
base_article, title, body
|
base_article,
|
||||||
|
title,
|
||||||
|
body,
|
||||||
|
theme,
|
||||||
}) => {
|
}) => {
|
||||||
let title = title.flatten();
|
let title = title.flatten();
|
||||||
let body = body.flatten();
|
let body = body.flatten();
|
||||||
Ok(Response::new()
|
Ok(Response::new()
|
||||||
.with_status(hyper::StatusCode::Ok)
|
.with_status(hyper::StatusCode::Ok)
|
||||||
.with_header(ContentType(APPLICATION_JSON.clone()))
|
.with_header(ContentType(APPLICATION_JSON.clone()))
|
||||||
.with_body(serde_json::to_string(&PutResponse {
|
.with_body(
|
||||||
|
serde_json::to_string(&PutResponse {
|
||||||
conflict: true,
|
conflict: true,
|
||||||
slug: &base_article.slug,
|
slug: &base_article.slug,
|
||||||
revision: base_article.revision,
|
revision: base_article.revision,
|
||||||
title: &title,
|
title: &title,
|
||||||
body: &body,
|
body: &body,
|
||||||
|
theme,
|
||||||
rendered: &Template {
|
rendered: &Template {
|
||||||
title: &title,
|
title: &title,
|
||||||
rendered: render_markdown(&body),
|
rendered: render_markdown(&body),
|
||||||
}.to_string(),
|
}
|
||||||
|
.to_string(),
|
||||||
last_updated: &last_updated(
|
last_updated: &last_updated(
|
||||||
base_article.article_id,
|
base_article.article_id,
|
||||||
&Local.from_utc_datetime(&base_article.created),
|
&Local.from_utc_datetime(&base_article.created),
|
||||||
base_article.author.as_ref().map(|x| &**x)
|
base_article.author.as_deref(),
|
||||||
),
|
),
|
||||||
}).expect("Should never fail"))
|
|
||||||
)
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
.expect("Should never fail"),
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -209,53 +258,67 @@ impl Resource for ArticleResource {
|
||||||
|
|
||||||
use futures::Stream;
|
use futures::Stream;
|
||||||
|
|
||||||
Box::new(body
|
Box::new(
|
||||||
.concat2()
|
body.concat2()
|
||||||
.map_err(Into::into)
|
.map_err(Into::into)
|
||||||
.and_then(|body| {
|
.and_then(|body| serde_urlencoded::from_bytes(&body).map_err(Into::into))
|
||||||
serde_urlencoded::from_bytes(&body)
|
|
||||||
.map_err(Into::into)
|
|
||||||
})
|
|
||||||
.and_then(move |update: UpdateArticle| {
|
.and_then(move |update: UpdateArticle| {
|
||||||
self.state.update_article(self.article_id, update.base_revision, update.title, update.body, identity)
|
self.state.update_article(
|
||||||
|
self.article_id,
|
||||||
|
update.base_revision,
|
||||||
|
update.title,
|
||||||
|
update.body,
|
||||||
|
identity,
|
||||||
|
update.theme,
|
||||||
|
)
|
||||||
})
|
})
|
||||||
.and_then(|updated| {
|
.and_then(|updated| match updated {
|
||||||
match updated {
|
|
||||||
UpdateResult::Success(updated) => Ok(Response::new()
|
UpdateResult::Success(updated) => Ok(Response::new()
|
||||||
.with_status(hyper::StatusCode::SeeOther)
|
.with_status(hyper::StatusCode::SeeOther)
|
||||||
.with_header(ContentType(TEXT_PLAIN.clone()))
|
.with_header(ContentType(TEXT_PLAIN.clone()))
|
||||||
.with_header(Location::new(updated.link().to_owned()))
|
.with_header(Location::new(updated.link().to_owned()))
|
||||||
.with_body("See other")
|
.with_body("See other")),
|
||||||
),
|
|
||||||
UpdateResult::RebaseConflict(RebaseConflict {
|
UpdateResult::RebaseConflict(RebaseConflict {
|
||||||
base_article, title, body
|
base_article,
|
||||||
|
title,
|
||||||
|
body,
|
||||||
|
theme,
|
||||||
}) => {
|
}) => {
|
||||||
let title = title.flatten();
|
let title = title.flatten();
|
||||||
let body = body.flatten();
|
let body = body.flatten();
|
||||||
Ok(Response::new()
|
Ok(Response::new()
|
||||||
.with_status(hyper::StatusCode::Ok)
|
.with_status(hyper::StatusCode::Ok)
|
||||||
.with_header(ContentType(TEXT_HTML.clone()))
|
.with_header(ContentType(TEXT_HTML.clone()))
|
||||||
.with_body(Layout {
|
.with_body(
|
||||||
|
Layout {
|
||||||
base: None,
|
base: None,
|
||||||
title: &title,
|
title: &title,
|
||||||
|
theme,
|
||||||
body: &Template {
|
body: &Template {
|
||||||
revision: base_article.revision,
|
revision: base_article.revision,
|
||||||
last_updated: Some(&last_updated(
|
last_updated: Some(&last_updated(
|
||||||
base_article.article_id,
|
base_article.article_id,
|
||||||
&Local.from_utc_datetime(&base_article.created),
|
&Local.from_utc_datetime(&base_article.created),
|
||||||
base_article.author.as_ref().map(|x| &**x)
|
base_article.author.as_deref(),
|
||||||
)),
|
)),
|
||||||
edit: true,
|
edit: true,
|
||||||
cancel_url: Some(base_article.link()),
|
cancel_url: Some(base_article.link()),
|
||||||
title: &title,
|
title: &title,
|
||||||
raw: &body,
|
raw: &body,
|
||||||
rendered: render_markdown(&body),
|
rendered: render_markdown(&body),
|
||||||
},
|
themes: &theme::THEMES
|
||||||
}.to_string())
|
.iter()
|
||||||
)
|
.map(|&x| SelectableTheme {
|
||||||
}
|
theme: x,
|
||||||
}
|
selected: x == theme,
|
||||||
})
|
})
|
||||||
|
.collect::<Vec<_>>(),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
.to_string(),
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,14 +1,14 @@
|
||||||
use chrono::{TimeZone, DateTime, Local};
|
use chrono::{DateTime, Local, TimeZone};
|
||||||
use futures::{self, Future};
|
use futures::{self, Future};
|
||||||
use hyper;
|
|
||||||
use hyper::header::ContentType;
|
use hyper::header::ContentType;
|
||||||
use hyper::server::*;
|
use hyper::server::*;
|
||||||
|
|
||||||
use mimes::*;
|
use crate::mimes::*;
|
||||||
use models;
|
use crate::models;
|
||||||
use rendering::render_markdown;
|
use crate::rendering::render_markdown;
|
||||||
use site::Layout;
|
use crate::site::system_page;
|
||||||
use web::{Resource, ResponseFuture};
|
use crate::web::{Resource, ResponseFuture};
|
||||||
|
|
||||||
use super::changes_resource::QueryParameters;
|
use super::changes_resource::QueryParameters;
|
||||||
use super::diff_resource;
|
use super::diff_resource;
|
||||||
|
@ -24,7 +24,12 @@ impl ArticleRevisionResource {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn timestamp_and_author(sequence_number: i32, article_id: i32, created: &DateTime<Local>, author: Option<&str>) -> String {
|
pub fn timestamp_and_author(
|
||||||
|
sequence_number: i32,
|
||||||
|
article_id: i32,
|
||||||
|
created: &DateTime<Local>,
|
||||||
|
author: Option<&str>,
|
||||||
|
) -> String {
|
||||||
struct Author<'a> {
|
struct Author<'a> {
|
||||||
author: &'a str,
|
author: &'a str,
|
||||||
history: String,
|
history: String,
|
||||||
|
@ -42,15 +47,17 @@ pub fn timestamp_and_author(sequence_number: i32, article_id: i32, created: &Dat
|
||||||
|
|
||||||
Template {
|
Template {
|
||||||
created: &created.to_rfc2822(),
|
created: &created.to_rfc2822(),
|
||||||
article_history: &format!("_changes{}",
|
article_history: &format!(
|
||||||
|
"_changes{}",
|
||||||
QueryParameters::default()
|
QueryParameters::default()
|
||||||
.pagination(pagination)
|
.pagination(pagination)
|
||||||
.article_id(Some(article_id))
|
.article_id(Some(article_id))
|
||||||
.into_link()
|
.into_link()
|
||||||
),
|
),
|
||||||
author: author.map(|author| Author {
|
author: author.map(|author| Author {
|
||||||
author: &author,
|
author,
|
||||||
history: format!("_changes{}",
|
history: format!(
|
||||||
|
"_changes{}",
|
||||||
QueryParameters::default()
|
QueryParameters::default()
|
||||||
.pagination(pagination)
|
.pagination(pagination)
|
||||||
.article_id(Some(article_id))
|
.article_id(Some(article_id))
|
||||||
|
@ -58,7 +65,8 @@ pub fn timestamp_and_author(sequence_number: i32, article_id: i32, created: &Dat
|
||||||
.into_link()
|
.into_link()
|
||||||
),
|
),
|
||||||
}),
|
}),
|
||||||
}.to_string()
|
}
|
||||||
|
.to_string()
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Resource for ArticleRevisionResource {
|
impl Resource for ArticleRevisionResource {
|
||||||
|
@ -68,44 +76,42 @@ impl Resource for ArticleRevisionResource {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn head(&self) -> ResponseFuture {
|
fn head(&self) -> ResponseFuture {
|
||||||
Box::new(futures::finished(Response::new()
|
Box::new(futures::finished(
|
||||||
|
Response::new()
|
||||||
.with_status(hyper::StatusCode::Ok)
|
.with_status(hyper::StatusCode::Ok)
|
||||||
.with_header(ContentType(TEXT_HTML.clone()))
|
.with_header(ContentType(TEXT_HTML.clone())),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get(self: Box<Self>) -> ResponseFuture {
|
fn get(self: Box<Self>) -> ResponseFuture {
|
||||||
#[derive(BartDisplay)]
|
#[derive(BartDisplay)]
|
||||||
#[template="templates/article_revision.html"]
|
#[template = "templates/article_revision.html"]
|
||||||
struct Template<'a> {
|
struct Template<'a> {
|
||||||
link_current: &'a str,
|
link_current: &'a str,
|
||||||
timestamp_and_author: &'a str,
|
timestamp_and_author: &'a str,
|
||||||
diff_link: Option<String>,
|
diff_link: Option<String>,
|
||||||
|
|
||||||
title: &'a str,
|
|
||||||
rendered: String,
|
rendered: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
let head = self.head();
|
let head = self.head();
|
||||||
let data = self.data;
|
let data = self.data;
|
||||||
|
|
||||||
Box::new(head
|
Box::new(head.and_then(move |head| {
|
||||||
.and_then(move |head|
|
Ok(head.with_body(
|
||||||
Ok(head
|
system_page(
|
||||||
.with_body(Layout {
|
Some("../../"), // Hmm, should perhaps accept `base` as argument
|
||||||
base: Some("../../"), // Hmm, should perhaps accept `base` as argument
|
&data.title,
|
||||||
title: &data.title,
|
&Template {
|
||||||
body: &Template {
|
|
||||||
link_current: &format!("_by_id/{}", data.article_id),
|
link_current: &format!("_by_id/{}", data.article_id),
|
||||||
timestamp_and_author: ×tamp_and_author(
|
timestamp_and_author: ×tamp_and_author(
|
||||||
data.sequence_number,
|
data.sequence_number,
|
||||||
data.article_id,
|
data.article_id,
|
||||||
&Local.from_utc_datetime(&data.created),
|
&Local.from_utc_datetime(&data.created),
|
||||||
data.author.as_ref().map(|x| &**x)
|
data.author.as_deref(),
|
||||||
),
|
),
|
||||||
diff_link:
|
diff_link: if data.revision > 1 {
|
||||||
if data.revision > 1 {
|
Some(format!(
|
||||||
Some(format!("_diff/{}?{}",
|
"_diff/{}?{}",
|
||||||
data.article_id,
|
data.article_id,
|
||||||
diff_resource::QueryParameters::new(
|
diff_resource::QueryParameters::new(
|
||||||
data.revision as u32 - 1,
|
data.revision as u32 - 1,
|
||||||
|
@ -115,10 +121,11 @@ impl Resource for ArticleRevisionResource {
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
},
|
},
|
||||||
title: &data.title,
|
|
||||||
rendered: render_markdown(&data.body),
|
rendered: render_markdown(&data.body),
|
||||||
},
|
},
|
||||||
}.to_string()))
|
)
|
||||||
|
.to_string(),
|
||||||
))
|
))
|
||||||
|
}))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,16 +1,14 @@
|
||||||
use diesel;
|
|
||||||
use futures::{self, Future};
|
|
||||||
use futures::future::{done, finished};
|
use futures::future::{done, finished};
|
||||||
use hyper;
|
use futures::{self, Future};
|
||||||
|
|
||||||
use hyper::header::ContentType;
|
use hyper::header::ContentType;
|
||||||
use hyper::server::*;
|
use hyper::server::*;
|
||||||
use serde_urlencoded;
|
|
||||||
|
|
||||||
use mimes::*;
|
use crate::mimes::*;
|
||||||
use schema::article_revisions;
|
use crate::schema::article_revisions;
|
||||||
use site::Layout;
|
use crate::site::system_page;
|
||||||
use state::State;
|
use crate::state::State;
|
||||||
use web::{Resource, ResponseFuture};
|
use crate::web::{Resource, ResponseFuture};
|
||||||
|
|
||||||
use super::diff_resource;
|
use super::diff_resource;
|
||||||
use super::pagination::Pagination;
|
use super::pagination::Pagination;
|
||||||
|
@ -18,7 +16,7 @@ use super::TemporaryRedirectResource;
|
||||||
|
|
||||||
const DEFAULT_LIMIT: i32 = 30;
|
const DEFAULT_LIMIT: i32 = 30;
|
||||||
|
|
||||||
type BoxResource = Box<Resource + Sync + Send>;
|
type BoxResource = Box<dyn Resource + Sync + Send>;
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct ChangesLookup {
|
pub struct ChangesLookup {
|
||||||
|
@ -40,8 +38,16 @@ pub struct QueryParameters {
|
||||||
impl QueryParameters {
|
impl QueryParameters {
|
||||||
pub fn pagination(self, pagination: Pagination<i32>) -> Self {
|
pub fn pagination(self, pagination: Pagination<i32>) -> Self {
|
||||||
Self {
|
Self {
|
||||||
after: if let Pagination::After(x) = pagination { Some(x) } else { None },
|
after: if let Pagination::After(x) = pagination {
|
||||||
before: if let Pagination::Before(x) = pagination { Some(x) } else { None },
|
Some(x)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
},
|
||||||
|
before: if let Pagination::Before(x) = pagination {
|
||||||
|
Some(x)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
},
|
||||||
..self
|
..self
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -56,14 +62,18 @@ impl QueryParameters {
|
||||||
|
|
||||||
pub fn limit(self, limit: i32) -> Self {
|
pub fn limit(self, limit: i32) -> Self {
|
||||||
Self {
|
Self {
|
||||||
limit: if limit != DEFAULT_LIMIT { Some(limit) } else { None },
|
limit: if limit != DEFAULT_LIMIT {
|
||||||
|
Some(limit)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
},
|
||||||
..self
|
..self
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn into_link(self) -> String {
|
pub fn into_link(self) -> String {
|
||||||
let args = serde_urlencoded::to_string(self).expect("Serializing to String cannot fail");
|
let args = serde_urlencoded::to_string(self).expect("Serializing to String cannot fail");
|
||||||
if args.len() > 0 {
|
if !args.is_empty() {
|
||||||
format!("?{}", args)
|
format!("?{}", args)
|
||||||
} else {
|
} else {
|
||||||
"_changes".to_owned()
|
"_changes".to_owned()
|
||||||
|
@ -71,14 +81,12 @@ impl QueryParameters {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn apply_query_config<'a>(
|
fn apply_query_config(
|
||||||
mut query: article_revisions::BoxedQuery<'a, diesel::sqlite::Sqlite>,
|
mut query: article_revisions::BoxedQuery<diesel::sqlite::Sqlite>,
|
||||||
article_id: Option<i32>,
|
article_id: Option<i32>,
|
||||||
author: Option<String>,
|
author: Option<String>,
|
||||||
limit: i32,
|
limit: i32,
|
||||||
)
|
) -> article_revisions::BoxedQuery<diesel::sqlite::Sqlite> {
|
||||||
-> article_revisions::BoxedQuery<'a, diesel::sqlite::Sqlite>
|
|
||||||
{
|
|
||||||
use diesel::prelude::*;
|
use diesel::prelude::*;
|
||||||
|
|
||||||
if let Some(article_id) = article_id {
|
if let Some(article_id) = article_id {
|
||||||
|
@ -94,10 +102,16 @@ fn apply_query_config<'a>(
|
||||||
|
|
||||||
impl ChangesLookup {
|
impl ChangesLookup {
|
||||||
pub fn new(state: State, show_authors: bool) -> ChangesLookup {
|
pub fn new(state: State, show_authors: bool) -> ChangesLookup {
|
||||||
Self { state, show_authors }
|
Self {
|
||||||
|
state,
|
||||||
|
show_authors,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn lookup(&self, query: Option<&str>) -> Box<Future<Item=Option<BoxResource>, Error=::web::Error>> {
|
pub fn lookup(
|
||||||
|
&self,
|
||||||
|
query: Option<&str>,
|
||||||
|
) -> Box<dyn Future<Item = Option<BoxResource>, Error = crate::web::Error>> {
|
||||||
use super::pagination;
|
use super::pagination;
|
||||||
|
|
||||||
let state = self.state.clone();
|
let state = self.state.clone();
|
||||||
|
@ -111,31 +125,34 @@ impl ChangesLookup {
|
||||||
|
|
||||||
let limit = match params.limit {
|
let limit = match params.limit {
|
||||||
None => Ok(DEFAULT_LIMIT),
|
None => Ok(DEFAULT_LIMIT),
|
||||||
Some(x) if 1 <= x && x <= 100 => Ok(x),
|
Some(x) if (1..=100).contains(&x) => Ok(x),
|
||||||
_ => Err("`limit` argument must be in range [1, 100]"),
|
_ => Err("`limit` argument must be in range [1, 100]"),
|
||||||
}?;
|
}?;
|
||||||
|
|
||||||
Ok((pagination, params.article_id, params.author, limit))
|
Ok((pagination, params.article_id, params.author, limit))
|
||||||
})())
|
})())
|
||||||
.and_then(move |(pagination, article_id, author, limit)| match pagination {
|
.and_then(move |(pagination, article_id, author, limit)| {
|
||||||
|
match pagination {
|
||||||
Pagination::After(x) => {
|
Pagination::After(x) => {
|
||||||
let author2 = author.clone();
|
let author2 = author.clone();
|
||||||
|
|
||||||
Box::new(state.query_article_revision_stubs(move |query| {
|
Box::new(
|
||||||
|
state
|
||||||
|
.query_article_revision_stubs(move |query| {
|
||||||
use diesel::prelude::*;
|
use diesel::prelude::*;
|
||||||
|
|
||||||
apply_query_config(query, article_id, author2, limit)
|
apply_query_config(query, article_id, author2, limit)
|
||||||
.filter(article_revisions::sequence_number.gt(x))
|
.filter(article_revisions::sequence_number.gt(x))
|
||||||
.order(article_revisions::sequence_number.asc())
|
.order(article_revisions::sequence_number.asc())
|
||||||
}).and_then(move |mut data| {
|
})
|
||||||
|
.and_then(move |mut data| {
|
||||||
let extra_element = if data.len() > limit as usize {
|
let extra_element = if data.len() > limit as usize {
|
||||||
data.pop()
|
data.pop()
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
};
|
};
|
||||||
|
|
||||||
let args =
|
let args = QueryParameters {
|
||||||
QueryParameters {
|
|
||||||
after: None,
|
after: None,
|
||||||
before: None,
|
before: None,
|
||||||
article_id,
|
article_id,
|
||||||
|
@ -146,19 +163,42 @@ impl ChangesLookup {
|
||||||
|
|
||||||
Ok(Some(match extra_element {
|
Ok(Some(match extra_element {
|
||||||
Some(x) => Box::new(TemporaryRedirectResource::new(
|
Some(x) => Box::new(TemporaryRedirectResource::new(
|
||||||
args
|
args.pagination(Pagination::Before(x.sequence_number))
|
||||||
.pagination(Pagination::Before(x.sequence_number))
|
.into_link(),
|
||||||
.into_link()
|
))
|
||||||
)) as BoxResource,
|
as BoxResource,
|
||||||
None => Box::new(TemporaryRedirectResource::new(
|
None => Box::new(TemporaryRedirectResource::new(
|
||||||
args.into_link()
|
args.into_link(),
|
||||||
)) as BoxResource,
|
))
|
||||||
|
as BoxResource,
|
||||||
}))
|
}))
|
||||||
})) as Box<Future<Item=Option<BoxResource>, Error=::web::Error>>
|
}),
|
||||||
},
|
)
|
||||||
Pagination::Before(x) => Box::new(finished(Some(Box::new(ChangesResource::new(state, show_authors, Some(x), article_id, author, limit)) as BoxResource))),
|
as Box<
|
||||||
Pagination::None => Box::new(finished(Some(Box::new(ChangesResource::new(state, show_authors, None, article_id, author, limit)) as BoxResource))),
|
dyn Future<Item = Option<BoxResource>, Error = crate::web::Error>,
|
||||||
})
|
>
|
||||||
|
}
|
||||||
|
Pagination::Before(x) => {
|
||||||
|
Box::new(finished(Some(Box::new(ChangesResource::new(
|
||||||
|
state,
|
||||||
|
show_authors,
|
||||||
|
Some(x),
|
||||||
|
article_id,
|
||||||
|
author,
|
||||||
|
limit,
|
||||||
|
)) as BoxResource)))
|
||||||
|
}
|
||||||
|
Pagination::None => Box::new(finished(Some(Box::new(ChangesResource::new(
|
||||||
|
state,
|
||||||
|
show_authors,
|
||||||
|
None,
|
||||||
|
article_id,
|
||||||
|
author,
|
||||||
|
limit,
|
||||||
|
))
|
||||||
|
as BoxResource))),
|
||||||
|
}
|
||||||
|
}),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -173,8 +213,22 @@ pub struct ChangesResource {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ChangesResource {
|
impl ChangesResource {
|
||||||
pub fn new(state: State, show_authors: bool, before: Option<i32>, article_id: Option<i32>, author: Option<String>, limit: i32) -> Self {
|
pub fn new(
|
||||||
Self { state, show_authors, before, article_id, author, limit }
|
state: State,
|
||||||
|
show_authors: bool,
|
||||||
|
before: Option<i32>,
|
||||||
|
article_id: Option<i32>,
|
||||||
|
author: Option<String>,
|
||||||
|
limit: i32,
|
||||||
|
) -> Self {
|
||||||
|
Self {
|
||||||
|
state,
|
||||||
|
show_authors,
|
||||||
|
before,
|
||||||
|
article_id,
|
||||||
|
author,
|
||||||
|
limit,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn query_args(&self) -> QueryParameters {
|
fn query_args(&self) -> QueryParameters {
|
||||||
|
@ -196,14 +250,15 @@ impl Resource for ChangesResource {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn head(&self) -> ResponseFuture {
|
fn head(&self) -> ResponseFuture {
|
||||||
Box::new(futures::finished(Response::new()
|
Box::new(futures::finished(
|
||||||
|
Response::new()
|
||||||
.with_status(hyper::StatusCode::Ok)
|
.with_status(hyper::StatusCode::Ok)
|
||||||
.with_header(ContentType(TEXT_HTML.clone()))
|
.with_header(ContentType(TEXT_HTML.clone())),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get(self: Box<Self>) -> ResponseFuture {
|
fn get(self: Box<Self>) -> ResponseFuture {
|
||||||
use chrono::{TimeZone, Local};
|
use chrono::{Local, TimeZone};
|
||||||
|
|
||||||
struct Row<'a> {
|
struct Row<'a> {
|
||||||
resource: &'a ChangesResource,
|
resource: &'a ChangesResource,
|
||||||
|
@ -224,7 +279,8 @@ impl Resource for ChangesResource {
|
||||||
|
|
||||||
impl<'a> Row<'a> {
|
impl<'a> Row<'a> {
|
||||||
fn author_link(&self) -> String {
|
fn author_link(&self) -> String {
|
||||||
self.resource.query_args()
|
self.resource
|
||||||
|
.query_args()
|
||||||
.pagination(Pagination::After(self.sequence_number))
|
.pagination(Pagination::After(self.sequence_number))
|
||||||
.author(self.author.clone())
|
.author(self.author.clone())
|
||||||
.into_link()
|
.into_link()
|
||||||
|
@ -237,7 +293,7 @@ impl Resource for ChangesResource {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(BartDisplay)]
|
#[derive(BartDisplay)]
|
||||||
#[template="templates/changes.html"]
|
#[template = "templates/changes.html"]
|
||||||
struct Template<'a> {
|
struct Template<'a> {
|
||||||
resource: &'a ChangesResource,
|
resource: &'a ChangesResource,
|
||||||
|
|
||||||
|
@ -251,7 +307,7 @@ impl Resource for ChangesResource {
|
||||||
fn subject_clause(&self) -> String {
|
fn subject_clause(&self) -> String {
|
||||||
match self.resource.article_id {
|
match self.resource.article_id {
|
||||||
Some(x) => format!(" <a href=\"_by_id/{}\">this article</a>", x),
|
Some(x) => format!(" <a href=\"_by_id/{}\">this article</a>", x),
|
||||||
None => format!(" the wiki"),
|
None => " the wiki".to_string(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -260,24 +316,25 @@ impl Resource for ChangesResource {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn all_articles_link(&self) -> Option<String> {
|
fn all_articles_link(&self) -> Option<String> {
|
||||||
self.resource.article_id.map(|_| {
|
self.resource
|
||||||
self.resource.query_args()
|
.article_id
|
||||||
.article_id(None)
|
.map(|_| self.resource.query_args().article_id(None).into_link())
|
||||||
.into_link()
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn all_authors_link(&self) -> Option<String> {
|
fn all_authors_link(&self) -> Option<String> {
|
||||||
self.resource.author.as_ref().map(|_| {
|
self.resource
|
||||||
self.resource.query_args()
|
.author
|
||||||
.author(None)
|
.as_ref()
|
||||||
.into_link()
|
.map(|_| self.resource.query_args().author(None).into_link())
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let (before, article_id, author, limit) =
|
let (before, article_id, author, limit) = (
|
||||||
(self.before.clone(), self.article_id.clone(), self.author.clone(), self.limit);
|
self.before,
|
||||||
|
self.article_id,
|
||||||
|
self.author.clone(),
|
||||||
|
self.limit,
|
||||||
|
);
|
||||||
let data = self.state.query_article_revision_stubs(move |query| {
|
let data = self.state.query_article_revision_stubs(move |query| {
|
||||||
use diesel::prelude::*;
|
use diesel::prelude::*;
|
||||||
|
|
||||||
|
@ -292,10 +349,7 @@ impl Resource for ChangesResource {
|
||||||
|
|
||||||
let head = self.head();
|
let head = self.head();
|
||||||
|
|
||||||
Box::new(data.join(head)
|
Box::new(data.join(head).and_then(move |(mut data, head)| {
|
||||||
.and_then(move |(mut data, head)| {
|
|
||||||
use std::iter::Iterator;
|
|
||||||
|
|
||||||
let extra_element = if data.len() > self.limit as usize {
|
let extra_element = if data.len() > self.limit as usize {
|
||||||
data.pop()
|
data.pop()
|
||||||
} else {
|
} else {
|
||||||
|
@ -305,29 +359,41 @@ impl Resource for ChangesResource {
|
||||||
let (newer, older) = match self.before {
|
let (newer, older) = match self.before {
|
||||||
Some(x) => (
|
Some(x) => (
|
||||||
Some(NavLinks {
|
Some(NavLinks {
|
||||||
more: self.query_args().pagination(Pagination::After(x-1)).into_link(),
|
more: self
|
||||||
|
.query_args()
|
||||||
|
.pagination(Pagination::After(x - 1))
|
||||||
|
.into_link(),
|
||||||
end: self.query_args().pagination(Pagination::None).into_link(),
|
end: self.query_args().pagination(Pagination::None).into_link(),
|
||||||
}),
|
}),
|
||||||
extra_element.map(|_| NavLinks {
|
extra_element.map(|_| NavLinks {
|
||||||
more: self.query_args()
|
more: self
|
||||||
|
.query_args()
|
||||||
.pagination(Pagination::Before(data.last().unwrap().sequence_number))
|
.pagination(Pagination::Before(data.last().unwrap().sequence_number))
|
||||||
.into_link(),
|
.into_link(),
|
||||||
end: self.query_args().pagination(Pagination::After(0)).into_link(),
|
end: self
|
||||||
})
|
.query_args()
|
||||||
|
.pagination(Pagination::After(0))
|
||||||
|
.into_link(),
|
||||||
|
}),
|
||||||
),
|
),
|
||||||
None => (
|
None => (
|
||||||
None,
|
None,
|
||||||
extra_element.map(|_| NavLinks {
|
extra_element.map(|_| NavLinks {
|
||||||
more: self.query_args()
|
more: self
|
||||||
|
.query_args()
|
||||||
.pagination(Pagination::Before(data.last().unwrap().sequence_number))
|
.pagination(Pagination::Before(data.last().unwrap().sequence_number))
|
||||||
.into_link(),
|
.into_link(),
|
||||||
end: self.query_args().pagination(Pagination::After(0)).into_link(),
|
end: self
|
||||||
|
.query_args()
|
||||||
|
.pagination(Pagination::After(0))
|
||||||
|
.into_link(),
|
||||||
}),
|
}),
|
||||||
),
|
),
|
||||||
};
|
};
|
||||||
|
|
||||||
let changes = &data.into_iter().map(|x| {
|
let changes = &data
|
||||||
Row {
|
.into_iter()
|
||||||
|
.map(|x| Row {
|
||||||
resource: &self,
|
resource: &self,
|
||||||
sequence_number: x.sequence_number,
|
sequence_number: x.sequence_number,
|
||||||
article_id: x.article_id,
|
article_id: x.article_id,
|
||||||
|
@ -337,9 +403,9 @@ impl Resource for ChangesResource {
|
||||||
_slug: x.slug,
|
_slug: x.slug,
|
||||||
title: x.title,
|
title: x.title,
|
||||||
_latest: x.latest,
|
_latest: x.latest,
|
||||||
diff_link:
|
diff_link: if x.revision > 1 {
|
||||||
if x.revision > 1 {
|
Some(format!(
|
||||||
Some(format!("_diff/{}?{}",
|
"_diff/{}?{}",
|
||||||
x.article_id,
|
x.article_id,
|
||||||
diff_resource::QueryParameters::new(
|
diff_resource::QueryParameters::new(
|
||||||
x.revision as u32 - 1,
|
x.revision as u32 - 1,
|
||||||
|
@ -349,21 +415,23 @@ impl Resource for ChangesResource {
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
},
|
},
|
||||||
}
|
})
|
||||||
}).collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
Ok(head
|
Ok(head.with_body(
|
||||||
.with_body(Layout {
|
system_page(
|
||||||
base: None, // Hmm, should perhaps accept `base` as argument
|
None, // Hmm, should perhaps accept `base` as argument
|
||||||
title: "Changes",
|
"Changes",
|
||||||
body: &Template {
|
Template {
|
||||||
resource: &self,
|
resource: &self,
|
||||||
show_authors: self.show_authors,
|
show_authors: self.show_authors,
|
||||||
newer,
|
newer,
|
||||||
older,
|
older,
|
||||||
changes
|
changes,
|
||||||
},
|
},
|
||||||
}.to_string()))
|
)
|
||||||
|
.to_string(),
|
||||||
|
))
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,23 +1,22 @@
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
|
||||||
use diff;
|
|
||||||
use futures::{self, Future};
|
|
||||||
use futures::future::done;
|
use futures::future::done;
|
||||||
use hyper;
|
use futures::{self, Future};
|
||||||
|
|
||||||
use hyper::header::ContentType;
|
use hyper::header::ContentType;
|
||||||
use hyper::server::*;
|
use hyper::server::*;
|
||||||
use serde_urlencoded;
|
|
||||||
|
|
||||||
use mimes::*;
|
use crate::mimes::*;
|
||||||
use models::ArticleRevision;
|
use crate::models::ArticleRevision;
|
||||||
use site::Layout;
|
use crate::site::Layout;
|
||||||
use state::State;
|
use crate::state::State;
|
||||||
use web::{Resource, ResponseFuture};
|
use crate::theme;
|
||||||
|
use crate::web::{Resource, ResponseFuture};
|
||||||
|
|
||||||
use super::changes_resource;
|
use super::changes_resource;
|
||||||
use super::pagination::Pagination;
|
use super::pagination::Pagination;
|
||||||
|
|
||||||
type BoxResource = Box<Resource + Sync + Send>;
|
type BoxResource = Box<dyn Resource + Sync + Send>;
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct DiffLookup {
|
pub struct DiffLookup {
|
||||||
|
@ -47,25 +46,28 @@ impl DiffLookup {
|
||||||
Self { state }
|
Self { state }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn lookup(&self, article_id: u32, query: Option<&str>) -> Box<Future<Item=Option<BoxResource>, Error=::web::Error>> {
|
pub fn lookup(
|
||||||
|
&self,
|
||||||
|
article_id: u32,
|
||||||
|
query: Option<&str>,
|
||||||
|
) -> Box<dyn Future<Item = Option<BoxResource>, Error = crate::web::Error>> {
|
||||||
let state = self.state.clone();
|
let state = self.state.clone();
|
||||||
|
|
||||||
Box::new(done(
|
Box::new(
|
||||||
serde_urlencoded::from_str(query.unwrap_or(""))
|
done(serde_urlencoded::from_str(query.unwrap_or("")).map_err(Into::into))
|
||||||
.map_err(Into::into)
|
.and_then(move |params: QueryParameters| {
|
||||||
).and_then(move |params: QueryParameters| {
|
|
||||||
let from = state.get_article_revision(article_id as i32, params.from as i32);
|
let from = state.get_article_revision(article_id as i32, params.from as i32);
|
||||||
let to = state.get_article_revision(article_id as i32, params.to as i32);
|
let to = state.get_article_revision(article_id as i32, params.to as i32);
|
||||||
|
|
||||||
from.join(to)
|
from.join(to)
|
||||||
}).and_then(move |(from, to)| {
|
})
|
||||||
match (from, to) {
|
.and_then(move |(from, to)| match (from, to) {
|
||||||
(Some(from), Some(to)) =>
|
(Some(from), Some(to)) => {
|
||||||
Ok(Some(Box::new(DiffResource::new(from, to)) as BoxResource)),
|
Ok(Some(Box::new(DiffResource::new(from, to)) as BoxResource))
|
||||||
_ =>
|
|
||||||
Ok(None),
|
|
||||||
}
|
}
|
||||||
}))
|
_ => Ok(None),
|
||||||
|
}),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -88,9 +90,10 @@ impl Resource for DiffResource {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn head(&self) -> ResponseFuture {
|
fn head(&self) -> ResponseFuture {
|
||||||
Box::new(futures::finished(Response::new()
|
Box::new(futures::finished(
|
||||||
|
Response::new()
|
||||||
.with_status(hyper::StatusCode::Ok)
|
.with_status(hyper::StatusCode::Ok)
|
||||||
.with_header(ContentType(TEXT_HTML.clone()))
|
.with_header(ContentType(TEXT_HTML.clone())),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -100,6 +103,8 @@ impl Resource for DiffResource {
|
||||||
struct Template<'a> {
|
struct Template<'a> {
|
||||||
consecutive: bool,
|
consecutive: bool,
|
||||||
article_id: u32,
|
article_id: u32,
|
||||||
|
author: Option<&'a str>,
|
||||||
|
author_link: &'a str,
|
||||||
article_history_link: &'a str,
|
article_history_link: &'a str,
|
||||||
from_link: &'a str,
|
from_link: &'a str,
|
||||||
to_link: &'a str,
|
to_link: &'a str,
|
||||||
|
@ -116,41 +121,88 @@ impl Resource for DiffResource {
|
||||||
|
|
||||||
let head = self.head();
|
let head = self.head();
|
||||||
|
|
||||||
Box::new(head
|
Box::new(head.and_then(move |head| {
|
||||||
.and_then(move |head| {
|
let consecutive = self.to.revision - self.from.revision == 1;
|
||||||
Ok(head
|
|
||||||
.with_body(Layout {
|
let author = match consecutive {
|
||||||
base: Some("../"), // Hmm, should perhaps accept `base` as argument
|
true => self.to.author.as_deref(),
|
||||||
title: "Difference",
|
false => None,
|
||||||
body: &Template {
|
};
|
||||||
consecutive: self.to.revision - self.from.revision == 1,
|
|
||||||
article_id: self.from.article_id as u32,
|
let author_link = &format!(
|
||||||
article_history_link: &format!("_changes{}",
|
"_changes{}",
|
||||||
|
changes_resource::QueryParameters::default()
|
||||||
|
.author(author.map(|x| x.to_owned()))
|
||||||
|
.pagination(Pagination::After(self.from.sequence_number))
|
||||||
|
.into_link()
|
||||||
|
);
|
||||||
|
|
||||||
|
let article_history_link = &format!(
|
||||||
|
"_changes{}",
|
||||||
changes_resource::QueryParameters::default()
|
changes_resource::QueryParameters::default()
|
||||||
.article_id(Some(self.from.article_id))
|
.article_id(Some(self.from.article_id))
|
||||||
.pagination(Pagination::After(self.from.revision))
|
.pagination(Pagination::After(self.from.sequence_number))
|
||||||
.into_link()
|
.into_link()
|
||||||
),
|
);
|
||||||
from_link: &format!("_revisions/{}/{}", self.from.article_id, self.from.revision),
|
|
||||||
to_link: &format!("_revisions/{}/{}", self.to.article_id, self.to.revision),
|
let title = &diff::chars(&self.from.title, &self.to.title)
|
||||||
title: &diff::chars(&self.from.title, &self.to.title)
|
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|x| match x {
|
.map(|x| match x {
|
||||||
diff::Result::Left(x) => Diff { removed: Some(x), ..Default::default() },
|
diff::Result::Left(x) => Diff {
|
||||||
diff::Result::Both(x, _) => Diff { same: Some(x), ..Default::default() },
|
removed: Some(x),
|
||||||
diff::Result::Right(x) => Diff { added: Some(x), ..Default::default() },
|
..Default::default()
|
||||||
})
|
|
||||||
.collect::<Vec<_>>(),
|
|
||||||
lines: &diff::lines(&self.from.body, &self.to.body)
|
|
||||||
.into_iter()
|
|
||||||
.map(|x| match x {
|
|
||||||
diff::Result::Left(x) => Diff { removed: Some(x), ..Default::default() },
|
|
||||||
diff::Result::Both(x, _) => Diff { same: Some(x), ..Default::default() },
|
|
||||||
diff::Result::Right(x) => Diff { added: Some(x), ..Default::default() },
|
|
||||||
})
|
|
||||||
.collect::<Vec<_>>()
|
|
||||||
},
|
},
|
||||||
}.to_string()))
|
diff::Result::Both(x, _) => Diff {
|
||||||
|
same: Some(x),
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
diff::Result::Right(x) => Diff {
|
||||||
|
added: Some(x),
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
})
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
|
let lines = &diff::lines(&self.from.body, &self.to.body)
|
||||||
|
.into_iter()
|
||||||
|
.map(|x| match x {
|
||||||
|
diff::Result::Left(x) => Diff {
|
||||||
|
removed: Some(x),
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
diff::Result::Both(x, _) => Diff {
|
||||||
|
same: Some(x),
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
diff::Result::Right(x) => Diff {
|
||||||
|
added: Some(x),
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
})
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
|
Ok(head.with_body(
|
||||||
|
Layout {
|
||||||
|
base: Some("../"), // Hmm, should perhaps accept `base` as argument
|
||||||
|
title: "Difference",
|
||||||
|
theme: theme::theme_from_str_hash("Difference"),
|
||||||
|
body: &Template {
|
||||||
|
consecutive,
|
||||||
|
article_id: self.from.article_id as u32,
|
||||||
|
author,
|
||||||
|
author_link,
|
||||||
|
article_history_link,
|
||||||
|
from_link: &format!(
|
||||||
|
"_revisions/{}/{}",
|
||||||
|
self.from.article_id, self.from.revision
|
||||||
|
),
|
||||||
|
to_link: &format!("_revisions/{}/{}", self.to.article_id, self.to.revision),
|
||||||
|
title,
|
||||||
|
lines,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
.to_string(),
|
||||||
|
))
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
use futures::{self, Future};
|
use futures::{self, Future};
|
||||||
use hyper;
|
|
||||||
use hyper::header::ContentType;
|
use hyper::header::ContentType;
|
||||||
use hyper::server::*;
|
use hyper::server::*;
|
||||||
|
|
||||||
use mimes::*;
|
use crate::mimes::*;
|
||||||
use site::Layout;
|
use crate::site::system_page;
|
||||||
use web::{Resource, ResponseFuture};
|
use crate::web::{Resource, ResponseFuture};
|
||||||
|
|
||||||
pub struct HtmlResource {
|
pub struct HtmlResource {
|
||||||
base: Option<&'static str>,
|
base: Option<&'static str>,
|
||||||
|
@ -15,15 +15,12 @@ pub struct HtmlResource {
|
||||||
|
|
||||||
impl HtmlResource {
|
impl HtmlResource {
|
||||||
pub fn new(base: Option<&'static str>, title: &'static str, html_body: &'static str) -> Self {
|
pub fn new(base: Option<&'static str>, title: &'static str, html_body: &'static str) -> Self {
|
||||||
HtmlResource { base, title, html_body }
|
HtmlResource {
|
||||||
|
base,
|
||||||
|
title,
|
||||||
|
html_body,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(BartDisplay)]
|
|
||||||
#[template="templates/simple.html"]
|
|
||||||
struct Template<'a> {
|
|
||||||
title: &'a str,
|
|
||||||
html_body: &'a str,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Resource for HtmlResource {
|
impl Resource for HtmlResource {
|
||||||
|
@ -33,26 +30,18 @@ impl Resource for HtmlResource {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn head(&self) -> ResponseFuture {
|
fn head(&self) -> ResponseFuture {
|
||||||
Box::new(futures::finished(Response::new()
|
Box::new(futures::finished(
|
||||||
|
Response::new()
|
||||||
.with_status(hyper::StatusCode::Ok)
|
.with_status(hyper::StatusCode::Ok)
|
||||||
.with_header(ContentType(TEXT_HTML.clone()))
|
.with_header(ContentType(TEXT_HTML.clone())),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get(self: Box<Self>) -> ResponseFuture {
|
fn get(self: Box<Self>) -> ResponseFuture {
|
||||||
let head = self.head();
|
let head = self.head();
|
||||||
|
|
||||||
Box::new(head
|
Box::new(head.and_then(move |head| {
|
||||||
.and_then(move |head| {
|
Ok(head.with_body(system_page(self.base, self.title, self.html_body).to_string()))
|
||||||
Ok(head
|
|
||||||
.with_body(Layout {
|
|
||||||
base: self.base,
|
|
||||||
title: self.title,
|
|
||||||
body: &Template {
|
|
||||||
title: self.title,
|
|
||||||
html_body: self.html_body,
|
|
||||||
},
|
|
||||||
}.to_string()))
|
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,23 +1,25 @@
|
||||||
pub mod pagination;
|
pub mod pagination;
|
||||||
|
|
||||||
mod about_resource;
|
mod about_resource;
|
||||||
mod article_revision_resource;
|
|
||||||
mod article_resource;
|
mod article_resource;
|
||||||
|
mod article_revision_resource;
|
||||||
mod changes_resource;
|
mod changes_resource;
|
||||||
mod diff_resource;
|
mod diff_resource;
|
||||||
mod html_resource;
|
mod html_resource;
|
||||||
mod new_article_resource;
|
mod new_article_resource;
|
||||||
|
mod read_only_resource;
|
||||||
mod search_resource;
|
mod search_resource;
|
||||||
mod sitemap_resource;
|
mod sitemap_resource;
|
||||||
mod temporary_redirect_resource;
|
mod temporary_redirect_resource;
|
||||||
|
|
||||||
pub use self::about_resource::AboutResource;
|
pub use self::about_resource::AboutResource;
|
||||||
pub use self::article_revision_resource::ArticleRevisionResource;
|
|
||||||
pub use self::article_resource::ArticleResource;
|
pub use self::article_resource::ArticleResource;
|
||||||
|
pub use self::article_revision_resource::ArticleRevisionResource;
|
||||||
pub use self::changes_resource::{ChangesLookup, ChangesResource};
|
pub use self::changes_resource::{ChangesLookup, ChangesResource};
|
||||||
pub use self::diff_resource::{DiffLookup, DiffResource};
|
pub use self::diff_resource::{DiffLookup, DiffResource};
|
||||||
pub use self::html_resource::HtmlResource;
|
pub use self::html_resource::HtmlResource;
|
||||||
pub use self::new_article_resource::NewArticleResource;
|
pub use self::new_article_resource::NewArticleResource;
|
||||||
|
pub use self::read_only_resource::ReadOnlyResource;
|
||||||
pub use self::search_resource::SearchLookup;
|
pub use self::search_resource::SearchLookup;
|
||||||
pub use self::sitemap_resource::SitemapResource;
|
pub use self::sitemap_resource::SitemapResource;
|
||||||
pub use self::temporary_redirect_resource::TemporaryRedirectResource;
|
pub use self::temporary_redirect_resource::TemporaryRedirectResource;
|
||||||
|
|
|
@ -1,16 +1,15 @@
|
||||||
use futures::{self, Future};
|
use futures::{self, Future};
|
||||||
use hyper;
|
|
||||||
use hyper::header::{ContentType, Location};
|
use hyper::header::{ContentType, Location};
|
||||||
use hyper::server::*;
|
use hyper::server::*;
|
||||||
use serde_json;
|
|
||||||
use serde_urlencoded;
|
|
||||||
|
|
||||||
use assets::ScriptJs;
|
use crate::assets::ScriptJs;
|
||||||
use mimes::*;
|
use crate::mimes::*;
|
||||||
use rendering::render_markdown;
|
use crate::rendering::render_markdown;
|
||||||
use site::Layout;
|
use crate::site::Layout;
|
||||||
use state::State;
|
use crate::state::State;
|
||||||
use web::{Resource, ResponseFuture};
|
use crate::theme::{self, Theme};
|
||||||
|
use crate::web::{Resource, ResponseFuture};
|
||||||
|
|
||||||
const NEW: &str = "NEW";
|
const NEW: &str = "NEW";
|
||||||
|
|
||||||
|
@ -27,6 +26,7 @@ fn title_from_slug(slug: &str) -> String {
|
||||||
pub struct NewArticleResource {
|
pub struct NewArticleResource {
|
||||||
state: State,
|
state: State,
|
||||||
slug: Option<String>,
|
slug: Option<String>,
|
||||||
|
edit: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
|
@ -34,11 +34,12 @@ struct CreateArticle {
|
||||||
base_revision: String,
|
base_revision: String,
|
||||||
title: String,
|
title: String,
|
||||||
body: String,
|
body: String,
|
||||||
|
theme: Option<Theme>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl NewArticleResource {
|
impl NewArticleResource {
|
||||||
pub fn new(state: State, slug: Option<String>) -> Self {
|
pub fn new(state: State, slug: Option<String>, edit: bool) -> Self {
|
||||||
Self { state, slug }
|
Self { state, slug, edit }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -49,15 +50,22 @@ impl Resource for NewArticleResource {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn head(&self) -> ResponseFuture {
|
fn head(&self) -> ResponseFuture {
|
||||||
Box::new(futures::finished(Response::new()
|
Box::new(futures::finished(
|
||||||
|
Response::new()
|
||||||
.with_status(hyper::StatusCode::NotFound)
|
.with_status(hyper::StatusCode::NotFound)
|
||||||
.with_header(ContentType(TEXT_HTML.clone()))
|
.with_header(ContentType(TEXT_HTML.clone())),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get(self: Box<Self>) -> ResponseFuture {
|
fn get(self: Box<Self>) -> ResponseFuture {
|
||||||
|
// TODO Remove duplication with article_resource.rs:
|
||||||
|
struct SelectableTheme {
|
||||||
|
theme: Theme,
|
||||||
|
selected: bool,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(BartDisplay)]
|
#[derive(BartDisplay)]
|
||||||
#[template="templates/article.html"]
|
#[template = "templates/article.html"]
|
||||||
struct Template<'a> {
|
struct Template<'a> {
|
||||||
revision: &'a str,
|
revision: &'a str,
|
||||||
last_updated: Option<&'a str>,
|
last_updated: Option<&'a str>,
|
||||||
|
@ -67,36 +75,44 @@ impl Resource for NewArticleResource {
|
||||||
title: &'a str,
|
title: &'a str,
|
||||||
raw: &'a str,
|
raw: &'a str,
|
||||||
rendered: &'a str,
|
rendered: &'a str,
|
||||||
|
themes: &'a [SelectableTheme],
|
||||||
}
|
}
|
||||||
impl<'a> Template<'a> {
|
impl<'a> Template<'a> {
|
||||||
fn script_js_checksum(&self) -> &'static str {
|
fn script_js(&self) -> &'static str {
|
||||||
ScriptJs::checksum()
|
ScriptJs::resource_name()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let title = self.slug.as_ref()
|
let title = self
|
||||||
|
.slug
|
||||||
|
.as_ref()
|
||||||
.map_or("".to_owned(), |x| title_from_slug(x));
|
.map_or("".to_owned(), |x| title_from_slug(x));
|
||||||
|
|
||||||
Box::new(self.head()
|
Box::new(self.head().and_then(move |head| {
|
||||||
.and_then(move |head| {
|
Ok(head.with_body(
|
||||||
Ok(head
|
Layout {
|
||||||
.with_body(Layout {
|
|
||||||
base: None, // Hmm, should perhaps accept `base` as argument
|
base: None, // Hmm, should perhaps accept `base` as argument
|
||||||
title: &title,
|
title: &title,
|
||||||
|
theme: theme::Theme::Gray,
|
||||||
body: &Template {
|
body: &Template {
|
||||||
revision: NEW,
|
revision: NEW,
|
||||||
last_updated: None,
|
last_updated: None,
|
||||||
|
edit: self.edit,
|
||||||
// Implicitly start in edit-mode when no slug is given. This
|
cancel_url: self.slug.as_deref(),
|
||||||
// currently directly corresponds to the /_new endpoint
|
|
||||||
edit: self.slug.is_none(),
|
|
||||||
|
|
||||||
cancel_url: self.slug.as_ref().map(|x| &**x),
|
|
||||||
title: &title,
|
title: &title,
|
||||||
raw: "",
|
raw: "",
|
||||||
rendered: EMPTY_ARTICLE_MESSAGE,
|
rendered: EMPTY_ARTICLE_MESSAGE,
|
||||||
|
themes: &theme::THEMES
|
||||||
|
.iter()
|
||||||
|
.map(|&x| SelectableTheme {
|
||||||
|
theme: x,
|
||||||
|
selected: false,
|
||||||
|
})
|
||||||
|
.collect::<Vec<_>>(),
|
||||||
},
|
},
|
||||||
}.to_string()))
|
}
|
||||||
|
.to_string(),
|
||||||
|
))
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -104,11 +120,11 @@ impl Resource for NewArticleResource {
|
||||||
// TODO Check incoming Content-Type
|
// TODO Check incoming Content-Type
|
||||||
// TODO Refactor? Reduce duplication with ArticleResource::put?
|
// TODO Refactor? Reduce duplication with ArticleResource::put?
|
||||||
|
|
||||||
use chrono::{TimeZone, Local};
|
use chrono::{Local, TimeZone};
|
||||||
use futures::Stream;
|
use futures::Stream;
|
||||||
|
|
||||||
#[derive(BartDisplay)]
|
#[derive(BartDisplay)]
|
||||||
#[template="templates/article_contents.html"]
|
#[template = "templates/article_contents.html"]
|
||||||
struct Template<'a> {
|
struct Template<'a> {
|
||||||
title: &'a str,
|
title: &'a str,
|
||||||
rendered: String,
|
rendered: String,
|
||||||
|
@ -121,45 +137,56 @@ impl Resource for NewArticleResource {
|
||||||
revision: i32,
|
revision: i32,
|
||||||
title: &'a str,
|
title: &'a str,
|
||||||
body: &'a str,
|
body: &'a str,
|
||||||
|
theme: Theme,
|
||||||
rendered: &'a str,
|
rendered: &'a str,
|
||||||
last_updated: &'a str,
|
last_updated: &'a str,
|
||||||
}
|
}
|
||||||
|
|
||||||
Box::new(body
|
Box::new(
|
||||||
.concat2()
|
body.concat2()
|
||||||
.map_err(Into::into)
|
.map_err(Into::into)
|
||||||
.and_then(|body| {
|
.and_then(|body| serde_urlencoded::from_bytes(&body).map_err(Into::into))
|
||||||
serde_urlencoded::from_bytes(&body)
|
|
||||||
.map_err(Into::into)
|
|
||||||
})
|
|
||||||
.and_then(move |arg: CreateArticle| {
|
.and_then(move |arg: CreateArticle| {
|
||||||
if arg.base_revision != NEW {
|
if arg.base_revision != NEW {
|
||||||
unimplemented!("Version update conflict");
|
unimplemented!("Version update conflict");
|
||||||
}
|
}
|
||||||
self.state.create_article(self.slug.clone(), arg.title, arg.body, identity)
|
let theme = arg.theme.unwrap_or_else(theme::random);
|
||||||
|
self.state.create_article(
|
||||||
|
self.slug.clone(),
|
||||||
|
arg.title,
|
||||||
|
arg.body,
|
||||||
|
identity,
|
||||||
|
theme,
|
||||||
|
)
|
||||||
})
|
})
|
||||||
.and_then(|updated| {
|
.and_then(|updated| {
|
||||||
futures::finished(Response::new()
|
futures::finished(
|
||||||
|
Response::new()
|
||||||
.with_status(hyper::StatusCode::Ok)
|
.with_status(hyper::StatusCode::Ok)
|
||||||
.with_header(ContentType(APPLICATION_JSON.clone()))
|
.with_header(ContentType(APPLICATION_JSON.clone()))
|
||||||
.with_body(serde_json::to_string(&PutResponse {
|
.with_body(
|
||||||
|
serde_json::to_string(&PutResponse {
|
||||||
slug: &updated.slug,
|
slug: &updated.slug,
|
||||||
article_id: updated.article_id,
|
article_id: updated.article_id,
|
||||||
revision: updated.revision,
|
revision: updated.revision,
|
||||||
title: &updated.title,
|
title: &updated.title,
|
||||||
body: &updated.body,
|
body: &updated.body,
|
||||||
|
theme: updated.theme,
|
||||||
rendered: &Template {
|
rendered: &Template {
|
||||||
title: &updated.title,
|
title: &updated.title,
|
||||||
rendered: render_markdown(&updated.body),
|
rendered: render_markdown(&updated.body),
|
||||||
}.to_string(),
|
}
|
||||||
|
.to_string(),
|
||||||
last_updated: &super::article_resource::last_updated(
|
last_updated: &super::article_resource::last_updated(
|
||||||
updated.article_id,
|
updated.article_id,
|
||||||
&Local.from_utc_datetime(&updated.created),
|
&Local.from_utc_datetime(&updated.created),
|
||||||
updated.author.as_ref().map(|x| &**x)
|
updated.author.as_deref(),
|
||||||
),
|
),
|
||||||
}).expect("Should never fail"))
|
|
||||||
)
|
|
||||||
})
|
})
|
||||||
|
.expect("Should never fail"),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
}),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -169,27 +196,32 @@ impl Resource for NewArticleResource {
|
||||||
|
|
||||||
use futures::Stream;
|
use futures::Stream;
|
||||||
|
|
||||||
Box::new(body
|
Box::new(
|
||||||
.concat2()
|
body.concat2()
|
||||||
.map_err(Into::into)
|
.map_err(Into::into)
|
||||||
.and_then(|body| {
|
.and_then(|body| serde_urlencoded::from_bytes(&body).map_err(Into::into))
|
||||||
serde_urlencoded::from_bytes(&body)
|
|
||||||
.map_err(Into::into)
|
|
||||||
})
|
|
||||||
.and_then(move |arg: CreateArticle| {
|
.and_then(move |arg: CreateArticle| {
|
||||||
if arg.base_revision != NEW {
|
if arg.base_revision != NEW {
|
||||||
unimplemented!("Version update conflict");
|
unimplemented!("Version update conflict");
|
||||||
}
|
}
|
||||||
self.state.create_article(self.slug.clone(), arg.title, arg.body, identity)
|
let theme = arg.theme.unwrap_or_else(theme::random);
|
||||||
|
self.state.create_article(
|
||||||
|
self.slug.clone(),
|
||||||
|
arg.title,
|
||||||
|
arg.body,
|
||||||
|
identity,
|
||||||
|
theme,
|
||||||
|
)
|
||||||
})
|
})
|
||||||
.and_then(|updated| {
|
.and_then(|updated| {
|
||||||
futures::finished(Response::new()
|
futures::finished(
|
||||||
|
Response::new()
|
||||||
.with_status(hyper::StatusCode::SeeOther)
|
.with_status(hyper::StatusCode::SeeOther)
|
||||||
.with_header(ContentType(TEXT_PLAIN.clone()))
|
.with_header(ContentType(TEXT_PLAIN.clone()))
|
||||||
.with_header(Location::new(updated.link().to_owned()))
|
.with_header(Location::new(updated.link().to_owned()))
|
||||||
.with_body("See other")
|
.with_body("See other"),
|
||||||
)
|
)
|
||||||
})
|
}),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,15 +8,11 @@ pub struct Error;
|
||||||
|
|
||||||
impl fmt::Display for Error {
|
impl fmt::Display for Error {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
|
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
|
||||||
write!(f, "{}", (self as &error::Error).description())
|
write!(f, "`after` and `before` are mutually exclusive")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl error::Error for Error {
|
impl error::Error for Error {}
|
||||||
fn description(&self) -> &str {
|
|
||||||
"`after` and `before` are mutually exclusive"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
struct PaginationStruct<T> {
|
struct PaginationStruct<T> {
|
||||||
|
@ -37,16 +33,16 @@ impl<T> PaginationStruct<T> {
|
||||||
(Some(x), None) => Ok(Pagination::After(x)),
|
(Some(x), None) => Ok(Pagination::After(x)),
|
||||||
(None, Some(x)) => Ok(Pagination::Before(x)),
|
(None, Some(x)) => Ok(Pagination::Before(x)),
|
||||||
(None, None) => Ok(Pagination::None),
|
(None, None) => Ok(Pagination::None),
|
||||||
_ => Err(Error)
|
_ => Err(Error),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn _from_str<'a, T: serde::Deserialize<'a>>(s: &'a str) -> Result<Pagination<T>, Error> {
|
pub fn _from_str<'a, T: serde::Deserialize<'a>>(s: &'a str) -> Result<Pagination<T>, Error> {
|
||||||
let pagination: PaginationStruct<T> = serde_urlencoded::from_str(s).map_err(|_| Error)?; // TODO Proper error reporting
|
let pagination: PaginationStruct<T> = serde_urlencoded::from_str(s).map_err(|_| Error)?; // TODO Proper error reporting
|
||||||
Ok(pagination.into_enum()?)
|
pagination.into_enum()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn from_fields<T>(after: Option<T>, before: Option<T>) -> Result<Pagination<T>, Error> {
|
pub fn from_fields<T>(after: Option<T>, before: Option<T>) -> Result<Pagination<T>, Error> {
|
||||||
Ok(PaginationStruct { after, before }.into_enum()?)
|
PaginationStruct { after, before }.into_enum()
|
||||||
}
|
}
|
||||||
|
|
38
src/resources/read_only_resource.rs
Normal file
38
src/resources/read_only_resource.rs
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
use futures::Future;
|
||||||
|
use hyper::header::{CacheControl, CacheDirective, ContentLength, ContentType};
|
||||||
|
use hyper::server::*;
|
||||||
|
use hyper::StatusCode;
|
||||||
|
|
||||||
|
use crate::web::{Resource, ResponseFuture};
|
||||||
|
|
||||||
|
#[allow(unused)]
|
||||||
|
pub struct ReadOnlyResource {
|
||||||
|
pub content_type: ::hyper::mime::Mime,
|
||||||
|
pub body: Vec<u8>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Resource for ReadOnlyResource {
|
||||||
|
fn allow(&self) -> Vec<::hyper::Method> {
|
||||||
|
use ::hyper::Method::*;
|
||||||
|
vec![Options, Head, Get]
|
||||||
|
}
|
||||||
|
|
||||||
|
fn head(&self) -> ResponseFuture {
|
||||||
|
Box::new(::futures::finished(
|
||||||
|
Response::new()
|
||||||
|
.with_status(StatusCode::Ok)
|
||||||
|
.with_header(ContentType(self.content_type.clone()))
|
||||||
|
.with_header(CacheControl(vec![
|
||||||
|
CacheDirective::MustRevalidate,
|
||||||
|
CacheDirective::NoStore,
|
||||||
|
])),
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get(self: Box<Self>) -> ResponseFuture {
|
||||||
|
Box::new(self.head().map(move |head| {
|
||||||
|
head.with_header(ContentLength(self.body.len() as u64))
|
||||||
|
.with_body(self.body.clone())
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,20 +1,18 @@
|
||||||
use futures::{self, Future};
|
use futures::{self, Future};
|
||||||
use hyper;
|
|
||||||
use hyper::header::{Accept, ContentType};
|
use hyper::header::{Accept, ContentType};
|
||||||
use hyper::server::*;
|
use hyper::server::*;
|
||||||
use serde_json;
|
|
||||||
use serde_urlencoded;
|
|
||||||
|
|
||||||
use mimes::*;
|
use crate::mimes::*;
|
||||||
use models::SearchResult;
|
use crate::models::SearchResult;
|
||||||
use site::Layout;
|
use crate::site::system_page;
|
||||||
use state::State;
|
use crate::state::State;
|
||||||
use web::{Resource, ResponseFuture};
|
use crate::web::{Resource, ResponseFuture};
|
||||||
|
|
||||||
const DEFAULT_LIMIT: u32 = 10;
|
const DEFAULT_LIMIT: u32 = 10;
|
||||||
const DEFAULT_SNIPPET_SIZE: u32 = 30;
|
const DEFAULT_SNIPPET_SIZE: u32 = 30;
|
||||||
|
|
||||||
type BoxResource = Box<Resource + Sync + Send>;
|
type BoxResource = Box<dyn Resource + Sync + Send>;
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Default)]
|
#[derive(Serialize, Deserialize, Default)]
|
||||||
pub struct QueryParameters {
|
pub struct QueryParameters {
|
||||||
|
@ -34,21 +32,29 @@ impl QueryParameters {
|
||||||
|
|
||||||
pub fn limit(self, limit: u32) -> Self {
|
pub fn limit(self, limit: u32) -> Self {
|
||||||
Self {
|
Self {
|
||||||
limit: if limit != DEFAULT_LIMIT { Some(limit) } else { None },
|
limit: if limit != DEFAULT_LIMIT {
|
||||||
|
Some(limit)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
},
|
||||||
..self
|
..self
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn snippet_size(self, snippet_size: u32) -> Self {
|
pub fn snippet_size(self, snippet_size: u32) -> Self {
|
||||||
Self {
|
Self {
|
||||||
snippet_size: if snippet_size != DEFAULT_SNIPPET_SIZE { Some(snippet_size) } else { None },
|
snippet_size: if snippet_size != DEFAULT_SNIPPET_SIZE {
|
||||||
|
Some(snippet_size)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
},
|
||||||
..self
|
..self
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn into_link(self) -> String {
|
pub fn into_link(self) -> String {
|
||||||
let args = serde_urlencoded::to_string(self).expect("Serializing to String cannot fail");
|
let args = serde_urlencoded::to_string(self).expect("Serializing to String cannot fail");
|
||||||
if args.len() > 0 {
|
if !args.is_empty() {
|
||||||
format!("_search?{}", args)
|
format!("_search?{}", args)
|
||||||
} else {
|
} else {
|
||||||
"_search".to_owned()
|
"_search".to_owned()
|
||||||
|
@ -66,18 +72,16 @@ impl SearchLookup {
|
||||||
Self { state }
|
Self { state }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn lookup(&self, query: Option<&str>) -> Result<Option<BoxResource>, ::web::Error> {
|
pub fn lookup(&self, query: Option<&str>) -> Result<Option<BoxResource>, crate::web::Error> {
|
||||||
let args: QueryParameters = serde_urlencoded::from_str(query.unwrap_or(""))?;
|
let args: QueryParameters = serde_urlencoded::from_str(query.unwrap_or(""))?;
|
||||||
|
|
||||||
Ok(Some(Box::new(
|
Ok(Some(Box::new(SearchResource::new(
|
||||||
SearchResource::new(
|
|
||||||
self.state.clone(),
|
self.state.clone(),
|
||||||
args.q,
|
args.q,
|
||||||
args.limit.unwrap_or(DEFAULT_LIMIT),
|
args.limit.unwrap_or(DEFAULT_LIMIT),
|
||||||
args.offset.unwrap_or(0),
|
args.offset.unwrap_or(0),
|
||||||
args.snippet_size.unwrap_or(DEFAULT_SNIPPET_SIZE),
|
args.snippet_size.unwrap_or(DEFAULT_SNIPPET_SIZE),
|
||||||
)
|
))))
|
||||||
)))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -98,8 +102,21 @@ pub enum ResponseType {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SearchResource {
|
impl SearchResource {
|
||||||
pub fn new(state: State, query: Option<String>, limit: u32, offset: u32, snippet_size: u32) -> Self {
|
pub fn new(
|
||||||
Self { state, response_type: ResponseType::Html, query, limit, offset, snippet_size }
|
state: State,
|
||||||
|
query: Option<String>,
|
||||||
|
limit: u32,
|
||||||
|
offset: u32,
|
||||||
|
snippet_size: u32,
|
||||||
|
) -> Self {
|
||||||
|
Self {
|
||||||
|
state,
|
||||||
|
response_type: ResponseType::Html,
|
||||||
|
query,
|
||||||
|
limit,
|
||||||
|
offset,
|
||||||
|
snippet_size,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn query_args(&self) -> QueryParameters {
|
fn query_args(&self) -> QueryParameters {
|
||||||
|
@ -126,21 +143,24 @@ impl Resource for SearchResource {
|
||||||
|
|
||||||
self.response_type = match accept.first() {
|
self.response_type = match accept.first() {
|
||||||
Some(&QualityItem { item: ref mime, .. })
|
Some(&QualityItem { item: ref mime, .. })
|
||||||
if mime.type_() == mime::APPLICATION && mime.subtype() == mime::JSON
|
if mime.type_() == mime::APPLICATION && mime.subtype() == mime::JSON =>
|
||||||
=> ResponseType::Json,
|
{
|
||||||
|
ResponseType::Json
|
||||||
|
}
|
||||||
_ => ResponseType::Html,
|
_ => ResponseType::Html,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
fn head(&self) -> ResponseFuture {
|
fn head(&self) -> ResponseFuture {
|
||||||
let content_type = match &self.response_type {
|
let content_type = match self.response_type {
|
||||||
&ResponseType::Json => ContentType(APPLICATION_JSON.clone()),
|
ResponseType::Json => ContentType(APPLICATION_JSON.clone()),
|
||||||
&ResponseType::Html => ContentType(TEXT_HTML.clone()),
|
ResponseType::Html => ContentType(TEXT_HTML.clone()),
|
||||||
};
|
};
|
||||||
|
|
||||||
Box::new(futures::finished(Response::new()
|
Box::new(futures::finished(
|
||||||
|
Response::new()
|
||||||
.with_status(hyper::StatusCode::Ok)
|
.with_status(hyper::StatusCode::Ok)
|
||||||
.with_header(content_type)
|
.with_header(content_type),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -154,7 +174,7 @@ impl Resource for SearchResource {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(BartDisplay)]
|
#[derive(BartDisplay)]
|
||||||
#[template="templates/search.html"]
|
#[template = "templates/search.html"]
|
||||||
struct Template<'a> {
|
struct Template<'a> {
|
||||||
query: &'a str,
|
query: &'a str,
|
||||||
hits: &'a [(usize, &'a SearchResult)],
|
hits: &'a [(usize, &'a SearchResult)],
|
||||||
|
@ -163,17 +183,26 @@ impl Resource for SearchResource {
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Show a search "front page" when no query is given:
|
// TODO: Show a search "front page" when no query is given:
|
||||||
let query = self.query.as_ref().map(|x| x.clone()).unwrap_or("".to_owned());
|
let query = self
|
||||||
|
.query
|
||||||
|
.as_ref()
|
||||||
|
.cloned()
|
||||||
|
.unwrap_or_else(|| "".to_owned());
|
||||||
|
|
||||||
let data = self.state.search_query(query, (self.limit + 1) as i32, self.offset as i32, self.snippet_size as i32);
|
let data = self.state.search_query(
|
||||||
|
query,
|
||||||
|
(self.limit + 1) as i32,
|
||||||
|
self.offset as i32,
|
||||||
|
self.snippet_size as i32,
|
||||||
|
);
|
||||||
let head = self.head();
|
let head = self.head();
|
||||||
|
|
||||||
Box::new(data.join(head)
|
Box::new(data.join(head).and_then(move |(mut data, head)| {
|
||||||
.and_then(move |(mut data, head)| {
|
|
||||||
let prev = if self.offset > 0 {
|
let prev = if self.offset > 0 {
|
||||||
Some(self.query_args()
|
Some(
|
||||||
|
self.query_args()
|
||||||
.offset(self.offset.saturating_sub(self.limit))
|
.offset(self.offset.saturating_sub(self.limit))
|
||||||
.into_link()
|
.into_link(),
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
|
@ -181,36 +210,38 @@ impl Resource for SearchResource {
|
||||||
|
|
||||||
let next = if data.len() > self.limit as usize {
|
let next = if data.len() > self.limit as usize {
|
||||||
data.pop();
|
data.pop();
|
||||||
Some(self.query_args()
|
Some(
|
||||||
|
self.query_args()
|
||||||
.offset(self.offset + self.limit)
|
.offset(self.offset + self.limit)
|
||||||
.into_link()
|
.into_link(),
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
};
|
};
|
||||||
|
|
||||||
match &self.response_type {
|
match self.response_type {
|
||||||
&ResponseType::Json => Ok(head
|
ResponseType::Json => Ok(head.with_body(
|
||||||
.with_body(serde_json::to_string(&JsonResponse {
|
serde_json::to_string(&JsonResponse {
|
||||||
query: self.query.as_ref().map(|x| &**x).unwrap_or(""),
|
query: self.query.as_deref().unwrap_or(""),
|
||||||
hits: &data,
|
hits: &data,
|
||||||
prev,
|
prev,
|
||||||
next,
|
next,
|
||||||
}).expect("Should never fail"))
|
})
|
||||||
),
|
.expect("Should never fail"),
|
||||||
&ResponseType::Html => Ok(head
|
)),
|
||||||
.with_body(Layout {
|
ResponseType::Html => Ok(head.with_body(
|
||||||
base: None, // Hmm, should perhaps accept `base` as argument
|
system_page(
|
||||||
title: "Search",
|
None, // Hmm, should perhaps accept `base` as argument
|
||||||
body: &Template {
|
"Search",
|
||||||
query: self.query.as_ref().map(|x| &**x).unwrap_or(""),
|
&Template {
|
||||||
hits: &data.iter()
|
query: self.query.as_deref().unwrap_or(""),
|
||||||
.enumerate()
|
hits: &data.iter().enumerate().collect::<Vec<_>>(),
|
||||||
.collect::<Vec<_>>(),
|
|
||||||
prev,
|
prev,
|
||||||
next,
|
next,
|
||||||
},
|
},
|
||||||
}.to_string())),
|
)
|
||||||
|
.to_string(),
|
||||||
|
)),
|
||||||
}
|
}
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,13 +1,13 @@
|
||||||
use futures::{self, Future};
|
use futures::{self, Future};
|
||||||
use hyper;
|
|
||||||
use hyper::header::ContentType;
|
use hyper::header::ContentType;
|
||||||
use hyper::server::*;
|
use hyper::server::*;
|
||||||
|
|
||||||
use mimes::*;
|
use crate::mimes::*;
|
||||||
use models::ArticleRevisionStub;
|
use crate::models::ArticleRevisionStub;
|
||||||
use site::Layout;
|
use crate::site::system_page;
|
||||||
use state::State;
|
use crate::state::State;
|
||||||
use web::{Resource, ResponseFuture};
|
use crate::web::{Resource, ResponseFuture};
|
||||||
|
|
||||||
pub struct SitemapResource {
|
pub struct SitemapResource {
|
||||||
state: State,
|
state: State,
|
||||||
|
@ -26,15 +26,16 @@ impl Resource for SitemapResource {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn head(&self) -> ResponseFuture {
|
fn head(&self) -> ResponseFuture {
|
||||||
Box::new(futures::finished(Response::new()
|
Box::new(futures::finished(
|
||||||
|
Response::new()
|
||||||
.with_status(hyper::StatusCode::Ok)
|
.with_status(hyper::StatusCode::Ok)
|
||||||
.with_header(ContentType(TEXT_HTML.clone()))
|
.with_header(ContentType(TEXT_HTML.clone())),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get(self: Box<Self>) -> ResponseFuture {
|
fn get(self: Box<Self>) -> ResponseFuture {
|
||||||
#[derive(BartDisplay)]
|
#[derive(BartDisplay)]
|
||||||
#[template="templates/sitemap.html"]
|
#[template = "templates/sitemap.html"]
|
||||||
struct Template<'a> {
|
struct Template<'a> {
|
||||||
articles: &'a [ArticleRevisionStub],
|
articles: &'a [ArticleRevisionStub],
|
||||||
}
|
}
|
||||||
|
@ -42,16 +43,17 @@ impl Resource for SitemapResource {
|
||||||
let data = self.state.get_latest_article_revision_stubs();
|
let data = self.state.get_latest_article_revision_stubs();
|
||||||
let head = self.head();
|
let head = self.head();
|
||||||
|
|
||||||
Box::new(data.join(head)
|
Box::new(data.join(head).and_then(move |(articles, head)| {
|
||||||
.and_then(move |(articles, head)| {
|
Ok(head.with_body(
|
||||||
Ok(head
|
system_page(
|
||||||
.with_body(Layout {
|
None, // Hmm, should perhaps accept `base` as argument
|
||||||
base: None, // Hmm, should perhaps accept `base` as argument
|
"Sitemap",
|
||||||
title: "Sitemap",
|
Template {
|
||||||
body: &Template {
|
|
||||||
articles: &articles,
|
articles: &articles,
|
||||||
},
|
},
|
||||||
}.to_string()))
|
)
|
||||||
|
.to_string(),
|
||||||
|
))
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
use futures::{self, Future};
|
use futures::{self, Future};
|
||||||
use hyper;
|
|
||||||
use hyper::header::Location;
|
use hyper::header::Location;
|
||||||
use hyper::server::*;
|
use hyper::server::*;
|
||||||
|
|
||||||
use web::{Resource, ResponseFuture};
|
use crate::web::{Resource, ResponseFuture};
|
||||||
|
|
||||||
pub struct TemporaryRedirectResource {
|
pub struct TemporaryRedirectResource {
|
||||||
location: String,
|
location: String,
|
||||||
|
@ -14,14 +14,17 @@ impl TemporaryRedirectResource {
|
||||||
Self { location }
|
Self { location }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn from_slug<S: AsRef<str>>(slug: S) -> Self {
|
pub fn from_slug<S: AsRef<str>>(slug: S, edit: bool) -> Self {
|
||||||
Self {
|
let base = if slug.as_ref().is_empty() {
|
||||||
location:
|
|
||||||
if slug.as_ref().is_empty() {
|
|
||||||
"."
|
"."
|
||||||
} else {
|
} else {
|
||||||
slug.as_ref()
|
slug.as_ref()
|
||||||
}.to_owned()
|
};
|
||||||
|
|
||||||
|
let tail = if edit { "?edit" } else { "" };
|
||||||
|
|
||||||
|
Self {
|
||||||
|
location: format!("{}{}", base, tail),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -33,18 +36,18 @@ impl Resource for TemporaryRedirectResource {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn head(&self) -> ResponseFuture {
|
fn head(&self) -> ResponseFuture {
|
||||||
Box::new(futures::finished(Response::new()
|
Box::new(futures::finished(
|
||||||
|
Response::new()
|
||||||
.with_status(hyper::StatusCode::TemporaryRedirect)
|
.with_status(hyper::StatusCode::TemporaryRedirect)
|
||||||
.with_header(Location::new(self.location.clone()))
|
.with_header(Location::new(self.location.clone())),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get(self: Box<Self>) -> ResponseFuture {
|
fn get(self: Box<Self>) -> ResponseFuture {
|
||||||
Box::new(self.head()
|
Box::new(
|
||||||
.and_then(move |head| {
|
self.head()
|
||||||
Ok(head
|
.and_then(move |head| Ok(head.with_body(format!("Moved to {}", self.location)))),
|
||||||
.with_body(format!("Moved to {}", self.location)))
|
)
|
||||||
}))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn put(self: Box<Self>, _body: hyper::Body, _identity: Option<String>) -> ResponseFuture {
|
fn put(self: Box<Self>, _body: hyper::Body, _identity: Option<String>) -> ResponseFuture {
|
||||||
|
|
106
src/site.rs
106
src/site.rs
|
@ -4,20 +4,20 @@
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
|
||||||
use futures::{self, Future};
|
use futures::{self, Future};
|
||||||
|
|
||||||
use hyper::header::{Accept, ContentType, Server};
|
use hyper::header::{Accept, ContentType, Server};
|
||||||
use hyper::mime;
|
use hyper::mime;
|
||||||
use hyper::server::*;
|
use hyper::server::*;
|
||||||
use hyper;
|
|
||||||
|
|
||||||
use assets::{StyleCss, SearchJs};
|
use crate::assets::{SearchJs, StyleCss, ThemesCss};
|
||||||
use build_config;
|
use crate::build_config;
|
||||||
use web::Lookup;
|
use crate::theme;
|
||||||
use wiki_lookup::WikiLookup;
|
use crate::web::Lookup;
|
||||||
|
use crate::wiki_lookup::WikiLookup;
|
||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
static ref TEXT_HTML: mime::Mime = "text/html;charset=utf-8".parse().unwrap();
|
static ref TEXT_HTML: mime::Mime = "text/html;charset=utf-8".parse().unwrap();
|
||||||
static ref SERVER: Server =
|
static ref SERVER: Server = Server::new(build_config::HTTP_SERVER.as_str());
|
||||||
Server::new(build_config::HTTP_SERVER.as_str());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
header! { (XIdentity, "X-Identity") => [String] }
|
header! { (XIdentity, "X-Identity") => [String] }
|
||||||
|
@ -27,15 +27,53 @@ header! { (XIdentity, "X-Identity") => [String] }
|
||||||
pub struct Layout<'a, T: 'a + fmt::Display> {
|
pub struct Layout<'a, T: 'a + fmt::Display> {
|
||||||
pub base: Option<&'a str>,
|
pub base: Option<&'a str>,
|
||||||
pub title: &'a str,
|
pub title: &'a str,
|
||||||
pub body: &'a T,
|
pub theme: theme::Theme,
|
||||||
|
pub body: T,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, T: 'a + fmt::Display> Layout<'a, T> {
|
impl<'a, T: 'a + fmt::Display> Layout<'a, T> {
|
||||||
pub fn style_css_checksum(&self) -> &str { StyleCss::checksum() }
|
pub fn themes_css(&self) -> &str {
|
||||||
pub fn search_js_checksum(&self) -> &str { SearchJs::checksum() }
|
ThemesCss::resource_name()
|
||||||
|
}
|
||||||
|
pub fn style_css(&self) -> &str {
|
||||||
|
StyleCss::resource_name()
|
||||||
|
}
|
||||||
|
pub fn search_js(&self) -> &str {
|
||||||
|
SearchJs::resource_name()
|
||||||
|
}
|
||||||
|
|
||||||
pub fn project_name(&self) -> &str { build_config::PROJECT_NAME }
|
pub fn project_name(&self) -> &str {
|
||||||
pub fn version(&self) -> &str { build_config::VERSION.as_str() }
|
build_config::PROJECT_NAME
|
||||||
|
}
|
||||||
|
pub fn version(&self) -> &str {
|
||||||
|
build_config::VERSION.as_str()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(BartDisplay)]
|
||||||
|
#[template = "templates/system_page_layout.html"]
|
||||||
|
pub struct SystemPageLayout<'a, T: 'a + fmt::Display> {
|
||||||
|
title: &'a str,
|
||||||
|
html_body: T,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn system_page<'a, T>(
|
||||||
|
base: Option<&'a str>,
|
||||||
|
title: &'a str,
|
||||||
|
body: T,
|
||||||
|
) -> Layout<'a, SystemPageLayout<'a, T>>
|
||||||
|
where
|
||||||
|
T: 'a + fmt::Display,
|
||||||
|
{
|
||||||
|
Layout {
|
||||||
|
base,
|
||||||
|
title,
|
||||||
|
theme: theme::theme_from_str_hash(title),
|
||||||
|
body: SystemPageLayout {
|
||||||
|
title,
|
||||||
|
html_body: body,
|
||||||
|
},
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(BartDisplay)]
|
#[derive(BartDisplay)]
|
||||||
|
@ -53,41 +91,39 @@ pub struct Site {
|
||||||
|
|
||||||
impl Site {
|
impl Site {
|
||||||
pub fn new(root: WikiLookup, trust_identity: bool) -> Site {
|
pub fn new(root: WikiLookup, trust_identity: bool) -> Site {
|
||||||
Site { root, trust_identity }
|
Site {
|
||||||
|
root,
|
||||||
|
trust_identity,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn not_found(base: Option<&str>) -> Response {
|
fn not_found(base: Option<&str>) -> Response {
|
||||||
Response::new()
|
Response::new()
|
||||||
.with_header(ContentType(TEXT_HTML.clone()))
|
.with_header(ContentType(TEXT_HTML.clone()))
|
||||||
.with_body(Layout {
|
.with_body(system_page(base, "Not found", NotFound).to_string())
|
||||||
base: base,
|
|
||||||
title: "Not found",
|
|
||||||
body: &NotFound,
|
|
||||||
}.to_string())
|
|
||||||
.with_status(hyper::StatusCode::NotFound)
|
.with_status(hyper::StatusCode::NotFound)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn internal_server_error(base: Option<&str>, err: Box<::std::error::Error + Send + Sync>) -> Response {
|
fn internal_server_error(
|
||||||
|
base: Option<&str>,
|
||||||
|
err: Box<dyn ::std::error::Error + Send + Sync>,
|
||||||
|
) -> Response {
|
||||||
eprintln!("Internal Server Error:\n{:#?}", err);
|
eprintln!("Internal Server Error:\n{:#?}", err);
|
||||||
|
|
||||||
Response::new()
|
Response::new()
|
||||||
.with_header(ContentType(TEXT_HTML.clone()))
|
.with_header(ContentType(TEXT_HTML.clone()))
|
||||||
.with_body(Layout {
|
.with_body(system_page(base, "Internal server error", InternalServerError).to_string())
|
||||||
base,
|
|
||||||
title: "Internal server error",
|
|
||||||
body: &InternalServerError,
|
|
||||||
}.to_string())
|
|
||||||
.with_status(hyper::StatusCode::InternalServerError)
|
.with_status(hyper::StatusCode::InternalServerError)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn root_base_from_request_uri(path: &str) -> Option<String> {
|
fn root_base_from_request_uri(path: &str) -> Option<String> {
|
||||||
assert!(path.starts_with("/"));
|
assert!(path.starts_with('/'));
|
||||||
let slashes = path[1..].matches('/').count();
|
let slashes = path[1..].matches('/').count();
|
||||||
|
|
||||||
match slashes {
|
match slashes {
|
||||||
0 => None,
|
0 => None,
|
||||||
n => Some(::std::iter::repeat("../").take(n).collect())
|
n => Some("../".repeat(n)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -95,7 +131,7 @@ impl Service for Site {
|
||||||
type Request = Request;
|
type Request = Request;
|
||||||
type Response = Response;
|
type Response = Response;
|
||||||
type Error = hyper::Error;
|
type Error = hyper::Error;
|
||||||
type Future = Box<futures::Future<Item = Response, Error = Self::Error>>;
|
type Future = Box<dyn futures::Future<Item = Response, Error = Self::Error>>;
|
||||||
|
|
||||||
fn call(&self, req: Request) -> Self::Future {
|
fn call(&self, req: Request) -> Self::Future {
|
||||||
let (method, uri, _http_version, headers, body) = req.deconstruct();
|
let (method, uri, _http_version, headers, body) = req.deconstruct();
|
||||||
|
@ -107,12 +143,14 @@ impl Service for Site {
|
||||||
false => None,
|
false => None,
|
||||||
};
|
};
|
||||||
|
|
||||||
let accept_header = headers.get().map(|x: &Accept| x.clone()).unwrap_or(Accept(vec![]));
|
let accept_header = headers.get().cloned().unwrap_or_else(|| Accept(vec![]));
|
||||||
|
|
||||||
let base = root_base_from_request_uri(uri.path());
|
let base = root_base_from_request_uri(uri.path());
|
||||||
let base2 = base.clone(); // Bah, stupid clone
|
let base2 = base.clone(); // Bah, stupid clone
|
||||||
|
|
||||||
Box::new(self.root.lookup(uri.path(), uri.query())
|
Box::new(
|
||||||
|
self.root
|
||||||
|
.lookup(uri.path(), uri.query())
|
||||||
.and_then(move |resource| match resource {
|
.and_then(move |resource| match resource {
|
||||||
Some(mut resource) => {
|
Some(mut resource) => {
|
||||||
use hyper::Method::*;
|
use hyper::Method::*;
|
||||||
|
@ -123,13 +161,13 @@ impl Service for Site {
|
||||||
Get => resource.get(),
|
Get => resource.get(),
|
||||||
Put => resource.put(body, identity),
|
Put => resource.put(body, identity),
|
||||||
Post => resource.post(body, identity),
|
Post => resource.post(body, identity),
|
||||||
_ => Box::new(futures::finished(resource.method_not_allowed()))
|
_ => Box::new(futures::finished(resource.method_not_allowed())),
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
None => Box::new(futures::finished(Self::not_found(base.as_ref().map(|x| &**x))))
|
None => Box::new(futures::finished(Self::not_found(base.as_deref()))),
|
||||||
})
|
})
|
||||||
.or_else(move |err| Ok(Self::internal_server_error(base2.as_ref().map(|x| &**x), err)))
|
.or_else(move |err| Ok(Self::internal_server_error(base2.as_deref(), err)))
|
||||||
.map(|response| response.with_header(SERVER.clone()))
|
.map(|response| response.with_header(SERVER.clone())),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
607
src/state.rs
607
src/state.rs
|
@ -1,15 +1,13 @@
|
||||||
use std;
|
|
||||||
|
|
||||||
use diesel;
|
|
||||||
use diesel::sqlite::SqliteConnection;
|
|
||||||
use diesel::prelude::*;
|
use diesel::prelude::*;
|
||||||
|
use diesel::sqlite::SqliteConnection;
|
||||||
use futures_cpupool::{self, CpuFuture};
|
use futures_cpupool::{self, CpuFuture};
|
||||||
use r2d2::Pool;
|
use r2d2::Pool;
|
||||||
use r2d2_diesel::ConnectionManager;
|
use r2d2_diesel::ConnectionManager;
|
||||||
|
|
||||||
use merge;
|
use crate::merge;
|
||||||
use models;
|
use crate::models;
|
||||||
use schema::*;
|
use crate::schema::*;
|
||||||
|
use crate::theme::Theme;
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct State {
|
pub struct State {
|
||||||
|
@ -17,19 +15,16 @@ pub struct State {
|
||||||
cpu_pool: futures_cpupool::CpuPool,
|
cpu_pool: futures_cpupool::CpuPool,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type Error = Box<std::error::Error + Send + Sync>;
|
pub type Error = Box<dyn std::error::Error + Send + Sync>;
|
||||||
|
|
||||||
pub enum SlugLookup {
|
pub enum SlugLookup {
|
||||||
Miss,
|
Miss,
|
||||||
Hit {
|
Hit { article_id: i32, revision: i32 },
|
||||||
article_id: i32,
|
|
||||||
revision: i32,
|
|
||||||
},
|
|
||||||
Redirect(String),
|
Redirect(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Insertable)]
|
#[derive(Insertable)]
|
||||||
#[table_name="article_revisions"]
|
#[table_name = "article_revisions"]
|
||||||
struct NewRevision<'a> {
|
struct NewRevision<'a> {
|
||||||
article_id: i32,
|
article_id: i32,
|
||||||
revision: i32,
|
revision: i32,
|
||||||
|
@ -38,6 +33,7 @@ struct NewRevision<'a> {
|
||||||
body: &'a str,
|
body: &'a str,
|
||||||
author: Option<&'a str>,
|
author: Option<&'a str>,
|
||||||
latest: bool,
|
latest: bool,
|
||||||
|
theme: Theme,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq)]
|
#[derive(Debug, PartialEq)]
|
||||||
|
@ -45,11 +41,16 @@ pub struct RebaseConflict {
|
||||||
pub base_article: models::ArticleRevisionStub,
|
pub base_article: models::ArticleRevisionStub,
|
||||||
pub title: merge::MergeResult<char>,
|
pub title: merge::MergeResult<char>,
|
||||||
pub body: merge::MergeResult<String>,
|
pub body: merge::MergeResult<String>,
|
||||||
|
pub theme: Theme,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq)]
|
#[derive(Debug, PartialEq)]
|
||||||
enum RebaseResult {
|
enum RebaseResult {
|
||||||
Clean { title: String, body: String },
|
Clean {
|
||||||
|
title: String,
|
||||||
|
body: String,
|
||||||
|
theme: Theme,
|
||||||
|
},
|
||||||
Conflict(RebaseConflict),
|
Conflict(RebaseConflict),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -58,11 +59,17 @@ pub enum UpdateResult {
|
||||||
RebaseConflict(RebaseConflict),
|
RebaseConflict(RebaseConflict),
|
||||||
}
|
}
|
||||||
|
|
||||||
fn decide_slug(conn: &SqliteConnection, article_id: i32, prev_title: &str, title: &str, prev_slug: Option<&str>) -> Result<String, Error> {
|
fn decide_slug(
|
||||||
|
conn: &SqliteConnection,
|
||||||
|
article_id: i32,
|
||||||
|
prev_title: &str,
|
||||||
|
title: &str,
|
||||||
|
prev_slug: Option<&str>,
|
||||||
|
) -> Result<String, Error> {
|
||||||
let base_slug = ::slug::slugify(title);
|
let base_slug = ::slug::slugify(title);
|
||||||
|
|
||||||
if let Some(prev_slug) = prev_slug {
|
if let Some(prev_slug) = prev_slug {
|
||||||
if prev_slug == "" {
|
if prev_slug.is_empty() {
|
||||||
// Never give a non-empty slug to the front page
|
// Never give a non-empty slug to the front page
|
||||||
return Ok(String::new());
|
return Ok(String::new());
|
||||||
}
|
}
|
||||||
|
@ -76,9 +83,11 @@ fn decide_slug(conn: &SqliteConnection, article_id: i32, prev_title: &str, title
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let base_slug = if base_slug.is_empty() { "article" } else { &base_slug };
|
let base_slug = if base_slug.is_empty() {
|
||||||
|
"article"
|
||||||
use schema::article_revisions;
|
} else {
|
||||||
|
&base_slug
|
||||||
|
};
|
||||||
|
|
||||||
let mut slug = base_slug.to_owned();
|
let mut slug = base_slug.to_owned();
|
||||||
let mut disambiguator = 1;
|
let mut disambiguator = 1;
|
||||||
|
@ -89,7 +98,8 @@ fn decide_slug(conn: &SqliteConnection, article_id: i32, prev_title: &str, title
|
||||||
.filter(article_revisions::slug.eq(&slug))
|
.filter(article_revisions::slug.eq(&slug))
|
||||||
.filter(article_revisions::latest.eq(true))
|
.filter(article_revisions::latest.eq(true))
|
||||||
.count()
|
.count()
|
||||||
.first::<i64>(conn)? != 0;
|
.first::<i64>(conn)?
|
||||||
|
!= 0;
|
||||||
|
|
||||||
if !slug_in_use {
|
if !slug_in_use {
|
||||||
break Ok(slug);
|
break Ok(slug);
|
||||||
|
@ -110,8 +120,6 @@ impl<'a> SyncState<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_article_slug(&self, article_id: i32) -> Result<Option<String>, Error> {
|
pub fn get_article_slug(&self, article_id: i32) -> Result<Option<String>, Error> {
|
||||||
use schema::article_revisions;
|
|
||||||
|
|
||||||
Ok(article_revisions::table
|
Ok(article_revisions::table
|
||||||
.filter(article_revisions::article_id.eq(article_id))
|
.filter(article_revisions::article_id.eq(article_id))
|
||||||
.filter(article_revisions::latest.eq(true))
|
.filter(article_revisions::latest.eq(true))
|
||||||
|
@ -120,9 +128,11 @@ impl<'a> SyncState<'a> {
|
||||||
.optional()?)
|
.optional()?)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_article_revision(&self, article_id: i32, revision: i32) -> Result<Option<models::ArticleRevision>, Error> {
|
pub fn get_article_revision(
|
||||||
use schema::article_revisions;
|
&self,
|
||||||
|
article_id: i32,
|
||||||
|
revision: i32,
|
||||||
|
) -> Result<Option<models::ArticleRevision>, Error> {
|
||||||
Ok(article_revisions::table
|
Ok(article_revisions::table
|
||||||
.filter(article_revisions::article_id.eq(article_id))
|
.filter(article_revisions::article_id.eq(article_id))
|
||||||
.filter(article_revisions::revision.eq(revision))
|
.filter(article_revisions::revision.eq(revision))
|
||||||
|
@ -130,14 +140,17 @@ impl<'a> SyncState<'a> {
|
||||||
.optional()?)
|
.optional()?)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn query_article_revision_stubs<F>(&self, f: F) -> Result<Vec<models::ArticleRevisionStub>, Error>
|
pub fn query_article_revision_stubs<F>(
|
||||||
|
&self,
|
||||||
|
f: F,
|
||||||
|
) -> Result<Vec<models::ArticleRevisionStub>, Error>
|
||||||
where
|
where
|
||||||
F: 'static + Send + Sync,
|
F: 'static + Send + Sync,
|
||||||
for <'x> F:
|
for<'x> F: FnOnce(
|
||||||
FnOnce(article_revisions::BoxedQuery<'x, diesel::sqlite::Sqlite>) ->
|
|
||||||
article_revisions::BoxedQuery<'x, diesel::sqlite::Sqlite>,
|
article_revisions::BoxedQuery<'x, diesel::sqlite::Sqlite>,
|
||||||
|
) -> article_revisions::BoxedQuery<'x, diesel::sqlite::Sqlite>,
|
||||||
{
|
{
|
||||||
use schema::article_revisions::dsl::*;
|
use crate::schema::article_revisions::dsl::*;
|
||||||
|
|
||||||
Ok(f(article_revisions.into_boxed())
|
Ok(f(article_revisions.into_boxed())
|
||||||
.select((
|
.select((
|
||||||
|
@ -149,20 +162,24 @@ impl<'a> SyncState<'a> {
|
||||||
title,
|
title,
|
||||||
latest,
|
latest,
|
||||||
author,
|
author,
|
||||||
|
theme,
|
||||||
))
|
))
|
||||||
.load(self.db_connection)?
|
.load(self.db_connection)?)
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_article_revision_stub(&self, article_id: i32, revision: i32) -> Result<Option<models::ArticleRevisionStub>, Error> {
|
fn get_article_revision_stub(
|
||||||
use schema::article_revisions;
|
&self,
|
||||||
|
article_id: i32,
|
||||||
Ok(self.query_article_revision_stubs(move |query| {
|
revision: i32,
|
||||||
|
) -> Result<Option<models::ArticleRevisionStub>, Error> {
|
||||||
|
Ok(self
|
||||||
|
.query_article_revision_stubs(move |query| {
|
||||||
query
|
query
|
||||||
.filter(article_revisions::article_id.eq(article_id))
|
.filter(article_revisions::article_id.eq(article_id))
|
||||||
.filter(article_revisions::revision.eq(revision))
|
.filter(article_revisions::revision.eq(revision))
|
||||||
.limit(1)
|
.limit(1)
|
||||||
})?.pop())
|
})?
|
||||||
|
.pop())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn lookup_slug(&self, slug: String) -> Result<SlugLookup, Error> {
|
pub fn lookup_slug(&self, slug: String) -> Result<SlugLookup, Error> {
|
||||||
|
@ -174,9 +191,8 @@ impl<'a> SyncState<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
self.db_connection.transaction(|| {
|
self.db_connection.transaction(|| {
|
||||||
use schema::article_revisions;
|
Ok(
|
||||||
|
match article_revisions::table
|
||||||
Ok(match article_revisions::table
|
|
||||||
.filter(article_revisions::slug.eq(slug))
|
.filter(article_revisions::slug.eq(slug))
|
||||||
.order(article_revisions::sequence_number.desc())
|
.order(article_revisions::sequence_number.desc())
|
||||||
.select((
|
.select((
|
||||||
|
@ -197,100 +213,154 @@ impl<'a> SyncState<'a> {
|
||||||
.filter(article_revisions::latest.eq(true))
|
.filter(article_revisions::latest.eq(true))
|
||||||
.filter(article_revisions::article_id.eq(stub.article_id))
|
.filter(article_revisions::article_id.eq(stub.article_id))
|
||||||
.select(article_revisions::slug)
|
.select(article_revisions::slug)
|
||||||
.first::<String>(self.db_connection)?
|
.first::<String>(self.db_connection)?,
|
||||||
|
),
|
||||||
|
},
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn rebase_update(&self, article_id: i32, target_base_revision: i32, existing_base_revision: i32, title: String, body: String)
|
fn rebase_update(
|
||||||
-> Result<RebaseResult, Error>
|
&self,
|
||||||
{
|
article_id: i32,
|
||||||
|
target_base_revision: i32,
|
||||||
|
existing_base_revision: i32,
|
||||||
|
title: String,
|
||||||
|
body: String,
|
||||||
|
theme: Theme,
|
||||||
|
) -> Result<RebaseResult, Error> {
|
||||||
let mut title_a = title;
|
let mut title_a = title;
|
||||||
let mut body_a = body;
|
let mut body_a = body;
|
||||||
|
let mut theme_a = theme;
|
||||||
|
|
||||||
|
// TODO: Improve this implementation.
|
||||||
|
// Weakness: If the range of revisions is big, _one_ request from the
|
||||||
|
// client can cause _many_ database requests, cheaply causing lots
|
||||||
|
// of work for the server. Possible attack vector.
|
||||||
|
// Weakness: When the range is larger than just one iteration, the
|
||||||
|
// same title and body are retrieved from the database multiple
|
||||||
|
// times. Unnecessary extra copies.
|
||||||
|
|
||||||
for revision in existing_base_revision..target_base_revision {
|
for revision in existing_base_revision..target_base_revision {
|
||||||
let mut stored = article_revisions::table
|
let mut stored = article_revisions::table
|
||||||
.filter(article_revisions::article_id.eq(article_id))
|
.filter(article_revisions::article_id.eq(article_id))
|
||||||
.filter(article_revisions::revision.ge(revision))
|
.filter(article_revisions::revision.ge(revision))
|
||||||
.filter(article_revisions::revision.le(revision+1))
|
.filter(article_revisions::revision.le(revision + 1))
|
||||||
.order(article_revisions::revision.asc())
|
.order(article_revisions::revision.asc())
|
||||||
.select((
|
.select((
|
||||||
article_revisions::title,
|
article_revisions::title,
|
||||||
article_revisions::body,
|
article_revisions::body,
|
||||||
|
article_revisions::theme,
|
||||||
))
|
))
|
||||||
.load::<(String, String)>(self.db_connection)?;
|
.load::<(String, String, Theme)>(self.db_connection)?;
|
||||||
|
|
||||||
let (title_b, body_b) = stored.pop().expect("Application layer guarantee");
|
let (title_b, body_b, theme_b) = stored.pop().expect("Application layer guarantee");
|
||||||
let (title_o, body_o) = stored.pop().expect("Application layer guarantee");
|
let (title_o, body_o, theme_o) = stored.pop().expect("Application layer guarantee");
|
||||||
|
|
||||||
use merge::MergeResult::*;
|
use crate::merge::MergeResult::*;
|
||||||
|
|
||||||
|
fn merge_themes(a: Theme, o: Theme, b: Theme) -> Theme {
|
||||||
|
// Last change wins
|
||||||
|
if a != o {
|
||||||
|
a
|
||||||
|
} else {
|
||||||
|
b
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let update = {
|
let update = {
|
||||||
let title_merge = merge::merge_chars(&title_a, &title_o, &title_b);
|
let title_merge = merge::merge_chars(&title_a, &title_o, &title_b);
|
||||||
let body_merge = merge::merge_lines(&body_a, &body_o, &body_b);
|
let body_merge = merge::merge_lines(&body_a, &body_o, &body_b);
|
||||||
|
let theme = merge_themes(theme_a, theme_o, theme_b);
|
||||||
|
|
||||||
match (title_merge, body_merge) {
|
match (title_merge, body_merge) {
|
||||||
(Clean(title), Clean(body)) => (title, body),
|
(Clean(title), Clean(body)) => (title, body, theme),
|
||||||
(title_merge, body_merge) => {
|
(title_merge, body_merge) => {
|
||||||
return Ok(RebaseResult::Conflict(RebaseConflict {
|
return Ok(RebaseResult::Conflict(RebaseConflict {
|
||||||
base_article: self.get_article_revision_stub(article_id, revision+1)?.expect("Application layer guarantee"),
|
base_article: self
|
||||||
|
.get_article_revision_stub(article_id, revision + 1)?
|
||||||
|
.expect("Application layer guarantee"),
|
||||||
title: title_merge,
|
title: title_merge,
|
||||||
body: body_merge.to_strings(),
|
body: body_merge.into_strings(),
|
||||||
|
theme,
|
||||||
}));
|
}));
|
||||||
},
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
title_a = update.0;
|
title_a = update.0;
|
||||||
body_a = update.1;
|
body_a = update.1;
|
||||||
|
theme_a = update.2;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(RebaseResult::Clean { title: title_a, body: body_a })
|
Ok(RebaseResult::Clean {
|
||||||
|
title: title_a,
|
||||||
|
body: body_a,
|
||||||
|
theme: theme_a,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update_article(&self, article_id: i32, base_revision: i32, title: String, body: String, author: Option<String>)
|
pub fn update_article(
|
||||||
-> Result<UpdateResult, Error>
|
&self,
|
||||||
{
|
article_id: i32,
|
||||||
|
base_revision: i32,
|
||||||
|
title: String,
|
||||||
|
body: String,
|
||||||
|
author: Option<String>,
|
||||||
|
theme: Option<Theme>,
|
||||||
|
) -> Result<UpdateResult, Error> {
|
||||||
if title.is_empty() {
|
if title.is_empty() {
|
||||||
Err("title cannot be empty")?;
|
return Err("title cannot be empty".into());
|
||||||
}
|
}
|
||||||
|
|
||||||
self.db_connection.transaction(|| {
|
self.db_connection.transaction(|| {
|
||||||
use schema::article_revisions;
|
let (latest_revision, prev_title, prev_slug, prev_theme) = article_revisions::table
|
||||||
|
|
||||||
let (latest_revision, prev_title, prev_slug) = article_revisions::table
|
|
||||||
.filter(article_revisions::article_id.eq(article_id))
|
.filter(article_revisions::article_id.eq(article_id))
|
||||||
.order(article_revisions::revision.desc())
|
.order(article_revisions::revision.desc())
|
||||||
.select((
|
.select((
|
||||||
article_revisions::revision,
|
article_revisions::revision,
|
||||||
article_revisions::title,
|
article_revisions::title,
|
||||||
article_revisions::slug,
|
article_revisions::slug,
|
||||||
|
article_revisions::theme,
|
||||||
))
|
))
|
||||||
.first::<(i32, String, String)>(self.db_connection)?;
|
.first::<(i32, String, String, Theme)>(self.db_connection)?;
|
||||||
|
|
||||||
// TODO: If this is an historic edit repeated, just respond OK
|
// TODO: If this is an historic edit repeated, just respond OK
|
||||||
// This scheme would make POST idempotent.
|
// This scheme would make POST idempotent.
|
||||||
|
|
||||||
if base_revision > latest_revision {
|
if base_revision > latest_revision {
|
||||||
Err("This edit is based on a future version of the article")?;
|
return Err("This edit is based on a future version of the article".into());
|
||||||
}
|
}
|
||||||
|
|
||||||
let rebase_result = self.rebase_update(article_id, latest_revision, base_revision, title, body)?;
|
let theme = theme.unwrap_or(prev_theme);
|
||||||
|
let rebase_result = self.rebase_update(
|
||||||
|
article_id,
|
||||||
|
latest_revision,
|
||||||
|
base_revision,
|
||||||
|
title,
|
||||||
|
body,
|
||||||
|
theme,
|
||||||
|
)?;
|
||||||
|
|
||||||
let (title, body) = match rebase_result {
|
let (title, body, theme) = match rebase_result {
|
||||||
RebaseResult::Clean { title, body } => (title, body),
|
RebaseResult::Clean { title, body, theme } => (title, body, theme),
|
||||||
RebaseResult::Conflict(x) => return Ok(UpdateResult::RebaseConflict(x)),
|
RebaseResult::Conflict(x) => return Ok(UpdateResult::RebaseConflict(x)),
|
||||||
};
|
};
|
||||||
|
|
||||||
let new_revision = latest_revision + 1;
|
let new_revision = latest_revision + 1;
|
||||||
|
|
||||||
let slug = decide_slug(self.db_connection, article_id, &prev_title, &title, Some(&prev_slug))?;
|
let slug = decide_slug(
|
||||||
|
self.db_connection,
|
||||||
|
article_id,
|
||||||
|
&prev_title,
|
||||||
|
&title,
|
||||||
|
Some(&prev_slug),
|
||||||
|
)?;
|
||||||
|
|
||||||
diesel::update(
|
diesel::update(
|
||||||
article_revisions::table
|
article_revisions::table
|
||||||
.filter(article_revisions::article_id.eq(article_id))
|
.filter(article_revisions::article_id.eq(article_id))
|
||||||
.filter(article_revisions::revision.eq(latest_revision))
|
.filter(article_revisions::revision.eq(latest_revision)),
|
||||||
)
|
)
|
||||||
.set(article_revisions::latest.eq(false))
|
.set(article_revisions::latest.eq(false))
|
||||||
.execute(self.db_connection)?;
|
.execute(self.db_connection)?;
|
||||||
|
@ -302,44 +372,58 @@ impl<'a> SyncState<'a> {
|
||||||
slug: &slug,
|
slug: &slug,
|
||||||
title: &title,
|
title: &title,
|
||||||
body: &body,
|
body: &body,
|
||||||
author: author.as_ref().map(|x| &**x),
|
author: author.as_deref(),
|
||||||
latest: true,
|
latest: true,
|
||||||
|
theme,
|
||||||
})
|
})
|
||||||
.execute(self.db_connection)?;
|
.execute(self.db_connection)?;
|
||||||
|
|
||||||
Ok(UpdateResult::Success(article_revisions::table
|
Ok(UpdateResult::Success(
|
||||||
|
article_revisions::table
|
||||||
.filter(article_revisions::article_id.eq(article_id))
|
.filter(article_revisions::article_id.eq(article_id))
|
||||||
.filter(article_revisions::revision.eq(new_revision))
|
.filter(article_revisions::revision.eq(new_revision))
|
||||||
.first::<models::ArticleRevision>(self.db_connection)?
|
.first::<models::ArticleRevision>(self.db_connection)?,
|
||||||
))
|
))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn create_article(&self, target_slug: Option<String>, title: String, body: String, author: Option<String>)
|
pub fn create_article(
|
||||||
-> Result<models::ArticleRevision, Error>
|
&self,
|
||||||
{
|
target_slug: Option<String>,
|
||||||
|
title: String,
|
||||||
|
body: String,
|
||||||
|
author: Option<String>,
|
||||||
|
theme: Theme,
|
||||||
|
) -> Result<models::ArticleRevision, Error> {
|
||||||
if title.is_empty() {
|
if title.is_empty() {
|
||||||
Err("title cannot be empty")?;
|
return Err("title cannot be empty".into());
|
||||||
}
|
}
|
||||||
|
|
||||||
self.db_connection.transaction(|| {
|
self.db_connection.transaction(|| {
|
||||||
#[derive(Insertable)]
|
#[derive(Insertable)]
|
||||||
#[table_name="articles"]
|
#[table_name = "articles"]
|
||||||
struct NewArticle {
|
struct NewArticle {
|
||||||
id: Option<i32>
|
id: Option<i32>,
|
||||||
}
|
}
|
||||||
|
|
||||||
let article_id = {
|
let article_id = {
|
||||||
use diesel::expression::sql_literal::sql;
|
use diesel::expression::sql_literal::sql;
|
||||||
// Diesel and SQLite are a bit in disagreement for how this should look:
|
// Diesel and SQLite are a bit in disagreement for how this should look:
|
||||||
sql::<(diesel::sql_types::Integer)>("INSERT INTO articles VALUES (null)")
|
sql::<diesel::sql_types::Integer>("INSERT INTO articles VALUES (null)")
|
||||||
.execute(self.db_connection)?;
|
.execute(self.db_connection)?;
|
||||||
sql::<(diesel::sql_types::Integer)>("SELECT LAST_INSERT_ROWID()")
|
sql::<diesel::sql_types::Integer>("SELECT LAST_INSERT_ROWID()")
|
||||||
.load::<i32>(self.db_connection)?
|
.load::<i32>(self.db_connection)?
|
||||||
.pop().expect("Statement must evaluate to an integer")
|
.pop()
|
||||||
|
.expect("Statement must evaluate to an integer")
|
||||||
};
|
};
|
||||||
|
|
||||||
let slug = decide_slug(self.db_connection, article_id, "", &title, target_slug.as_ref().map(|x| &**x))?;
|
let slug = decide_slug(
|
||||||
|
self.db_connection,
|
||||||
|
article_id,
|
||||||
|
"",
|
||||||
|
&title,
|
||||||
|
target_slug.as_deref(),
|
||||||
|
)?;
|
||||||
|
|
||||||
let new_revision = 1;
|
let new_revision = 1;
|
||||||
|
|
||||||
|
@ -350,20 +434,26 @@ impl<'a> SyncState<'a> {
|
||||||
slug: &slug,
|
slug: &slug,
|
||||||
title: &title,
|
title: &title,
|
||||||
body: &body,
|
body: &body,
|
||||||
author: author.as_ref().map(|x| &**x),
|
author: author.as_deref(),
|
||||||
latest: true,
|
latest: true,
|
||||||
|
theme,
|
||||||
})
|
})
|
||||||
.execute(self.db_connection)?;
|
.execute(self.db_connection)?;
|
||||||
|
|
||||||
Ok(article_revisions::table
|
Ok(article_revisions::table
|
||||||
.filter(article_revisions::article_id.eq(article_id))
|
.filter(article_revisions::article_id.eq(article_id))
|
||||||
.filter(article_revisions::revision.eq(new_revision))
|
.filter(article_revisions::revision.eq(new_revision))
|
||||||
.first::<models::ArticleRevision>(self.db_connection)?
|
.first::<models::ArticleRevision>(self.db_connection)?)
|
||||||
)
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn search_query(&self, query_string: String, limit: i32, offset: i32, snippet_size: i32) -> Result<Vec<models::SearchResult>, Error> {
|
pub fn search_query(
|
||||||
|
&self,
|
||||||
|
query_string: String,
|
||||||
|
limit: i32,
|
||||||
|
offset: i32,
|
||||||
|
snippet_size: i32,
|
||||||
|
) -> Result<Vec<models::SearchResult>, Error> {
|
||||||
use diesel::sql_query;
|
use diesel::sql_query;
|
||||||
use diesel::sql_types::{Integer, Text};
|
use diesel::sql_types::{Integer, Text};
|
||||||
|
|
||||||
|
@ -401,7 +491,10 @@ impl<'a> SyncState<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl State {
|
impl State {
|
||||||
pub fn new(connection_pool: Pool<ConnectionManager<SqliteConnection>>, cpu_pool: futures_cpupool::CpuPool) -> State {
|
pub fn new(
|
||||||
|
connection_pool: Pool<ConnectionManager<SqliteConnection>>,
|
||||||
|
cpu_pool: futures_cpupool::CpuPool,
|
||||||
|
) -> State {
|
||||||
State {
|
State {
|
||||||
connection_pool,
|
connection_pool,
|
||||||
cpu_pool,
|
cpu_pool,
|
||||||
|
@ -411,7 +504,7 @@ impl State {
|
||||||
fn execute<F, T>(&self, f: F) -> CpuFuture<T, Error>
|
fn execute<F, T>(&self, f: F) -> CpuFuture<T, Error>
|
||||||
where
|
where
|
||||||
F: 'static + Sync + Send,
|
F: 'static + Sync + Send,
|
||||||
for <'a> F: FnOnce(SyncState<'a>) -> Result<T, Error>,
|
for<'a> F: FnOnce(SyncState<'a>) -> Result<T, Error>,
|
||||||
T: 'static + Send,
|
T: 'static + Send,
|
||||||
{
|
{
|
||||||
let connection_pool = self.connection_pool.clone();
|
let connection_pool = self.connection_pool.clone();
|
||||||
|
@ -427,21 +520,30 @@ impl State {
|
||||||
self.execute(move |state| state.get_article_slug(article_id))
|
self.execute(move |state| state.get_article_slug(article_id))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_article_revision(&self, article_id: i32, revision: i32) -> CpuFuture<Option<models::ArticleRevision>, Error> {
|
pub fn get_article_revision(
|
||||||
|
&self,
|
||||||
|
article_id: i32,
|
||||||
|
revision: i32,
|
||||||
|
) -> CpuFuture<Option<models::ArticleRevision>, Error> {
|
||||||
self.execute(move |state| state.get_article_revision(article_id, revision))
|
self.execute(move |state| state.get_article_revision(article_id, revision))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn query_article_revision_stubs<F>(&self, f: F) -> CpuFuture<Vec<models::ArticleRevisionStub>, Error>
|
pub fn query_article_revision_stubs<F>(
|
||||||
|
&self,
|
||||||
|
f: F,
|
||||||
|
) -> CpuFuture<Vec<models::ArticleRevisionStub>, Error>
|
||||||
where
|
where
|
||||||
F: 'static + Send + Sync,
|
F: 'static + Send + Sync,
|
||||||
for <'a> F:
|
for<'a> F: FnOnce(
|
||||||
FnOnce(article_revisions::BoxedQuery<'a, diesel::sqlite::Sqlite>) ->
|
|
||||||
article_revisions::BoxedQuery<'a, diesel::sqlite::Sqlite>,
|
article_revisions::BoxedQuery<'a, diesel::sqlite::Sqlite>,
|
||||||
|
) -> article_revisions::BoxedQuery<'a, diesel::sqlite::Sqlite>,
|
||||||
{
|
{
|
||||||
self.execute(move |state| state.query_article_revision_stubs(f))
|
self.execute(move |state| state.query_article_revision_stubs(f))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_latest_article_revision_stubs(&self) -> CpuFuture<Vec<models::ArticleRevisionStub>, Error> {
|
pub fn get_latest_article_revision_stubs(
|
||||||
|
&self,
|
||||||
|
) -> CpuFuture<Vec<models::ArticleRevisionStub>, Error> {
|
||||||
self.query_article_revision_stubs(|query| {
|
self.query_article_revision_stubs(|query| {
|
||||||
query
|
query
|
||||||
.filter(article_revisions::latest.eq(true))
|
.filter(article_revisions::latest.eq(true))
|
||||||
|
@ -453,19 +555,38 @@ impl State {
|
||||||
self.execute(move |state| state.lookup_slug(slug))
|
self.execute(move |state| state.lookup_slug(slug))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update_article(&self, article_id: i32, base_revision: i32, title: String, body: String, author: Option<String>)
|
pub fn update_article(
|
||||||
-> CpuFuture<UpdateResult, Error>
|
&self,
|
||||||
{
|
article_id: i32,
|
||||||
self.execute(move |state| state.update_article(article_id, base_revision, title, body, author))
|
base_revision: i32,
|
||||||
|
title: String,
|
||||||
|
body: String,
|
||||||
|
author: Option<String>,
|
||||||
|
theme: Option<Theme>,
|
||||||
|
) -> CpuFuture<UpdateResult, Error> {
|
||||||
|
self.execute(move |state| {
|
||||||
|
state.update_article(article_id, base_revision, title, body, author, theme)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn create_article(&self, target_slug: Option<String>, title: String, body: String, author: Option<String>)
|
pub fn create_article(
|
||||||
-> CpuFuture<models::ArticleRevision, Error>
|
&self,
|
||||||
{
|
target_slug: Option<String>,
|
||||||
self.execute(move |state| state.create_article(target_slug, title, body, author))
|
title: String,
|
||||||
|
body: String,
|
||||||
|
author: Option<String>,
|
||||||
|
theme: Theme,
|
||||||
|
) -> CpuFuture<models::ArticleRevision, Error> {
|
||||||
|
self.execute(move |state| state.create_article(target_slug, title, body, author, theme))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn search_query(&self, query_string: String, limit: i32, offset: i32, snippet_size: i32) -> CpuFuture<Vec<models::SearchResult>, Error> {
|
pub fn search_query(
|
||||||
|
&self,
|
||||||
|
query_string: String,
|
||||||
|
limit: i32,
|
||||||
|
offset: i32,
|
||||||
|
snippet_size: i32,
|
||||||
|
) -> CpuFuture<Vec<models::SearchResult>, Error> {
|
||||||
self.execute(move |state| state.search_query(query_string, limit, offset, snippet_size))
|
self.execute(move |state| state.search_query(query_string, limit, offset, snippet_size))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -473,13 +594,13 @@ impl State {
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use super::*;
|
use super::*;
|
||||||
use db;
|
use crate::db;
|
||||||
|
|
||||||
impl UpdateResult {
|
impl UpdateResult {
|
||||||
pub fn unwrap(self) -> models::ArticleRevision {
|
pub fn unwrap(self) -> models::ArticleRevision {
|
||||||
match self {
|
match self {
|
||||||
UpdateResult::Success(x) => x,
|
UpdateResult::Success(x) => x,
|
||||||
_ => panic!("Expected success")
|
_ => panic!("Expected success"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -488,7 +609,7 @@ mod test {
|
||||||
($state:ident) => {
|
($state:ident) => {
|
||||||
let db = db::test_connection();
|
let db = db::test_connection();
|
||||||
let $state = SyncState::new(&db);
|
let $state = SyncState::new(&db);
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -500,16 +621,27 @@ mod test {
|
||||||
#[test]
|
#[test]
|
||||||
fn create_article() {
|
fn create_article() {
|
||||||
init!(state);
|
init!(state);
|
||||||
let article_revision = state.create_article(None, "Title".into(), "Body".into(), None).unwrap();
|
let article_revision = state
|
||||||
|
.create_article(None, "Title".into(), "Body".into(), None, Theme::Cyan)
|
||||||
|
.unwrap();
|
||||||
assert_eq!("title", article_revision.slug);
|
assert_eq!("title", article_revision.slug);
|
||||||
assert_eq!(true, article_revision.latest);
|
assert!(article_revision.latest);
|
||||||
|
assert_eq!(Theme::Cyan, article_revision.theme);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn create_article_when_empty_slug_then_empty_slug() {
|
fn create_article_when_empty_slug_then_empty_slug() {
|
||||||
// Front page gets to keep its empty slug
|
// Front page gets to keep its empty slug
|
||||||
init!(state);
|
init!(state);
|
||||||
let article_revision = state.create_article(Some("".into()), "Title".into(), "Body".into(), None).unwrap();
|
let article_revision = state
|
||||||
|
.create_article(
|
||||||
|
Some("".into()),
|
||||||
|
"Title".into(),
|
||||||
|
"Body".into(),
|
||||||
|
None,
|
||||||
|
Theme::Cyan,
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
assert_eq!("", article_revision.slug);
|
assert_eq!("", article_revision.slug);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -517,9 +649,21 @@ mod test {
|
||||||
fn update_article() {
|
fn update_article() {
|
||||||
init!(state);
|
init!(state);
|
||||||
|
|
||||||
let article = state.create_article(None, "Title".into(), "Body".into(), None).unwrap();
|
let article = state
|
||||||
|
.create_article(None, "Title".into(), "Body".into(), None, Theme::Cyan)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
let new_revision = state.update_article(article.article_id, article.revision, article.title.clone(), "New body".into(), None).unwrap().unwrap();
|
let new_revision = state
|
||||||
|
.update_article(
|
||||||
|
article.article_id,
|
||||||
|
article.revision,
|
||||||
|
article.title.clone(),
|
||||||
|
"New body".into(),
|
||||||
|
None,
|
||||||
|
Some(Theme::BlueGray),
|
||||||
|
)
|
||||||
|
.unwrap()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
assert_eq!(article.article_id, new_revision.article_id);
|
assert_eq!(article.article_id, new_revision.article_id);
|
||||||
|
|
||||||
|
@ -532,46 +676,135 @@ mod test {
|
||||||
assert_eq!(article.slug, new_revision.slug);
|
assert_eq!(article.slug, new_revision.slug);
|
||||||
|
|
||||||
assert_eq!("New body", new_revision.body);
|
assert_eq!("New body", new_revision.body);
|
||||||
|
assert_eq!(Theme::BlueGray, new_revision.theme);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn update_article_when_sequential_edits_then_last_wins() {
|
fn update_article_when_sequential_edits_then_last_wins() {
|
||||||
init!(state);
|
init!(state);
|
||||||
|
|
||||||
let article = state.create_article(None, "Title".into(), "Body".into(), None).unwrap();
|
let article = state
|
||||||
|
.create_article(None, "Title".into(), "Body".into(), None, Theme::Cyan)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
let first_edit = state.update_article(article.article_id, article.revision, article.title.clone(), "New body".into(), None).unwrap().unwrap();
|
let first_edit = state
|
||||||
let second_edit = state.update_article(article.article_id, first_edit.revision, article.title.clone(), "Newer body".into(), None).unwrap().unwrap();
|
.update_article(
|
||||||
|
article.article_id,
|
||||||
|
article.revision,
|
||||||
|
article.title.clone(),
|
||||||
|
"New body".into(),
|
||||||
|
None,
|
||||||
|
Some(Theme::Blue),
|
||||||
|
)
|
||||||
|
.unwrap()
|
||||||
|
.unwrap();
|
||||||
|
let second_edit = state
|
||||||
|
.update_article(
|
||||||
|
article.article_id,
|
||||||
|
first_edit.revision,
|
||||||
|
article.title,
|
||||||
|
"Newer body".into(),
|
||||||
|
None,
|
||||||
|
Some(Theme::Amber),
|
||||||
|
)
|
||||||
|
.unwrap()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
assert_eq!("Newer body", second_edit.body);
|
assert_eq!("Newer body", second_edit.body);
|
||||||
|
assert_eq!(Theme::Amber, second_edit.theme);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn update_article_when_edit_conflict_then_merge() {
|
fn update_article_when_edit_conflict_then_merge() {
|
||||||
init!(state);
|
init!(state);
|
||||||
|
|
||||||
let article = state.create_article(None, "Title".into(), "a\nb\nc\n".into(), None).unwrap();
|
let article = state
|
||||||
|
.create_article(None, "Title".into(), "a\nb\nc\n".into(), None, Theme::Cyan)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
let first_edit = state.update_article(article.article_id, article.revision, article.title.clone(), "a\nx\nb\nc\n".into(), None).unwrap().unwrap();
|
let first_edit = state
|
||||||
let second_edit = state.update_article(article.article_id, article.revision, article.title.clone(), "a\nb\ny\nc\n".into(), None).unwrap().unwrap();
|
.update_article(
|
||||||
|
article.article_id,
|
||||||
|
article.revision,
|
||||||
|
article.title.clone(),
|
||||||
|
"a\nx\nb\nc\n".into(),
|
||||||
|
None,
|
||||||
|
Some(Theme::Blue),
|
||||||
|
)
|
||||||
|
.unwrap()
|
||||||
|
.unwrap();
|
||||||
|
let second_edit = state
|
||||||
|
.update_article(
|
||||||
|
article.article_id,
|
||||||
|
article.revision,
|
||||||
|
article.title.clone(),
|
||||||
|
"a\nb\ny\nc\n".into(),
|
||||||
|
None,
|
||||||
|
Some(Theme::Amber),
|
||||||
|
)
|
||||||
|
.unwrap()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
assert!(article.revision < first_edit.revision);
|
assert!(article.revision < first_edit.revision);
|
||||||
assert!(first_edit.revision < second_edit.revision);
|
assert!(first_edit.revision < second_edit.revision);
|
||||||
|
|
||||||
assert_eq!("a\nx\nb\ny\nc\n", second_edit.body);
|
assert_eq!("a\nx\nb\ny\nc\n", second_edit.body);
|
||||||
|
assert_eq!(Theme::Amber, second_edit.theme);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn update_article_when_edit_conflict_then_rebase_over_multiple_revisions() {
|
fn update_article_when_edit_conflict_then_rebase_over_multiple_revisions() {
|
||||||
init!(state);
|
init!(state);
|
||||||
|
|
||||||
let article = state.create_article(None, "Title".into(), "a\nb\nc\n".into(), None).unwrap();
|
let article = state
|
||||||
|
.create_article(None, "Title".into(), "a\nb\nc\n".into(), None, Theme::Cyan)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
let edit = state.update_article(article.article_id, article.revision, article.title.clone(), "a\nx1\nb\nc\n".into(), None).unwrap().unwrap();
|
let edit = state
|
||||||
let edit = state.update_article(article.article_id, edit.revision, article.title.clone(), "a\nx1\nx2\nb\nc\n".into(), None).unwrap().unwrap();
|
.update_article(
|
||||||
let edit = state.update_article(article.article_id, edit.revision, article.title.clone(), "a\nx1\nx2\nx3\nb\nc\n".into(), None).unwrap().unwrap();
|
article.article_id,
|
||||||
|
article.revision,
|
||||||
|
article.title.clone(),
|
||||||
|
"a\nx1\nb\nc\n".into(),
|
||||||
|
None,
|
||||||
|
Some(article.theme),
|
||||||
|
)
|
||||||
|
.unwrap()
|
||||||
|
.unwrap();
|
||||||
|
let edit = state
|
||||||
|
.update_article(
|
||||||
|
article.article_id,
|
||||||
|
edit.revision,
|
||||||
|
article.title.clone(),
|
||||||
|
"a\nx1\nx2\nb\nc\n".into(),
|
||||||
|
None,
|
||||||
|
Some(article.theme),
|
||||||
|
)
|
||||||
|
.unwrap()
|
||||||
|
.unwrap();
|
||||||
|
let edit = state
|
||||||
|
.update_article(
|
||||||
|
article.article_id,
|
||||||
|
edit.revision,
|
||||||
|
article.title.clone(),
|
||||||
|
"a\nx1\nx2\nx3\nb\nc\n".into(),
|
||||||
|
None,
|
||||||
|
Some(article.theme),
|
||||||
|
)
|
||||||
|
.unwrap()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
let rebase_edit = state.update_article(article.article_id, article.revision, article.title.clone(), "a\nb\ny\nc\n".into(), None).unwrap().unwrap();
|
let rebase_edit = state
|
||||||
|
.update_article(
|
||||||
|
article.article_id,
|
||||||
|
article.revision,
|
||||||
|
article.title.clone(),
|
||||||
|
"a\nb\ny\nc\n".into(),
|
||||||
|
None,
|
||||||
|
Some(article.theme),
|
||||||
|
)
|
||||||
|
.unwrap()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
assert!(article.revision < edit.revision);
|
assert!(article.revision < edit.revision);
|
||||||
assert!(edit.revision < rebase_edit.revision);
|
assert!(edit.revision < rebase_edit.revision);
|
||||||
|
@ -583,10 +816,32 @@ mod test {
|
||||||
fn update_article_when_title_edit_conflict_then_merge_title() {
|
fn update_article_when_title_edit_conflict_then_merge_title() {
|
||||||
init!(state);
|
init!(state);
|
||||||
|
|
||||||
let article = state.create_article(None, "titlle".into(), "".into(), None).unwrap();
|
let article = state
|
||||||
|
.create_article(None, "titlle".into(), "".into(), None, Theme::Cyan)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
let first_edit = state.update_article(article.article_id, article.revision, "Titlle".into(), article.body.clone(), None).unwrap().unwrap();
|
let first_edit = state
|
||||||
let second_edit = state.update_article(article.article_id, article.revision, "title".into(), article.body.clone(), None).unwrap().unwrap();
|
.update_article(
|
||||||
|
article.article_id,
|
||||||
|
article.revision,
|
||||||
|
"Titlle".into(),
|
||||||
|
article.body.clone(),
|
||||||
|
None,
|
||||||
|
Some(article.theme),
|
||||||
|
)
|
||||||
|
.unwrap()
|
||||||
|
.unwrap();
|
||||||
|
let second_edit = state
|
||||||
|
.update_article(
|
||||||
|
article.article_id,
|
||||||
|
article.revision,
|
||||||
|
"title".into(),
|
||||||
|
article.body.clone(),
|
||||||
|
None,
|
||||||
|
Some(article.theme),
|
||||||
|
)
|
||||||
|
.unwrap()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
assert!(article.revision < first_edit.revision);
|
assert!(article.revision < first_edit.revision);
|
||||||
assert!(first_edit.revision < second_edit.revision);
|
assert!(first_edit.revision < second_edit.revision);
|
||||||
|
@ -598,20 +853,110 @@ mod test {
|
||||||
fn update_article_when_merge_conflict() {
|
fn update_article_when_merge_conflict() {
|
||||||
init!(state);
|
init!(state);
|
||||||
|
|
||||||
let article = state.create_article(None, "Title".into(), "a".into(), None).unwrap();
|
let article = state
|
||||||
|
.create_article(None, "Title".into(), "a".into(), None, Theme::Cyan)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
let first_edit = state.update_article(article.article_id, article.revision, article.title.clone(), "b".into(), None).unwrap().unwrap();
|
let first_edit = state
|
||||||
let conflict_edit = state.update_article(article.article_id, article.revision, article.title.clone(), "c".into(), None).unwrap();
|
.update_article(
|
||||||
|
article.article_id,
|
||||||
|
article.revision,
|
||||||
|
article.title.clone(),
|
||||||
|
"b".into(),
|
||||||
|
None,
|
||||||
|
Some(Theme::Blue),
|
||||||
|
)
|
||||||
|
.unwrap()
|
||||||
|
.unwrap();
|
||||||
|
let conflict_edit = state
|
||||||
|
.update_article(
|
||||||
|
article.article_id,
|
||||||
|
article.revision,
|
||||||
|
article.title.clone(),
|
||||||
|
"c".into(),
|
||||||
|
None,
|
||||||
|
Some(Theme::Amber),
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
match conflict_edit {
|
match conflict_edit {
|
||||||
UpdateResult::Success(..) => panic!("Expected conflict"),
|
UpdateResult::Success(..) => panic!("Expected conflict"),
|
||||||
UpdateResult::RebaseConflict(RebaseConflict { base_article, title, body }) => {
|
UpdateResult::RebaseConflict(RebaseConflict {
|
||||||
|
base_article,
|
||||||
|
title,
|
||||||
|
body,
|
||||||
|
theme,
|
||||||
|
}) => {
|
||||||
assert_eq!(first_edit.revision, base_article.revision);
|
assert_eq!(first_edit.revision, base_article.revision);
|
||||||
assert_eq!(title, merge::MergeResult::Clean(article.title.clone()));
|
assert_eq!(title, merge::MergeResult::Clean(article.title));
|
||||||
assert_eq!(body, merge::MergeResult::Conflicted(vec![
|
assert_eq!(
|
||||||
merge::Output::Conflict(vec!["c"], vec!["a"], vec!["b"]),
|
body,
|
||||||
]).to_strings());
|
merge::MergeResult::Conflicted(vec![merge::Output::Conflict(
|
||||||
|
vec!["c"],
|
||||||
|
vec!["a"],
|
||||||
|
vec!["b"]
|
||||||
|
),])
|
||||||
|
.into_strings()
|
||||||
|
);
|
||||||
|
assert_eq!(Theme::Amber, theme);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn update_article_when_theme_conflict_then_ignore_unchanged() {
|
||||||
|
init!(state);
|
||||||
|
|
||||||
|
let article = state
|
||||||
|
.create_article(None, "Title".into(), "a\nb\nc\n".into(), None, Theme::Cyan)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let _first_edit = state
|
||||||
|
.update_article(
|
||||||
|
article.article_id,
|
||||||
|
article.revision,
|
||||||
|
article.title.clone(),
|
||||||
|
"a\nx\nb\nc\n".into(),
|
||||||
|
None,
|
||||||
|
Some(Theme::Blue),
|
||||||
|
)
|
||||||
|
.unwrap()
|
||||||
|
.unwrap();
|
||||||
|
let second_edit = state
|
||||||
|
.update_article(
|
||||||
|
article.article_id,
|
||||||
|
article.revision,
|
||||||
|
article.title,
|
||||||
|
"a\nb\ny\nc\n".into(),
|
||||||
|
None,
|
||||||
|
Some(Theme::Cyan),
|
||||||
|
)
|
||||||
|
.unwrap()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
assert_eq!(Theme::Blue, second_edit.theme);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn update_article_with_no_given_theme_then_theme_unchanged() {
|
||||||
|
init!(state);
|
||||||
|
|
||||||
|
let article = state
|
||||||
|
.create_article(None, "Title".into(), "a\nb\nc\n".into(), None, Theme::Cyan)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let edit = state
|
||||||
|
.update_article(
|
||||||
|
article.article_id,
|
||||||
|
article.revision,
|
||||||
|
article.title,
|
||||||
|
article.body,
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
)
|
||||||
|
.unwrap()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
assert_eq!(Theme::Cyan, edit.theme);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
232
src/theme.rs
Normal file
232
src/theme.rs
Normal file
|
@ -0,0 +1,232 @@
|
||||||
|
use std::io::Write;
|
||||||
|
|
||||||
|
use diesel::backend::Backend;
|
||||||
|
use diesel::deserialize::{self, FromSql};
|
||||||
|
use diesel::serialize::{self, Output, ToSql};
|
||||||
|
use diesel::sql_types::Text;
|
||||||
|
use diesel::sqlite::Sqlite;
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq, Eq, Clone, Copy, Serialize, Deserialize)] // Serde
|
||||||
|
#[serde(rename_all = "kebab-case")]
|
||||||
|
#[derive(AsExpression, FromSqlRow)] // Diesel
|
||||||
|
#[sql_type = "Text"]
|
||||||
|
pub enum Theme {
|
||||||
|
Red,
|
||||||
|
Pink,
|
||||||
|
Purple,
|
||||||
|
DeepPurple,
|
||||||
|
Indigo,
|
||||||
|
Blue,
|
||||||
|
LightBlue,
|
||||||
|
Cyan,
|
||||||
|
Teal,
|
||||||
|
Green,
|
||||||
|
LightGreen,
|
||||||
|
Lime,
|
||||||
|
Yellow,
|
||||||
|
Amber,
|
||||||
|
Orange,
|
||||||
|
DeepOrange,
|
||||||
|
Brown,
|
||||||
|
Gray,
|
||||||
|
BlueGray,
|
||||||
|
}
|
||||||
|
|
||||||
|
use self::Theme::*;
|
||||||
|
|
||||||
|
forward_display_to_serde!(Theme);
|
||||||
|
forward_from_str_to_serde!(Theme);
|
||||||
|
|
||||||
|
pub const THEMES: [Theme; 19] = [
|
||||||
|
Red, Pink, Purple, DeepPurple, Indigo, Blue, LightBlue, Cyan, Teal, Green, LightGreen, Lime,
|
||||||
|
Yellow, Amber, Orange, DeepOrange, Brown, Gray, BlueGray,
|
||||||
|
];
|
||||||
|
|
||||||
|
pub fn theme_from_str_hash(x: &str) -> Theme {
|
||||||
|
let hash = seahash::hash(x.as_bytes()) as usize;
|
||||||
|
let choice = hash % THEMES.len();
|
||||||
|
THEMES[choice]
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn random() -> Theme {
|
||||||
|
use rand::Rng;
|
||||||
|
*rand::thread_rng()
|
||||||
|
.choose(&THEMES)
|
||||||
|
.expect("Could only fail for an empty slice")
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ToSql<Text, Sqlite> for Theme {
|
||||||
|
fn to_sql<W: Write>(&self, out: &mut Output<W, Sqlite>) -> serialize::Result {
|
||||||
|
ToSql::<Text, Sqlite>::to_sql(&self.to_string(), out)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl FromSql<Text, Sqlite> for Theme {
|
||||||
|
fn from_sql(value: Option<&<Sqlite as Backend>::RawValue>) -> deserialize::Result<Self> {
|
||||||
|
// See Diesel's documentation on how to implement FromSql for Sqlite,
|
||||||
|
// especially with regards to the unsafe conversion below.
|
||||||
|
// http://docs.diesel.rs/diesel/deserialize/trait.FromSql.html
|
||||||
|
let text_ptr = <*const str as FromSql<Text, Sqlite>>::from_sql(value)?;
|
||||||
|
let text = unsafe { &*text_ptr };
|
||||||
|
text.parse().map_err(Into::into)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct CssClass(Theme);
|
||||||
|
|
||||||
|
impl Theme {
|
||||||
|
pub fn css_class(self) -> CssClass {
|
||||||
|
CssClass(self)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
use std::fmt::{self, Display, Formatter};
|
||||||
|
|
||||||
|
impl Display for CssClass {
|
||||||
|
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
|
||||||
|
write!(fmt, "theme-{}", self.0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test {
|
||||||
|
use std::error::Error;
|
||||||
|
|
||||||
|
use diesel::prelude::*;
|
||||||
|
use diesel::sql_query;
|
||||||
|
use diesel::sql_types::Text;
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn basic_serialize() {
|
||||||
|
assert_eq!(serde_plain::to_string(&Theme::Red).unwrap(), "red");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn serialize_kebab_case() {
|
||||||
|
assert_eq!(
|
||||||
|
serde_plain::to_string(&Theme::LightGreen).unwrap(),
|
||||||
|
"light-green"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn serialize_json() {
|
||||||
|
#[derive(Serialize)]
|
||||||
|
struct Test {
|
||||||
|
x: Theme,
|
||||||
|
}
|
||||||
|
assert_eq!(
|
||||||
|
serde_json::to_string(&Test { x: Theme::Red }).unwrap(),
|
||||||
|
"{\"x\":\"red\"}"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn deserialize_json() {
|
||||||
|
#[derive(Deserialize, Debug, PartialEq, Eq)]
|
||||||
|
struct Test {
|
||||||
|
x: Theme,
|
||||||
|
}
|
||||||
|
assert_eq!(
|
||||||
|
serde_json::from_str::<Test>("{\"x\":\"red\"}").unwrap(),
|
||||||
|
Test { x: Theme::Red }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn serialize_urlencoded() {
|
||||||
|
#[derive(Serialize)]
|
||||||
|
struct Test {
|
||||||
|
x: Theme,
|
||||||
|
}
|
||||||
|
assert_eq!(
|
||||||
|
serde_urlencoded::to_string(&Test { x: Theme::Red }).unwrap(),
|
||||||
|
"x=red"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn deserialize_urlencoded() {
|
||||||
|
#[derive(Deserialize, Debug, PartialEq, Eq)]
|
||||||
|
struct Test {
|
||||||
|
x: Theme,
|
||||||
|
}
|
||||||
|
assert_eq!(
|
||||||
|
serde_urlencoded::from_str::<Test>("x=red").unwrap(),
|
||||||
|
Test { x: Theme::Red }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn basic_display() {
|
||||||
|
assert_eq!(&Theme::Red.to_string(), "red");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn display_kebab_case() {
|
||||||
|
assert_eq!(&Theme::LightGreen.to_string(), "light-green");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn basic_from_str() {
|
||||||
|
let indigo: Theme = "indigo".parse().unwrap();
|
||||||
|
assert_eq!(indigo, Theme::Indigo);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn to_number() {
|
||||||
|
assert_eq!(Theme::Red as i32, 0);
|
||||||
|
assert_eq!(Theme::LightGreen as i32, 10);
|
||||||
|
assert_eq!(Theme::BlueGray as i32, 18);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn from_str_hash() {
|
||||||
|
assert_eq!(theme_from_str_hash("Bartefjes"), Theme::Orange);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn css_class_display() {
|
||||||
|
assert_eq!(&Theme::Red.css_class().to_string(), "theme-red");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn basic_db_roundtrip() -> Result<(), Box<dyn Error>> {
|
||||||
|
let conn = SqliteConnection::establish(":memory:")?;
|
||||||
|
|
||||||
|
#[derive(QueryableByName, PartialEq, Eq, Debug)]
|
||||||
|
struct Row {
|
||||||
|
#[sql_type = "Text"]
|
||||||
|
theme: Theme,
|
||||||
|
}
|
||||||
|
|
||||||
|
let res = sql_query("SELECT ? as theme")
|
||||||
|
.bind::<Text, _>(DeepPurple)
|
||||||
|
.load::<Row>(&conn)?;
|
||||||
|
|
||||||
|
assert_eq!(&[Row { theme: DeepPurple }], res.as_slice());
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn db_invalid_value_gives_error() -> Result<(), Box<dyn Error>> {
|
||||||
|
let conn = SqliteConnection::establish(":memory:")?;
|
||||||
|
|
||||||
|
#[derive(QueryableByName, PartialEq, Eq, Debug)]
|
||||||
|
struct Row {
|
||||||
|
#[sql_type = "Text"]
|
||||||
|
theme: Theme,
|
||||||
|
}
|
||||||
|
|
||||||
|
let res = sql_query("SELECT 'green' as theme").load::<Row>(&conn);
|
||||||
|
assert!(res.is_ok());
|
||||||
|
|
||||||
|
let res = sql_query("SELECT 'blueish-yellow' as theme").load::<Row>(&conn);
|
||||||
|
assert!(res.is_err());
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,9 +1,7 @@
|
||||||
use futures;
|
|
||||||
|
|
||||||
pub trait Lookup {
|
pub trait Lookup {
|
||||||
type Resource;
|
type Resource;
|
||||||
type Error;
|
type Error;
|
||||||
type Future: futures::Future<Item=Option<Self::Resource>, Error=Self::Error>;
|
type Future: futures::Future<Item = Option<Self::Resource>, Error = Self::Error>;
|
||||||
|
|
||||||
fn lookup(&self, path: &str, query: Option<&str>) -> Self::Future;
|
fn lookup(&self, path: &str, query: Option<&str>) -> Self::Future;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
mod resource;
|
|
||||||
mod lookup;
|
mod lookup;
|
||||||
|
mod resource;
|
||||||
|
|
||||||
pub use self::resource::*;
|
|
||||||
pub use self::lookup::*;
|
pub use self::lookup::*;
|
||||||
|
pub use self::resource::*;
|
||||||
|
|
|
@ -1,15 +1,13 @@
|
||||||
use futures;
|
|
||||||
use futures::{Future, Stream};
|
use futures::{Future, Stream};
|
||||||
use hyper::{self, header, mime, server};
|
|
||||||
use hyper::server::Response;
|
use hyper::server::Response;
|
||||||
use std;
|
use hyper::{self, header, mime, server};
|
||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
static ref TEXT_PLAIN: mime::Mime = "text/plain;charset=utf-8".parse().unwrap();
|
static ref TEXT_PLAIN: mime::Mime = "text/plain;charset=utf-8".parse().unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type Error = Box<std::error::Error + Send + Sync>;
|
pub type Error = Box<dyn std::error::Error + Send + Sync>;
|
||||||
pub type ResponseFuture = Box<futures::Future<Item = server::Response, Error = Error>>;
|
pub type ResponseFuture = Box<dyn futures::Future<Item = server::Response, Error = Error>>;
|
||||||
|
|
||||||
pub trait Resource {
|
pub trait Resource {
|
||||||
fn allow(&self) -> Vec<hyper::Method>;
|
fn allow(&self) -> Vec<hyper::Method>;
|
||||||
|
@ -23,22 +21,24 @@ pub trait Resource {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn put(self: Box<Self>, body: hyper::Body, _identity: Option<String>) -> ResponseFuture
|
fn put(self: Box<Self>, body: hyper::Body, _identity: Option<String>) -> ResponseFuture
|
||||||
where Self: 'static
|
where
|
||||||
|
Self: 'static,
|
||||||
{
|
{
|
||||||
Box::new(body
|
Box::new(
|
||||||
.fold((), |_, _| -> Result<(), hyper::Error> { Ok(()) })
|
body.fold((), |_, _| -> Result<(), hyper::Error> { Ok(()) })
|
||||||
.map_err(Into::into)
|
.map_err(Into::into)
|
||||||
.and_then(move |_| futures::finished(self.method_not_allowed()))
|
.and_then(move |_| futures::finished(self.method_not_allowed())),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn post(self: Box<Self>, body: hyper::Body, _identity: Option<String>) -> ResponseFuture
|
fn post(self: Box<Self>, body: hyper::Body, _identity: Option<String>) -> ResponseFuture
|
||||||
where Self: 'static
|
where
|
||||||
|
Self: 'static,
|
||||||
{
|
{
|
||||||
Box::new(body
|
Box::new(
|
||||||
.fold((), |_, _| -> Result<(), hyper::Error> { Ok(()) })
|
body.fold((), |_, _| -> Result<(), hyper::Error> { Ok(()) })
|
||||||
.map_err(Into::into)
|
.map_err(Into::into)
|
||||||
.and_then(move |_| futures::finished(self.method_not_allowed()))
|
.and_then(move |_| futures::finished(self.method_not_allowed())),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,48 +2,36 @@ use std::borrow::Cow;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::str::Utf8Error;
|
use std::str::Utf8Error;
|
||||||
|
|
||||||
use futures::{Future, finished, failed, done};
|
|
||||||
use futures::future::FutureResult;
|
use futures::future::FutureResult;
|
||||||
|
use futures::{done, failed, finished, Future};
|
||||||
use percent_encoding::percent_decode;
|
use percent_encoding::percent_decode;
|
||||||
use slug::slugify;
|
use slug::slugify;
|
||||||
|
|
||||||
use resources::*;
|
use crate::resources::*;
|
||||||
use assets::*;
|
use crate::state::State;
|
||||||
use state::State;
|
use crate::web::{Lookup, Resource};
|
||||||
use web::{Lookup, Resource};
|
|
||||||
|
|
||||||
type BoxResource = Box<Resource + Sync + Send>;
|
#[allow(unused)]
|
||||||
type ResourceFn = Box<Fn() -> BoxResource + Sync + Send>;
|
use crate::assets::*;
|
||||||
|
|
||||||
|
type BoxResource = Box<dyn Resource + Sync + Send>;
|
||||||
|
type ResourceFn = Box<dyn Fn() -> BoxResource + Sync + Send>;
|
||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
static ref ASSETS_MAP: HashMap<String, ResourceFn> = hashmap!{
|
static ref LICENSES_MAP: HashMap<&'static str, ResourceFn> = hashmap! {
|
||||||
format!("style-{}.css", StyleCss::checksum()) =>
|
"bsd-3-clause" => Box::new(|| Box::new(
|
||||||
Box::new(|| Box::new(StyleCss) as BoxResource) as ResourceFn,
|
|
||||||
|
|
||||||
format!("script-{}.js", ScriptJs::checksum()) =>
|
|
||||||
Box::new(|| Box::new(ScriptJs) as BoxResource) as ResourceFn,
|
|
||||||
|
|
||||||
format!("search-{}.js", SearchJs::checksum()) =>
|
|
||||||
Box::new(|| Box::new(SearchJs) as BoxResource) as ResourceFn,
|
|
||||||
|
|
||||||
format!("amatic-sc-v9-latin-regular.woff") =>
|
|
||||||
Box::new(|| Box::new(AmaticFont) as BoxResource) as ResourceFn,
|
|
||||||
};
|
|
||||||
|
|
||||||
static ref LICENSES_MAP: HashMap<String, ResourceFn> = hashmap!{
|
|
||||||
"bsd-3-clause".to_owned() => Box::new(|| Box::new(
|
|
||||||
HtmlResource::new(Some("../"), "The 3-Clause BSD License", include_str!("licenses/bsd-3-clause.html"))
|
HtmlResource::new(Some("../"), "The 3-Clause BSD License", include_str!("licenses/bsd-3-clause.html"))
|
||||||
) as BoxResource) as ResourceFn,
|
) as BoxResource) as ResourceFn,
|
||||||
"gpl3".to_owned() => Box::new(|| Box::new(
|
"gpl3" => Box::new(|| Box::new(
|
||||||
HtmlResource::new(Some("../"), "GNU General Public License", include_str!("licenses/gpl3.html"))
|
HtmlResource::new(Some("../"), "GNU General Public License", include_str!("licenses/gpl3.html"))
|
||||||
) as BoxResource) as ResourceFn,
|
) as BoxResource) as ResourceFn,
|
||||||
"mit".to_owned() => Box::new(|| Box::new(
|
"mit" => Box::new(|| Box::new(
|
||||||
HtmlResource::new(Some("../"), "The MIT License", include_str!("licenses/mit.html"))
|
HtmlResource::new(Some("../"), "The MIT License", include_str!("licenses/mit.html"))
|
||||||
) as BoxResource) as ResourceFn,
|
) as BoxResource) as ResourceFn,
|
||||||
"mpl2".to_owned() => Box::new(|| Box::new(
|
"mpl2" => Box::new(|| Box::new(
|
||||||
HtmlResource::new(Some("../"), "Mozilla Public License Version 2.0", include_str!("licenses/mpl2.html"))
|
HtmlResource::new(Some("../"), "Mozilla Public License Version 2.0", include_str!("licenses/mpl2.html"))
|
||||||
) as BoxResource) as ResourceFn,
|
) as BoxResource) as ResourceFn,
|
||||||
"sil-ofl-1.1".to_owned() => Box::new(|| Box::new(
|
"sil-ofl-1.1" => Box::new(|| Box::new(
|
||||||
HtmlResource::new(Some("../"), "SIL Open Font License", include_str!("licenses/sil-ofl-1.1.html"))
|
HtmlResource::new(Some("../"), "SIL Open Font License", include_str!("licenses/sil-ofl-1.1.html"))
|
||||||
) as BoxResource) as ResourceFn,
|
) as BoxResource) as ResourceFn,
|
||||||
};
|
};
|
||||||
|
@ -66,9 +54,10 @@ fn split_one(path: &str) -> Result<(Cow<str>, Option<&str>), Utf8Error> {
|
||||||
Ok((head, tail))
|
Ok((head, tail))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn map_lookup(map: &HashMap<String, ResourceFn>, path: &str) ->
|
fn map_lookup(
|
||||||
FutureResult<Option<BoxResource>, Box<::std::error::Error + Send + Sync>>
|
map: &HashMap<&str, ResourceFn>,
|
||||||
{
|
path: &str,
|
||||||
|
) -> FutureResult<Option<BoxResource>, Box<dyn ::std::error::Error + Send + Sync>> {
|
||||||
let (head, tail) = match split_one(path) {
|
let (head, tail) = match split_one(path) {
|
||||||
Ok(x) => x,
|
Ok(x) => x,
|
||||||
Err(x) => return failed(x.into()),
|
Err(x) => return failed(x.into()),
|
||||||
|
@ -84,13 +73,49 @@ fn map_lookup(map: &HashMap<String, ResourceFn>, path: &str) ->
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(unused)]
|
||||||
|
fn fs_lookup(
|
||||||
|
root: &str,
|
||||||
|
path: &str,
|
||||||
|
) -> FutureResult<Option<BoxResource>, Box<dyn ::std::error::Error + Send + Sync>> {
|
||||||
|
use std::fs::File;
|
||||||
|
use std::io::prelude::*;
|
||||||
|
|
||||||
|
let extension = path.rsplit_once('.').map(|x| x.1);
|
||||||
|
|
||||||
|
let content_type = match extension {
|
||||||
|
Some("html") => "text/html",
|
||||||
|
Some("css") => "text/css",
|
||||||
|
Some("js") => "application/javascript",
|
||||||
|
Some("woff") => "application/font-woff",
|
||||||
|
_ => "application/binary",
|
||||||
|
}
|
||||||
|
.parse()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let mut filename = root.to_string();
|
||||||
|
filename.push_str(path);
|
||||||
|
|
||||||
|
let mut f = File::open(&filename).unwrap_or_else(|_| panic!("Not found: {}", filename));
|
||||||
|
|
||||||
|
let mut body = Vec::new();
|
||||||
|
f.read_to_end(&mut body).expect("Unable to read file");
|
||||||
|
|
||||||
|
finished(Some(Box::new(ReadOnlyResource { content_type, body })))
|
||||||
|
}
|
||||||
|
|
||||||
impl WikiLookup {
|
impl WikiLookup {
|
||||||
pub fn new(state: State, show_authors: bool) -> WikiLookup {
|
pub fn new(state: State, show_authors: bool) -> WikiLookup {
|
||||||
let changes_lookup = ChangesLookup::new(state.clone(), show_authors);
|
let changes_lookup = ChangesLookup::new(state.clone(), show_authors);
|
||||||
let diff_lookup = DiffLookup::new(state.clone());
|
let diff_lookup = DiffLookup::new(state.clone());
|
||||||
let search_lookup = SearchLookup::new(state.clone());
|
let search_lookup = SearchLookup::new(state.clone());
|
||||||
|
|
||||||
WikiLookup { state, changes_lookup, diff_lookup, search_lookup }
|
WikiLookup {
|
||||||
|
state,
|
||||||
|
changes_lookup,
|
||||||
|
diff_lookup,
|
||||||
|
search_lookup,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn revisions_lookup(&self, path: &str, _query: Option<&str>) -> <Self as Lookup>::Future {
|
fn revisions_lookup(&self, path: &str, _query: Option<&str>) -> <Self as Lookup>::Future {
|
||||||
|
@ -108,12 +133,12 @@ impl WikiLookup {
|
||||||
};
|
};
|
||||||
|
|
||||||
Box::new(
|
Box::new(
|
||||||
self.state.get_article_revision(article_id, revision)
|
self.state
|
||||||
.and_then(|article_revision|
|
.get_article_revision(article_id, revision)
|
||||||
Ok(article_revision.map(move |x| Box::new(
|
.and_then(|article_revision| {
|
||||||
ArticleRevisionResource::new(x)
|
Ok(article_revision
|
||||||
) as BoxResource))
|
.map(move |x| Box::new(ArticleRevisionResource::new(x)) as BoxResource))
|
||||||
)
|
}),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -130,14 +155,11 @@ impl WikiLookup {
|
||||||
Err(_) => return Box::new(finished(None)),
|
Err(_) => return Box::new(finished(None)),
|
||||||
};
|
};
|
||||||
|
|
||||||
Box::new(
|
Box::new(self.state.get_article_slug(article_id).and_then(|slug| {
|
||||||
self.state.get_article_slug(article_id)
|
Ok(slug.map(|slug| {
|
||||||
.and_then(|slug|
|
Box::new(TemporaryRedirectResource::new(format!("../{}", slug))) as BoxResource
|
||||||
Ok(slug.map(|slug| Box::new(
|
}))
|
||||||
TemporaryRedirectResource::new(format!("../{}", slug))
|
}))
|
||||||
) as BoxResource))
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn diff_lookup_f(&self, path: &str, query: Option<&str>) -> <Self as Lookup>::Future {
|
fn diff_lookup_f(&self, path: &str, query: Option<&str>) -> <Self as Lookup>::Future {
|
||||||
|
@ -163,26 +185,30 @@ impl WikiLookup {
|
||||||
};
|
};
|
||||||
|
|
||||||
match (head.as_ref(), tail) {
|
match (head.as_ref(), tail) {
|
||||||
("_about", None) =>
|
("_about", None) => Box::new(finished(Some(
|
||||||
Box::new(finished(Some(Box::new(AboutResource::new()) as BoxResource))),
|
Box::new(AboutResource::new()) as BoxResource
|
||||||
("_about", Some(license)) =>
|
))),
|
||||||
Box::new(map_lookup(&LICENSES_MAP, license)),
|
("_about", Some(license)) => Box::new(map_lookup(&LICENSES_MAP, license)),
|
||||||
("_assets", Some(asset)) =>
|
#[cfg(feature = "dynamic-assets")]
|
||||||
Box::new(map_lookup(&ASSETS_MAP, asset)),
|
("_assets", Some(asset)) => Box::new(fs_lookup(
|
||||||
("_by_id", Some(tail)) =>
|
concat!(env!("CARGO_MANIFEST_DIR"), "/assets/"),
|
||||||
self.by_id_lookup(tail, query),
|
asset,
|
||||||
("_changes", None) =>
|
)),
|
||||||
Box::new(self.changes_lookup.lookup(query)),
|
#[cfg(not(feature = "dynamic-assets"))]
|
||||||
("_diff", Some(tail)) =>
|
("_assets", Some(asset)) => Box::new(map_lookup(&ASSETS_MAP, asset)),
|
||||||
self.diff_lookup_f(tail, query),
|
("_by_id", Some(tail)) => self.by_id_lookup(tail, query),
|
||||||
("_new", None) =>
|
("_changes", None) => Box::new(self.changes_lookup.lookup(query)),
|
||||||
Box::new(finished(Some(Box::new(NewArticleResource::new(self.state.clone(), None)) as BoxResource))),
|
("_diff", Some(tail)) => self.diff_lookup_f(tail, query),
|
||||||
("_revisions", Some(tail)) =>
|
("_new", None) => Box::new(finished(Some(Box::new(NewArticleResource::new(
|
||||||
self.revisions_lookup(tail, query),
|
self.state.clone(),
|
||||||
("_search", None) =>
|
None,
|
||||||
Box::new(done(self.search_lookup.lookup(query))),
|
true,
|
||||||
("_sitemap", None) =>
|
)) as BoxResource))),
|
||||||
Box::new(finished(Some(Box::new(SitemapResource::new(self.state.clone())) as BoxResource))),
|
("_revisions", Some(tail)) => self.revisions_lookup(tail, query),
|
||||||
|
("_search", None) => Box::new(done(self.search_lookup.lookup(query))),
|
||||||
|
("_sitemap", None) => Box::new(finished(Some(Box::new(SitemapResource::new(
|
||||||
|
self.state.clone(),
|
||||||
|
)) as BoxResource))),
|
||||||
_ => Box::new(finished(None)),
|
_ => Box::new(finished(None)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -198,42 +224,49 @@ impl WikiLookup {
|
||||||
return Box::new(finished(None));
|
return Box::new(finished(None));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let edit = query == Some("edit");
|
||||||
|
|
||||||
// Normalize all user-generated slugs:
|
// Normalize all user-generated slugs:
|
||||||
let slugified_slug = slugify(&slug);
|
let slugified_slug = slugify(&slug);
|
||||||
if slugified_slug != slug {
|
if slugified_slug != slug {
|
||||||
return Box::new(finished(Some(
|
return Box::new(finished(Some(
|
||||||
Box::new(TemporaryRedirectResource::from_slug(slugified_slug)) as BoxResource
|
Box::new(TemporaryRedirectResource::from_slug(slugified_slug, edit)) as BoxResource,
|
||||||
)));
|
)));
|
||||||
}
|
}
|
||||||
|
|
||||||
let state = self.state.clone();
|
let state = self.state.clone();
|
||||||
let edit = query == Some("edit");
|
|
||||||
let slug = slug.into_owned();
|
let slug = slug.into_owned();
|
||||||
|
|
||||||
use state::SlugLookup;
|
use crate::state::SlugLookup;
|
||||||
Box::new(self.state.lookup_slug(slug.clone())
|
Box::new(self.state.lookup_slug(slug.clone()).and_then(move |x| {
|
||||||
.and_then(move |x| Ok(Some(match x {
|
Ok(Some(match x {
|
||||||
SlugLookup::Miss =>
|
SlugLookup::Miss => {
|
||||||
Box::new(NewArticleResource::new(state, Some(slug))) as BoxResource,
|
Box::new(NewArticleResource::new(state, Some(slug), edit)) as BoxResource
|
||||||
SlugLookup::Hit { article_id, revision } =>
|
}
|
||||||
Box::new(ArticleResource::new(state, article_id, revision, edit)) as BoxResource,
|
SlugLookup::Hit {
|
||||||
SlugLookup::Redirect(slug) =>
|
article_id,
|
||||||
Box::new(TemporaryRedirectResource::from_slug(slug)) as BoxResource,
|
revision,
|
||||||
})))
|
} => {
|
||||||
)
|
Box::new(ArticleResource::new(state, article_id, revision, edit)) as BoxResource
|
||||||
|
}
|
||||||
|
SlugLookup::Redirect(slug) => {
|
||||||
|
Box::new(TemporaryRedirectResource::from_slug(slug, edit)) as BoxResource
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
}))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Lookup for WikiLookup {
|
impl Lookup for WikiLookup {
|
||||||
type Resource = BoxResource;
|
type Resource = BoxResource;
|
||||||
type Error = Box<::std::error::Error + Send + Sync>;
|
type Error = Box<dyn ::std::error::Error + Send + Sync>;
|
||||||
type Future = Box<Future<Item = Option<Self::Resource>, Error = Self::Error>>;
|
type Future = Box<dyn Future<Item = Option<Self::Resource>, Error = Self::Error>>;
|
||||||
|
|
||||||
fn lookup(&self, path: &str, query: Option<&str>) -> Self::Future {
|
fn lookup(&self, path: &str, query: Option<&str>) -> Self::Future {
|
||||||
assert!(path.starts_with("/"));
|
assert!(path.starts_with('/'));
|
||||||
let path = &path[1..];
|
let path = &path[1..];
|
||||||
|
|
||||||
if path.starts_with("_") {
|
if path.starts_with('_') {
|
||||||
self.reserved_lookup(path, query)
|
self.reserved_lookup(path, query)
|
||||||
} else {
|
} else {
|
||||||
self.article_lookup(path, query)
|
self.article_lookup(path, query)
|
||||||
|
|
|
@ -1,9 +1,3 @@
|
||||||
<div class="container">
|
|
||||||
<header>
|
|
||||||
<h1>About Sausagewiki</h1>
|
|
||||||
</header>
|
|
||||||
|
|
||||||
<article>
|
|
||||||
<p>This site is running Sausagewiki, a simple, self-contained wiki engine,
|
<p>This site is running Sausagewiki, a simple, self-contained wiki engine,
|
||||||
version {{version()}}.</p>
|
version {{version()}}.</p>
|
||||||
<p>Copyright © 2017 Magnus Hovland Hoff.</p>
|
<p>Copyright © 2017 Magnus Hovland Hoff.</p>
|
||||||
|
@ -20,9 +14,9 @@ See also <a href="_about/gpl3">the full license text</a> and the
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
A huge thanks to <a href="https://www.rust-lang.org/en-US/">Rust</a> and
|
Without <a href="https://www.rust-lang.org/en-US/">Rust</a> and
|
||||||
<a href="https://www.sqlite.org/">SQLite</a>. Without these amazing projects,
|
<a href="https://www.sqlite.org/">SQLite</a>, Sausagewiki would never have
|
||||||
Sausagewiki would never have materialized. Another big thanks for the support,
|
materialized. Huge thanks to the creators. Another big thanks for the support,
|
||||||
discussions and testing by the amazing developers at Revolverhuset.
|
discussions and testing by the amazing developers at Revolverhuset.
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
|
@ -39,7 +33,3 @@ copyright holders and distributed under various licenses:
|
||||||
{{/deps}}
|
{{/deps}}
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
</article>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
{{>footer/default.html}}
|
|
||||||
|
|
|
@ -1,16 +1,24 @@
|
||||||
<script src="_assets/script-{{script_js_checksum()}}.js" defer></script>
|
<script src="_assets/{{script_js()}}" defer></script>
|
||||||
|
|
||||||
<div class="container {{#edit?}}edit{{/edit}}">
|
<div class="container {{#edit?}}edit{{/edit}}">
|
||||||
<div class="rendered">
|
<div class="rendered">
|
||||||
{{>article_contents.html}}
|
{{>article_contents.html}}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="editor">
|
<form autocomplete="off" id="article-editor" action="" method="POST">
|
||||||
<form action="" method="POST">
|
|
||||||
|
|
||||||
|
<div class="editor">
|
||||||
|
<div class="hero">
|
||||||
<header>
|
<header>
|
||||||
<h1><input autocomplete=off type=text name=title value="{{title}}" placeholder="Title" required></h1>
|
<h1><input autocomplete=off type=text name=title value="{{title}}" placeholder="Title" required></h1>
|
||||||
</header>
|
</header>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="theme-picker">
|
||||||
|
{{#themes}}
|
||||||
|
<input autocomplete="off" type="radio" name="theme" value="{{.theme}}"{{#.selected?}} checked{{/.selected}} class="theme-picker--option {{.theme.css_class()}} themed">
|
||||||
|
{{/themes}}
|
||||||
|
</div>
|
||||||
|
|
||||||
<article>
|
<article>
|
||||||
<p>
|
<p>
|
||||||
|
@ -20,15 +28,26 @@
|
||||||
</p>
|
</p>
|
||||||
</article>
|
</article>
|
||||||
|
|
||||||
<div class="editor-controls">
|
|
||||||
{{#cancel_url}}
|
|
||||||
<a class="cancel" href="{{.}}">Cancel</a>
|
|
||||||
{{/cancel_url}}
|
|
||||||
<button type=submit>Save</button>
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
</form>
|
<div class="editor-controls">
|
||||||
|
{{#edit?}}
|
||||||
|
<div class="cancel-interaction-group {{#cancel_url}}interaction-group--root--enabled{{/cancel_url}}{{^cancel_url}}interaction-group--root--disabled{{/cancel_url}}">
|
||||||
|
<a class="interaction-group--enabled button button-cancel cancel" href="{{#cancel_url}}{{.}}{{/cancel_url}}">Cancel</a>
|
||||||
|
<button class="interaction-group--disabled button button-cancel" disabled>Cancel</a>
|
||||||
|
</div>
|
||||||
|
<button class="button button-default" type=submit {{^edit?}}disabled{{/edit}}>Save</button>
|
||||||
|
{{/edit}}
|
||||||
|
{{^edit?}}
|
||||||
|
<div class="cancel-interaction-group interaction-group--root--disabled">
|
||||||
|
<a class="interaction-group--enabled button button-cancel cancel" href="{{#cancel_url}}{{.}}{{/cancel_url}}">Cancel</a>
|
||||||
|
<button class="interaction-group--disabled button button-cancel" disabled>Cancel</a>
|
||||||
|
</div>
|
||||||
|
<button class="button button-default" type=submit disabled>Save</button>
|
||||||
|
{{/edit}}
|
||||||
</div>
|
</div>
|
||||||
|
</form>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<footer>
|
<footer>
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
|
<div class="hero">
|
||||||
<header>
|
<header>
|
||||||
<h1>{{title}}</h1>
|
<h1>{{title}}</h1>
|
||||||
</header>
|
</header>
|
||||||
|
</div>
|
||||||
|
|
||||||
<article>
|
<article>
|
||||||
{{{rendered}}}
|
{{{rendered}}}
|
||||||
|
|
|
@ -1,5 +1,3 @@
|
||||||
<div class="container">
|
|
||||||
|
|
||||||
<div class="notice">
|
<div class="notice">
|
||||||
<p>
|
<p>
|
||||||
You are viewing an historical version of <a href="{{link_current}}">this article</a>,
|
You are viewing an historical version of <a href="{{link_current}}">this article</a>,
|
||||||
|
@ -11,11 +9,4 @@
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="rendered">
|
{{{rendered}}}
|
||||||
{{>article_contents.html}}
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<footer>
|
|
||||||
{{>footer/items.html}}
|
|
||||||
</footer>
|
|
||||||
|
|
|
@ -1,9 +1,3 @@
|
||||||
<div class="container">
|
|
||||||
<header>
|
|
||||||
<h1>Changes</h1>
|
|
||||||
</header>
|
|
||||||
|
|
||||||
<article>
|
|
||||||
<p>
|
<p>
|
||||||
These are the {{^newer}}most recent{{/newer}} changes
|
These are the {{^newer}}most recent{{/newer}} changes
|
||||||
made to{{{subject_clause()}}}{{#author()}} by {{.}}{{/author()}}.
|
made to{{{subject_clause()}}}{{#author()}} by {{.}}{{/author()}}.
|
||||||
|
@ -44,7 +38,3 @@
|
||||||
><li><a rel="last" href="{{.end}}">First changes</a></li
|
><li><a rel="last" href="{{.end}}">First changes</a></li
|
||||||
></ul></nav>{{/older}}
|
></ul></nav>{{/older}}
|
||||||
{{#changes?}}{{^older}}<p>There are no older changes.</p>{{/older}}{{/changes}}
|
{{#changes?}}{{^older}}<p>There are no older changes.</p>{{/older}}{{/changes}}
|
||||||
</article>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
{{>footer/default.html}}
|
|
||||||
|
|
|
@ -1,9 +1,16 @@
|
||||||
<div class="container">
|
<div class="container">
|
||||||
|
|
||||||
|
<div class="hero">
|
||||||
|
<header>
|
||||||
|
<h1>{{#title}}{{#.removed}}<span class="removed">{{.}}</span>{{/.removed}}{{#.same}}{{.}}{{/.same}}{{#.added}}<span class="added">{{.}}</span>{{/.added}}{{/title}}</h1>
|
||||||
|
</header>
|
||||||
|
</div>
|
||||||
|
|
||||||
<div class="notice">
|
<div class="notice">
|
||||||
<p>
|
<p>
|
||||||
You are viewing the difference between two {{#consecutive?}}consecutive{{/consecutive}}
|
You are viewing the difference between two {{#consecutive?}}consecutive{{/consecutive}}
|
||||||
revisions of <a href="_by_id/{{article_id}}">this article</a>.
|
revisions of <a href="_by_id/{{article_id}}">this article</a>.
|
||||||
|
{{#author}}This changeset was authored by <a href="{{..author_link}}">{{.}}</a>.{{/author}}
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
|
@ -12,10 +19,6 @@
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<header>
|
|
||||||
<h1>{{#title}}{{#.removed}}<span class="removed">{{.}}</span>{{/.removed}}{{#.same}}{{.}}{{/.same}}{{#.added}}<span class="added">{{.}}</span>{{/.added}}{{/title}}</h1>
|
|
||||||
</header>
|
|
||||||
|
|
||||||
<article>
|
<article>
|
||||||
<pre class="diff">{{#lines}}{{#.removed}}<span class="removed">{{.}}
|
<pre class="diff">{{#lines}}{{#.removed}}<span class="removed">{{.}}
|
||||||
</span>{{/.removed}}{{#.same}}<span class="same">{{.}}
|
</span>{{/.removed}}{{#.same}}<span class="same">{{.}}
|
||||||
|
|
|
@ -1,11 +1 @@
|
||||||
<div class="container">
|
<p>This page was not found.</p>
|
||||||
<header>
|
|
||||||
<h1>Not found</h1>
|
|
||||||
</header>
|
|
||||||
|
|
||||||
<article>
|
|
||||||
<p>This page was not found.</p>
|
|
||||||
</article>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
{{>../footer/default.html}}
|
|
||||||
|
|
|
@ -1,11 +1 @@
|
||||||
<div class="container">
|
<p>An error has occurred.</p>
|
||||||
<header>
|
|
||||||
<h1>Internal server error</h1>
|
|
||||||
</header>
|
|
||||||
|
|
||||||
<article>
|
|
||||||
<p>An error has occurred.</p>
|
|
||||||
</article>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
{{>../footer/default.html}}
|
|
||||||
|
|
|
@ -3,12 +3,13 @@
|
||||||
<head>
|
<head>
|
||||||
<title>{{title}}</title>
|
<title>{{title}}</title>
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||||
|
<meta charset="utf-8">
|
||||||
{{#base}}<base href="{{.}}">{{/base}}
|
{{#base}}<base href="{{.}}">{{/base}}
|
||||||
<link rel=preload href="_assets/amatic-sc-v9-latin-regular.woff" as=font type="font/woff" crossorigin=anonymous>
|
<link href="_assets/{{themes_css()}}" rel="stylesheet">
|
||||||
<link href="_assets/style-{{style_css_checksum()}}.css" rel="stylesheet">
|
<link href="_assets/{{style_css()}}" rel="stylesheet">
|
||||||
<meta name="generator" content="{{project_name()}} {{version()}}" />
|
<meta name="generator" content="{{project_name()}} {{version()}}" />
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body class="{{theme.css_class()}}">
|
||||||
{{>search_input.html}}
|
{{>search_input.html}}
|
||||||
{{{body}}}
|
{{{body}}}
|
||||||
</body>
|
</body>
|
||||||
|
|
|
@ -1,9 +1,3 @@
|
||||||
<div class="container">
|
|
||||||
<header>
|
|
||||||
<h1>Search</h1>
|
|
||||||
</header>
|
|
||||||
|
|
||||||
<article>
|
|
||||||
{{#hits?}}
|
{{#hits?}}
|
||||||
<p>Search results for the query <b>{{query}}</b>:</p>
|
<p>Search results for the query <b>{{query}}</b>:</p>
|
||||||
|
|
||||||
|
@ -25,8 +19,3 @@
|
||||||
{{^hits?}}
|
{{^hits?}}
|
||||||
<p>Your search for <b>{{query}}</b> gave no results.</p>
|
<p>Your search for <b>{{query}}</b> gave no results.</p>
|
||||||
{{/hits}}
|
{{/hits}}
|
||||||
|
|
||||||
</article>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
{{>footer/default.html}}
|
|
||||||
|
|
|
@ -1,7 +1,11 @@
|
||||||
|
<div class="search-container">
|
||||||
<form class="search keyboard-focus-control" action=_search method=GET>
|
<form class="search keyboard-focus-control" action=_search method=GET>
|
||||||
<input data-focusindex="0" type=search name=q placeholder=search autocomplete=off>
|
<div class="search-widget-container">
|
||||||
|
<input data-focusindex="0" type=search name=q placeholder=Search autocomplete=off>
|
||||||
<ul class="live-results search-results">
|
<ul class="live-results search-results">
|
||||||
</ul>
|
</ul>
|
||||||
|
</div>
|
||||||
</form>
|
</form>
|
||||||
<ul id="search-result-prototype" class="prototype"><li class="search-result"><a tabindex="0" class="link" href=""><span class="title"></span> – <span class="snippet"></span></a></li></ul>
|
<ul id="search-result-prototype" class="prototype"><li class="search-result"><a tabindex="0" class="link" href=""><span class="title"></span> – <span class="snippet"></span></a></li></ul>
|
||||||
<script src="_assets/search-{{search_js_checksum()}}.js" defer></script>
|
<script src="_assets/{{search_js()}}" defer></script>
|
||||||
|
</div>
|
||||||
|
|
|
@ -1,15 +1,5 @@
|
||||||
<div class="container">
|
|
||||||
<header>
|
|
||||||
<h1>Sitemap</h1>
|
|
||||||
</header>
|
|
||||||
|
|
||||||
<article>
|
|
||||||
<ul class="dense"
|
<ul class="dense"
|
||||||
{{#articles}}
|
{{#articles}}
|
||||||
><li><a href="{{.link()}}">{{.title}}</a></li
|
><li><a href="{{.link()}}">{{.title}}</a></li
|
||||||
{{/articles}}
|
{{/articles}}
|
||||||
></ul>
|
></ul>
|
||||||
</article>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
{{>footer/default.html}}
|
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
<div class="container">
|
<div class="container">
|
||||||
|
<div class="hero">
|
||||||
<header>
|
<header>
|
||||||
<h1>{{title}}</h1>
|
<h1>{{title}}</h1>
|
||||||
</header>
|
</header>
|
||||||
|
</div>
|
||||||
|
|
||||||
<article>
|
<article>
|
||||||
{{{html_body}}}
|
{{{html_body}}}
|
74
themes/generate.py
Executable file
74
themes/generate.py
Executable file
|
@ -0,0 +1,74 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
import json, colorsys
|
||||||
|
|
||||||
|
colors = json.load(open("material-colors.json", "r"))
|
||||||
|
palettes = colors['palettes']
|
||||||
|
|
||||||
|
def hex_to_rgb(h):
|
||||||
|
return tuple(int(h[i:i+2], 16) / 255 for i in (0, 2 ,4))
|
||||||
|
|
||||||
|
def to_linear(x):
|
||||||
|
if x < 0.04045:
|
||||||
|
return x / 12.92
|
||||||
|
else:
|
||||||
|
return pow((x + 0.055) / 1.055, 2.4)
|
||||||
|
|
||||||
|
def rgb_to_linear(rgb):
|
||||||
|
return [to_linear(x) for x in rgb]
|
||||||
|
|
||||||
|
def luma(rgb):
|
||||||
|
r, g, b = rgb_to_linear(rgb)
|
||||||
|
return (0.2126*r + 0.7152*g + 0.0722*b)
|
||||||
|
|
||||||
|
def prep(x):
|
||||||
|
cols = x['colors']
|
||||||
|
rgb = [hex_to_rgb(c[1:]) for c in cols]
|
||||||
|
brightness = [luma(c) for c in rgb]
|
||||||
|
hue = [colorsys.rgb_to_hsv(*c)[0] * 360 for c in rgb]
|
||||||
|
sat = [colorsys.rgb_to_hsv(*c)[1] for c in rgb]
|
||||||
|
|
||||||
|
main_index = 5
|
||||||
|
if brightness[main_index] >= 0.4:
|
||||||
|
main_index = 6
|
||||||
|
|
||||||
|
dark_main = brightness[main_index] < 0.5
|
||||||
|
|
||||||
|
input_index = main_index + (-2 if dark_main else 1)
|
||||||
|
|
||||||
|
h = hue[main_index]
|
||||||
|
s = sat[main_index]
|
||||||
|
|
||||||
|
alt = blues
|
||||||
|
if s > 0.3 and (h < 40 or h >= 300):
|
||||||
|
alt = yellows
|
||||||
|
|
||||||
|
return {
|
||||||
|
"name": x['shade'].lower().replace(' ', '-'),
|
||||||
|
"main": cols[main_index],
|
||||||
|
"input": cols[input_index],
|
||||||
|
"text": "white",
|
||||||
|
"link": alt[2 if dark_main else 7],
|
||||||
|
}
|
||||||
|
|
||||||
|
blues = [x for x in palettes if x['shade'] == "Blue"][0]["colors"]
|
||||||
|
yellows = [x for x in palettes if x['shade'] == "Yellow"][0]["colors"]
|
||||||
|
|
||||||
|
themes = [prep(palette) for palette in palettes]
|
||||||
|
|
||||||
|
print(
|
||||||
|
"\n".join(
|
||||||
|
"\
|
||||||
|
.theme-{name} {{\n\
|
||||||
|
--theme-main: {main};\n\
|
||||||
|
--theme-text: {text};\n\
|
||||||
|
--theme-input: {input};\n\
|
||||||
|
--theme-link: {link};\n\
|
||||||
|
}}\n".format(**x)
|
||||||
|
for x in themes
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
print()
|
||||||
|
|
||||||
|
# print("[" + ', '.join('"'+x['name']+'"' for x in themes) + "]")
|
369
themes/material-colors.json
Normal file
369
themes/material-colors.json
Normal file
|
@ -0,0 +1,369 @@
|
||||||
|
{
|
||||||
|
"names": [
|
||||||
|
"50",
|
||||||
|
"100",
|
||||||
|
"200",
|
||||||
|
"300",
|
||||||
|
"400",
|
||||||
|
"500",
|
||||||
|
"600",
|
||||||
|
"700",
|
||||||
|
"800",
|
||||||
|
"900",
|
||||||
|
"A100",
|
||||||
|
"A200",
|
||||||
|
"A400",
|
||||||
|
"A700"
|
||||||
|
],
|
||||||
|
"palettes": [
|
||||||
|
{
|
||||||
|
"shade": "Red",
|
||||||
|
"colors": [
|
||||||
|
"#FFEBEE",
|
||||||
|
"#FFCDD2",
|
||||||
|
"#EF9A9A",
|
||||||
|
"#E57373",
|
||||||
|
"#EF5350",
|
||||||
|
"#F44336",
|
||||||
|
"#E53935",
|
||||||
|
"#D32F2F",
|
||||||
|
"#C62828",
|
||||||
|
"#B71C1C",
|
||||||
|
"#FF8A80",
|
||||||
|
"#FF5252",
|
||||||
|
"#FF1744",
|
||||||
|
"#D50000"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"shade": "Pink",
|
||||||
|
"colors": [
|
||||||
|
"#FCE4EC",
|
||||||
|
"#F8BBD0",
|
||||||
|
"#F48FB1",
|
||||||
|
"#F06292",
|
||||||
|
"#EC407A",
|
||||||
|
"#E91E63",
|
||||||
|
"#D81B60",
|
||||||
|
"#C2185B",
|
||||||
|
"#AD1457",
|
||||||
|
"#880E4F",
|
||||||
|
"#FF80AB",
|
||||||
|
"#FF4081",
|
||||||
|
"#F50057",
|
||||||
|
"#C51162"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"shade": "Purple",
|
||||||
|
"colors": [
|
||||||
|
"#F3E5F5",
|
||||||
|
"#E1BEE7",
|
||||||
|
"#CE93D8",
|
||||||
|
"#BA68C8",
|
||||||
|
"#AB47BC",
|
||||||
|
"#9C27B0",
|
||||||
|
"#8E24AA",
|
||||||
|
"#7B1FA2",
|
||||||
|
"#6A1B9A",
|
||||||
|
"#4A148C",
|
||||||
|
"#EA80FC",
|
||||||
|
"#E040FB",
|
||||||
|
"#D500F9",
|
||||||
|
"#AA00FF"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"shade": "Deep purple",
|
||||||
|
"colors": [
|
||||||
|
"#EDE7F6",
|
||||||
|
"#D1C4E9",
|
||||||
|
"#B39DDB",
|
||||||
|
"#9575CD",
|
||||||
|
"#7E57C2",
|
||||||
|
"#673AB7",
|
||||||
|
"#5E35B1",
|
||||||
|
"#512DA8",
|
||||||
|
"#4527A0",
|
||||||
|
"#311B92",
|
||||||
|
"#B388FF",
|
||||||
|
"#7C4DFF",
|
||||||
|
"#651FFF",
|
||||||
|
"#6200EA"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"shade": "Indigo",
|
||||||
|
"colors": [
|
||||||
|
"#E8EAF6",
|
||||||
|
"#C5CAE9",
|
||||||
|
"#9FA8DA",
|
||||||
|
"#7986CB",
|
||||||
|
"#5C6BC0",
|
||||||
|
"#3F51B5",
|
||||||
|
"#3949AB",
|
||||||
|
"#303F9F",
|
||||||
|
"#283593",
|
||||||
|
"#1A237E",
|
||||||
|
"#8C9EFF",
|
||||||
|
"#536DFE",
|
||||||
|
"#3D5AFE",
|
||||||
|
"#304FFE"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"shade": "Blue",
|
||||||
|
"colors": [
|
||||||
|
"#E3F2FD",
|
||||||
|
"#BBDEFB",
|
||||||
|
"#90CAF9",
|
||||||
|
"#64B5F6",
|
||||||
|
"#42A5F5",
|
||||||
|
"#2196F3",
|
||||||
|
"#1E88E5",
|
||||||
|
"#1976D2",
|
||||||
|
"#1565C0",
|
||||||
|
"#0D47A1",
|
||||||
|
"#82B1FF",
|
||||||
|
"#448AFF",
|
||||||
|
"#2979FF",
|
||||||
|
"#2962FF"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"shade": "Light Blue",
|
||||||
|
"colors": [
|
||||||
|
"#E1F5FE",
|
||||||
|
"#B3E5FC",
|
||||||
|
"#81D4FA",
|
||||||
|
"#4FC3F7",
|
||||||
|
"#29B6F6",
|
||||||
|
"#03A9F4",
|
||||||
|
"#039BE5",
|
||||||
|
"#0288D1",
|
||||||
|
"#0277BD",
|
||||||
|
"#01579B",
|
||||||
|
"#80D8FF",
|
||||||
|
"#40C4FF",
|
||||||
|
"#00B0FF",
|
||||||
|
"#0091EA"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"shade": "Cyan",
|
||||||
|
"colors": [
|
||||||
|
"#E0F7FA",
|
||||||
|
"#B2EBF2",
|
||||||
|
"#80DEEA",
|
||||||
|
"#4DD0E1",
|
||||||
|
"#26C6DA",
|
||||||
|
"#00BCD4",
|
||||||
|
"#00ACC1",
|
||||||
|
"#0097A7",
|
||||||
|
"#00838F",
|
||||||
|
"#006064",
|
||||||
|
"#84FFFF",
|
||||||
|
"#18FFFF",
|
||||||
|
"#00E5FF",
|
||||||
|
"#00B8D4"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"shade": "Teal",
|
||||||
|
"colors": [
|
||||||
|
"#E0F2F1",
|
||||||
|
"#B2DFDB",
|
||||||
|
"#80CBC4",
|
||||||
|
"#4DB6AC",
|
||||||
|
"#26A69A",
|
||||||
|
"#009688",
|
||||||
|
"#00897B",
|
||||||
|
"#00796B",
|
||||||
|
"#00695C",
|
||||||
|
"#004D40",
|
||||||
|
"#A7FFEB",
|
||||||
|
"#64FFDA",
|
||||||
|
"#1DE9B6",
|
||||||
|
"#00BFA5"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"shade": "Green",
|
||||||
|
"colors": [
|
||||||
|
"#E8F5E9",
|
||||||
|
"#C8E6C9",
|
||||||
|
"#A5D6A7",
|
||||||
|
"#81C784",
|
||||||
|
"#66BB6A",
|
||||||
|
"#4CAF50",
|
||||||
|
"#43A047",
|
||||||
|
"#388E3C",
|
||||||
|
"#2E7D32",
|
||||||
|
"#1B5E20",
|
||||||
|
"#B9F6CA",
|
||||||
|
"#69F0AE",
|
||||||
|
"#00E676",
|
||||||
|
"#00C853"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"shade": "Light Green",
|
||||||
|
"colors": [
|
||||||
|
"#F1F8E9",
|
||||||
|
"#DCEDC8",
|
||||||
|
"#C5E1A5",
|
||||||
|
"#AED581",
|
||||||
|
"#9CCC65",
|
||||||
|
"#8BC34A",
|
||||||
|
"#7CB342",
|
||||||
|
"#689F38",
|
||||||
|
"#558B2F",
|
||||||
|
"#33691E",
|
||||||
|
"#CCFF90",
|
||||||
|
"#B2FF59",
|
||||||
|
"#76FF03",
|
||||||
|
"#64DD17"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"shade": "Lime",
|
||||||
|
"colors": [
|
||||||
|
"#F9FBE7",
|
||||||
|
"#F0F4C3",
|
||||||
|
"#E6EE9C",
|
||||||
|
"#DCE775",
|
||||||
|
"#D4E157",
|
||||||
|
"#CDDC39",
|
||||||
|
"#C0CA33",
|
||||||
|
"#AFB42B",
|
||||||
|
"#9E9D24",
|
||||||
|
"#827717",
|
||||||
|
"#F4FF81",
|
||||||
|
"#EEFF41",
|
||||||
|
"#C6FF00",
|
||||||
|
"#AEEA00"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"shade": "Yellow",
|
||||||
|
"colors": [
|
||||||
|
"#FFFDE7",
|
||||||
|
"#FFF9C4",
|
||||||
|
"#FFF59D",
|
||||||
|
"#FFF176",
|
||||||
|
"#FFEE58",
|
||||||
|
"#FFEB3B",
|
||||||
|
"#FDD835",
|
||||||
|
"#FBC02D",
|
||||||
|
"#F9A825",
|
||||||
|
"#F57F17",
|
||||||
|
"#FFFF8D",
|
||||||
|
"#FFFF00",
|
||||||
|
"#FFEA00",
|
||||||
|
"#FFD600"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"shade": "Amber",
|
||||||
|
"colors": [
|
||||||
|
"#FFF8E1",
|
||||||
|
"#FFECB3",
|
||||||
|
"#FFE082",
|
||||||
|
"#FFD54F",
|
||||||
|
"#FFCA28",
|
||||||
|
"#FFC107",
|
||||||
|
"#FFB300",
|
||||||
|
"#FFA000",
|
||||||
|
"#FF8F00",
|
||||||
|
"#FF6F00",
|
||||||
|
"#FFE57F",
|
||||||
|
"#FFD740",
|
||||||
|
"#FFC400",
|
||||||
|
"#FFAB00"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"shade": "Orange",
|
||||||
|
"colors": [
|
||||||
|
"#FFF3E0",
|
||||||
|
"#FFE0B2",
|
||||||
|
"#FFCC80",
|
||||||
|
"#FFB74D",
|
||||||
|
"#FFA726",
|
||||||
|
"#FF9800",
|
||||||
|
"#FB8C00",
|
||||||
|
"#F57C00",
|
||||||
|
"#EF6C00",
|
||||||
|
"#E65100",
|
||||||
|
"#FFD180",
|
||||||
|
"#FFAB40",
|
||||||
|
"#FF9100",
|
||||||
|
"#FF6D00"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"shade": "Deep Orange",
|
||||||
|
"colors": [
|
||||||
|
"#FBE9E7",
|
||||||
|
"#FFCCBC",
|
||||||
|
"#FFAB91",
|
||||||
|
"#FF8A65",
|
||||||
|
"#FF7043",
|
||||||
|
"#FF5722",
|
||||||
|
"#F4511E",
|
||||||
|
"#E64A19",
|
||||||
|
"#D84315",
|
||||||
|
"#BF360C",
|
||||||
|
"#FF9E80",
|
||||||
|
"#FF6E40",
|
||||||
|
"#FF3D00",
|
||||||
|
"#DD2C00"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"shade": "Brown",
|
||||||
|
"colors": [
|
||||||
|
"#EFEBE9",
|
||||||
|
"#D7CCC8",
|
||||||
|
"#BCAAA4",
|
||||||
|
"#A1887F",
|
||||||
|
"#8D6E63",
|
||||||
|
"#795548",
|
||||||
|
"#6D4C41",
|
||||||
|
"#5D4037",
|
||||||
|
"#4E342E",
|
||||||
|
"#3E2723"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"shade": "Gray",
|
||||||
|
"colors": [
|
||||||
|
"#FAFAFA",
|
||||||
|
"#F5F5F5",
|
||||||
|
"#EEEEEE",
|
||||||
|
"#E0E0E0",
|
||||||
|
"#BDBDBD",
|
||||||
|
"#9E9E9E",
|
||||||
|
"#757575",
|
||||||
|
"#616161",
|
||||||
|
"#424242",
|
||||||
|
"#212121"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"shade": "Blue Gray",
|
||||||
|
"colors": [
|
||||||
|
"#ECEFF1",
|
||||||
|
"#CFD8DC",
|
||||||
|
"#B0BEC5",
|
||||||
|
"#90A4AE",
|
||||||
|
"#78909C",
|
||||||
|
"#607D8B",
|
||||||
|
"#546E7A",
|
||||||
|
"#455A64",
|
||||||
|
"#37474F",
|
||||||
|
"#263238"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
Loading…
Reference in a new issue