コード例 #1
0
ファイル: InlineAnalyzer.cs プロジェクト: xhute/Kooboo
        private Dictionary <string, string> ReplaceInlineCssUrl(Kooboo.Dom.Element element, AnalyzerContext Context)
        {
            string csstext = element.getAttribute("style");

            if (string.IsNullOrEmpty(csstext))
            {
                return(null);
            }

            Dictionary <string, string> replace = new Dictionary <string, string>();

            var urlInfos = Service.CssService.GetUrlInfos(csstext);

            foreach (var item in urlInfos)
            {
                if (string.IsNullOrEmpty(item.PureUrl) || item.PureUrl.Trim().ToLower().StartsWith("#"))
                {
                    continue;
                }

                string newurl = string.Empty;
                if (item.isImportRule)
                {
                    newurl = CssManager.AddImport(item.PureUrl, Context.AbsoluteUrl, Context.DownloadManager, Context.ObjectId);
                }
                else
                {
                    if (Kooboo.Lib.Utilities.DataUriService.isDataUri(item.PureUrl))
                    {
                        newurl = CssManager.ParseDataUri(item.PureUrl, Context.DownloadManager);
                    }
                    else
                    {
                        newurl = CssManager.DownloadCssFile(item.PureUrl, Context.AbsoluteUrl, Context.DownloadManager, Context.ObjectId);
                    }
                }

                if (newurl != item.PureUrl)
                {
                    replace.Add(item.PureUrl, newurl);
                }
            }

            return(replace);
        }
コード例 #2
0
        /// <summary>
        /// process the embedded style.
        /// </summary>
        /// <param name="context"></param>
        private void ProcessInPage(AnalyzerContext context)
        {
            HTMLCollection embedStyle = context.Dom.getElementsByTagName("style");

            //int itemindexcounter = 0;
            foreach (var item in embedStyle.item)
            {
                string csstext = item.InnerHtml;

                if (string.IsNullOrEmpty(csstext))
                {
                    continue;
                }
                //var style = new Style
                //{
                //    IsEmbedded = true,
                //    OwnerObjectId = context.ObjectId,
                //    OwnerConstType = context.ObjectType,
                //    ItemIndex = itemindexcounter,
                //    Name = UrlHelper.FileName(context.AbsoluteUrl)
                //};

                CssManager.ProcessResource(ref csstext, context.AbsoluteUrl, context.DownloadManager, context.ObjectId);

                //style.Body = csstext;
                //context.SiteDb.Styles.AddOrUpdate(style, context.DownloadManager.UserId);
                //itemindexcounter += 1;

                if (item.InnerHtml != csstext)
                {
                    var change = new AnalyzerUpdate()
                    {
                        StartIndex = item.location.openTokenEndIndex + 1,
                        EndIndex   = item.location.endTokenStartIndex - 1,
                        NewValue   = csstext
                    };

                    if (change.EndIndex > change.StartIndex)
                    {
                        context.Changes.Add(change);
                    }
                }
            }
        }
コード例 #3
0
        public static SiteObject AddDownload(DownloadManager manager, DownloadContent download, string absoluteUrl, bool DefaultStartPage, bool AnalyzePage, string originalimporturl = "")
        {
            var sitedb = manager.SiteDb;

            bool issamehost = UrlHelper.isSameHost(originalimporturl, absoluteUrl);

            string relativeurl = UrlHelper.RelativePath(absoluteUrl, issamehost);

            byte consttype = 0;

            var FileType = Kooboo.Lib.Helper.UrlHelper.GetFileType(relativeurl, download.ContentType);

            switch (FileType)
            {
            case UrlHelper.UrlFileType.Image:
            {
                consttype = ConstObjectType.Image;
            }
            break;

            case UrlHelper.UrlFileType.JavaScript:
            {
                consttype = ConstObjectType.Script;
            }
            break;

            case UrlHelper.UrlFileType.Style:
            {
                consttype = ConstObjectType.Style;
            }
            break;

            case UrlHelper.UrlFileType.File:
            {
                consttype = ConstObjectType.File;
            }
            break;

            default:
            {
                string htmlsource = download.GetString();

                if (string.IsNullOrEmpty(htmlsource) || Kooboo.HtmlUtility.HasHtmlOrBodyTag(htmlsource))
                {
                    consttype = ConstObjectType.Page;
                }
                else
                {
                    consttype = ConstObjectType.View;
                }
            }
            break;
            }

            switch (consttype)
            {
            case ConstObjectType.Page:
            {
                Page page = new Page();
                page.IsStatic     = true;
                page.DefaultStart = DefaultStartPage;

                page.Name = Service.PageService.GetPageNameFromUrl(relativeurl);

                string htmlsource = UrlHelper.ReplaceMetaCharSet(download.GetString());

                if (AnalyzePage)
                {
                    var context = AnalyzerManager.Execute(htmlsource, absoluteUrl, page.Id, page.ConstType, manager, originalimporturl);
                    htmlsource = context.HtmlSource;
                }
                page.Body = htmlsource;

                if (page.Name == "untitled")
                {
                    var titleel = Service.DomService.GetTitleElement(page.Dom);
                    if (titleel != null)
                    {
                        string title = titleel.InnerHtml;
                        if (!string.IsNullOrEmpty(title))
                        {
                            title     = Lib.Helper.StringHelper.SementicSubString(title, 0, 30);
                            page.Name = title.Trim();
                        }
                    }
                }

                sitedb.Routes.AddOrUpdate(relativeurl, page, manager.UserId);
                sitedb.Pages.AddOrUpdate(page, manager.UserId);

                return(page);
            }

            case ConstObjectType.Style:
            {
                string csstext = download.GetString();
                if (AnalyzePage)
                {
                    CssManager.ProcessResource(ref csstext, absoluteUrl, manager, default(Guid));
                }
                var style = new Style();
                style.Body = csstext;
                sitedb.Routes.AddOrUpdate(relativeurl, style, manager.UserId);
                sitedb.Styles.AddOrUpdate(style, manager.UserId);
                return(style);
            }

            case ConstObjectType.Script:
            {
                Script script = new Script {
                    ConstType = ConstObjectType.Script, Body = download.GetString()
                };

                sitedb.Routes.AddOrUpdate(relativeurl, script, manager.UserId);
                sitedb.Scripts.AddOrUpdate(script, manager.UserId);
                return(script);
            }

            case ConstObjectType.Image:
            {
                Image koobooimage = new Image
                {
                    ContentBytes = download.DataBytes,
                    Extension    = UrlHelper.FileExtension(relativeurl),
                    Name         = UrlHelper.FileName(relativeurl),
                };


                sitedb.Routes.AddOrUpdate(relativeurl, koobooimage, manager.UserId);
                sitedb.Images.AddOrUpdate(koobooimage, manager.UserId);
                return(koobooimage);
            }

            case ConstObjectType.View:
            {
                var view = new View();

                string name = System.IO.Path.GetFileNameWithoutExtension(absoluteUrl);
                if (string.IsNullOrEmpty(name))
                {
                    name = "untitlted";
                }
                view.Name = name;

                string htmlsource = UrlHelper.ReplaceMetaCharSet(download.GetString());
                if (AnalyzePage)
                {
                    var context = AnalyzerManager.Execute(htmlsource, absoluteUrl, view.Id, view.ConstType, manager, originalimporturl);
                    htmlsource = context.HtmlSource;
                }

                view.Body = htmlsource;

                sitedb.Routes.AddOrUpdate(relativeurl, view, manager.UserId);
                sitedb.Views.AddOrUpdate(view, manager.UserId);
                return(view);
            }

            default:
            {
                /// default treat it as file.
                var kooboofile = new CmsFile
                {
                    ContentType   = download.ContentType,
                    ContentBytes  = download.DataBytes,
                    ContentString = download.ContentString,
                    Extension     = UrlHelper.FileExtension(relativeurl),
                    Name          = UrlHelper.FileName(relativeurl)
                };
                sitedb.Routes.AddOrUpdate(relativeurl, kooboofile, manager.UserId);
                sitedb.Files.AddOrUpdate(kooboofile, manager.UserId);
                return(kooboofile);
            }
            }
        }