xref: /btstack/port/arduino/docs/update_apis.py (revision 36327e5a477375af95206026b682a10acb1b5354)
1#!/usr/bin/env python3
2import os
3import re
4import sys
5
6class State:
7    SearchStartAPI = 0
8    RemoveEmptyLinesAfterAPIStart = 1
9    SearchEndAPI = 2
10    DoneAPI = 3
11
12docs_folder = "docs/appendix/"
13appendix_file = docs_folder + "apis.md"
14btstack_folder = "../../../"
15code_identation = "    "
16
17api_header = """
18
19## API_TITLE API
20<a name ="appendix:API_LABLE"></a>
21
22"""
23
24api_ending = """
25"""
26
27# [file_name, api_title, api_lable]
28list_of_apis = [
29    [btstack_folder+"port/arduino/BTstack.h", "BTstack", "api_btstack"],
30]
31
32def replacePlaceholder(template, title, lable):
33    api_title = title + " API"
34    snippet = template.replace("API_TITLE", title).replace("API_LABLE", lable)
35    return snippet
36
37def writeAPI(fout, infile_name):
38    global code_identation
39    state = State.SearchStartAPI
40    with open(infile_name, 'rb') as fin:
41        for line in fin:
42            if state == State.SearchStartAPI:
43                parts = re.match('\s*(/\*).*API_START.*(\*/)',line)
44                if parts:
45                    state = State.RemoveEmptyLinesAfterAPIStart
46                    continue
47
48            if state == State.RemoveEmptyLinesAfterAPIStart:
49                if line == "" or line == "\n":
50                    continue
51                state = State.SearchEndAPI
52
53            if state == State.SearchEndAPI:
54                parts = re.match('\s*(/\*).*API_END.*(\*/)',line)
55                if parts:
56                    state = State.DoneAPI
57                    return
58                fout.write(code_identation + line)
59
60def process_and_write_api(fout, api_tuple):
61    infile_name = api_tuple[0]
62    if not infile_name:
63        return
64
65    api_title = api_tuple[1]
66    api_lable = api_tuple[2]
67
68    fout.write(replacePlaceholder(api_header, api_title, api_lable))
69    writeAPI(fout, infile_name)
70
71
72with open(appendix_file, 'w') as aout:
73    for api_tuple in list_of_apis:
74        infile_name = api_tuple[0]
75        if not infile_name:
76            continue
77        process_and_write_api(aout, api_tuple)
78
79