//private void Button_Click_1(object sender, RoutedEventArgs e) //{ // Microsoft.Win32.OpenFileDialog sfd = new OpenFileDialog(); // sfd.DefaultExt = ".css"; // sfd.Filter = "Css documents (.css)|*.css"; // // Show save file dialog box // Nullable<bool> result = sfd.ShowDialog(); // if (result.HasValue && result.Value) // FileNameTxt.Text = sfd.FileName; //} private void Button_Click_2(object sender, RoutedEventArgs e) { if (Model == null) Model = new CssOptimizerModel(); Task.Run(() => CssOptimizer.CssOptimizer.OptimizeSite(Model, _current)); }
public MainWindow() { InitializeComponent(); Model = new CssOptimizerModel(); DataContext = Model; CssOptimizer.CssOptimizer.OnTreatmentComplete += CssOptimizer_OnTreamentComplete; }
/// <summary> /// Générer Shared File /// </summary> /// <param name="model"></param> /// <param name="current"></param> public static void GenererSharedFile(CssOptimizerModel model, SynchronizationContext current = null) { if (_listDocument == null) throw new ArgumentNullException("_listDocument"); //using (StreamWriter writer = File.CreateText(model.PathGeneratedSharedFile)) // foreach (var doc in _listDocument) // { // StreamReader streamCss = new StreamReader(doc.Value); // var reglesCssCollection = Regex.Matches(streamCss.ReadToEnd(), @"(?<selector>(?:(?:[^,{]+),?)*?)(\{(?:(?<name>[^}:]+):?(?<value>[^};]+);?)*?\})"); // if (reglesCssCollection != null) // foreach (Match match in reglesCssCollection.Cast<Match>()) // { // string allUsedRules = string.Empty; // foreach (string rule in match.Groups[2].Value.Split(',')) // { // //il faut chercher l'occurence dans au moins un des autres fichiers // writer.WriteLine(match.Value); // } // } // } }
public static void OptimizeSite(CssOptimizerModel model, SynchronizationContext current = null) { _listDocument = new Dictionary<string, HtmlDocument>(); foreach (Url url in model.ListUrl) _listDocument.Add(url.FileName, new HtmlWeb().Load(url.Path)); foreach (var document in _listDocument) { Debug.WriteLine(string.Format("Start treatment url {0} at {1}", document.Key, DateTime.Now)); IEnumerable<HtmlNode> htmlNodeCollection = (document.Value.DocumentNode.SelectNodes("//link").Cast<HtmlNode>()).Where(x => x.Attributes["href"] != null && x.Attributes["href"].Value.EndsWith(".css")).ToList(); using (StreamWriter writer = File.CreateText(document.Key)) { Parallel.ForEach(htmlNodeCollection, (link, state, index) => { try { string filePath = link.Attributes["href"].Value; using (WebClient client = new WebClient()) { Debug.WriteLine(string.Format(" => Start a treatment of the file {0}", filePath.Split('/').Last())); MAJListCssFile(model, current, filePath); string content = client.DownloadString(StringHelper.GetValidUrl(model.ListUrl.FirstOrDefault(x => x.FileName.Equals(document.Key)).Path, filePath)); model.GlobalLength += content.Length; var reglesCssCollection = Regex.Matches(content, @"(?<selector>(?:(?:[^,{]+),?)*?)(\{(?:(?<name>[^}:]+):?(?<value>[^};]+);?)*?\})"); if (reglesCssCollection != null) { string res = string.Empty; foreach (Match match in reglesCssCollection.Cast<Match>()) { string tmp = TraiterRule(document.Value, match, _listDocument); if (!res.Contains(tmp)) res += tmp; } writer.WriteLine(res.Trim()); } } Debug.WriteLine(string.Format(" The end of Treatment of the file {0} at {1}", filePath.Split('/').Last(), DateTime.Now)); ReportProgress(model, current, htmlNodeCollection.Count()); } catch (Exception ex) { throw ex; } }); } if (model.IsOrgonizeFile) CssTidy(document.Key, model.CompressQuality); model.RatioOfCompression = (model.GlobalLength / new FileInfo(document.Key).Length) * 100; } //Raise the event if (OnTreatmentComplete != null) OnTreatmentComplete(model, null); Debug.WriteLine(string.Format("The end of Treatment : at {0}", DateTime.Now)); }
/// <summary> /// Faire Progresser le ProgressBar /// </summary> /// <param name="currentIndex"></param> /// <param name="maxRecords"></param> private static void ReportProgress(CssOptimizerModel model, SynchronizationContext current, int maxRecords) { if (current != null) current.Send((x) => { model.ProgessBarEtat += Convert.ToInt32((1 / (decimal)maxRecords) * 100); }, null); }
/// <summary> /// MAJ List Css File /// </summary>heni82 /// <param name="model"></param> /// <param name="current">SynchronizationContext</param> /// <param name="filePath"></param> private static void MAJListCssFile(CssOptimizerModel model, SynchronizationContext current, string filePath) { if (current != null) current.Send((x) => { if (model.ListCssFiles == null) model.ListCssFiles = new ObservableCollection<FileInformation>(); model.ListCssFiles.Add(new FileInformation { Name = filePath.Split('/').Last() }); }, null); else { if (model.ListCssFiles == null) model.ListCssFiles = new ObservableCollection<FileInformation>(); model.ListCssFiles.Add(new FileInformation { Name = filePath.Split('/').Last() }); } }