Debian Codegraph @

tools/faust2appls/faust2md

raw symbols SCIP
1 #!/usr/bin/env python3
2 #
3 # ---------------------- faust2md ----------------------
4 # Usage: `faust2md [-t 4] [-c] [-f] foo.dsp > foo.md`
5 #
6 # Ultra simple automatic documentation system for Faust.
7 # Creates a markdown file by extracting the comments from
8 # a Faust file. The option -t n can be used to change the
9 # default (4) tab setting. The option -c can be used to
10 # include the Faust code itself into the generated doc.
11 # And the option -f can be used to include a YAML front
12 # matter with the name of the file and the date.
13 #
14 # The format of a title is :
15 # //############# Title Name #################
16 # // markdown text....
17 # // markdown text....
18 # //##########################################
19 #
20 # The format of a section is :
21 # //============== Section Name ==============
22 # // markdown text....
23 # // markdown text....
24 # //==========================================
25 #
26 # The format of a comment is :
27 # //-------------- foo(x,y) ------------------
28 # // markdown text....
29 # // markdown text....
30 # //------------------------------------------
31 # everything else is considered Faust code.
32 # The translation is the following:
33 # ## foo(x,y)
34 # markdown text....
35 # markdown text....
36 # ------------------------------------------------------
37
38 import sys
39 import re
40 import datetime
41 import getopt
42
43
44 # Outdent a comment line by n characters in
45 # order to remove the prefix "// "
46 def outdent(line, n):
47 if len(line) <= n:
48 return "\n"
49 else:
50 return line[n:]
51
52
53 # Match the first line of a title
54 # of type "//**** Title ****"
55 # at least 3 * are needed
56 def matchBeginTitle(line):
57 return re.search(r'^\s*//#{3,}\s*([^#]+)#{3,}', line)
58
59
60 # Match the last line of a title
61 # of type "//********"
62 # or a blank line
63 def matchEndTitle(line):
64 return re.search(r'^\s*((//#{3,})|(\s*))$', line)
65
66
67 # Match the first line of a section
68 # of type "//==== Section ===="
69 # at least 3 = are needed
70 def matchBeginSection(line):
71 return re.search(r'^\s*//={3,}\s*([^=]+)={3,}', line)
72
73
74 # Match the last line of a section
75 # of type "//======="
76 # or a blank line
77 def matchEndSection(line):
78 return re.search(r'^\s*((//={3,})|(\s*))$', line)
79
80
81 # Match the first line of a comment
82 # of type "//--- foo(x,y) ----"
83 # at least 3 - are needed
84 def matchBeginComment(line):
85 return re.search(r'^\s*//-{3,}\s*([^-]+)-{3,}', line)
86
87
88 # Match the last line of a comment
89 # of type "//-----------------"
90 # or a blank line
91 def matchEndComment(line):
92 return re.search(r'^\s*((//-{3,})|(\s*))$', line)
93
94
95 # Compute the indentation of a line,
96 # that is the position of the first word character
97 # after "// "
98 def indentation(line):
99 matchComment = re.search(r'(^\s*//\s*\w)', line)
100 if matchComment:
101 return len(matchComment.group(1))-1
102 else:
103 return 0
104
105
106 # Indicates if a line is a comment
107 def isComment(line):
108 matchComment = re.search(r'^\s*//', line)
109 if matchComment:
110 return 1
111 else:
112 return 0
113
114
115 # Print the front matter of the file
116 def frontMatter(file):
117 print('---')
118 print('file: %s' % (file,))
119 print('date: %s' % (datetime.date.today(),))
120 print('---')
121 print('')
122
123
124 #
125 # THE PROGRAM STARTS HERE
126 #
127 tabsize = 4 # tabsize used for expanding tabs
128 codeflag = 0 # 0: no source code; 1: print also source code
129 frontflag = 0 # 0: no front matter; 1: print front matter
130 mode = 0 # 0: in code; 1: in md-comment
131 idt = 0 # indentation retained to outdent comment lines
132
133 # Analyze command line arguments
134 try:
135 opts, args = getopt.getopt(sys.argv[1:], "t:cf")
136 if not args:
137 raise getopt.error("At least one file argument required")
138 except getopt.error as e:
139 print(e.msg)
140 print("usage: %s [-t tabsize] [-c] [-f] file ..." % (sys.argv[0],))
141 sys.exit(1)
142 for optname, optvalue in opts:
143 if optname == '-t':
144 tabsize = int(optvalue)
145 if optname == '-c':
146 codeflag = 1
147 if optname == '-f':
148 frontflag = 1
149
150
151 # Process all the files and print the documentation on the standard output
152 for file in args:
153 with open(file) as f:
154 if frontflag:
155 frontMatter(file)
156 for text in f:
157 line = text.expandtabs(tabsize)
158 if isComment(line) == 0:
159 if mode == 1:
160 # we are closing a md-comment
161 print('')
162 mode = 0
163 if codeflag:
164 print('\t%s' % line,)
165 else:
166 if mode == 0: # we are in code
167 matchComment = matchBeginComment(line)
168 matchSection = matchBeginSection(line)
169 matchTitle = matchBeginTitle(line)
170 if matchComment:
171 print('')
172 print("### %s" % (matchComment.group(1),))
173 elif matchSection:
174 print('')
175 print("## %s" % (matchSection.group(1),))
176 elif matchTitle:
177 print('')
178 print("# %s" % (matchTitle.group(1),))
179 if matchComment or matchSection or matchTitle:
180 mode = 1 # we just started a md-comment
181 idt = 0 # we have to measure the indentation
182 else:
183 # it is a comment but not a md-comment
184 # therefore it is part of the code
185 if codeflag:
186 print('\t%s' % (line,))
187 else:
188 # we are in a md-comment
189 if idt == 0:
190 # we have to measure the indentation
191 idt = indentation(line)
192 # check end of md-comment
193 matchComment = matchEndComment(line)
194 matchSection = matchEndSection(line)
195 matchTitle = matchEndTitle(line)
196 if matchComment:
197 print('')
198 print("---")
199 print('')
200 if matchComment or matchSection or matchTitle:
201 # end of md-comment switch back to mode O
202 mode = 0
203 else:
204 # lien of content of md-comment
205 # we print it unindented
206 print(outdent(line, idt)),