manual: fix script

This commit is contained in:
Milanka Ringwald 2015-04-24 14:21:04 +02:00
parent f2c3367255
commit cf18d7621d
2 changed files with 51 additions and 33 deletions

View File

@ -8,6 +8,7 @@ example:
./update_listings.py -s
subl examples.tex
pdflatex examples.tex
pdflatex examples.tex
open examples.pdf
clean:

View File

@ -11,7 +11,7 @@ list_of_examples = {
"Hello World" : [["led_counter"]],
"GAP" : [["gap_inquiry"]],
"SDP Queries" :[["sdp_general_query"],
# ["sdp_bnep_query"]
["sdp_bnep_query"]
],
"SPP Server" : [["spp_counter"],
["spp_flowcontrol"]],
@ -64,7 +64,7 @@ example_subsection = """
listing_start = """
$ $
\\begin{lstlisting}[caption= LISTING_CAPTION., label=listing:LISTING_LABLE]
\\begin{lstlisting}[caption= LISTING_CAPTION., label=listing:FILE_NAME:LISTING_LABLE]
"""
listing_ending = """\end{lstlisting}
@ -75,17 +75,20 @@ def replacePlaceholder(template, title, lable):
snippet = template.replace("API_TITLE", title).replace("API_LABLE", lable)
return snippet
def latexText(text):
def latexText(text, ref_prefix):
if not text:
return ""
brief = text.replace("_","\_")
# TODO: restore paths
brief = brief.replace(" in the BTstack manual","")
refs = re.match('.*(Listing\s*)(\w*).*',brief)
refs = re.match('.*Listing\s*\s*(\w+)(?:.|\s).*',brief)
if refs:
brief = brief.replace(refs.group(2), "\\ref{listing:"+refs.group(2)+"}")
refs = re.match('.*(Section\s*)(\w*).*',brief)
brief = brief.replace(refs.group(1), "\\ref{listing:"+ref_prefix+":" + refs.group(1)+"}")
refs = re.match('.*(Section\s*)(\w*).*',brief, re.I)
if refs:
brief = brief.replace(refs.group(2), "\\ref{section:"+refs.group(2)+"}")
@ -95,7 +98,7 @@ def isEmptyCommentLine(line):
return re.match('(\s*\*\s*)\n',line)
def isEndOfComment(line):
return re.match('\s*\*/.*', line)
return re.match('\s*\*/.*', line)
def isNewItem(line):
return re.match('(\s*\*\s*\-\s*)(.*)',line)
@ -106,18 +109,18 @@ def isTextTag(line):
def isItemizeTag(line):
return re.match("(\s+\*\s+)(-\s)(.*)", line)
def processTextLine(line):
def processTextLine(line, ref_prefix):
if isTextTag(line):
text_line_parts = re.match(".*(@text)(.*)", line)
return " " + latexText(text_line_parts.group(2))
return " " + latexText(text_line_parts.group(2), ref_prefix)
if isItemizeTag(line):
text_line_parts = re.match("(\s*\*\s*\-\s*)(.*)", line)
return "\n \item " + latexText(text_line_parts.group(2))
return "\n \item " + latexText(text_line_parts.group(2), ref_prefix)
text_line_parts = re.match("(\s+\*\s+)(.*)", line)
if text_line_parts:
return " " + latexText(text_line_parts.group(2))
return " " + latexText(text_line_parts.group(2), ref_prefix)
return ""
def getExampleTitle(example_path):
@ -126,7 +129,7 @@ def getExampleTitle(example_path):
for line in fin:
parts = re.match('.*(EXAMPLE_START)\((.*)\):\s*(.*)(\*/)?\n',line)
if parts:
example_title = latexText(parts.group(3))
example_title = parts.group(3).replace("_","\_")
continue
return example_title
@ -139,7 +142,7 @@ class State:
SearchItemizeEnd = 5
ReachedExampleEnd = 6
def writeListings(aout, infile_name):
def writeListings(aout, infile_name, ref_prefix):
itemText = None
state = State.SearchExampleStart
code_in_listing = ""
@ -152,8 +155,8 @@ def writeListings(aout, infile_name):
parts = re.match('.*(EXAMPLE_START)\((.*)\):\s*(.*)(\*/)?\n',line)
if parts:
lable = parts.group(2)
title = latexText(parts.group(2))
desc = latexText(parts.group(3))
title = latexText(parts.group(2), ref_prefix)
desc = latexText(parts.group(3), ref_prefix)
aout.write(example_section.replace("EXAMPLE_TITLE", title).replace("EXAMPLE_DESC", desc).replace("EXAMPLE_LABLE", lable))
state = State.SearchListingStart
continue
@ -172,26 +175,35 @@ def writeListings(aout, infile_name):
continue
if isTextTag(line):
text_block = processTextLine(line)
text_block = processTextLine(line, ref_prefix)
continue
if text_block:
if isEndOfComment(line):
if text_block or itemize_block:
if isEndOfComment(line) or isEmptyCommentLine(line):
if itemize_block:
# finish itemize
aout.write(itemize_block + "\n\end{itemize}\n")
itemize_block = None
else:
else:
if isEmptyCommentLine(line):
text_block = text_block + "\n"
else:
# finish text
aout.write(text_block)
text_block = None
else:
if isNewItem(line) and not itemize_block:
# finish text, start itemize
aout.write(text_block)
text_block = None
else:
if isNewItem(line):
if not itemize_block:
itemize_block = "\n \\begin{itemize}"
itemize_block = "\n \\begin{itemize}"
if itemize_block:
itemize_block = itemize_block + processTextLine(line)
itemize_block = itemize_block + processTextLine(line, ref_prefix)
else:
text_block = text_block + processTextLine(line)
# append text
text_block = text_block + processTextLine(line, ref_prefix)
continue
@ -200,8 +212,8 @@ def writeListings(aout, infile_name):
if parts:
lst_lable = parts.group(2)
lst_caption = latexText(parts.group(3))
listing = listing_start.replace("LISTING_CAPTION", lst_caption).replace("LISTING_LABLE", lst_lable)
lst_caption = latexText(parts.group(3), ref_prefix)
listing = listing_start.replace("LISTING_CAPTION", lst_caption).replace("FILE_NAME", ref_prefix).replace("LISTING_LABLE", lst_lable)
if listing:
aout.write("\n" + listing)
state = State.SearchListingEnd
@ -243,6 +255,7 @@ def writeListings(aout, infile_name):
def processExamples(examples_folder, standalone, examples_ofile):
with open(examples_ofile, 'w') as aout:
for group_title in list_of_groups:
if not list_of_examples.has_key(group_title): continue
examples = list_of_examples[group_title]
for example in examples:
example_path = examples_folder + example[0] + ".c"
@ -256,7 +269,9 @@ def processExamples(examples_folder, standalone, examples_ofile):
aout.write("\n \\begin{itemize}\n");
for group_title in list_of_groups:
if not list_of_examples.has_key(group_title): continue
examples = list_of_examples[group_title]
group_title = group_title + " example"
if len(examples) > 1:
group_title = group_title + "s"
@ -265,18 +280,20 @@ def processExamples(examples_folder, standalone, examples_ofile):
aout.write(" \item " + group_title + "\n");
aout.write(" \\begin{itemize}\n");
for example in examples:
lable = example[0]
title = latexText(example[0])
desc = latexText(example[1])
aout.write(example_item.replace("EXAMPLE_TITLE", title).replace("EXAMPLE_DESC", desc).replace("EXAMPLE_LABLE", lable))
ref_prefix = example[0]
title = latexText(example[0], ref_prefix)
desc = latexText(example[1], ref_prefix)
aout.write(example_item.replace("EXAMPLE_TITLE", title).replace("EXAMPLE_DESC", desc).replace("EXAMPLE_LABLE", ref_prefix))
aout.write(" \\end{itemize}\n")
aout.write("\\end{itemize}\n")
for group_title in list_of_groups:
if not list_of_examples.has_key(group_title): continue
examples = list_of_examples[group_title]
for example in examples:
file_name = examples_folder + example[0] + ".c"
writeListings(aout, file_name)
writeListings(aout, file_name, example[0])
if standalone:
aout.write(document_end)