-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathExampleConverter.cs
More file actions
224 lines (198 loc) · 9.15 KB
/
ExampleConverter.cs
File metadata and controls
224 lines (198 loc) · 9.15 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CodeSnippetCutter
{
/// <summary>
/// Read snippets from source file
/// </summary>
class ExampleConverter
{
/// <summary>
/// Read a source file and find code snippets
/// Save the code snippets to the specified folder
/// </summary>
/// <param name="sourceFile">source file to read</param>
/// <param name="snippetsFolder">Folder where snippets will be saved</param>
/// <returns>Number of snippets created</returns>
public int ProcessExample(String sourceFile, String snippetsFolder)
{
int snippetsCreated = 0;
//Console.WriteLine("Processing " + sourceFile);
// Read all lines from the source file
String[] lines = File.ReadAllLines(sourceFile);
int lineNo = 1, startLineNo = 0, endLineNo = 0;
foreach (String line in lines)
{
// Start - Find the snippet
if (line.Contains(Common.ExStart) == true)
{
startLineNo = lineNo;
//Console.Write("Found ExStart at line " + startLineNo + " - ");
// Start - Find the ID of the snippet
int startID = line.IndexOf(Common.ExStart) + Common.ExStart.Length;
String snippetID = line.Substring(startID).Trim();
//Console.WriteLine("ID:" + snippetID);
// End - Find the snippet end in next lines
for (int iLines = startLineNo + 1; iLines < lines.Length; iLines++)
{
String strLine = lines[iLines];
if (strLine.Contains(Common.ExEnd) == true)
{
// End - Find the ID of the snippet
int endID = strLine.IndexOf(Common.ExEnd) + Common.ExEnd.Length;
String snippetEndID = strLine.Substring(endID).Trim();
if (snippetID == snippetEndID)
{
endLineNo = iLines + 1;
//Console.WriteLine("Found ExEnd at line " + endLineNo);
// Save the snippet
SaveSnippet(snippetID, startLineNo, endLineNo, lines, snippetsFolder, sourceFile);
snippetsCreated++;
}
}
}
}
lineNo++;
}
return snippetsCreated;
}
/// <summary>
/// Save the code snippet in a file
/// </summary>
/// <param name="snippetID">Snippet ID e.g. ExStart:SnippetID</param>
/// <param name="startLineNo">Start line number</param>
/// <param name="endLineNo">End line number</param>
/// <param name="lines">All lines of the source file</param>
/// <param name="snippetsFolder">Folder where the snippet will be saved</param>
/// <param name="sourceFile">Source file</param>
private void SaveSnippet(string snippetID, int startLineNo, int endLineNo, string[] lines,
string snippetsFolder, string sourceFile)
{
// Get the name of the snippet
String repoURL = "";
String snippetFile = snippetsFolder + Path.DirectorySeparatorChar +
GetSnippetFileName(sourceFile, snippetID, ref repoURL);
// Get the lines required by the snippet
List<String> lstLines = GetSnippetLines(startLineNo, endLineNo, lines);
// Remove white spaces at the start
lstLines = RemoveWhiteSpaces(lstLines);
// Add the GitHub repo URL if found
AddGitHubRepositoryURL(lstLines, repoURL, sourceFile);
File.WriteAllLines(snippetFile, lstLines);
//Console.WriteLine("Snippet file: " + snippetFile);
}
private void AddGitHubRepositoryURL(List<string> lstLines, string repoURL, string sourceFile)
{
if (repoURL.Trim().Length == 0)
return;
String comment = "";
// First line should be a comment, referring to the GitHub repository URL
// Detect the language from the source file extention
String ext = Path.GetExtension(sourceFile).ToLower();
switch(ext)
{
// All languages in which comment starts with //
case ".cs":
case ".java":
comment += "// ";
break;
// All languages in which comment starts with '
case ".vb":
comment += "' ";
break;
}
comment += "For complete examples and data files, please go to " + repoURL;
lstLines.Insert(0, comment);
}
private List<string> RemoveWhiteSpaces(List<string> lstLines)
{
// Count the white spaces in the first line
int spaceCount = lstLines[0].TakeWhile(Char.IsWhiteSpace).Count();
// Process all lines
for (int iLine = 0; iLine < lstLines.Count; iLine++ )
{
String line = lstLines[iLine];
if (line.Length <= spaceCount)
continue;
// If all of the first characters are spaces, remove them
if (line.Substring(0, spaceCount).Trim().Length == 0)
{
lstLines[iLine] = line.Remove(0, spaceCount);
}
else
{
lstLines[iLine] = line.TrimStart(' ');
}
}
return lstLines;
}
private List<string> GetSnippetLines(int startLineNo, int endLineNo, string[] lines)
{
// Create an empty list
List<String> lstLines = new List<string>();
for (int iLines = startLineNo - 1; iLines < endLineNo; iLines++)
{
// Do not add the line, if it contains ExStart or ExEnd
if (lines[iLines].Contains(Common.ExStart) || lines[iLines].Contains(Common.ExEnd))
continue;
lstLines.Add(lines[iLines]);
}
return lstLines;
}
private String GetSnippetFileName(String sourceFile, String snippetID, ref String repoURL)
{
DirectoryInfo dirInfo = new DirectoryInfo(Path.GetDirectoryName(sourceFile));
String snippetFile = Path.GetFileNameWithoutExtension(sourceFile) + "-" + snippetID + Path.GetExtension(sourceFile);
// Read parent folder until Examples or Plugins folder are found
while (dirInfo != null)
{
String dirName = dirInfo.Name;
// Break if we reach at the GitHub repository folder
// e.g. Aspose_{Product}_{Platform}
if (dirName.StartsWith("aspose_", StringComparison.CurrentCultureIgnoreCase))
{
if (dirName.EndsWith("_NET", StringComparison.CurrentCultureIgnoreCase) ||
dirName.EndsWith(".NET", StringComparison.CurrentCultureIgnoreCase) ||
dirName.EndsWith(".Java", StringComparison.CurrentCultureIgnoreCase) ||
dirName.EndsWith("_Java", StringComparison.CurrentCultureIgnoreCase) ||
dirName.EndsWith("_C", StringComparison.CurrentCultureIgnoreCase) ||
dirName.EndsWith("_Cloud", StringComparison.CurrentCultureIgnoreCase) ||
dirName.EndsWith("_Android", StringComparison.CurrentCultureIgnoreCase))
{
// Read the URL from .git/config file in main repository folder
repoURL = GetRepositoryURL(dirInfo);
break;
}
}
snippetFile = dirName + "-" + snippetFile;
// Go to parent directory
dirInfo = dirInfo.Parent;
//dirInfo = new DirectoryInfo(Path.GetDirectoryName(sourceFile)).Name;
}
return snippetFile;
}
private string GetRepositoryURL(DirectoryInfo dirInfo)
{
String url = "";
// Read the file repo/.git/config
String[] lines = File.ReadAllLines(dirInfo.FullName + Path.DirectorySeparatorChar +
".git" + Path.DirectorySeparatorChar + "config");
foreach(String line in lines)
{
if (line.Contains("url") && line.Contains("github.com/") && line.Contains(".git"))
{
string[] arrURL = line.Split('=');
if (arrURL[0].Trim() == "url" && arrURL[1].Trim().StartsWith("https://"))
{
url = arrURL[1].Trim().Replace(".git", "");
}
}
}
return url;
}
}
}