Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 34 additions & 3 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,44 @@
import re
from enum import Enum
import sys
import requests as http_requests
from datetime import datetime

import mwparserfromhell
from mwparserfromhell.nodes import Tag

app = Flask(__name__)
CORS(app) # Enable CORS for all routes

CSP_POLICY = (
"default-src 'self'; "
"script-src 'self' 'unsafe-inline' https://tools-static.wmflabs.org; "
"style-src 'self' 'unsafe-inline' https://tools-static.wmflabs.org; "
"connect-src 'self' https://tools-static.wmflabs.org; "
"img-src 'self' data:; "
"font-src 'self' https://tools-static.wmflabs.org data:"
)

def get_last_updated_date():
try:
resp = http_requests.get(
"https://api.github.com/repos/indictechcom/translatable-wikitext-converter/commits",
timeout=5,
)
data = resp.json()
if data and isinstance(data, list) and len(data) > 0:
raw = data[0]["commit"]["committer"]["date"]
dt = datetime.strptime(raw, "%Y-%m-%dT%H:%M:%SZ")
return dt.strftime("%B %-d, %Y")
except Exception:
pass
return "Unavailable"

@app.after_request
def set_security_headers(response):
response.headers['Content-Security-Policy'] = CSP_POLICY
return response

behaviour_switches = ['__NOTOC__', '__FORCETOC__', '__TOC__', '__NOEDITSECTION__', '__NEWSECTIONLINK__', '__NONEWSECTIONLINK__', '__NOGALLERY__', '__HIDDENCAT__', '__EXPECTUNUSEDCATEGORY__', '__NOCONTENTCONVERT__', '__NOCC__', '__NOTITLECONVERT__', '__NOTC__', '__START__', '__END__', '__INDEX__', '__NOINDEX__', '__STATICREDIRECT__', '__EXPECTUNUSEDTEMPLATE__', '__NOGLOBAL__', '__DISAMBIG__', '__EXPECTED_UNCONNECTED_PAGE__', '__ARCHIVEDTALK__', '__NOTALK__', '__EXPECTWITHOUTSCANS__']

# --- Helper Functions for Processing Different Wikitext Elements ---
Expand Down Expand Up @@ -933,17 +964,17 @@ def convert_to_translatable_wikitext(wikitext):

@app.route('/')
def index():
return render_template('home.html')
return render_template('home.html', last_updated=get_last_updated_date())

@app.route('/convert', methods=['GET'])
def redirect_to_home():
return render_template('home.html')
return render_template('home.html', last_updated=get_last_updated_date())

@app.route('/convert', methods=['POST'])
def convert():
wikitext = request.form.get('wikitext', '')
converted_text = convert_to_translatable_wikitext(wikitext)
return render_template('home.html', original=wikitext, converted=converted_text)
return render_template('home.html', original=wikitext, converted=converted_text, last_updated=get_last_updated_date())

@app.route('/api/convert', methods=['GET', 'POST'])
def api_convert():
Expand Down
35 changes: 5 additions & 30 deletions templates/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
<title>Wikitext to Translatable Wikitext Converter</title>
<link
rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.1.0/css/bootstrap.min.css"
href="https://tools-static.wmflabs.org/cdnjs/ajax/libs/bootstrap/5.3.8/css/bootstrap.min.css"
/>

<link
rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/atom-one-light.min.css"
href="https://tools-static.wmflabs.org/cdnjs/ajax/libs/highlight.js/11.11.1/styles/atom-one-light.min.css"
/>
<style>
html,
Expand Down Expand Up @@ -133,15 +133,15 @@ <h5>Translatable Wikitext Output</h5>
</p>
<span class="mx-2">|</span>
<p class="mb-0">
Last updated on: <span id="lastUpdatedDate"></span>
Last updated on: {{ last_updated }}
</p>
</div>
</footer>
</div>

<!-- Bootstrap JS and dependencies -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.1.0/js/bootstrap.bundle.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js"></script>
<script src="https://tools-static.wmflabs.org/cdnjs/ajax/libs/bootstrap/5.3.8/js/bootstrap.bundle.min.js"></script>
<script src="https://tools-static.wmflabs.org/cdnjs/ajax/libs/highlight.js/11.11.1/highlight.min.js"></script>
<script>
hljs.highlightAll();

Expand Down Expand Up @@ -169,31 +169,6 @@ <h5>Translatable Wikitext Output</h5>
);
}

// Fetch the last commit date from the GitHub API
fetch(
"https://api.github.com/repos/indictechcom/translatable-wikitext-converter/commits",
)
.then((response) => response.json())
.then((data) => {
if (data && data.length > 0) {
const lastCommitDate = new Date(data[0].commit.committer.date);
const formattedDate = lastCommitDate.toLocaleDateString("en-US", {
year: "numeric",
month: "long",
day: "numeric",
});
document.getElementById("lastUpdatedDate").textContent =
formattedDate;
} else {
document.getElementById("lastUpdatedDate").textContent =
"Unavailable";
}
})
.catch((error) => {
console.error("Error fetching commit data:", error);
document.getElementById("lastUpdatedDate").textContent =
"Unavailable";
});
</script>
</body>
</html>