summaryrefslogtreecommitdiffstats
path: root/scripts/docparser.py
blob: a6019c96e18e4f5dcaa5ad2de70e5414ddbc6d1e (plain)
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
from bs4 import BeautifulSoup
import sys
import os
import json


def get_section(doc, section):
    element = doc.find(section)
    if element:
        return element.get_text()


def get_description(doc):
    text = get_section(doc, "refsect1")
    if text:
        lines = filter(lambda x: x.strip(), text.split("\n"))

        if len(lines) > 1 and lines[0] == "Description":
            return lines[0] + "\n" + lines[1]


def parse(file_name):
    with open(file_name, "r") as file:
        doc = BeautifulSoup(file.read(), "html.parser")
        desc = get_description(doc)
        synopsis = get_section(doc, "synopsis")
        if desc and synopsis:
            return {"description": desc, "synopsis": synopsis}


if __name__ == "__main__":
    if len(sys.argv) < 2:
        print("Parse postgres SGML reference files into JSON")
        print("Usage:")
        print(
            'echo -n "commands = " > command_help.py; python parser.py ref/ | python -mjson.tool | sed \'s/"\\: null/": None/g\' >>  command_help.py'
        )
        print("")
        sys.exit(0)

    dir = sys.argv[1]
    docs = {}

    for file_name in os.listdir(dir):
        if file_name.endswith(".sgml"):
            path = dir.rstrip("/") + "/" + file_name
            command = file_name[:-5].replace("_", " ")
            parsed = parse(path)
            if parsed:
                docs[command.upper()] = parsed
    print(json.dumps(docs))