Example #1
0
        static void ScanDir(Options options, string path, CSharpSaver output)
        {
            string index = Path.Combine(path, "index.html");
            string[] files;
            if (File.Exists(index))
                files = new string[] { index };
            else
                files = Directory.GetFiles(path, "*.html", SearchOption.TopDirectoryOnly);

            foreach (string f in files)
            {
                Console.WriteLine("Parsing: " + f);

                HtmlData data;

                //Parse HTML ID and class and write minimized/obfuscated html at the same time
                string outputPath = null;
                if (options.OutputHTML != null)
                {
                    outputPath = Path.Combine(options.OutputHTML, f.Substring(options.WebRoot.Length + 1));
                    Directory.CreateDirectory(Path.GetDirectoryName(outputPath));

                    using (FileStream input = File.Open(f, FileMode.Open))
                    using (var writer = new HtmlFileWriter(outputPath))
                    {
                        ITagOutput tagout = writer;
                        tagout = new HtmlCompressor(tagout);
                        tagout = new HtmlObfuscator(ob, tagout);
                        var extract = new HtmlClassIdExtractor(tagout, f);
                        TagParser.Parse(input, extract);

                        data = extract.HtmlData;
                    }
                }
                else
                {
                    using (FileStream input = File.Open(f, FileMode.Open))
                    {
                        var hob = new HtmlObfuscator(ob, new NullOutput());
                        var extract = new HtmlClassIdExtractor(hob, f);
                        TagParser.Parse(input, extract);

                        data = extract.HtmlData;
                    }
                }

                //Get IDs and Classes
                //Extract global classes
                data.GetClasses(classes);
                data.GetIDs(ids);

                //Determine Namespace
                string dir = Path.GetDirectoryName(f);
                string ns = dir.Substring(options.WebRoot.Length);
                ns = ns.Replace("/", ".").Trim('.');

                if (ns != "" && data.FragmentName == "Index")
                {
                    //Move one namespace to classname
                    string[] parts = ns.Split('.');
                    ns = string.Join(".", parts, 0, parts.Length - 1);

                    data.FragmentName = parts[parts.Length - 1];
                }

                //Prepare
                SelectorBubbler.Bubble(data, options);

                data.FragmentName += options.FileSuffix;

                //Save
                output.WriteFragment(ns, data);
            }

            string[] dirs = Directory.GetDirectories(path);
            foreach (string d in dirs)
            {
                ScanDir(options, d, output);
            }
        }
Example #2
0
        public static Options Parse(string[] args)
        {
            var result = CommandLine.Parser.Default.ParseArguments<Options>(args);
            var options = result.Value;
            if (result.Errors.Any())
            {
                //Console.WriteLine(HelpText.AutoBuild<Options>(result));
                return null;
            }

            options.WebRoot = Path.GetFullPath(options.WebRoot).TrimEnd(Path.DirectorySeparatorChar);
            options.OutputCS = Path.GetFullPath(options.OutputCS);
            if(options.OutputHTML != null)
                options.OutputHTML = Path.GetFullPath(options.OutputHTML);
            Instance = options;
            return options;
        }