# lines starting with # are comments
s/#.*//

# lines starting with * should be passed without interpretation (after
# removing the *)
/^\*/{
s///
b
}

# lines starting with "Module" start a new chapter
/^\(Module \)\([^ ]*\)$/{
# just the name of the module is given
s//\\chapter{Built-in module \2}\
\\section{\2 functions and variables}\
\\bimodindex{\2}\
\
\\renewcommand{\\indexsubitem}{(in module \2)}/
b
}

/^\(Module \)\([^ ]*\)\( .*\)/{
# if more than the name is given, pass the whole sentence on
s//\\chapter{Built-in module \2}\
\\section{\2 functions and variables}\
\\bimodindex{\2}\
\
The module\3\
\
\\renewcommand{\\indexsubitem}{(in module \2)}/
b
}

# lines containing the word "objects" start a new section on the object
/^\(.* object\)s$/{
# just the name of the object is given
s//\\section{\1}\
\
\\renewcommand{\\indexsubitem}{(\1 attribute)}\
/
b
}

/^\(.* object\)s .*/{
# a whole sentence is given, pass it on
s//\\section{\1}\
\
&\
\
\\renewcommand{\\indexsubitem}{(\1 attribute)}\
/
b
}

# convert commas in the argument list to escaped commas
/([^)]*)/{
h
s/[^(]*(//
s/).*//
s/,/\\,/g
G
s/^\(.*\)\n\([^(]*(\)[^)]*\().*\)/\2\1\3/
}

# deal with optional arguments
/^[^(]*(\([^)]*\)).*/{
h
s//\1/
: loop1
s/ *\[ *\(.*[^ ]\) *\] */\\optional{\1}/
t loop1
G
s/\(.*\)\n\([^(]*(\)[^)]*)/\2\1)/
}

# function that returns a value with the X equivalent
/^\([^(]*\)(\([^)]*\)) -> \(.*\) or \(.*\) \[\(.*\)\]/{
s//\\begin{funcdesc}{\1}{\2}\
Returns \\code{\3} or \\code{\4}.\
This function corresponds with \\code{\5}.\
\\end{funcdesc}\
/
b
}

# function that returns a value with the X equivalent
/^\([^(]*\)(\([^)]*\)) -> \(.*\) \[\(.*\)\]/{
s//\\begin{funcdesc}{\1}{\2}\
Returns \\code{\3}.\
This function corresponds with \\code{\4}.\
\\end{funcdesc}\
/
b
}

# function that returns a value with no X equivalent
/^\([^(]*\)(\([^)]*\)) -> \(.*\) or \(.*\)/{
s//\\begin{funcdesc}{\1}{\2}\
Returns \\code{\3} or \\code{\4}.\
\\end{funcdesc}\
/
b
}

# function that returns a value with no X equivalent
/^\([^(]*\)(\([^)]*\)) -> \(.*\)/{
s//\\begin{funcdesc}{\1}{\2}\
Returns \\code{\3}.\
\\end{funcdesc}\
/
b
}

# function that does not returns a value with the X equivalent
/^\([^(]*\)(\(.*\)) \[\(.*\)\]/{
s//\\begin{funcdesc}{\1}{\2}\
Returns \\code{None}.\
This function corresponds with \\code{\3}.\
\\end{funcdesc}\
/
b
}

# function that does not returns a value with no X equivalent
/^\([^(]*\)(\([^)]*\))$/{
s//\\begin{funcdesc}{\1}{\2}\
Returns \\code{None}.\
\\end{funcdesc}\
/
b
}

# data attribute with description
/^[^ ]*:/{
s/\([^:]*\): *\(.*\)/\\begin{datadesc}{\1}\
\2\
\\end{datadesc}\
/
b
}

/ /{
s/.*/&\
/
b
}

# exception
/^Error$/{
s//\\begin{excdesc}{Error}\
Exception generated by functions in this module.\
\\end{excdesc}\
/
b
}

# data attribute without description
s/..*/\\begin{datadesc}{&}\
Description to be supplied.\
\\end{datadesc}\
/
