Пример #1
0
        /// <summary>
        /// Parses the CSS.
        /// </summary>
        /// <param name="cssFileName">
        /// Name of the CSS file.
        /// </param>
        /// <param name="selectorPrefix">
        /// The selector prefix.
        /// </param>
        /// <param name="allowImports">
        /// if set to <c>true</c> [allow imports].
        /// </param>
        /// <param name="parseImports">
        /// if set to <c>true</c> [parse imports].
        /// </param>
        /// <param name="includeComments">
        /// if set to <c>true</c> [include comments].
        /// </param>
        /// <param name="sb">
        /// The sb.
        /// </param>
        /// <returns>
        /// A string value...
        /// </returns>
        public string ParseCss(
            string cssFileName,
            string selectorPrefix,
            bool allowImports,
            bool parseImports,
            bool includeComments,
            StringBuilder sb)
        {
            using (var sr = new StreamReader(cssFileName))
            {
                var st = new StringTokenizer(sr);

                if (sb == null)
                {
                    sb = new StringBuilder();
                }

                Token token;

                try
                {
                    do
                    {
                        token = st.Next();

                        switch (token.Kind)
                        {
                            case TokenKind.Comment:

                                if (includeComments)
                                {
                                    sb.Append(token.Value);

                                    // sb.Append("\n");
                                }

                                break;

                            case TokenKind.Selector:

                                if (selectorPrefix == string.Empty)
                                {
                                    sb.Append(token.Value);
                                }
                                else
                                {
                                    sb.Append(selectorPrefix);
                                    sb.Append(" ");
                                    sb.Append(token.Value);
                                }

                                break;

                            case TokenKind.AtRule:

                            case TokenKind.Block:
                                sb.Append(token.Value);
                                break;

                            case TokenKind.ImportRule:

                                if (allowImports && parseImports)
                                {
                                    // temp
                                    // sb.Append(token.Value);
                                    var filename = token.Value.Replace("@import", string.Empty);
                                    filename = filename.Replace("url", string.Empty);
                                    filename = filename.Replace("(", string.Empty);
                                    filename = filename.Replace(")", string.Empty);
                                    filename = filename.Replace("'", string.Empty);
                                    filename = filename.Replace("\"", string.Empty);
                                    filename = filename.Replace(";", string.Empty).Trim();
                                    filename =
                                        string.Concat(
                                            cssFileName.Substring(0, cssFileName.LastIndexOf(@"\")).Trim(),
                                            "\\",
                                            filename);
                                    var loop = new CssHelper();
                                    loop.ParseCss(
                                        filename, selectorPrefix, allowImports, parseImports, includeComments, sb);
                                }
                                else if (allowImports && !parseImports)
                                {
                                    sb.Append(token.Value);
                                }

                                break;
                            default:
                                sb.Append(token.Value);
                                break;
                        }
                    }
                    while (token.Kind != TokenKind.EOF);
                }
                catch (Exception ex)
                {
                    ErrorHandler.Publish(
                        LogLevel.Error,
                        string.Format("Error in parsing CSS file: {0} Message was: {1}", cssFileName, ex.Message));
                }
                finally
                {
                    sr.Close();
                }

                return sb.ToString();
            }
        }
Пример #2
0
        /// <summary>
        /// Parses the CSS.
        /// </summary>
        /// <param name="cssFileName">
        /// Name of the CSS file.
        /// </param>
        /// <param name="selectorPrefix">
        /// The selector prefix.
        /// </param>
        /// <param name="allowImports">
        /// if set to <c>true</c> [allow imports].
        /// </param>
        /// <param name="parseImports">
        /// if set to <c>true</c> [parse imports].
        /// </param>
        /// <param name="includeComments">
        /// if set to <c>true</c> [include comments].
        /// </param>
        /// <param name="sb">
        /// The sb.
        /// </param>
        /// <returns>
        /// A string value...
        /// </returns>
        public string ParseCss(
            string cssFileName,
            string selectorPrefix,
            bool allowImports,
            bool parseImports,
            bool includeComments,
            StringBuilder sb)
        {
            using (var sr = new StreamReader(cssFileName))
            {
                var st = new StringTokenizer(sr);

                if (sb == null)
                {
                    sb = new StringBuilder();
                }

                Token token;

                try
                {
                    do
                    {
                        token = st.Next();

                        switch (token.Kind)
                        {
                        case TokenKind.Comment:

                            if (includeComments)
                            {
                                sb.Append(token.Value);

                                // sb.Append("\n");
                            }

                            break;

                        case TokenKind.Selector:

                            if (selectorPrefix == string.Empty)
                            {
                                sb.Append(token.Value);
                            }
                            else
                            {
                                sb.Append(selectorPrefix);
                                sb.Append(" ");
                                sb.Append(token.Value);
                            }

                            break;

                        case TokenKind.AtRule:

                        case TokenKind.Block:
                            sb.Append(token.Value);
                            break;

                        case TokenKind.ImportRule:

                            if (allowImports && parseImports)
                            {
                                // temp
                                // sb.Append(token.Value);
                                var filename = token.Value.Replace("@import", string.Empty);
                                filename = filename.Replace("url", string.Empty);
                                filename = filename.Replace("(", string.Empty);
                                filename = filename.Replace(")", string.Empty);
                                filename = filename.Replace("'", string.Empty);
                                filename = filename.Replace("\"", string.Empty);
                                filename = filename.Replace(";", string.Empty).Trim();
                                filename =
                                    string.Concat(
                                        cssFileName.Substring(0, cssFileName.LastIndexOf(@"\")).Trim(),
                                        "\\",
                                        filename);
                                var loop = new CssHelper();
                                loop.ParseCss(
                                    filename, selectorPrefix, allowImports, parseImports, includeComments, sb);
                            }
                            else if (allowImports && !parseImports)
                            {
                                sb.Append(token.Value);
                            }

                            break;

                        default:
                            sb.Append(token.Value);
                            break;
                        }
                    }while (token.Kind != TokenKind.EOF);
                }
                catch (Exception ex)
                {
                    ErrorHandler.Publish(
                        LogLevel.Error,
                        string.Format("Error in parsing CSS file: {0} Message was: {1}", cssFileName, ex.Message));
                }
                finally
                {
                }

                return(sb.ToString());
            }
        }