private static void AddSamples(DirectoryInfo catDir, DirectoryInfo subCatDir, SampleCategory sampleList, List <string> sampleNameList, List <string> existingImgNames, FileInfo[] screenshots)
        {
            FileInfo[] files;

            if (subCatDir != null)
            {
                files = subCatDir.GetFiles("*.html");
            }
            else
            {
                files = catDir.GetFiles("*.html");
            }

            if (files.Length > 0)
            {
                string path, sourcePath, fileName;

                var screenshotNameLookup = new Dictionary <string, string>();

                foreach (var ss in screenshots)
                {
                    screenshotNameLookup.Add(ss.Name.Replace(ss.Extension, ""), ss.Name);
                }

                foreach (var f in files)
                {
                    if (!f.Name.Contains("- Private"))
                    {
                        if (subCatDir != null)
                        {
                            path       = catDir.Name + "/" + subCatDir.Name + "/" + f.Name.ToString();
                            sourcePath = catDir.Name + "/" + subCatDir.Name;
                        }
                        else
                        {
                            path       = catDir.Name + "/" + f.Name.ToString();
                            sourcePath = path;
                        }

                        path       = path.Replace(" ", "%20");
                        sourcePath = sourcePath.Replace(" ", "%20");

                        fileName = f.Name.Replace(".html", "");

                        var sample = new SampleInfo()
                        {
                            Path       = path,
                            SourcePath = sourcePath,
                            Created    = f.CreationTime.Date.ToShortDateString()
                        };

                        using (var reader = new StreamReader(f.OpenRead()))
                        {
                            var doc = reader.ReadToEnd();

                            int idx = 0;
                            idx = doc.IndexOf("<title>");

                            if (idx >= 0)
                            {
                                sample.Title = doc.Substring(idx + 7, doc.IndexOf(" - Azure Maps Web SDK Samples</title>", idx) - idx - 7);

                                if (!string.IsNullOrWhiteSpace(sample.Title))
                                {
                                    sample.Title = sample.Title.Replace(" - Azure Maps Web SDK Samples", "");
                                }
                            }

                            if (string.IsNullOrWhiteSpace(sample.Title))
                            {
                                idx = doc.IndexOf("<h1 ");

                                if (idx > 0)
                                {
                                    idx          = doc.IndexOf(">", idx);
                                    sample.Title = doc.Substring(idx + 1, doc.IndexOf("</h1>", idx) - idx - 1);

                                    if (sample.Title.EndsWith(" sample", StringComparison.OrdinalIgnoreCase))
                                    {
                                        sample.Title = sample.Title.Replace(" Sample", "").Replace(" sample", "");
                                    }
                                }
                            }

                            if (string.IsNullOrWhiteSpace(sample.Title))
                            {
                                sample.Title = fileName;
                            }

                            idx = doc.IndexOf("<meta name=\"description\" content=\"");

                            if (idx >= 0)
                            {
                                sample.Description = doc.Substring(idx + 34, doc.IndexOf("/>", idx) - idx - 34);

                                if (!string.IsNullOrWhiteSpace(sample.Description))
                                {
                                    sample.Description = sample.Description.Trim();
                                    if (sample.Description.EndsWith("\""))
                                    {
                                        sample.Description = sample.Description.Remove(sample.Description.LastIndexOf("\""), 1);
                                    }
                                }
                            }

                            idx = doc.IndexOf("<meta name=\"keywords\" content=\"");

                            if (idx >= 0)
                            {
                                sample.Keywords = doc.Substring(idx + 31, doc.IndexOf("/>", idx) - idx - 31);

                                if (!string.IsNullOrWhiteSpace(sample.Keywords))
                                {
                                    sample.Keywords = sample.Keywords.Trim().ToLowerInvariant();
                                    if (sample.Keywords.EndsWith("\""))
                                    {
                                        sample.Keywords = sample.Keywords.Remove(sample.Keywords.LastIndexOf("\""), 1);
                                    }
                                }
                            }
                        }

                        if (!sampleNameList.Contains(sample.Title))
                        {
                            var temp = sample.Title;

                            if (screenshotNameLookup.ContainsKey(temp))
                            {
                                sample.Screenshot = screenshotNameLookup[temp];
                            }

                            if (String.IsNullOrEmpty(sample.Screenshot))
                            {
                                temp = sample.Title.Replace(" ", "-");

                                if (screenshotNameLookup.ContainsKey(temp))
                                {
                                    sample.Screenshot = screenshotNameLookup[temp];
                                }
                            }

                            if (String.IsNullOrEmpty(sample.Screenshot) || !sample.Screenshot.Contains("."))
                            {
                                temp = fileName;

                                if (screenshotNameLookup.ContainsKey(temp))
                                {
                                    sample.Screenshot = screenshotNameLookup[temp];
                                }

                                if (String.IsNullOrEmpty(sample.Screenshot))
                                {
                                    temp = fileName.Replace(" ", "-");

                                    if (screenshotNameLookup.ContainsKey(temp))
                                    {
                                        sample.Screenshot = screenshotNameLookup[temp];
                                    }
                                }
                            }

                            if (String.IsNullOrEmpty(sample.Screenshot) || !sample.Screenshot.Contains("."))
                            {
                                sample.Screenshot = string.Empty;
                            }

                            sampleList.Samples.Add(sample);

                            NumberOfSamples++;

                            Console.WriteLine(sample.Title);
                        }
                        else
                        {
                            Console.WriteLine("Error: File name used multiple times: " + fileName);
                        }
                    }
                }
            }

            if (subCatDir == null)
            {
                var subDirs = catDir.GetDirectories();

                foreach (var subDir in subDirs)
                {
                    if (SampleListHelper.ScanFolder(subDir.Name))
                    {
                        AddSamples(catDir, subDir, sampleList, sampleNameList, existingImgNames, screenshots);
                    }
                }
            }

            //if (sampleList[sampleList.Length - 1] == ',')
            //{
            //    sampleList.Length--;
            //}
        }
示例#2
0
        private static void AddSamples(DirectoryInfo catDir, DirectoryInfo subCatDir, SampleCategory sampleList, List <string> sampleNameList, List <string> existingImgNames, FileInfo[] screenshots)
        {
            FileInfo[] files;

            if (subCatDir != null)
            {
                files = subCatDir.GetFiles("*.html");
            }
            else
            {
                files = catDir.GetFiles("*.html");
            }

            if (files.Length > 0)
            {
                string path, sourcePath, fileName;

                foreach (var f in files)
                {
                    if (!f.Name.Contains("- Private"))
                    {
                        if (subCatDir != null)
                        {
                            path       = catDir.Name + "/" + subCatDir.Name + "/" + f.Name.ToString();
                            sourcePath = catDir.Name + "/" + subCatDir.Name;
                        }
                        else
                        {
                            path       = catDir.Name + "/" + f.Name.ToString();
                            sourcePath = path;
                        }

                        path       = path.Replace(" ", "%20");
                        sourcePath = sourcePath.Replace(" ", "%20");

                        fileName = f.Name.Replace(".html", "");

                        var sample = new SampleInfo()
                        {
                            Path       = path,
                            SourcePath = sourcePath,
                            Created    = f.CreationTime.Date.ToShortDateString()
                        };

                        using (var reader = new StreamReader(f.OpenRead()))
                        {
                            var doc = reader.ReadToEnd();

                            int idx = 0;
                            idx = doc.IndexOf("<title>");

                            if (idx >= 0)
                            {
                                sample.Title = doc.Substring(idx + 7, doc.IndexOf("</title>", idx) - idx - 7);

                                if (!string.IsNullOrWhiteSpace(sample.Title))
                                {
                                    sample.Title = sample.Title.Replace("Azure Maps Web Control Samples - ", "");
                                }
                            }

                            if (string.IsNullOrWhiteSpace(sample.Title))
                            {
                                idx = doc.IndexOf("<label>");

                                if (idx > 0)
                                {
                                    sample.Title = doc.Substring(idx + 7, doc.IndexOf("</label>", idx) - idx - 7);

                                    if (sample.Title.EndsWith(" sample", StringComparison.OrdinalIgnoreCase))
                                    {
                                        sample.Title = sample.Title.Replace(" Sample", "").Replace(" sample", "");
                                    }
                                }
                            }

                            if (string.IsNullOrWhiteSpace(sample.Title))
                            {
                                sample.Title = fileName;
                            }

                            idx = doc.IndexOf("<meta name=\"description\" content=\"");

                            if (idx >= 0)
                            {
                                sample.Description = doc.Substring(idx + 34, doc.IndexOf("/>", idx) - idx - 34);

                                if (!string.IsNullOrWhiteSpace(sample.Description))
                                {
                                    sample.Description = sample.Description.Trim();
                                    if (sample.Description.EndsWith("\""))
                                    {
                                        sample.Description = sample.Description.Remove(sample.Description.LastIndexOf("\""), 1);
                                    }
                                }
                            }

                            idx = doc.IndexOf("<meta name=\"keywords\" content=\"");

                            if (idx >= 0)
                            {
                                sample.Keywords = doc.Substring(idx + 31, doc.IndexOf("/>", idx) - idx - 31);

                                if (!string.IsNullOrWhiteSpace(sample.Keywords))
                                {
                                    sample.Keywords = sample.Keywords.Trim().ToLowerInvariant();
                                    if (sample.Keywords.EndsWith("\""))
                                    {
                                        sample.Keywords = sample.Keywords.Remove(sample.Keywords.LastIndexOf("\""), 1);
                                    }
                                }
                            }
                        }

                        if (!sampleNameList.Contains(sample.Title))
                        {
                            sample.Screenshot = sample.Title.Replace(" ", "-");

                            foreach (var ss in screenshots)
                            {
                                if (string.Compare(ss.Name.Replace(ss.Extension, ""), sample.Screenshot) == 0)
                                {
                                    sample.Screenshot = ss.Name;
                                    break;
                                }
                            }

                            if (!sample.Screenshot.Contains("."))
                            {
                                sample.Screenshot = fileName.Replace(" ", "-");

                                foreach (var ss in screenshots)
                                {
                                    if (string.Compare(ss.Name.Replace(ss.Extension, ""), sample.Screenshot) == 0)
                                    {
                                        sample.Screenshot = ss.Name;
                                        break;
                                    }
                                }
                            }

                            if (!sample.Screenshot.Contains("."))
                            {
                                sample.Screenshot = string.Empty;
                            }

                            //sampleList.AppendFormat("\n\t\t{{\n\t\t\ttitle:'{0}',\n\t\t\tdesc:'{1}',\n\t\t\tpath:'{2}',\n\t\t\tsourcePath:'{3}',\n\t\t\tscreenshot:'{4}',\n\t\t\tkeywords:'{5}',\n\t\t\tcreated:'{6}'\n\t\t}},",
                            //    title.Replace("'", "\\'"),
                            //    description.Replace("'", "\\'"),
                            //    path,
                            //    sourcePath,
                            //    imageSrc,
                            //    keywords,
                            //    created
                            //);

                            sampleList.Samples.Add(sample);

                            NumberOfSamples++;

                            Console.WriteLine(sample.Title + "\n");
                        }
                        else
                        {
                            Console.WriteLine("Error: File name used multiple times: " + fileName);
                        }
                    }
                }
            }

            if (subCatDir == null)
            {
                var subDirs = catDir.GetDirectories();

                foreach (var subDir in subDirs)
                {
                    if (SampleListHelper.ScanFolder(subDir.Name))
                    {
                        AddSamples(catDir, subDir, sampleList, sampleNameList, existingImgNames, screenshots);
                    }
                }
            }

            //if (sampleList[sampleList.Length - 1] == ',')
            //{
            //    sampleList.Length--;
            //}
        }