-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgenDoc.m
98 lines (87 loc) · 3.21 KB
/
genDoc.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
basePath = '.';
createDocumentation(basePath)
%% A modified version
function createDocumentation(basePath)
% Check if the base path exists
if ~exist(basePath, 'dir')
error('The specified base directory does not exist.');
end
% Call recursive function to process directories
processDirectoriesRecursively(basePath);
end
function processDirectoriesRecursively(currentPath)
% Get a list of all folders in the current path
d = dir(currentPath);
isDir = [d(:).isdir]; % get logical index of directories
subDirs = {d(isDir).name}; % get directory names
% Filter out '.' and '..'
validDirs = subDirs(~ismember(subDirs, {'.', '..'}));
% Process each valid directory
for i = 1:length(validDirs)
folderName = validDirs{i};
folderPath = fullfile(currentPath, folderName);
% If directory starts with '@', process it
if startsWith(folderName, '@')
fprintf('Processing %s\n', folderPath);
processFolder(folderPath);
end
% Recursively call this function on all subdirectories
processDirectoriesRecursively(folderPath);
end
end
function processFolder(folderPath)
% Get all MATLAB files in the directory
files = dir(fullfile(folderPath, '*.m'));
for i = 1:length(files)
filePath = fullfile(folderPath, files(i).name);
comments = extractComments(filePath);
if ~isempty(comments)
writeReadme(folderPath, comments);
break; % Assume only one class definition per folder
end
end
end
function comments = extractComments(filePath)
fid = fopen(filePath, 'rt');
if fid == -1
error('Cannot open file %s.', filePath);
end
try
% Read file line by line
tline = fgetl(fid);
insideCommentBlock = false;
comments = '';
while ischar(tline)
if contains(tline, 'classdef')
insideCommentBlock = true; % Start capturing comments after classdef
elseif contains(tline, 'properties')
if insideCommentBlock
break; % Stop if properties block is reached
end
elseif insideCommentBlock && startsWith(strtrim(tline), '%')
% Clean and format the comment line
cleanLine = strtrim(tline(2:end)); % Remove leading '%' and white spaces
if startsWith(cleanLine, '%')
cleanLine = strtrim(cleanLine(2:end)); % Remove additional '%' for Markdown headers
end
comments = [comments, cleanLine, newline];
end
tline = fgetl(fid);
end
catch
fclose(fid);
rethrow(lasterror);
end
fclose(fid);
end
function writeReadme(folderPath, comments)
readmePath = fullfile(folderPath, 'Readme.md');
fid = fopen(readmePath, 'wt');
if fid == -1
error('Cannot create Readme.md in %s.', folderPath);
end
fprintf(fid, '# %s\n', comments); % Write comments with Markdown header
fclose(fid);
end
% basePath = 'C:\Users\sajja\Documents\MATLAB\matlab-instrument-control\'
% basePath = 'C:\Users\sajja\Documents\MATLAB\matlab-instrument-control\src\+mic\'