Rename save page to preview page

This commit is contained in:
Edward Betts 2022-08-21 11:31:21 +01:00
parent 16e776c058
commit 7813d1a7f0
4 changed files with 90 additions and 32 deletions

View file

@ -186,6 +186,9 @@ class Article:
a.set("href", "https://en.wikipedia.org" + href) a.set("href", "https://en.wikipedia.org" + href)
a.set("target", "_blank") a.set("target", "_blank")
def dab_link_to(self):
return [dab["link_to"] for dab in self.dab_list]
def process_links(self) -> None: def process_links(self) -> None:
"""Process links in parsed wikitext.""" """Process links in parsed wikitext."""
for dab_num, (a, link_to, title) in enumerate(self.iter_links()): for dab_num, (a, link_to, title) in enumerate(self.iter_links()):

View file

@ -54,7 +54,7 @@ a.new { color: red; }
<h1>{{ article.enwiki }}</h1> <h1>{{ article.enwiki }}</h1>
<div id="save-panel" class="d-none"> <div id="save-panel" class="d-none">
<form method="POST" action="{{ article.save_endpoint() }}"> <form method="POST" action="{{ article.save_endpoint() }}">
<button class="btn btn-primary" id="save-btn">Save</button> <button class="btn btn-primary" id="save-btn">Preview before save</button>
<span id="edit-count"></span> <span id="edit-count"></span>
<input type="hidden" value="{}" id="save-edits" name="edits"> <input type="hidden" value="{}" id="save-edits" name="edits">
</form> </form>
@ -62,6 +62,7 @@ a.new { color: red; }
<div>There are {{ article.dab_list | count }} links in the article that need disambiguating.</div> <div>There are {{ article.dab_list | count }} links in the article that need disambiguating.</div>
{% for dab in article.dab_list %} {% for dab in article.dab_list %}
<div class="card p-1 m-2"> <div class="card p-1 m-2">
<div class="card-body">
<h3 class="card-title" id="dab-card-title-{{ dab.num }}" onclick="return jump_to({{ dab.num }})">{{ dab.title }}</h3> <h3 class="card-title" id="dab-card-title-{{ dab.num }}" onclick="return jump_to({{ dab.num }})">{{ dab.title }}</h3>
{% if dab.title != dab.link_to %}<div>redirect from {{ dab.link_to }}</div>{% endif %} {% if dab.title != dab.link_to %}<div>redirect from {{ dab.link_to }}</div>{% endif %}
<div> <div>
@ -73,6 +74,7 @@ a.new { color: red; }
</div> </div>
<div class="dab-article d-none" id="dab-article-{{ dab.num }}">{{ dab.html | safe }}</div> <div class="dab-article d-none" id="dab-article-{{ dab.num }}">{{ dab.html | safe }}</div>
</div> </div>
</div>
{% endfor %} {% endfor %}
</div> </div>
<div id="article" class="pe-3"> <div id="article" class="pe-3">
@ -85,8 +87,8 @@ a.new { color: red; }
var edit_set = new Set(); var edit_set = new Set();
var edits = {}; var edits = {};
var dab_lookup = {{ article.dab_lookup | tojson }};
var dab_order = {{ article.dab_order | tojson }}; var dab_order = {{ article.dab_order | tojson }};
var dab_link_to = {{ article.dab_link_to() | tojson }};
var dab_links = document.getElementsByClassName("disambig"); var dab_links = document.getElementsByClassName("disambig");
for(var i=0; i<dab_links.length; i++) { for(var i=0; i<dab_links.length; i++) {
@ -156,7 +158,8 @@ a.new { color: red; }
} }
function update_edits() { function update_edits() {
var saves = dab_order.filter(t => edits[t]).map(t => [t, edits[t]]); var saves = dab_link_to.map((link_to, num) => (
{"num": num, "link_to": link_to, "title": edits[num]}));
var save_edits = document.getElementById("save-edits"); var save_edits = document.getElementById("save-edits");
save_edits.value = JSON.stringify(saves); save_edits.value = JSON.stringify(saves);
} }
@ -166,7 +169,7 @@ a.new { color: red; }
document.getElementById("cancel-" + dab_num).classList.remove("d-none"); document.getElementById("cancel-" + dab_num).classList.remove("d-none");
var title = element.getAttribute("title"); var title = element.getAttribute("title");
edits[dab_lookup[dab_num]] = title; edits[dab_num] = title;
edit_set.add(dab_num); edit_set.add(dab_num);
update_edits(); update_edits();
@ -188,7 +191,7 @@ a.new { color: red; }
} }
function cancel_selection(dab_num) { function cancel_selection(dab_num) {
delete edits[dab_lookup[dab_num]]; delete edits[dab_num];
document.getElementById("cancel-" + dab_num).classList.add("d-none"); document.getElementById("cancel-" + dab_num).classList.add("d-none");
clear_dab_highlight(dab_num); clear_dab_highlight(dab_num);
edit_set.delete(dab_num); edit_set.delete(dab_num);

View file

@ -4,15 +4,33 @@
<meta charset="utf-8"> <meta charset="utf-8">
<title>{{ title }} &ndash; dab mechanic</title> <title>{{ title }} &ndash; dab mechanic</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-gH2yIJqKdNHPEq0n4Mqa/HGKIhSkIHeL5AyhkYV8i59U5AR6csBvApHHNl/vI1Bx" crossorigin="anonymous"> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-gH2yIJqKdNHPEq0n4Mqa/HGKIhSkIHeL5AyhkYV8i59U5AR6csBvApHHNl/vI1Bx" crossorigin="anonymous">
<link rel="stylesheet" href="https://www.mediawiki.org/w/load.php?modules=mediawiki.diff.styles&only=styles">
</head> </head>
<body> <body>
<div class="m-3"> <div class="m-3">
<h2>Save edits: {{ title }}</h2> <h2>Preview of changes: {{ title }}</h2>
<p>Edit summary: {{ edit_summary }}</p> <div class="card">
<div class="card-body">
<h5 class="card-title">Edit summary</h5>
<p class="card-text">{{ edit_summary }}</p>
</div> </div>
<div class="m-3">
<pre>{{ text }}</pre>
</div> </div>
{# <pre>{{ text }}</pre> #}
<table class="diff my-3">
<colgroup>
<col class="diff-marker">
<col class="diff-content">
<col class="diff-marker">
<col class="diff-content">
</colgroup>
<tbody>
{{ diff | safe }}
</tbody>
</table>
<button class="btn btn-primary" id="save-btn">Save changes</button>
</body> </body>
</html> </html>

View file

@ -3,7 +3,8 @@
import inspect import inspect
import json import json
import re import re
from typing import Optional from typing import Optional, TypedDict
import mwparserfromhell
import flask import flask
import lxml.html import lxml.html
@ -73,19 +74,22 @@ def index():
return flask.render_template("index.html", articles=articles) return flask.render_template("index.html", articles=articles)
def make_disamb_link(edit: tuple[str, str]) -> str: class Edit(TypedDict):
"""Given an edit return the appropriate link.""" """Edit to an article."""
return f"[[{edit[1]}|{edit[0]}]]"
num: int
link_to: str
title: str
def apply_edits(article_text: str, edits: list[tuple[str, str]]) -> str: def apply_edits(article_text: str, edits: list[Edit]) -> str:
"""Apply edits to article text.""" """Apply edits to article text."""
def escape(s: str) -> str: def escape(s: str) -> str:
return re.escape(s).replace("_", "[ _]").replace(r"\ ", "[ _]") return re.escape(s).replace("_", "[ _]").replace(r"\ ", "[ _]")
for link_from, link_to in edits: for edit in edits:
print(rf"\[\[{escape(link_from)}\]\]") # print(rf"\[\[{escape(link_from)}\]\]")
article_text = re.sub( article_text = re.sub(
rf"\[\[{escape(link_from)}\]\]", rf"\[\[{escape(link_from)}\]\]",
f"[[{link_to}|{link_from}]]", f"[[{link_to}|{link_from}]]",
@ -95,31 +99,61 @@ def apply_edits(article_text: str, edits: list[tuple[str, str]]) -> str:
return article_text return article_text
@app.route("/save/<path:enwiki>", methods=["POST"]) def make_disamb_link(edit: Edit) -> str:
def save(enwiki: str) -> Response | str: """Given an edit return the appropriate link."""
"""Save edits to article.""" return f"[[{edit['title']}|{edit['link_to']}]]"
edits = [
(link_to, link_from)
for link_to, link_from in json.loads(flask.request.form["edits"])
]
enwiki = enwiki.replace("_", " ")
def build_edit_summary(edits: list[Edit]) -> str:
"""Given a list of edits return an edit summary."""
titles = ", ".join(make_disamb_link(edit) for edit in edits[:-1]) titles = ", ".join(make_disamb_link(edit) for edit in edits[:-1])
if len(titles) > 1: if len(titles) > 1:
titles += " and " titles += " and "
titles += make_disamb_link(edits[-1]) titles += make_disamb_link(edits[-1])
edit_summary = f"Disambiguate {titles} using [[User:Edward/Dab mechanic]]" return f"Disambiguate {titles} using [[User:Edward/Dab mechanic]]"
article_text = apply_edits(mediawiki_api.get_content(enwiki), edits) def get_links(wikicode, edits):
dab_titles = {dab["link_to"] for dab in edits}
return [
link for link in wikicode.filter_wikilinks() if str(link.title) in dab_titles
]
@app.route("/preview/<path:enwiki>", methods=["POST"])
def preview(enwiki: str) -> Response | str:
"""Save edits to article."""
enwiki = enwiki.replace("_", " ")
dab_links = json.loads(flask.request.form["edits"])
edits = [edit for edit in dab_links if edit.get("title")]
edit_summary = build_edit_summary(edits)
# return flask.jsonify(edits=dab_links, edit_summary=edit_summary)
text = mediawiki_api.get_content(enwiki)
wikicode = mwparserfromhell.parse(text)
links = get_links(wikicode, dab_links)
assert len(links) == len(dab_links)
for wikilink, edit in zip(links, dab_links):
print(edit, wikilink)
if not edit.get("title"):
continue
if not wikilink.text:
wikilink.text = wikilink.title
wikilink.title = edit["title"]
diff = mediawiki_api.compare(enwiki, str(wikicode))
return flask.render_template( return flask.render_template(
"save.html", "peview.html",
edit_summary=edit_summary, edit_summary=edit_summary,
title=enwiki, title=enwiki,
edits=edits, edits=dab_links,
text=article_text, # text=str(wikicode),
diff=diff,
) )