parse links from README

This commit is contained in:
Q
2021-10-12 20:21:55 +03:00
parent 8640a29d00
commit fecebedada

View File

@@ -8,6 +8,7 @@ import sys
import time
import string
import json
import re
import urllib.parse
from glob import fnmatch
import base64
@@ -18,7 +19,17 @@ VERSION = "20211002"
IMAGE_EXTENSIONS = ["png", "gif", "jpg", "jpeg", "tif", "tiff"]
AUDIO_EXTENSIONS = ["wav", "mp3", "ogg"]
VIDEO_EXTENSIONS = ["mp4", "ogg", "webm"]
SAFE_OPTS = ("hidden","title","parent","recursive","images","media","includes","no_readme")
SAFE_OPTS = (
"hidden",
"title",
"parent",
"recursive",
"images",
"media",
"includes",
"no_readme",
)
def setup():
""" Setup the command line options """
@@ -148,7 +159,7 @@ def HTML2setup(opts):
with open(os.path.join(opts.path, opts.filename), "rt") as f:
for l in f.readlines():
if l.find('<meta name="SimpleWebPageSetup"') > -1:
content = l[l.find('content=') :].rstrip("'/>\n")
content = l[l.find("content=") :].rstrip("'/>\n")
config = json.loads(content[9:])
for key in config:
if key in SAFE_OPTS:
@@ -223,7 +234,7 @@ def generate_index(opts):
for fi in files:
f.write(get_filelink(path, fi, opts.images, opts.media))
f.write(get_footer(readme))
f.write(get_download_lines(files, recursive = opts.recursive))
f.write(get_download_lines(files, recursive=opts.recursive))
f.close()
return
@@ -287,7 +298,7 @@ def get_filelink(path, fname, images=False, media=False):
)
def get_download_lines(files, recursive = False):
def get_download_lines(files, recursive=False):
s = "\n<!--\n"
for f in files:
s += "#FILE %s\n" % (urllib.parse.quote(f),)
@@ -442,19 +453,28 @@ iframe {
.replace("SECRET", secret, 1)
)
def get_readme(path, no_read):
if no_read:
return ""
if not os.path.exists("README.md"):
return ""
with open("README.md", 'rt') as fp:
return "<h2>README.md</h2><pre>{}</pre>".format(fp.read().strip())
with open("README.md", "rt") as fp:
return "<h2>README.md</h2><pre>{}</pre>".format(
re.sub(
r"(https?:\/\/[\w\.,\-\@?^=%&:/~\+#]+)",
'<a href="\\1">\\1</a>',
fp.read().strip(),
flags=re.IGNORECASE,
)
)
def get_header(opts):
opts_str = setup2JSON(opts)
js_code = '''
js_code = """
<script type="text/javascript">
/*
Table sorting script by Joost de Valk, check it out at http://www.joostdevalk.nl/code/sortable-table/.
@@ -778,8 +798,8 @@ function alternate(table) {
}
</script>
'''
css_style = '''
"""
css_style = """
<style>
/* Style modified from: https://css-tricks.com/snippets/php/display-styled-directory-contents/ */
* {
@@ -826,6 +846,7 @@ function alternate(table) {
padding-right: 5px;
}
a {
color: #EB6000;
text-decoration: none;
}
@@ -875,7 +896,7 @@ function alternate(table) {
margin: 10px 10px 10px 10px;
}
</style>
'''
"""
header = (
"""<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<html>
@@ -896,12 +917,12 @@ function alternate(table) {
<table class="sortable" id="fileList"><thead><tr><th>Name</th><th class="right">Size</th><th class="right">Size B</th><th class="right">Modified</th></tr></thead><tbody>
"""
).format(
title = opts.title,
config = opts_str,
program = "SimpleWebPage",
version = VERSION,
js_code = js_code,
css_style = css_style
title=opts.title,
config=opts_str,
program="SimpleWebPage",
version=VERSION,
js_code=js_code,
css_style=css_style,
)
return header
@@ -909,7 +930,7 @@ function alternate(table) {
def get_footer(readme):
return """</tbody></table>{README}</div>
</body></html>""".format(
README = readme
README=readme
)