1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
|
#!/usr/bin/env python
import re
import sys
import os.path
from textwrap import TextWrapper
import urllib.request
try:
from lxml import etree
except ImportError:
sys.stderr.write("Please install lxml to run this script.")
sys.exit(1)
DEPRECATED_COMMANDS = []
SCRIPT_PATH = os.path.dirname(os.path.realpath(__file__))
def get_text(elements, itemize=False):
paragraphs = []
highlight_elements = ["varname", "parameter"]
strip_elements = [
"returnvalue",
"command",
"link",
"footnote",
"simpara",
"footnoteref",
"function",
] + highlight_elements
for element in elements:
# put "Since MPD version..." in parenthese
etree.strip_tags(element, "application")
for e in element.xpath("footnote/simpara"):
e.text = "(" + e.text.strip() + ")"
for e in element.xpath("|".join(highlight_elements)):
e.text = "*" + e.text.strip() + "*"
etree.strip_tags(element, *strip_elements)
if itemize:
initial_indent = " * "
subsequent_indent = " "
else:
initial_indent = " "
subsequent_indent = " "
wrapper = TextWrapper(
subsequent_indent=subsequent_indent, initial_indent=initial_indent
)
text = element.text.replace("\n", " ").strip()
text = re.subn(r"\s+", " ", text)[0]
paragraphs.append(wrapper.fill(text))
return "\n\n".join(paragraphs)
def main(url):
header_file = os.path.join(SCRIPT_PATH, "commands_header.txt")
with open(header_file, "r") as f:
print(f.read())
r = urllib.request.urlopen(url)
tree = etree.parse(r)
chapter = tree.xpath('/book/chapter[@id="command_reference"]')[0]
for section in chapter.xpath("section"):
title = section.xpath("title")[0].text
print(title)
print(len(title) * "-")
print(get_text(section.xpath("para")))
print("")
for entry in section.xpath("variablelist/varlistentry"):
cmd = entry.xpath("term/cmdsynopsis/command")[0].text
if cmd in DEPRECATED_COMMANDS:
continue
subcommand = ""
args = ""
begin_optional = False
first_argument = True
for arg in entry.xpath("term/cmdsynopsis/arg"):
choice = arg.attrib.get("choice", None)
if choice == "opt" and not begin_optional:
begin_optional = True
args += "["
if args != "" and args != "[":
args += ", "
replaceables = arg.xpath("replaceable")
if len(replaceables) > 0:
for replaceable in replaceables:
args += replaceable.text.lower()
elif first_argument:
subcommand = arg.text
else:
args += '"{}"'.format(arg.text)
first_argument = False
if begin_optional:
args += "]"
if subcommand != "":
cmd += "_" + subcommand
print(".. function:: MPDClient." + cmd + "(" + args + ")")
description = get_text(entry.xpath("listitem/para"))
description = re.sub(r":$", r"::", description, flags=re.MULTILINE)
print("\n")
print(description)
print("\n")
for screen in entry.xpath("listitem/screen | listitem/programlisting"):
for line in screen.text.split("\n"):
print(" " + line)
for item in entry.xpath("listitem/itemizedlist/listitem"):
print(get_text(item.xpath("para"), itemize=True))
print("\n")
if __name__ == "__main__":
url = "https://raw.githubusercontent.com/MusicPlayerDaemon/MPD/master/doc/protocol.xml"
if len(sys.argv) > 1:
url += "?id=release-" + sys.argv[1]
main(url)
|