-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathgenerate_readme.m
More file actions
210 lines (173 loc) · 9.61 KB
/
Copy pathgenerate_readme.m
File metadata and controls
210 lines (173 loc) · 9.61 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
function generate_readme(tex_file, readme_file)
[tex_file_dir, tex_file_name] = fileparts(tex_file);
if nargin < 2
readme_file = [tex_file_dir filesep 'readme.md'];
end
tex = fileread(tex_file);
%% Analysis
% title
title = regexp(tex, '\\title(?:(.*?))?{([^{}]*)}', 'tokens', 'once');
title = strtrim(title{1});
% document
document = regexp(tex, '\\begin{document}(.*)\\end{document}', 'tokens', 'once');
document = document{1};
document = regexprep(document, '\\maketitle', '');
document = regexprep(document, '\\noindent', '');
document = regexprep(document, '\\clearpage', '');
document = regexprep(document, '\\begin\{abstract\}', '');
document = regexprep(document, '\\end\{abstract\}', '');
% parentesi graffe (1)
document = regexprep(document, '\\{', '¡!parentesi graffa aperta!¡');
document = regexprep(document, '\\}', '¡!parentesi graffa chiusa!¡');
% basic reformatting
document = regexprep(document, 'BRAPH~2', 'BRAPH 2');
document = regexprep(document, '---', '-'); % ---
document = regexprep(document, '\\\\\n', '\n\n'); % \\
document = regexprep(document, '\\fn{([^{}]*)}', '"$1"'); % \fn
document = regexprep(document, '\\code{([^{}]*)}', '`$1`'); % \code
document = regexprep(document, '\\url{([^{}]*)}', '[$1]($1)'); % \url{link}
document = regexprep(document, '\\href{([^{}]*)}{([^{}]*)}', '[$2]($1)'); % \href{link}{text}
document = regexprep(document, '``([^`'']*)''''', '"$1"'); % ``text''
document = regexprep(document, '\\emph{([^{}]*)}', '*$1*'); % \emph{...}
document = regexprep(document, '\\textbf{([^{}]*)}', '**$1**'); % \textbf{...}
document = regexprep(document, '\\texttt{([^{}]*)}', '`$1`'); % \texttt{...}
document = regexprep(document, '\\textit{([^{}]*)}', '_$1_'); % \textit{...}
document = regexprep(document, '\\lstinline{([^{}]*)}', '`$1`'); % \lstinline{...}
document = regexprep(document, '\\&', '&'); % \&
document = regexprep(document, '\\_', '_'); % \_
document = regexprep(document, '¥€¥', '€'); % ¥€¥
document = regexprep(document, '¤...¤', '. . . . .'); % ¤...¤
document = regexprep(document, '¤', ''); % ¤
document = regexprep(document, '{\\bf ([^{}]*)}', '**$1**'); % {\bf ...}
document = regexprep(document, '\\footnote{([^{}]*)}', ' ($1)'); % \footnote{...}
document = regexprep(document, '\\begin{fullwidth}', ''); % \begin{fullwidth}
document = regexprep(document, '\\end{fullwidth}', ''); % \end{fullwidth}
document = regexprep(document, '\\begin{quote}', ''); % \begin{fullwidth}
document = regexprep(document, '\\end{quote}', ''); % \end{quote}
document = regexprep(document, [newline() '{%TEX[^{}]*}%TEX\s*' newline()], ''); % {%TEX ... }%TEX
document = regexprep(document, [newline() '{%¡TEX[^¡]*}%¡TEX\s*' newline()], ''); % {%¡TEX ... }%¡TEX
document = regexprep(document, ['\\newcommand{[^' newline() ']*}{[^' newline() ']*}' newline()], ''); % \newcommand{...}{...}
document = regexprep(document, ['\\newcolumntype{[^' newline() ']*}{[^' newline() ']*}' newline()], ''); % \newcolumntype{...}{...}
% table of contents
sections = regexp(document, '\\(sub)?(sub)?section\{([^{}]*)\}', 'tokens', 'all');
toc = '## Table of Contents';
for i = 1:1:length(sections)
section = sections{i};
section_title = section{3};
if section{2} % subsubsection
level = 3;
elseif section{1} % subsection
level = 2;
else % section
level = 1;
end
toc = [toc newline() repmat('>', 1, level) ' [' section_title '](#' regexprep(regexprep(section_title, '[^a-zA-Z0-9"\s]', ''), ' ', '-') ')' newline() repmat('>', 1, level)]; %#ok<AGROW>
end
document = regexprep(document, '\\tableofcontents', toc);
% (sub)section
arrow_up_icon = char(11014);
document = regexprep(document, '\\section{([^{}]*)}', ['<a id="$1"></a>' newline() '## $1 [' arrow_up_icon '](#Table-of-Contents)']);
document = regexprep(document, '\\subsection{([^{}]*)}', ['<a id="$1"></a>' newline() '### $1 [' arrow_up_icon '](#Table-of-Contents)']);
document = regexprep(document, '\\subsubsection{([^{}]*)}', ['<a id="$1"></a>' newline() '#### $1 [' arrow_up_icon '](#Table-of-Contents)']);
a_start = regexp(document, '<a id="', 'end', 'all');
a_end = regexp(document, '"></a>', 'start', 'all');
for i = length(a_start):-1:1
section_title = regexprep(document(a_start(i):a_end(i)), '[^a-zA-Z0-9"\s]', '');
document = [document(1:a_start(i) - 1) strrep(section_title, ' ', '-') document(a_end(i) + 1:end)];
end
% itemize
document = regexprep(document, '\\begin{itemize}', '');
document = regexprep(document, '\\item ', '- ');
document = regexprep(document, '\\end{itemize}', '');
% enumerate
document = regexprep(document, '\\begin{enumerate}', '');
document = regexprep(document, '\\item{} ', '1. ');
document = regexprep(document, '\\end{enumerate}', '');
% description
document = regexprep(document, '\\begin{description}', '');
document = regexprep(document, '\\item\[([^\]]*)\] ', '- **$1** ');
document = regexprep(document, '\\end{description}', '');
% figures
figures = regexp(document, '\\fig{(marginfigure|figure|figure\*)}\s*{([^{}]*)}\s*{[^{}]*\\includegraphics{([^{}]*)}[^{}]*}\s*{([^{}]*)}\s*{([^{}]*)}', 'tokens', 'all');
document = regexprep(document, '\\fig{(marginfigure|figure|figure\*)}\s*{([^{}]*)}\s*{[^{}]*\\includegraphics{([^{}]*)}[^{}]*}\s*{([^{}]*)}\s*{([^{}]*)}', '');
figs = regexp(document, '(%! ?FIG\d*) ?(\w*) ?!%', 'tokens', 'all');
assert(length(figures) == length(figs), 'The number of figures and %!FIGxx!% should be equal!')
for i = length(figures):-1:1
figure = figures{i};
figure_file = strtrim(figure{3});
figure_title = strtrim(figure{4});
figure_caption = strtrim(figure{5});
if figs{i}{2}
fig_img = ['<img src="' figure_file '" alt="' figure_title '" height="' figs{i}{2} '">'];
else
fig_img = ['<img src="' figure_file '" alt="' figure_title '">'];
end
document = regexprep(document, [figs{i}{1} '[ \w]*!%'], [fig_img newline() strrep([newline() '**Figure ' int2str(i) '. ' figure_title '**' newline() figure_caption], newline(), [newline() '> '])]);
figure_ref = strtrim(figure{2});
document = regexprep(document, ['\\Figref{' figure_ref '}'], ['Figure ' int2str(i)]);
document = regexprep(document, ['\\Figsref{' figure_ref '}'], ['Figures ' int2str(i)]);
end
% codes
document = regexprep(document, '\\expand{([^{}]*)}', 'This code modifies Code ¡!parentesi graffa aperta!¡$1¡!parentesi graffa chiusa!¡.'); % \epxand{cd:xxx} (1)
codes_start = regexp(document, '\\begin{lstlisting}', 'end', 'all');
codes_end = regexp(document, '\\end{lstlisting}', 'start', 'all');
assert(length(codes_start) == length(codes_end), 'The number of start and end codes should be equal!')
code_labels = {};
for i = length(codes_start):-1:1
code = regexp(document(codes_start(i) + 1:codes_end(i) - 1), '\[\s*label=([^,]*),\s*caption={\s*([^{}]*)\s*}\s*\]\s*(.*)', 'tokens', 'all');
code_labels{i} = strtrim(code{1}{1});
code_caption = strtrim(code{1}{2});
code_code = strtrim(regexprep(code{1}{3}, '\t', ' '));
code_notes = regexp(code_code, '¥\\circled{\d*}\\circlednote{(\d*)}{([^¥]*)}¥', 'tokens', 'all');
code_code = regexprep(code_code, '¥\\circled{(\d*)}[^¥]*¥', ' ¡!$1!¡');
code_notes_txt = '';
for j = 1:1:length(code_notes)
code_notes_txt = [code_notes_txt newline() '¡!' code_notes{j}{1} '!¡' code_notes{j}{2} newline()]; %#ok<AGROW>
end
code_notes_txt = regexprep(code_notes_txt, '\\circled{(\d*)}', '¡!$1!¡');
document = [document(1:codes_start(i) - length('\begin{lstlisting}')) ...
strrep([ newline() ...
'**Code ' int2str(i) '.** ' code_caption newline() ...
'````matlab' newline() code_code newline() '````' newline() ...
code_notes_txt ...
], newline(), [newline() '> ']) ...
document(codes_end(i) + length('\end{lstlisting}'):end)];
end
for i = 1:1:length(code_labels)
document = regexprep(document, ['\\Coderef{' code_labels{i} '}'], ['Code ' int2str(i)]);
document = regexprep(document, ['Code ¡!parentesi graffa aperta!¡' code_labels{i} '¡!parentesi graffa chiusa!¡'], ['Code ' int2str(i)]); % \epxand{cd:xxx} (2)
document = regexprep(document, ['\\Codesref{' code_labels{i} '}'], ['Codes ' int2str(i)]);
end
for i = 1:1:20
document = regexprep(document, ['¡!' int2str(i) '!¡'], char(9311 + i));
end
for i = 21:1:50
document = regexprep(document, ['¡!' int2str(i) '!¡'], char(12860 + i));
end
% colorboxes
cb_start = regexp(document, '\\begin{tcolorbox}', 'end', 'all');
cb_end = regexp(document, '\\end{tcolorbox}', 'start', 'all');
assert(length(cb_start) == length(cb_end), 'The number of start and end colorboxes should be equal!')
for i = length(cb_start):-1:1
cb = regexp(document(cb_start(i) + 1:cb_end(i) - 1), '\[\s*title=([^\[\]]*)\s*\]\s*(.*)', 'tokens', 'all');
cb_title = strtrim(cb{1}{1});
cb_content = strtrim(cb{1}{2});
document = [document(1:cb_start(i) - length('\begin{tcolorbox}')) ...
strrep([newline() '**' cb_title '**' newline() cb_content], newline(), [newline() '> ']) ...
document(cb_end(i) + length('\end{tcolorbox}'):end)];
end
% parentesi graffe (1)
document = regexprep(document, '¡!parentesi graffa aperta!¡', '{');
document = regexprep(document, '¡!parentesi graffa chiusa!¡', '}');
% includegraphics
document = regexprep(document, '\\includegraphics\[[^{}]*\]{([^{}]*)}', '');
%% Generate README file
readme = [
'# ' title newline() ...
newline() ...
'[](' tex_file_name '.pdf)' newline() ...
newline() ...
strtrim(document)
];
%% Save readme
writelines(readme, readme_file)