Esempio n. 1
0
        /* Function: ApplySubstitutions
         * Finds all substitutions in the source that match those in the table and replaces them with their values.  Will also comment
         * out any substitution definitions found.
         */
        protected Tokenizer ApplySubstitutions(Tokenizer source, StringToStringTable substitutions, bool applyNestedSubstitutions = true)
        {
            TokenIterator iterator = source.FirstToken;


            // Find the first valid substitution identifier or definition.  If there aren't any we don't want to \do unnecessary memory
            // allocation and processing.

            bool   foundSubstitution = false;
            string identifier = null;
            string localeIdentifier, value, declaration;

            while (iterator.IsInBounds)
            {
                if (TryToSkipSubstitutionIdentifier(ref iterator, out identifier) ||
                    TryToSkipLocaleSubstitutionIdentifier(ref iterator, out identifier, out localeIdentifier))
                {
                    foundSubstitution = true;
                    break;
                }
                // else if (TryToSkipSubstitutionDefinition())
                // {
                // Unnecessary because definitions will start with identifiers so it will get picked up by that
                // }
                else
                {
                    GenericSkip(ref iterator);
                }
            }

            if (!foundSubstitution)
            {
                return(source);
            }


            // Now that we know we have one, we can back up the iterator and build new output

            iterator.PreviousByCharacters(identifier.Length);

            StringBuilder output = new StringBuilder(source.RawText.Length);

            output.Append(source.RawText, 0, iterator.RawTextIndex);

            while (iterator.IsInBounds)
            {
                TokenIterator previousIterator = iterator;

                if (TryToSkipSubstitutionDefinition(ref iterator, out identifier, out value, out declaration))
                {
                    if (this.blockCommentStringPairs != null)
                    {
                        output.Append(this.blockCommentStringPairs[0] + ' ' + declaration + ' ' + this.blockCommentStringPairs[1]);
                    }
                }
                else if (TryToSkipSubstitutionIdentifier(ref iterator, out identifier))
                {
                    string substitution = substitutions[identifier];

                    if (substitution == null)
                    {
                        output.Append(identifier);
                    }
                    else
                    {
                        if (applyNestedSubstitutions)
                        {
                            substitution = ApplyNestedSubstitutions(substitution, substitutions);
                        }

                        output.Append(substitution);
                    }
                }
                else if (TryToSkipLocaleSubstitutionIdentifier(ref iterator, out identifier, out localeIdentifier))
                {
                    string substitution = Engine.Locale.SafeGet("NaturalDocs.Engine", localeIdentifier, null);

                    if (substitution == null)
                    {
                        output.Append(identifier);
                    }
                    else
                    {
                        output.Append('"' + substitution.StringEscape() + '"');
                    }
                }
                else
                {
                    GenericSkip(ref iterator);
                    source.AppendTextBetweenTo(previousIterator, iterator, output);
                }
            }

            return(new Tokenizer(output.ToString()));
        }
Esempio n. 2
0
        override public string Process(string javascript, bool shrink = true)
        {
            Tokenizer           source        = new Tokenizer(javascript);
            StringToStringTable substitutions = FindSubstitutions(source);

            source = ApplySubstitutions(source, substitutions);

            if (!shrink)
            {
                return(source.RawText);
            }


            // Search comments for sections to include in the output

            StringBuilder output = new StringBuilder(javascript.Length);

            string includeInOutput = FindIncludeInOutput(GetPossibleDocumentationComments(source));

            if (includeInOutput != null)
            {
                output.AppendLine("/*");
                output.Append(includeInOutput);
                output.AppendLine("*/");
                output.AppendLine();
            }


            // Shrink the source

            TokenIterator iterator = source.FirstToken;
            string        spaceSeparatedSymbols = "+-";

            while (iterator.IsInBounds)
            {
                char lastChar = (output.Length > 0 ? output[output.Length - 1] : '\0');

                if (TryToSkipWhitespace(ref iterator) == true)                 // includes comments
                {
                    char nextChar = iterator.Character;

                    if ((spaceSeparatedSymbols.IndexOf(lastChar) != -1 &&
                         spaceSeparatedSymbols.IndexOf(nextChar) != -1) ||
                        (Tokenizer.FundamentalTypeOf(lastChar) == FundamentalType.Text &&
                         Tokenizer.FundamentalTypeOf(nextChar) == FundamentalType.Text))
                    {
                        output.Append(' ');
                    }
                }
                else
                {
                    TokenIterator prevIterator = iterator;

                    if (TryToSkipString(ref iterator) ||
                        TryToSkipRegex(ref iterator))
                    {
                        source.AppendTextBetweenTo(prevIterator, iterator, output);
                    }
                    else
                    {
                        iterator.AppendTokenTo(output);
                        iterator.Next();
                    }
                }
            }

            return(output.ToString());
        }
Esempio n. 3
0
        override public string Process(string css, bool shrink = true)
        {
            Tokenizer           source        = new Tokenizer(css);
            StringToStringTable substitutions = FindSubstitutions(source);

            source = ApplySubstitutions(source, substitutions);

            if (!shrink)
            {
                return(source.RawText);
            }


            // Search comments for sections to include in the output

            StringBuilder output = new StringBuilder(css.Length);

            string includeInOutput = FindIncludeInOutput(GetPossibleDocumentationComments(source));

            if (includeInOutput != null)
            {
                output.AppendLine("/*");
                output.Append(includeInOutput);
                output.AppendLine("*/");
                output.AppendLine();
            }


            // Shrink the source

            TokenIterator iterator = source.FirstToken;

            // We have to be more cautious than the JavaScript shrinker.  You don't want something like "head .class" to become
            // "head.class".  Colon is a special case because we only want to remove spaces after it ("font-size: 12pt") and not
            // before ("body :link").
            string safeToCondenseAround = "{},;:+>[]= \0\n\r";

            while (iterator.IsInBounds)
            {
                char lastChar = (output.Length > 0 ? output[output.Length - 1] : '\0');

                if (TryToSkipWhitespace(ref iterator))                 // includes comments
                {
                    char nextChar = iterator.Character;

                    if (nextChar == ':' ||
                        (safeToCondenseAround.IndexOf(lastChar) == -1 &&
                         safeToCondenseAround.IndexOf(nextChar) == -1))
                    {
                        output.Append(' ');
                    }
                }
                else
                {
                    TokenIterator prevIterator = iterator;

                    if (TryToSkipString(ref iterator))
                    {
                        source.AppendTextBetweenTo(prevIterator, iterator, output);
                    }
                    else
                    {
                        if (iterator.Character == '}' && lastChar == ';')
                        {
                            // Semicolons are unnecessary at the end of blocks.  However, we have to do this here instead of in a
                            // global search and replace for ";}" because we don't want to alter that sequence if it appears in a string.
                            output[output.Length - 1] = '}';
                        }
                        else
                        {
                            iterator.AppendTokenTo(output);
                        }

                        iterator.Next();
                    }
                }
            }

            return(output.ToString());
        }