-
-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
scrape, format functions signatures from Rmd
- Loading branch information
1 parent
6b20c36
commit 7ec8511
Showing
1 changed file
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#!/usr/bin/env python | ||
""" | ||
Extract function signatures from html comments in markdown. | ||
""" | ||
|
||
import glob | ||
import os | ||
import os.path | ||
import sys | ||
import contextlib | ||
|
||
|
||
@contextlib.contextmanager | ||
def pushd(new_dir): | ||
previous_dir = os.getcwd() | ||
os.chdir(new_dir) | ||
yield | ||
os.chdir(previous_dir) | ||
|
||
|
||
def main(): | ||
if len(sys.argv) > 2: | ||
stan_major = int(sys.argv[1]) | ||
stan_minor = int(sys.argv[2]) | ||
else: | ||
print('Expecting 2 arguments <MAJOR> <MINOR> version numbers') | ||
sys.exit(1) | ||
|
||
sigs = set() | ||
ref_dir = os.path.join('src', 'functions-reference') | ||
with pushd(ref_dir): | ||
for file in glob.glob('*.Rmd'): | ||
print(file) | ||
with open(file) as rmd_file: | ||
lines = rmd_file.readlines() | ||
for line in lines: | ||
if line.startswith('<!-- '): | ||
line = line.lstrip('<!- ') | ||
parts = [x.strip(' ~') for x in line.split(';')] | ||
if len(parts) == 3: | ||
parts[1] = parts[1] | ||
sigs.add('{}; ~; {}'.format(parts[1], parts[0])) | ||
elif len(parts) == 4: | ||
sigs.add('{}; {}; {}'.format(parts[1], parts[2], parts[0])) | ||
else: | ||
print('not a function sig: {}'.format(line)) | ||
|
||
outfile_name = 'stan-fuctions-{}_{}.txt'.format(str(stan_major), str(stan_minor)) | ||
with open(outfile_name, 'w') as outfile: | ||
outfile.write('# This file is semicolon delimited\n') | ||
outfile.write('StanFunction; Arguments; ReturnType\n') | ||
for sig in sorted(sigs): | ||
outfile.write(sig) | ||
outfile.write('\n') | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
7ec8511
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mitzimorris,
Thank you so much for this! I just realized the output filename is misspelled. It should be "stan-functions-" not "stan-fuctions". This is perhaps too trivial for a pull request, so I'm posting here.
7ec8511
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hi Kaz, good catch - just fixed this - put the "fun" back in "functions" - many thanks!