Пример #1
0
        public static void GenerateTemplate(string templateXmlFile, string destination, IEnumerable <string> imageNames, Dictionary <string, Dictionary <string, string> > replacementStrings, string title)
        {
            var xml = new XmlDocument();

            xml.Load(templateXmlFile);

            var templateNodes = TemplateXmlGenerator.Flatten(xml.ChildNodes).Where(
                n => n.LocalName == TemplateXmlGenerator.imageName && n.NamespaceURI == TemplateXmlGenerator.NamespaceURI).ToArray();

            foreach (var templateNode in templateNodes)
            {
                foreach (var imageName in imageNames)
                {
                    TemplateXmlGenerator.ImplementTemplate(xml, templateNode, imageName, replacementStrings[imageName]);
                }

                templateNode.ParentNode.RemoveChild(templateNode);
            }

            TemplateXmlGenerator.ImplementTitle(xml, title);

            var filename = Path.GetFileName(templateXmlFile);

            if (File.Exists(filename))
            {
                File.Delete(filename);
            }
            xml.Save(Path.Combine(destination, filename));
        }
Пример #2
0
        private static IEnumerable <XmlNode> Flatten(XmlNodeList nodes)
        {
            foreach (XmlNode node in nodes)
            {
                yield return(node);

                foreach (var subNode in TemplateXmlGenerator.Flatten(node.ChildNodes))
                {
                    yield return(subNode);
                }
            }
        }
Пример #3
0
        private static void ImplementTitle(XmlDocument xml, string title)
        {
            var titleNodes = TemplateXmlGenerator.Flatten(xml.ChildNodes).Where(
                n => n.LocalName == TemplateXmlGenerator.titleName && n.NamespaceURI == TemplateXmlGenerator.NamespaceURI).ToArray();

            foreach (var titleNode in titleNodes)
            {
                var newNode = xml.CreateTextNode(title);

                titleNode.ParentNode.InsertBefore(newNode, titleNode);
                titleNode.ParentNode.RemoveChild(titleNode);
            }
        }
Пример #4
0
        private static void ImplementTemplate(XmlDocument xml, XmlNode templateNode, string imageName, Dictionary <string, string> strings)
        {
            var newNode = templateNode.Clone();

            var childNodes = new List <XmlNode>();

            childNodes.AddRange(TemplateXmlGenerator.Flatten(newNode.ChildNodes));

            TemplateXmlGenerator.ReplaceText(strings, childNodes);

            newNode = xml.ImportNode(newNode, deep: true);
            childNodes.Clear();
            childNodes.AddRange(newNode.ChildNodes.Cast <XmlNode>());

            foreach (var childNode in childNodes)
            {
                templateNode.ParentNode.InsertBefore(childNode, templateNode);
            }
        }
Пример #5
0
        public static void GenerateDetails(string detailXmlFile, string destination, IEnumerable <string> imageNames, Dictionary <string, Dictionary <string, string> > replacementStrings, string title)
        {
            var xmlText = File.ReadAllText(detailXmlFile);

            foreach (var imageName in imageNames)
            {
                var xml = new XmlDocument();
                xml.LoadXml(xmlText);

                TemplateXmlGenerator.ReplaceText(replacementStrings[imageName], TemplateXmlGenerator.Flatten(xml.ChildNodes));

                TemplateXmlGenerator.ImplementTitle(xml, title);

                var destinationPath = Path.Combine(destination, imageName + ".xhtml");
                if (File.Exists(destinationPath))
                {
                    File.Delete(destinationPath);
                }
                xml.Save(destinationPath);
            }
        }
Пример #6
0
        public static void Main(string[] args)
        {
            try
            {
                var configFile = "config.xml";

                if (args.Length > 0)
                {
                    configFile = args[0];
                }

                configFile = Path.GetFullPath(configFile);

                var config = new Config(configFile);

                string path;
                if (args.Length > 1)
                {
                    path = Path.GetFullPath(args[1]);
                }
                else
                {
                    path = Environment.CurrentDirectory;
                }

                string destination;
                if (args.Length > 2)
                {
                    destination = Path.GetFullPath(args[2]);
                }
                else
                {
                    destination = Path.Combine(path, "generated");
                }

                if (false == Directory.Exists(destination))
                {
                    Directory.CreateDirectory(destination);
                }

                var title = "Image Gallery";
                if (args.Length > 3)
                {
                    title = args[3];
                }

                foreach (var size in config.Sizes)
                {
                    var sizePath = Path.Combine(destination, size.Name);
                    if (false == Directory.Exists(sizePath))
                    {
                        Directory.CreateDirectory(sizePath);
                    }
                }

                var imageFilesAndDates = MainClass.GetImageFilesAndDates(path);
                var imageNames         = imageFilesAndDates.Keys.OrderBy(n => imageFilesAndDates[n]).ToArray();

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

                foreach (var imageName in imageNames)
                {
                    var strings = new Dictionary <string, string>();
                    replacementStrings[imageName] = strings;

                    strings["name"] = imageName;

                    var date      = imageFilesAndDates[imageName];
                    var localDate = date.ToLocalTime();
                    strings["date_long"]             = localDate.ToLongDateString();
                    strings["time_long"]             = localDate.ToLongTimeString();
                    strings["date_short"]            = localDate.ToShortDateString();
                    strings["time_short"]            = localDate.ToShortTimeString();
                    strings["datetime_milliseconds"] = (date - MainClass.JavascriptEpoc).TotalMilliseconds.ToString("R");
                    strings["datetime_net"]          = date.ToString("o");
                    strings["datetime"] = date.ToString("r");
                }

                MainClass.actions = new Queue <Action>(Resizer.GenerateActions(config, path, imageNames, destination, replacementStrings));

                var threads = new List <Thread>();
                for (var ctr = 0; ctr < Environment.ProcessorCount - 1; ctr++)
                {
                    var thread = new Thread(MainClass.DoWork)
                    {
                        Name = "Thread " + ctr.ToString()
                    };
                    thread.Start();

                    threads.Add(thread);
                }

                MainClass.DoWork();

                foreach (var thread in threads)
                {
                    thread.Join();
                }

                var templatePath = Path.GetDirectoryName(configFile);
                foreach (var template in config.Templates)
                {
                    TemplateXmlGenerator.GenerateTemplate(Path.Combine(templatePath, template.File), destination, imageNames, replacementStrings, title);
                }

                foreach (var template in config.Details)
                {
                    TemplateXmlGenerator.GenerateDetails(Path.Combine(templatePath, template.File), destination, imageNames, replacementStrings, title);
                }

                foreach (var copyFile in config.CopyFiles)
                {
                    var sourceFileName = Path.Combine(templatePath, copyFile.File);
                    var destFileName   = Path.Combine(destination, copyFile.File);

                    if (File.Exists(destFileName))
                    {
                        File.Delete(destFileName);
                    }

                    File.Copy(sourceFileName, destFileName);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }