Example #1
0
        public static List <BlockInfo> Blocks(ref string _content, string _templateTag, string _aTag, string _bTag, int _targetBlockIndex = -1, string _searchBeforeTag = "", string _replaceHolderTemplateTag = "", ForeachATagPredicate _foreachATagPredicate = null)
        {
            _aTag = string.Format(_templateTag, _aTag);
            _bTag = string.Format(_templateTag, _bTag);

            int aTagLength = _aTag.Length;
            int bTagLength = _bTag.Length;

            int aTagIndex = 0;
            int bTagIndex = 0;

            int blockIndex = -1;

            bool hasReplaceHolderTemplateTag = !String.IsNullOrEmpty(_replaceHolderTemplateTag);

            string replaceHolderTemplateTagTmp = "";

            List <BlockInfo> tag_templates = new List <BlockInfo>();

            aTagIndex = _content.IndexOf(_aTag);

            if (aTagIndex == -1)
            {
                return(tag_templates);
            }

            if (!String.IsNullOrEmpty(_searchBeforeTag))
            {
                _searchBeforeTag = string.Format(_templateTag, _searchBeforeTag);

                if (aTagIndex >= _content.IndexOf(_searchBeforeTag))
                {
                    return(tag_templates);
                }
            }

            Template.Position tagPosition = new Position(ref _content, ref _aTag, ref _bTag);

            //bTagIndex = content.IndexOf(bTag, aTagIndex + 1);
            //
            //int tmpATagIndex = aTagIndex;
            //
            bTagIndex = tagPosition.GetEndPoint(aTagIndex);

            //string tmpBlockStr = "";

            while (aTagIndex != -1 && bTagIndex != -1 && (bTagIndex + bTagLength) <= _content.Length)
            {
                if (_foreachATagPredicate != null)
                {
                    if (!_foreachATagPredicate(aTagIndex, ref _content))
                    {
                        aTagIndex = _content.IndexOf(_aTag, bTagIndex + 1);

                        bTagIndex = tagPosition.GetEndPoint(ref _content, aTagIndex);

                        continue;
                    }
                }

                blockIndex++;

                BlockInfo blockInfo = new BlockInfo();

                blockInfo.Value = _content.Substring(aTagIndex + aTagLength, bTagIndex - (aTagIndex + aTagLength));

                if (!hasReplaceHolderTemplateTag)
                {
                    /*
                     *  [o1]<!-- Template_TABLE -->[i1]
                     *  {Tag}
                     *  [i2]<!-- Template_TABLE -->[o2]
                     */

                    // [i1] <-> [i2] <==> InnerAIndex <-> InnerBIndex
                    //
                    blockInfo.InnerAIndex = aTagIndex + aTagLength;
                    blockInfo.InnerBIndex = bTagIndex - 1;

                    // [o1](<...) <-> [o2](...>) <==> OuterAIndex ( < index ) <-> OuterBIndex ( > index )
                    //
                    blockInfo.OuterAIndex = aTagIndex;
                    blockInfo.OuterBIndex = bTagIndex + bTagLength - 1;
                }

                if (hasReplaceHolderTemplateTag)
                {
                    replaceHolderTemplateTagTmp = string.Format(_replaceHolderTemplateTag, blockIndex);

                    _content = _content.Substring(0, aTagIndex)

                               +
                               replaceHolderTemplateTagTmp
                               +

                               _content.Substring(bTagIndex + bTagLength);

                    bTagIndex = _content.IndexOf(replaceHolderTemplateTagTmp, aTagIndex);

                    if (bTagIndex == -1)
                    {
                        throw new ArgumentException("Block : ReplaceHolderTemplateTag Not Found");
                    }

                    bTagIndex += replaceHolderTemplateTagTmp.Length - 1;

                    blockInfo.ReplaceHolderTemplateTag = replaceHolderTemplateTagTmp;
                }

                //if (_fillTagInfos)
                //{
                //    // # DATA Item & Template.TagInfo #
                //    //
                //    blockInfo.FillTagInfos();
                //}

                tag_templates.Add(blockInfo);

                if (_targetBlockIndex == blockIndex)
                {
                    break;
                }

                aTagIndex = _content.IndexOf(_aTag, bTagIndex + 1);
                //
                //bTagIndex = content.IndexOf(bTag, aTagIndex + 1);
                //
                //tmpATagIndex = aTagIndex + 1;
                //
                //tmpATagIndex = aTagIndex;
                //
                bTagIndex = tagPosition.GetEndPoint(ref _content, aTagIndex);
            }

            return(tag_templates);
        }
Example #2
0
 public static List <BlockInfo> Blocks(string _content, string _templateTag, string _aTag, string _bTag, int _targetBlockIndex = -1, string _searchBeforeTag = "", string _replaceHolderTemplateTag = "", ForeachATagPredicate _foreachATagPredicate = null)
 {
     return(Template.Blocks(ref _content, _templateTag, _aTag, _bTag, _targetBlockIndex, _searchBeforeTag, _replaceHolderTemplateTag, _foreachATagPredicate));
 }