Browse Source

Testing ruff

master
David Larlet 1 year ago
parent
commit
3eba36dc6b
5 changed files with 51 additions and 36 deletions
  1. 30
    25
      david/blog/webmention/index.py
  2. 1
    2
      site.py
  3. 1
    1
      site2020.py
  4. 12
    3
      test_widont.py
  5. 7
    5
      widont.py

+ 30
- 25
david/blog/webmention/index.py View File

import os import os
import cgi import cgi
import cgitb import cgitb
cgitb.enable()
import subprocess import subprocess
import urllib.request import urllib.request


cgitb.enable()
form = cgi.FieldStorage() form = cgi.FieldStorage()
source = form.getvalue('source', None)
target = form.getvalue('target', None)
server = os.environ.get('SERVER_NAME', None)
method = os.environ.get('REQUEST_METHOD', 'GET')
source = form.getvalue("source", None)
target = form.getvalue("target", None)
server = os.environ.get("SERVER_NAME", None)
method = os.environ.get("REQUEST_METHOD", "GET")




def validate(source, target, server, method): def validate(source, target, server, method):
status = 400 status = 400
if method != 'POST':
message = 'Webmention MUST be performed through a POST request.'
if method != "POST":
message = "Webmention MUST be performed through a POST request."
elif source is None or target is None: elif source is None or target is None:
message = 'Please fill in the source and target fields.'
elif '://' not in source or '://' not in target:
message = 'Source and target fields should be URLs.'
elif not target.split('://', 1)[1].startswith(server):
message = 'Target should be a link to this domain: {0}'.format(server)
elif not target in urllib.request.urlopen(source).read().decode('utf-8'):
message = 'Source should have a link to the target: {0}'.format(target)
message = "Please fill in the source and target fields."
elif "://" not in source or "://" not in target:
message = "Source and target fields should be URLs."
elif not target.split("://", 1)[1].startswith(server):
message = "Target should be a link to this domain: {0}".format(server)
elif target not in urllib.request.urlopen(source).read().decode("utf-8"):
message = "Source should have a link to the target: {0}".format(target)
else: else:
message = ('Success! With source={source} and target={target}.'
'<a href="{target}">Back to the target article</a>.'
).format(
message = (
"Success! With source={source} and target={target}."
'<a href="{target}">Back to the target article</a>.'
).format(
source=source, source=source,
target=target, target=target,
) )




def send_response(callback, status, message, source, target): def send_response(callback, status, message, source, target):
print('Content-type: text/html')
print('Status: {0}'.format(status))
print("Content-type: text/html")
print("Status: {0}".format(status))
print() print()
print("""<!doctype html><html lang=en>
print(
"""<!doctype html><html lang=en>
<head><meta charset=utf-8><title>Webmention</title></head> <head><meta charset=utf-8><title>Webmention</title></head>
<body><p>{message}</p></body></html>""".format(message=message))
<body><p>{message}</p></body></html>""".format(
message=message
)
)
if status == 202: if status == 202:
callback(source, target) callback(source, target)




def on_valid_submission(source, target): def on_valid_submission(source, target):
commands = [ commands = [
'cd /home/larlet/www',
'source /home/larlet/.virtualenvs/webmention/bin/activate',
'fab --hide=everything add_webmention:{source},{target}'.format(
"cd /home/larlet/www",
"source /home/larlet/.virtualenvs/webmention/bin/activate",
"fab --hide=everything add_webmention:{source},{target}".format(
source=source, source=source,
target=target, target=target,
), ),
] ]
subprocess.call(' && '.join(commands), shell=True)
subprocess.call(" && ".join(commands), shell=True)




send_response(on_valid_submission, *validate(source, target, server, method)) send_response(on_valid_submission, *validate(source, target, server, method))

+ 1
- 2
site.py View File

#!/usr/bin/env python3 #!/usr/bin/env python3
import json import json
import locale import locale
import subprocess
from collections import defaultdict from collections import defaultdict
from dataclasses import dataclass from dataclasses import dataclass
from datetime import datetime, timedelta from datetime import datetime, timedelta
RULE_NAMES = mistune.InlineParser.RULE_NAMES + ("mark",) RULE_NAMES = mistune.InlineParser.RULE_NAMES + ("mark",)


def parse_mark(self, m, state): def parse_mark(self, m, state):
marker = m.group(1)
_ = m.group(1)
text = m.group(2) text = m.group(2)
return "mark", self.render(text, state) return "mark", self.render(text, state)



+ 1
- 1
site2020.py View File

RULE_NAMES = mistune.InlineParser.RULE_NAMES + ("mark",) RULE_NAMES = mistune.InlineParser.RULE_NAMES + ("mark",)


def parse_mark(self, m, state): def parse_mark(self, m, state):
marker = m.group(1)
_ = m.group(1)
text = m.group(2) text = m.group(2)
return "mark", self.render(text, state) return "mark", self.render(text, state)



+ 12
- 3
test_widont.py View File

), ),
# TODISCUSS: we probably want to widont the summary too? # TODISCUSS: we probably want to widont the summary too?
( (
"<details><summary>Unfold for content</summary><p>This is the content.</p></details>",
"<details><summary>Unfold for content</summary><p>This is the\u00a0content.</p></details>",
"<details><summary>Unfold for content</summary><p>This is the&nbsp;content.</p></details>",
(
"<details><summary>Unfold for content</summary>"
"<p>This is the content.</p></details>"
),
(
"<details><summary>Unfold for content</summary>"
"<p>This is the\u00a0content.</p></details>"
),
(
"<details><summary>Unfold for content</summary>"
"<p>This is the&nbsp;content.</p></details>"
),
), ),
], ],
) )

+ 7
- 5
widont.py View File

(?:{ALLOWED_INLINES})[^>]*>) # an approved inline opening (?:{ALLOWED_INLINES})[^>]*>) # an approved inline opening
|[^<>\s\.,;:!\?] # or a nontag/nonspace and not punctuation |[^<>\s\.,;:!\?] # or a nontag/nonspace and not punctuation
) )
\p{{Zs}}+ # the space to replace
([^<>\s]+ # must be followed by non-tag non-space characters
\s* # optional white space!
(</({ALLOWED_INLINES})>\s*)* # optional closing inline tags with optional white space after each
((</({COMPUTED_BLOCKS})>)|$)) # end with a closing block or the end of the string
\p{{Zs}}+ # the space to replace
([^<>\s]+ # must be followed by non-tag non-space characters
\s* # optional white space!
(</({ALLOWED_INLINES})>\s*)* # optional closing inline tags
# with optional white space after each
((</({COMPUTED_BLOCKS})>)|$)) # end with a closing block or the end of
# the string
""", """,
re.VERBOSE, re.VERBOSE,
) )

Loading…
Cancel
Save