Example #1
0
        /// <summary>
        /// Converts a virtual (relative) path to an application absolute path.
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public string Content(string path)
        {
            if (string.IsNullOrEmpty(path))
            {
                return(null);
            }

            //if this is a protocol-relative/protocol-less uri, then we need to add the protocol for the remaining
            // logic to work properly
            if (path.StartsWith("//"))
            {
                var scheme = _siteInfo.GetBaseUrl().Scheme;
                return(Regex.Replace(path, @"^\/\/", string.Format("{0}{1}", scheme, Constants.SchemeDelimiter)));
            }

            //This code is taken from the UrlHelper code ... which shouldn't need to be tucked away in there
            // since it is not dependent on the ActionContext
            if (path[0] == 126)
            {
                PathString pathBase = _siteInfo.GetBasePath();
                return(pathBase.Add(new PathString(path.Substring(1))).Value);
            }

            return(path);
        }
        /// <summary>
        /// Returns the CSS file with all of the url's formatted to be absolute locations
        /// </summary>
        /// <param name="fileContents"></param>
        /// <param name="url"></param>
        /// <returns></returns>
        internal string ReplaceUrlsWithAbsolutePaths(string fileContents, string url)
        {
            var uri = new Uri(url, UriKind.RelativeOrAbsolute);

            fileContents = ReplaceUrlsWithAbsolutePaths(fileContents, uri.MakeAbsoluteUri(_siteInfo.GetBaseUrl()));
            return(fileContents);
        }
        public Task ProcessAsync(FileProcessContext fileProcessContext, PreProcessorDelegate next)
        {
            var sb = new StringBuilder();

            using (var reader = new StringReader(fileProcessContext.FileContent))
            {
                var line = reader.ReadLine();
                while (line != null)
                {
                    var isTrim     = true;
                    var foundIndex = 0;
                    for (int i = 0; i < line.Length; i++)
                    {
                        char c = line[i];
                        if (isTrim && char.IsWhiteSpace(c))
                        {
                            continue;
                        }

                        isTrim = false;
                        if (c == _sourceMappingUrl[foundIndex])
                        {
                            foundIndex++;
                            if (foundIndex == _sourceMappingUrl.Length)
                            {
                                // found! parse it
                                var match = RegexStatements.SourceMap.Match(line);
                                if (match.Success)
                                {
                                    var url = match.Groups[1].Value;
                                    // convert to it's absolute path
                                    var contentPath = _requestHelper.Content(fileProcessContext.WebFile.FilePath);
                                    var uri         = new Uri(contentPath, UriKind.RelativeOrAbsolute).MakeAbsoluteUri(_siteInfo.GetBaseUrl());
                                    var absolute    = uri.ToAbsolutePath(url);
                                    var path        = _requestHelper.Content(absolute);
                                    // replace the source map with the correct url
                                    WriteLine(sb, $"{_sourceMappingUrl}={path};");
                                }
                                else
                                {
                                    // should have matched, perhaps the source map is formatted in a weird way, we're going to ignore
                                    // it since if it's rendered without the correct path then other errors will occur.
                                }
                                break; // exit for loop
                            }
                        }
                        else
                        {
                            // not found on this line
                            WriteLine(sb, line);
                            break; // exit for loop
                        }
                    }

                    // next
                    line = reader.ReadLine();
                }
            }

            fileProcessContext.Update(sb.ToString());
            return(next(fileProcessContext));
        }
Example #4
0
        public async Task ProcessAsync(FileProcessContext fileProcessContext, PreProcessorDelegate next)
        {
            var sb = new StringBuilder();

            IEnumerable <string> importedPaths;
            var removedImports = ParseImportStatements(fileProcessContext.FileContent, out importedPaths);

            //need to write the imported sheets first since these theoretically should *always* be at the top for browser to support them
            foreach (var importPath in importedPaths)
            {
                var uri      = new Uri(fileProcessContext.WebFile.FilePath, UriKind.RelativeOrAbsolute).MakeAbsoluteUri(_siteInfo.GetBaseUrl());
                var absolute = uri.ToAbsolutePath(importPath);

                var path = _requestHelper.Content(absolute);
                //is it external?
                if (path.Contains(Constants.SchemeDelimiter))
                {
                    //Pretty sure we just leave the external refs in there
                    //TODO: Look in CDF, we have tests for this, pretty sure the ParseImportStatements removes that
                }
                else
                {
                    //it's internal (in theory)
                    var filePath = _fileSystemHelper.GetFileInfo(path);
                    var content  = await _fileSystemHelper.ReadContentsAsync(filePath);

                    //This needs to be put back through the whole pre-processor pipeline before being added,
                    // so we'll clone the original webfile with it's new path, this will inherit the whole pipeline,
                    // and then we'll execute the pipeline for that file
                    var clone     = fileProcessContext.WebFile.Duplicate(path);
                    var processed = await clone.Pipeline.ProcessAsync(new FileProcessContext(content, clone, fileProcessContext.BundleContext));

                    sb.Append(processed);

                    ////  _fileSystemHelper.MapWebPath(path.StartsWith("/") ? path : string.Format("~/{0}", path));
                    //if (System.IO.File.Exists(filePath))
                    //{

                    //}
                    //else
                    //{
                    //    //TODO: Need to log this
                    //}
                }
            }

            sb.Append(removedImports);

            fileProcessContext.Update(sb.ToString());

            await next(fileProcessContext);
        }