mirror of
https://github.com/bluekitchen/btstack.git
synced 2025-01-26 21:35:16 +00:00
192 lines
6.4 KiB
Python
192 lines
6.4 KiB
Python
#!/usr/bin/env python
|
|
import os
|
|
import re
|
|
import sys
|
|
|
|
docs_folder = ""
|
|
appendix_file = docs_folder + "examples.tex"
|
|
|
|
lst_header = """
|
|
\\begin{lstlisting}
|
|
"""
|
|
|
|
lst_ending = """
|
|
\end{lstlisting}
|
|
"""
|
|
|
|
example_header = """
|
|
\documentclass[11pt, oneside]{article}
|
|
\usepackage{geometry}
|
|
\geometry{letterpaper}
|
|
%\geometry{landscape}
|
|
%\usepackage[parfill]{parskip}
|
|
\usepackage{graphicx}
|
|
\usepackage{amssymb}
|
|
\usepackage{listings}
|
|
\usepackage{hyperref}
|
|
\\begin{document}
|
|
\section{Examples}
|
|
"""
|
|
|
|
example_ending = """
|
|
\end{document}
|
|
"""
|
|
|
|
example_item = """
|
|
\item \emph{EXAMPLE_TITLE}: EXAMPLE_DESC, see Section \\ref{example:EXAMPLE_LABLE}.
|
|
"""
|
|
example_section = """
|
|
\subsection{EXAMPLE_TITLE: EXAMPLE_DESC}
|
|
\label{example:EXAMPLE_LABLE}
|
|
|
|
"""
|
|
example_subsection = """
|
|
\subsubsection{LISTING_CAPTION}
|
|
"""
|
|
|
|
listing_start = """
|
|
\\begin{lstlisting}[float, caption= LISTING_CAPTION., label=LISTING_LABLE]
|
|
"""
|
|
|
|
listing_ending = """\end{lstlisting}
|
|
|
|
"""
|
|
msp_folder = "../../platforms/msp-exp430f5438-cc2564b/example/"
|
|
# Example group title: [folder, example file, section title]
|
|
list_of_examples = {
|
|
"UART" : [[msp_folder, "led_counter", "provides UART and timer interrupt without Bluetooth"]]
|
|
#"GAP" : [["", "gap_inquiry"]],
|
|
#"SPP Server" : [["", "spp_counter"],
|
|
# ["", "spp_accel"],
|
|
# ["", "spp_flowcontrol"]],
|
|
}
|
|
|
|
class State:
|
|
SearchExampleStart = 0
|
|
SearchSnippetStart = 2
|
|
SearchSnippetPause = 4
|
|
SearchSnippetResume = 5
|
|
SearchSnippetEnd = 6
|
|
ReachedExampleEnd = 7
|
|
|
|
def replacePlaceholder(template, title, lable):
|
|
snippet = template.replace("API_TITLE", title).replace("API_LABLE", lable)
|
|
return snippet
|
|
|
|
|
|
def writeListings(fout, infile_name):
|
|
lst_lable = ""
|
|
lst_caption = ""
|
|
|
|
state = State.SearchExampleStart
|
|
with open(infile_name, 'rb') as fin:
|
|
for line in fin:
|
|
if state == State.SearchExampleStart:
|
|
parts = re.match('.*(EXAMPLE_START)\((.*)\):\s*(.*)(\*/)?\n',line)
|
|
if parts:
|
|
lable = parts.group(2)
|
|
title = parts.group(2).replace("_","\_")
|
|
desc = parts.group(3).replace("_","\_")
|
|
aout.write(example_section.replace("EXAMPLE_TITLE", title).replace("EXAMPLE_DESC", desc).replace("EXAMPLE_LABLE", lable))
|
|
state = State.SearchSnippetStart
|
|
continue
|
|
|
|
section_parts = re.match('.*(@section)\s*(.*)\s*(\*?/?)\n',line)
|
|
if section_parts:
|
|
aout.write("\n" + example_subsection.replace("LISTING_CAPTION", section_parts.group(2)))
|
|
continue
|
|
|
|
subsection_parts = re.match('.*(@subsection)\s*(.*)\s*(\*?/?)\n',line)
|
|
if section_parts:
|
|
subsubsection = example_subsection.replace("LISTING_CAPTION", section_parts.group(2)).replace('section', 'subsection')
|
|
aout.write("\n" + subsubsection)
|
|
continue
|
|
|
|
brief_parts = re.match('.*(@text)\s*(.*)',line)
|
|
if not brief_parts:
|
|
brief_parts = re.match('(\s\*\s)(.*)(\*/)?.*',line)
|
|
if brief_parts:
|
|
brief = " " + brief_parts.group(2)
|
|
else:
|
|
brief = brief_parts.group(2)
|
|
|
|
if brief_parts:
|
|
# replace refs
|
|
refs = re.match('.*(Listing\s*)(\w*).*',brief)
|
|
if refs:
|
|
brief = brief.replace(refs.group(2), "\\ref{"+refs.group(2)+"}")
|
|
aout.write(brief)
|
|
continue
|
|
|
|
if state == State.SearchSnippetStart:
|
|
parts = re.match('.*(SNIPPET_START)\((.*)\):\s*(.*\s*)(\*/).*',line)
|
|
|
|
if parts:
|
|
lst_lable = parts.group(2)
|
|
lst_caption = parts.group(3).replace("_","\_")
|
|
listing = listing_start.replace("LISTING_CAPTION", lst_caption).replace("LISTING_LABLE", lst_lable)
|
|
if listing:
|
|
aout.write("\n" + listing)
|
|
state = State.SearchSnippetEnd
|
|
continue
|
|
|
|
if state == State.SearchSnippetEnd:
|
|
parts_end = re.match('.*(SNIPPET_END).*',line)
|
|
parts_pause = re.match('.*(SNIPPET_PAUSE).*',line)
|
|
|
|
if not parts_end and not parts_pause:
|
|
end_comment_parts = re.match('.*(\*)/*\s*\n', line);
|
|
if not end_comment_parts:
|
|
aout.write(line)
|
|
elif parts_end:
|
|
aout.write(listing_ending)
|
|
state = State.SearchSnippetStart
|
|
elif parts_pause:
|
|
aout.write("...\n")
|
|
state = State.SearchSnippetResume
|
|
continue
|
|
|
|
if state == State.SearchSnippetResume:
|
|
parts = re.match('.*(SNIPPET_RESUME).*',line)
|
|
if parts:
|
|
state = State.SearchSnippetEnd
|
|
continue
|
|
|
|
parts = re.match('.*(EXAMPLE_END).*',line)
|
|
if parts:
|
|
if state != State.SearchSnippetStart:
|
|
print "Formating error detected"
|
|
state = State.ReachedExampleEnd
|
|
print "Reached end of the example"
|
|
|
|
|
|
|
|
# write list of examples
|
|
with open(appendix_file, 'w') as aout:
|
|
aout.write(example_header)
|
|
aout.write("\\begin{itemize}\n");
|
|
|
|
for group_title, examples in list_of_examples.iteritems():
|
|
group_title = group_title + " example"
|
|
if len(examples) > 1:
|
|
group_title = group_title + "s"
|
|
group_title = group_title + ":"
|
|
|
|
aout.write(" \item " + group_title + "\n");
|
|
aout.write(" \\begin{itemize}\n");
|
|
for example in examples:
|
|
lable = example[1]
|
|
title = example[1].replace("_","\_")
|
|
desc = example[2].replace("_","\_")
|
|
aout.write(example_item.replace("EXAMPLE_TITLE", title).replace("EXAMPLE_DESC", desc).replace("EXAMPLE_LABLE", lable))
|
|
aout.write(" \\end{itemize}\n")
|
|
aout.write("\\end{itemize}\n")
|
|
|
|
for group_title, examples in list_of_examples.iteritems():
|
|
for example in examples:
|
|
file_name = example[0] + example[1] + ".c"
|
|
writeListings(aout, file_name)
|
|
|
|
aout.write(example_ending)
|
|
|