Beispiel #1
0
        private static async Task <string> ParseImplAsync(IParseManager parseManager, string type, int no, string src, bool isFileName, bool isFileType, bool isFileSize, bool isCount, bool isLower, bool isUpper, string leftText, string rightText, NameValueCollection attributes)
        {
            var pageInfo    = parseManager.PageInfo;
            var contextInfo = parseManager.ContextInfo;

            if (!string.IsNullOrEmpty(contextInfo.InnerHtml))
            {
                var innerBuilder = new StringBuilder(contextInfo.InnerHtml);
                await parseManager.ParseInnerContentAsync(innerBuilder);

                contextInfo.InnerHtml = innerBuilder.ToString();
            }

            var contentInfo = await parseManager.GetContentAsync();

            var fileUrl = string.Empty;

            if (!string.IsNullOrEmpty(src))
            {
                fileUrl = src;
            }
            else
            {
                if (contextInfo.ContextType == ParseType.Undefined)
                {
                    contextInfo.ContextType = ParseType.Content;
                }
                if (contextInfo.ContextType == ParseType.Content)
                {
                    if (contextInfo.ContentId != 0)
                    {
                        if (!string.IsNullOrEmpty(contentInfo?.Get <string>(type)))
                        {
                            if (no <= 1)
                            {
                                fileUrl = contentInfo.Get <string>(type);
                            }
                            else
                            {
                                var extendName = ColumnsManager.GetExtendName(type, no - 1);
                                fileUrl = contentInfo.Get <string>(extendName);
                            }
                        }
                    }
                }
                else if (contextInfo.ContextType == ParseType.Each)
                {
                    fileUrl = contextInfo.ItemContainer.EachItem.Value as string;
                }
            }

            string parsedContent;

            if (isFileName)
            {
                parsedContent = PathUtils.RemoveExtension(PageUtils.GetFileNameFromUrl(fileUrl));
                if (isLower)
                {
                    parsedContent = StringUtils.ToLower(parsedContent);
                }
                if (isUpper)
                {
                    parsedContent = StringUtils.ToUpper(parsedContent);
                }
            }
            else if (isFileType)
            {
                var filePath = await parseManager.PathManager.ParseSitePathAsync(pageInfo.Site, fileUrl);

                parsedContent = PathUtils.GetExtension(filePath).Trim('.');
                if (isLower)
                {
                    parsedContent = StringUtils.ToLower(parsedContent);
                }
                if (isUpper)
                {
                    parsedContent = StringUtils.ToUpper(parsedContent);
                }
            }
            else if (isFileSize)
            {
                var filePath = await parseManager.PathManager.ParseSitePathAsync(pageInfo.Site, fileUrl);

                parsedContent = FileUtils.GetFileSizeByFilePath(filePath);
            }
            else if (isCount)
            {
                parsedContent = (contentInfo?.Downloads ?? 0).ToString();
            }
            else
            {
                var inputParser = new InputParserManager(parseManager.PathManager);

                parsedContent = contentInfo != null
                    ? inputParser.GetFileHtmlWithCount(pageInfo.Site, contentInfo.ChannelId,
                                                       contentInfo.Id, fileUrl, attributes, contextInfo.InnerHtml,
                                                       contextInfo.IsStlEntity, isLower, isUpper)
                    : inputParser.GetFileHtmlWithoutCount(pageInfo.Site, fileUrl, attributes,
                                                          contextInfo.InnerHtml, contextInfo.IsStlEntity, isLower, isUpper);
            }

            if (!string.IsNullOrEmpty(parsedContent))
            {
                parsedContent = leftText + parsedContent + rightText;
            }

            return(parsedContent);
        }
Beispiel #2
0
        private static async Task <string> ParseImplAsync(IParseManager parseManager, string leftText, string rightText, string formatString, string no, string separator, int startIndex, int length, int wordNum, string ellipsis, string replace, string to, bool isClearTags, string isReturnToBrStr, bool isLower, bool isUpper, bool isOriginal, string type, Content content, int contentId)
        {
            var pageInfo        = parseManager.PageInfo;
            var contextInfo     = parseManager.ContextInfo;
            var databaseManager = parseManager.DatabaseManager;

            if (content == null)
            {
                return(string.Empty);
            }

            var parsedContent = string.Empty;

            if (string.IsNullOrEmpty(type))
            {
                type = nameof(Content.Title);
            }

            var isReturnToBr = false;

            if (string.IsNullOrEmpty(isReturnToBrStr))
            {
                if (StringUtils.EqualsIgnoreCase(type, nameof(Content.Summary)))
                {
                    isReturnToBr = true;
                }
            }
            else
            {
                isReturnToBr = TranslateUtils.ToBool(isReturnToBrStr, true);
            }

            if (isOriginal)
            {
                if (content.ReferenceId > 0 && content.SourceId > 0 && TranslateType.Reference.GetValue() == content.Get <string>(ColumnsManager.TranslateContentType))
                {
                    var targetChannelId = content.SourceId;
                    //var targetSiteId = databaseManager.ChannelRepository.GetSiteId(targetChannelId);
                    var targetSiteId = await databaseManager.ChannelRepository.GetSiteIdAsync(targetChannelId);

                    var targetSite = await databaseManager.SiteRepository.GetAsync(targetSiteId);

                    var targetNodeInfo = await databaseManager.ChannelRepository.GetAsync(targetChannelId);

                    //var targetContentInfo = databaseManager.ContentRepository.GetContentInfo(tableStyle, tableName, content.ReferenceId);
                    var targetContentInfo = await databaseManager.ContentRepository.GetAsync(targetSite, targetNodeInfo, content.ReferenceId);

                    if (targetContentInfo != null && targetContentInfo.ChannelId > 0)
                    {
                        //标题可以使用自己的
                        targetContentInfo.Title = content.Title;
                        content = targetContentInfo;
                    }
                }
            }

            if (!string.IsNullOrEmpty(formatString))
            {
                formatString = formatString.Trim();
                if (!formatString.StartsWith("{0"))
                {
                    formatString = "{0:" + formatString;
                }
                if (!formatString.EndsWith("}"))
                {
                    formatString = formatString + "}";
                }
            }

            if (contentId != 0)
            {
                if (StringUtils.EqualsIgnoreCase(type, nameof(Content.Title)))
                {
                    var channel = await databaseManager.ChannelRepository.GetAsync(content.ChannelId);

                    var relatedIdentities = databaseManager.TableStyleRepository.GetRelatedIdentities(channel);
                    var tableName         = databaseManager.ChannelRepository.GetTableName(pageInfo.Site, channel);

                    var styleInfo = await databaseManager.TableStyleRepository.GetTableStyleAsync(tableName, type, relatedIdentities);

                    var inputParser = new InputParserManager(parseManager.PathManager);
                    parsedContent = await inputParser.GetContentByTableStyleAsync(content.Title, separator, pageInfo.Site, styleInfo, formatString, contextInfo.Attributes, contextInfo.InnerHtml, false);

                    parsedContent = InputTypeUtils.ParseString(styleInfo.InputType, parsedContent, replace, to, startIndex, length, wordNum, ellipsis, isClearTags, isReturnToBr, isLower, isUpper, formatString);

                    if (!isClearTags && !string.IsNullOrEmpty(content.Get <string>(ColumnsManager.GetFormatStringAttributeName(nameof(Content.Title)))))
                    {
                        parsedContent = ContentUtility.FormatTitle(content.Get <string>(ColumnsManager.GetFormatStringAttributeName(nameof(Content.Title))), parsedContent);
                    }

                    if (pageInfo.Site.IsContentTitleBreakLine)
                    {
                        parsedContent = parsedContent.Replace("  ", !contextInfo.IsInnerElement ? "<br />" : string.Empty);
                    }
                }
                else if (StringUtils.EqualsIgnoreCase(type, nameof(Content.SubTitle)))
                {
                    parsedContent = InputTypeUtils.ParseString(InputType.Text, content.SubTitle, replace, to, startIndex, length, wordNum, ellipsis, isClearTags, isReturnToBr, isLower, isUpper, formatString);
                    if (pageInfo.Site.IsContentSubTitleBreakLine)
                    {
                        parsedContent = parsedContent.Replace("  ", !contextInfo.IsInnerElement ? "<br />" : string.Empty);
                    }
                }
                else if (StringUtils.EqualsIgnoreCase(type, nameof(Content.Summary)))
                {
                    parsedContent = InputTypeUtils.ParseString(InputType.TextArea, content.Summary, replace, to, startIndex, length, wordNum, ellipsis, isClearTags, isReturnToBr, isLower, isUpper, formatString);
                }
                else if (StringUtils.EqualsIgnoreCase(type, nameof(Content.Body)) || StringUtils.EqualsIgnoreCase(type, nameof(Content)))
                {
                    parsedContent = await parseManager.PathManager.DecodeTextEditorAsync(pageInfo.Site, content.Body, pageInfo.IsLocal);

                    if (isClearTags)
                    {
                        parsedContent = StringUtils.StripTags(parsedContent);
                    }

                    if (!string.IsNullOrEmpty(replace))
                    {
                        parsedContent = StringUtils.Replace(parsedContent, replace, to);
                    }

                    if (wordNum > 0 && !string.IsNullOrEmpty(parsedContent))
                    {
                        parsedContent = StringUtils.MaxLengthText(parsedContent, wordNum, ellipsis);
                    }

                    if (!string.IsNullOrEmpty(formatString))
                    {
                        parsedContent = string.Format(formatString, parsedContent);
                    }

                    parsedContent = await EditorUtility.ParseAsync(pageInfo, parsedContent);
                }
                else if (StringUtils.EqualsIgnoreCase(type, nameof(ColumnsManager.PageContent)))
                {
                    //if (contextInfo.IsInnerElement)
                    // {
                    parsedContent = await parseManager.PathManager.DecodeTextEditorAsync(pageInfo.Site, content.Body, pageInfo.IsLocal);

                    if (isClearTags)
                    {
                        parsedContent = StringUtils.StripTags(parsedContent);
                    }

                    if (!string.IsNullOrEmpty(replace))
                    {
                        parsedContent = StringUtils.Replace(parsedContent, replace, to);
                    }

                    if (wordNum > 0 && !string.IsNullOrEmpty(parsedContent))
                    {
                        parsedContent = StringUtils.MaxLengthText(parsedContent, wordNum, ellipsis);
                    }

                    if (!string.IsNullOrEmpty(formatString))
                    {
                        parsedContent = string.Format(formatString, parsedContent);
                    }

                    parsedContent = await EditorUtility.ParseAsync(pageInfo, parsedContent);
                }
                else if (StringUtils.EqualsIgnoreCase(type, nameof(Content.AddDate)))
                {
                    parsedContent = DateUtils.Format(content.AddDate, formatString);
                }
                else if (StringUtils.EqualsIgnoreCase(type, nameof(Content.LastModifiedDate)))
                {
                    parsedContent = DateUtils.Format(content.LastModifiedDate, formatString);
                }
                else if (StringUtils.EqualsIgnoreCase(type, nameof(Content.LastHitsDate)))
                {
                    parsedContent = DateUtils.Format(content.LastHitsDate, formatString);
                }
                else if (StringUtils.EqualsIgnoreCase(type, nameof(Content.ImageUrl)))
                {
                    var inputParser = new InputParserManager(parseManager.PathManager);

                    if (no == "all")
                    {
                        var sbParsedContent = new StringBuilder();
                        //第一条
                        sbParsedContent.Append(contextInfo.IsStlEntity
                            ? await parseManager.PathManager.ParseSiteUrlAsync(pageInfo.Site,
                                                                               content.ImageUrl, pageInfo.IsLocal)
                            : await inputParser.GetImageOrFlashHtmlAsync(pageInfo.Site,
                                                                         content.ImageUrl,
                                                                         contextInfo.Attributes, false));

                        //第n条
                        var countName = ColumnsManager.GetCountName(nameof(Content.ImageUrl));
                        var count     = content.Get <int>(countName);
                        for (var i = 1; i <= count; i++)
                        {
                            var extendName = ColumnsManager.GetExtendName(nameof(Content.ImageUrl), i);
                            var extend     = content.Get <string>(extendName);

                            sbParsedContent.Append(contextInfo.IsStlEntity
                                ? await parseManager.PathManager.ParseSiteUrlAsync(pageInfo.Site, extend,
                                                                                   pageInfo.IsLocal)
                                : await inputParser.GetImageOrFlashHtmlAsync(pageInfo.Site, extend,
                                                                             contextInfo.Attributes, false));
                        }

                        //var extendAttributeName = ColumnsManager.GetExtendAttributeName(nameof(Content.ImageUrl));
                        //var extendValues = content.Get<string>(extendAttributeName);
                        //if (!string.IsNullOrEmpty(extendValues))
                        //{
                        //    foreach (var extendValue in ListUtils.GetStringList(extendValues))
                        //    {
                        //        var newExtendValue = extendValue;
                        //        sbParsedContent.Append(contextInfo.IsStlEntity
                        //            ? await parseManager.PathManager.ParseNavigationUrlAsync(pageInfo.Site, newExtendValue, pageInfo.IsLocal)
                        //            : await inputParser.GetImageOrFlashHtmlAsync(pageInfo.Site, newExtendValue, contextInfo.Attributes, false));
                        //    }
                        //}

                        parsedContent = sbParsedContent.ToString();
                    }
                    else
                    {
                        var num = TranslateUtils.ToInt(no);
                        if (num <= 1)
                        {
                            parsedContent = contextInfo.IsStlEntity ? await parseManager.PathManager.ParseSiteUrlAsync(pageInfo.Site, content.ImageUrl, pageInfo.IsLocal) : await inputParser.GetImageOrFlashHtmlAsync(pageInfo.Site, content.ImageUrl, contextInfo.Attributes, false);
                        }
                        else
                        {
                            var extendName = ColumnsManager.GetExtendName(nameof(Content.ImageUrl), num - 1);
                            var extend     = content.Get <string>(extendName);
                            if (!string.IsNullOrEmpty(extend))
                            {
                                parsedContent = contextInfo.IsStlEntity ? await parseManager.PathManager.ParseSiteUrlAsync(pageInfo.Site, extend, pageInfo.IsLocal) : await inputParser.GetImageOrFlashHtmlAsync(pageInfo.Site, extend, contextInfo.Attributes, false);
                            }

                            //var extendAttributeName = ColumnsManager.GetExtendAttributeName(nameof(Content.ImageUrl));
                            //var extendValues = content.Get<string>(extendAttributeName);
                            //if (!string.IsNullOrEmpty(extendValues))
                            //{
                            //    var index = 2;
                            //    foreach (var extendValue in ListUtils.GetStringList(extendValues))
                            //    {
                            //        var newExtendValue = extendValue;
                            //        if (index == num)
                            //        {
                            //            parsedContent = contextInfo.IsStlEntity ? await parseManager.PathManager.ParseNavigationUrlAsync(pageInfo.Site, newExtendValue, pageInfo.IsLocal) : await inputParser.GetImageOrFlashHtmlAsync(pageInfo.Site, newExtendValue, contextInfo.Attributes, false);
                            //            break;
                            //        }
                            //        index++;
                            //    }
                            //}
                        }
                    }
                }
                else if (StringUtils.EqualsIgnoreCase(type, nameof(Content.VideoUrl)))
                {
                    var inputParser = new InputParserManager(parseManager.PathManager);

                    if (no == "all")
                    {
                        var sbParsedContent = new StringBuilder();
                        //第一条
                        sbParsedContent.Append(await inputParser.GetVideoHtmlAsync(pageInfo.Site, content.VideoUrl, contextInfo.Attributes, contextInfo.IsStlEntity));

                        //第n条
                        var countName = ColumnsManager.GetCountName(nameof(Content.VideoUrl));
                        var count     = content.Get <int>(countName);
                        for (var i = 1; i <= count; i++)
                        {
                            var extendName = ColumnsManager.GetExtendName(nameof(Content.VideoUrl), i);
                            var extend     = content.Get <string>(extendName);

                            sbParsedContent.Append(await inputParser.GetVideoHtmlAsync(pageInfo.Site,
                                                                                       extend, contextInfo.Attributes, contextInfo.IsStlEntity));
                        }

                        //var extendAttributeName = ColumnsManager.GetExtendAttributeName(nameof(Content.VideoUrl));
                        //var extendValues = content.Get<string>(extendAttributeName);
                        //if (!string.IsNullOrEmpty(extendValues))
                        //{
                        //    foreach (string extendValue in ListUtils.GetStringList(extendValues))
                        //    {

                        //        sbParsedContent.Append(await inputParser.GetVideoHtmlAsync(pageInfo.Config, pageInfo.Site, extendValue, contextInfo.Attributes, contextInfo.IsStlEntity));

                        //    }
                        //}

                        parsedContent = sbParsedContent.ToString();
                    }
                    else
                    {
                        var num = TranslateUtils.ToInt(no);
                        if (num <= 1)
                        {
                            parsedContent = await inputParser.GetVideoHtmlAsync(pageInfo.Site, content.VideoUrl, contextInfo.Attributes, contextInfo.IsStlEntity);
                        }
                        else
                        {
                            var extendName = ColumnsManager.GetExtendName(nameof(Content.VideoUrl), num - 1);
                            var extend     = content.Get <string>(extendName);
                            if (!string.IsNullOrEmpty(extend))
                            {
                                parsedContent = await inputParser.GetVideoHtmlAsync(pageInfo.Site, extend, contextInfo.Attributes, contextInfo.IsStlEntity);
                            }
                        }
                    }
                }
                else if (StringUtils.EqualsIgnoreCase(type, nameof(Content.FileUrl)))
                {
                    if (no == "all")
                    {
                        var sbParsedContent = new StringBuilder();
                        if (contextInfo.IsStlEntity)
                        {
                            //第一条
                            sbParsedContent.Append(content.FileUrl);

                            //第n条
                            var countName = ColumnsManager.GetCountName(nameof(Content.FileUrl));
                            var count     = content.Get <int>(countName);
                            for (var i = 1; i <= count; i++)
                            {
                                var extendName = ColumnsManager.GetExtendName(nameof(Content.FileUrl), i);
                                var extend     = content.Get <string>(extendName);

                                sbParsedContent.Append(extend);
                            }
                        }
                        else
                        {
                            var inputParser = new InputParserManager(parseManager.PathManager);

                            //第一条
                            sbParsedContent.Append(inputParser.GetFileHtmlWithCount(pageInfo.Site, content.ChannelId, content.Id, content.FileUrl, contextInfo.Attributes, contextInfo.InnerHtml, false, isLower, isUpper));

                            //第n条
                            var countName = ColumnsManager.GetCountName(nameof(Content.FileUrl));
                            var count     = content.Get <int>(countName);
                            for (var i = 1; i <= count; i++)
                            {
                                var extendName = ColumnsManager.GetExtendName(nameof(Content.FileUrl), i);
                                var extend     = content.Get <string>(extendName);

                                sbParsedContent.Append(inputParser.GetFileHtmlWithCount(pageInfo.Site,
                                                                                        content.ChannelId, content.Id, extend, contextInfo.Attributes,
                                                                                        contextInfo.InnerHtml,
                                                                                        false, isLower, isUpper));
                            }
                        }

                        parsedContent = sbParsedContent.ToString();
                    }
                    else
                    {
                        var num = TranslateUtils.ToInt(no);
                        if (contextInfo.IsStlEntity)
                        {
                            if (num <= 1)
                            {
                                parsedContent = content.FileUrl;
                            }
                            else
                            {
                                var extendName = ColumnsManager.GetExtendName(nameof(Content.FileUrl), num - 1);
                                var extend     = content.Get <string>(extendName);
                                if (!string.IsNullOrEmpty(extend))
                                {
                                    parsedContent = extend;
                                }
                            }

                            if (!string.IsNullOrEmpty(parsedContent))
                            {
                                parsedContent = await parseManager.PathManager.ParseSiteUrlAsync(pageInfo.Site, parsedContent, pageInfo.IsLocal);
                            }
                        }
                        else
                        {
                            var inputParser = new InputParserManager(parseManager.PathManager);

                            if (num <= 1)
                            {
                                parsedContent = inputParser.GetFileHtmlWithCount(pageInfo.Site, content.ChannelId, content.Id, content.FileUrl, contextInfo.Attributes, contextInfo.InnerHtml, false, isLower, isUpper);
                            }
                            else
                            {
                                var extendName = ColumnsManager.GetExtendName(nameof(Content.FileUrl), num - 1);
                                var extend     = content.Get <string>(extendName);
                                if (!string.IsNullOrEmpty(extend))
                                {
                                    parsedContent = inputParser.GetFileHtmlWithCount(pageInfo.Site,
                                                                                     content.ChannelId, content.Id, extend, contextInfo.Attributes,
                                                                                     contextInfo.InnerHtml, false, isLower, isUpper);
                                }
                            }
                        }
                    }
                }
                else if (StringUtils.EqualsIgnoreCase(type, nameof(ColumnsManager.NavigationUrl)))
                {
                    if (contextInfo.Content != null)
                    {
                        parsedContent = await parseManager.PathManager.GetContentUrlAsync(pageInfo.Site, contextInfo.Content, pageInfo.IsLocal);
                    }
                    else
                    {
                        var channelInfo = await databaseManager.ChannelRepository.GetAsync(contextInfo.ChannelId);

                        parsedContent = await parseManager.PathManager.GetContentUrlAsync(pageInfo.Site, channelInfo, contextInfo.ContentId, pageInfo.IsLocal);
                    }
                }
                else if (StringUtils.EqualsIgnoreCase(type, nameof(Content.TagNames)))
                {
                    parsedContent = ListUtils.ToString(content.TagNames);
                }
                else if (StringUtils.StartsWithIgnoreCase(type, StlParserUtility.ItemIndex) && contextInfo.ItemContainer?.ContentItem != null)
                {
                    var itemIndex = StlParserUtility.ParseItemIndex(contextInfo.ItemContainer.ContentItem.Key, type, contextInfo);
                    parsedContent = !string.IsNullOrEmpty(formatString) ? string.Format(formatString, itemIndex) : itemIndex.ToString();
                }
                else
                {
                    var channel = await databaseManager.ChannelRepository.GetAsync(content.ChannelId);

                    if (content.ContainsKey(type))
                    {
                        if (!ListUtils.ContainsIgnoreCase(ColumnsManager.MetadataAttributes.Value, type))
                        {
                            var relatedIdentities = databaseManager.TableStyleRepository.GetRelatedIdentities(channel);
                            var tableName         = databaseManager.ChannelRepository.GetTableName(pageInfo.Site, channel);
                            var styleInfo         = await databaseManager.TableStyleRepository.GetTableStyleAsync(tableName, type, relatedIdentities);

                            //styleInfo.IsVisible = false 表示此字段不需要显示 styleInfo.TableStyleId = 0 不能排除,因为有可能是直接辅助表字段没有添加显示样式
                            var num = TranslateUtils.ToInt(no);

                            var inputParser = new InputParserManager(parseManager.PathManager);
                            parsedContent = await inputParser.GetContentByTableStyleAsync(content, separator, pageInfo.Site, styleInfo, formatString, num, contextInfo.Attributes, contextInfo.InnerHtml, false);

                            parsedContent = InputTypeUtils.ParseString(styleInfo.InputType, parsedContent, replace, to, startIndex, length, wordNum, ellipsis, isClearTags, isReturnToBr, isLower, isUpper, formatString);
                        }
                        else
                        {
                            parsedContent = content.Get <string>(type);
                            parsedContent = InputTypeUtils.ParseString(InputType.Text, parsedContent, replace, to, startIndex, length, wordNum, ellipsis, isClearTags, isReturnToBr, isLower, isUpper, formatString);
                        }
                    }
                }

                if (!string.IsNullOrEmpty(parsedContent))
                {
                    parsedContent = leftText + parsedContent + rightText;
                }
            }

            return(parsedContent);
        }