Override format option for new pastes (#16)

Added format override for uploaded pastes. Additionally removed line numbers and colour coding from plaintext option.
pull/17/head
WolfwithSword 3 years ago committed by GitHub
parent 7b705b5a47
commit e0881dac90
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

2
.gitignore vendored

@ -0,0 +1,2 @@
/[Dd]ata/
/pastey/__pycache__/

@ -1,5 +1,5 @@
from . import config from . import config
from __main__ import guess
import ipaddress import ipaddress
from os import path from os import path
from pathlib import Path from pathlib import Path
@ -62,6 +62,10 @@ def get_themes():
themes.append(str(path).split('/')[-1].split('.')[0]) themes.append(str(path).split('/')[-1].split('.')[0])
return sorted(themes, key=str.casefold) return sorted(themes, key=str.casefold)
# Get a list of all supported languages from guesslang
def get_languages():
return guess.supported_languages
# Get file path from unique id # Get file path from unique id
# This is a wrapper to check for files with the .expires extension # This is a wrapper to check for files with the .expires extension
def determine_file(unique_id): def determine_file(unique_id):

@ -79,7 +79,7 @@ def delete_paste(unique_id):
remove(paste) remove(paste)
# Create new paste # Create new paste
def new_paste(title, content, source_ip, expires=0, single=False, encrypt=False): def new_paste(title, content, source_ip, expires=0, single=False, encrypt=False, language="AUTO"):
unique_id = str(uuid.uuid4()) unique_id = str(uuid.uuid4())
output_file = config.data_directory + "/" + unique_id output_file = config.data_directory + "/" + unique_id
@ -89,8 +89,9 @@ def new_paste(title, content, source_ip, expires=0, single=False, encrypt=False)
output_file = config.data_directory + "/" + unique_id output_file = config.data_directory + "/" + unique_id
# Attempt to guess programming language # Attempt to guess programming language
guesses = guess.probabilities(content) if language == "AUTO":
language = guesses[0][0] if guesses[0][1] > config.guess_threshold and guesses[0][0] not in config.ignore_guess else "Plaintext" guesses = guess.probabilities(content)
language = guesses[0][0] if guesses[0][1] > config.guess_threshold and guesses[0][0] not in config.ignore_guess else "Plaintext"
# Check if encryption is necessary # Check if encryption is necessary
key = "" key = ""

@ -10,6 +10,9 @@ import json
# Load themes # Load themes
loaded_themes = common.get_themes() loaded_themes = common.get_themes()
# Load Languages
supported_languages = common.get_languages()
# Set rate limit # Set rate limit
# Workaround for @limiter annotations being parsed early # Workaround for @limiter annotations being parsed early
config.rate_limit = environ["PASTEY_RATE_LIMIT"] if "PASTEY_RATE_LIMIT" in environ else config.rate_limit config.rate_limit = environ["PASTEY_RATE_LIMIT"] if "PASTEY_RATE_LIMIT" in environ else config.rate_limit
@ -40,6 +43,7 @@ def new():
whitelisted = common.verify_whitelist(common.get_source_ip(request)) whitelisted = common.verify_whitelist(common.get_source_ip(request))
return render_template("new.html", return render_template("new.html",
whitelisted=whitelisted, whitelisted=whitelisted,
languages=supported_languages,
active_theme=common.set_theme(request), active_theme=common.set_theme(request),
themes=loaded_themes) themes=loaded_themes)
@ -118,9 +122,10 @@ def paste():
single = True if 'single' in request.form else False single = True if 'single' in request.form else False
encrypt = True if 'encrypt' in request.form else False encrypt = True if 'encrypt' in request.form else False
expiration = int(request.form['expiration']) if 'expiration' in request.form else -1 expiration = int(request.form['expiration']) if 'expiration' in request.form else -1
language = request.form['language'] if 'language' in request.form else "AUTO"
# Create paste # Create paste
unique_id, key = functions.new_paste(title, content, source_ip, expires=expiration, single=single, encrypt=encrypt) unique_id, key = functions.new_paste(title, content, source_ip, expires=expiration, single=single, encrypt=encrypt, language=language)
if encrypt: if encrypt:
# Return link if cli form option was set # Return link if cli form option was set
@ -176,9 +181,10 @@ def paste_json():
single = paste['single'] if ('single' in paste and type(paste['single']) == bool) else False single = paste['single'] if ('single' in paste and type(paste['single']) == bool) else False
encrypt = paste['encrypt'] if ('encrypt' in paste and type(paste['encrypt']) == bool) else False encrypt = paste['encrypt'] if ('encrypt' in paste and type(paste['encrypt']) == bool) else False
expiration = paste['expiration'] if ('expiration' in paste and type(paste['expiration']) == int) else -1 expiration = paste['expiration'] if ('expiration' in paste and type(paste['expiration']) == int) else -1
language = paste['language'] if ('language' in paste and type(paste['language']) == str) else "AUTO"
# Create paste # Create paste
unique_id, key = functions.new_paste(title, content, source_ip, expires=expiration, single=single, encrypt=encrypt) unique_id, key = functions.new_paste(title, content, source_ip, expires=expiration, single=single, encrypt=encrypt, language=language)
if encrypt: if encrypt:
return { return {
"link": common.build_url(request, "/view/" + unique_id + "#" + quote(key)) "link": common.build_url(request, "/view/" + unique_id + "#" + quote(key))

@ -136,7 +136,7 @@
</label> </label>
</div> </div>
</div> </div>
<div class="col-md-3"> <div class="col-md-2">
<select class="form-select form-select-md mb-3 pastey-select" id="expiration" name="expiration"> <select class="form-select form-select-md mb-3 pastey-select" id="expiration" name="expiration">
<option value="-1" selected>Expires never</option> <option value="-1" selected>Expires never</option>
<option value="1">Expires in 1 hour</option> <option value="1">Expires in 1 hour</option>
@ -148,8 +148,17 @@
<option value="8760">Expires in 1 year</option> <option value="8760">Expires in 1 year</option>
</select> </select>
</div> </div>
<div class="col-md-2">
<select class="form-select form-select-md mb-3 pastey-select" id="language" name="language">
<option value="AUTO" selected>AUTO</option>
<option value="Plaintext">Plaintext</option>
{% for language in languages %}
<option value="{{ language }}">{{ language }}</option>
{% endfor %}
</select>
</div>
<div class="col-md-1"></div> <div class="col-md-1"></div>
<div class="col-md-4"> <div class="col-md-3">
<button type="submit" class="btn btn-primary btn-block"><i class="fas fa-check"></i>&nbsp;&nbsp;&nbsp;Paste</button> <button type="submit" class="btn btn-primary btn-block"><i class="fas fa-check"></i>&nbsp;&nbsp;&nbsp;Paste</button>
</div> </div>
</div> </div>

@ -141,7 +141,11 @@
<div class="container"> <div class="container">
<div class="row"> <div class="row">
<div class="col-md-12 mb-4"> <div class="col-md-12 mb-4">
{% if paste.language == "Plaintext" %}
<pre class="prettyprint nocode" id="pastey-content">{{ paste.content }}</pre>
{% else %}
<pre class="prettyprint linenums" id="pastey-content">{{ paste.content }}</pre> <pre class="prettyprint linenums" id="pastey-content">{{ paste.content }}</pre>
{% endif %}
</div> </div>
</div> </div>

Loading…
Cancel
Save