Ejemplo n.º 1
0
        /// <summary>
        /// 格式化样式表
        /// </summary>
        /// <param name="style">样式表</param>
        /// <returns>样式表</returns>
        private static unsafe string formatStyle(SubString style)
        {
            if (style.Length != 0)
            {
                LeftArray <KeyValue <SubString, SubString> > values = new LeftArray <KeyValue <SubString, SubString> >(0);
                LeftArray <SubString> styles     = style.Split(';');
                SubString[]           styleArray = styles.Array;
                for (int index = 0; index != styles.Length; ++index)
                {
                    int valueIndex = styleArray[index].IndexOf(':');
                    if (valueIndex > 0)
                    {
                        SubString name = styleArray[index].GetSub(0, valueIndex);
                        if (SafeStyleAttributeName.Names.Contains(name))
                        {
                            styleArray[index].MoveStart(valueIndex + 1);
                            values.Add(new KeyValue <SubString, SubString>(ref name, ref styleArray[index]));
                        }
                    }
                }
                if (values.Length != 0)
                {
                    int length = (values.Count << 1) - 1;
                    foreach (KeyValue <SubString, SubString> value in values)
                    {
                        length += value.Key.Length + value.Value.Length;
                    }
                    string newStyle = AutoCSer.Extensions.StringExtension.FastAllocateString(length);
                    byte * bits     = Node.Bits.Byte;
                    fixed(char *newStyleFixed = newStyle, styleFixed = style.GetFixedBuffer())
                    {
                        char *write = newStyleFixed;

                        foreach (KeyValue <SubString, SubString> value in values)
                        {
                            if (write != newStyleFixed)
                            {
                                *write++ = ';';
                            }
                            for (char *start = styleFixed + value.Key.Start, end = start + value.Key.Length; start != end; *write++ = *start++)
                            {
                                ;
                            }
                            *write++ = ':';
                            for (char *start = styleFixed + value.Value.Start, end = start + value.Value.Length; start != end; ++start)
                            {
                                *write++ = ((bits[*(byte *)start] & Node.CssFilterBit) | *(((byte *)start) + 1)) == 0 ? ' ' : *start;
                            }
                        }
                    }

                    return(newStyle);
                }
            }
            return(null);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 生成代码
        /// </summary>
        /// <param name="fileName">模板文件名</param>
        /// <returns>代码</returns>
        private static LeftArray <string> getCode(string fileName)
        {
            string code = File.ReadAllText(fileName, System.Text.Encoding.UTF8);
            int    startIndex = code.IndexOf("/*", StringComparison.Ordinal), endIndex = code.IndexOf(@"*/

", startIndex + 2, StringComparison.Ordinal);

            if (endIndex > 0 && startIndex > 0)
            {
                LeftArray <string[][]> headerArray = new LeftArray <string[][]>(0);
                foreach (SubString line in new SubString(startIndex += 2, endIndex - startIndex, code).Trim().Split('\n'))
                {
                    SubString headerLine = line.Trim();
                    if (headerLine.Length != 0)
                    {
                        headerArray.Add(headerLine.Split(';').GetArray(p => p.Split(',').GetArray(v => (string)v)));
                    }
                }
                if (headerArray.Length != 0)
                {
                    CombinationTemplateLink header = new CombinationTemplateLink(code = code.Substring(endIndex + 4));
                    int row = 0;
                    foreach (string[][] line in headerArray)
                    {
                        int col = 0;
                        foreach (string value in line[0])
                        {
                            bool isSplit = false;
                            CombinationTemplateLink link = header;
                            do
                            {
                                link = link.Split(value, row, col, ref isSplit);
                            }while (link != null);
                            if (!isSplit)
                            {
                                Messages.Error("自定义简单组合模板文件内容没有找到匹配替换标记 " + value + " : " + fileName);
                                return(new LeftArray <string>(0));
                            }
                            ++col;
                        }
                        ++row;
                    }
                    LeftArray <CombinationTemplateLink> linkArray = new LeftArray <CombinationTemplateLink>(0);
                    LeftArray <string> codeArray = new LeftArray <string>(0);
                    do
                    {
                        if (header.Row >= 0)
                        {
                            linkArray.Add(header);
                            header.Index = codeArray.Length;
                            codeArray.Add(string.Empty);
                        }
                        else
                        {
                            codeArray.Add(header.Code);
                        }
                    }while ((header = header.Next) != null);
                    CombinationTemplateHeaderEnumerable headerEnumerable = new CombinationTemplateHeaderEnumerable(ref headerArray, codeArray.ToArray(), linkArray.ToArray());
                    codeArray.Length = 0;
                    foreach (string enumerableCode in headerEnumerable.GetCode())
                    {
                        codeArray.Add(enumerableCode);
                    }
                    return(codeArray);
                }
                else
                {
                    Messages.Error("自定义简单组合模板文件缺少头部信息解析失败 : " + fileName);
                }
            }
            else
            {
                Messages.Error("自定义简单组合模板文件缺少头部注释信息 : " + fileName);
            }
            return(new LeftArray <string>(0));
        }