mirror of
https://github.com/MacRimi/ProxMenux.git
synced 2026-11-01 10:22:08 +00:00
An addon is run where the catalog lists it: on the Proxmox host, inside an LXC chosen from a list, or wherever is chosen when it can be both. The LXC is started when it is stopped, and OCI containers are left out. Until now every addon ran on the host. A script is downloaded whole before anything runs, so a failed or empty download runs nothing. Descriptions and notes the catalog writes in HTML are shown as plain text. The menu opens one script given as argument, and is as tall as the main menu.
47 lines
1.4 KiB
Python
47 lines
1.4 KiB
Python
#!/usr/bin/env python3
|
|
"""Convert catalogue HTML to terminal text without altering shell examples."""
|
|
import re
|
|
import sys
|
|
from html.parser import HTMLParser
|
|
|
|
|
|
class PlainText(HTMLParser):
|
|
def __init__(self):
|
|
super().__init__(convert_charrefs=True)
|
|
self.parts = []
|
|
self.hidden = 0
|
|
|
|
def handle_starttag(self, tag, attrs):
|
|
if tag in ('script', 'style'):
|
|
self.hidden += 1
|
|
elif not self.hidden:
|
|
if tag in ('p', 'div', 'br', 'ul', 'ol', 'pre', 'blockquote',
|
|
'h1', 'h2', 'h3', 'h4', 'h5', 'h6'):
|
|
self.parts.append('\n')
|
|
elif tag == 'li':
|
|
self.parts.append('\n• ')
|
|
|
|
def handle_endtag(self, tag):
|
|
if tag in ('script', 'style'):
|
|
self.hidden = max(0, self.hidden - 1)
|
|
elif not self.hidden and tag in ('p', 'div', 'li', 'ul', 'ol', 'pre',
|
|
'blockquote', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6'):
|
|
self.parts.append('\n')
|
|
|
|
def handle_data(self, data):
|
|
if not self.hidden:
|
|
self.parts.append(data)
|
|
|
|
|
|
def plain_text(value):
|
|
parser = PlainText()
|
|
parser.feed(value)
|
|
parser.close()
|
|
result = ''.join(parser.parts).replace('\xa0', ' ')
|
|
result = '\n'.join(line.strip() for line in result.splitlines())
|
|
return re.sub(r'\n{3,}', '\n\n', result).strip()
|
|
|
|
|
|
if __name__ == '__main__':
|
|
sys.stdout.write(plain_text(sys.stdin.read()))
|