public void CanAppendExtensionToPath(string extension, string expected)
            {
                // Given
                NormalizedPath path = new NormalizedPath("temp/hello.txt");

                // When
                path = path.AppendExtension(extension);

                // Then
                Assert.AreEqual(expected, path.ToString());
            }
            public void ShouldThrowIfExtensionIsNull()
            {
                // Given
                NormalizedPath path = new NormalizedPath("temp/hello.txt");

                // When
                TestDelegate test = () => path.AppendExtension(null);

                // Then
                Assert.Throws <ArgumentNullException>(test);
            }
        private async Task <string> GetFileVariationsAsync(NormalizedPath filePath, NormalizedPath requestedFilePath)
        {
            // ...as specified
            string scss = await GetFileAsync(filePath, requestedFilePath);

            if (scss != null)
            {
                return(scss);
            }

            // ...with extension (if not already)
            if (!filePath.HasExtension || filePath.Extension != ".scss")
            {
                NormalizedPath extensionPath = filePath.AppendExtension(".scss");
                scss = await GetFileAsync(extensionPath, requestedFilePath);

                if (scss != null)
                {
                    return(scss);
                }

                // ...and with underscore prefix (if not already)
                if (!extensionPath.FileName.FullPath.StartsWith("_"))
                {
                    extensionPath = extensionPath.ChangeFileName("_" + extensionPath.FileName.FullPath);
                    scss          = await GetFileAsync(extensionPath, requestedFilePath);

                    if (scss != null)
                    {
                        return(scss);
                    }
                }
            }

            // ...with underscore prefix (if not already)
            if (!filePath.FileName.FullPath.StartsWith("_"))
            {
                filePath = filePath.ChangeFileName("_" + filePath.FileName.FullPath);
                scss     = await GetFileAsync(filePath, requestedFilePath);

                if (scss != null)
                {
                    return(scss);
                }
            }

            return(null);
        }
Example #4
0
        private IFile GetInputFile(NormalizedPath filePath)
        {
            // Find the requested file
            // ...as specified
            IFile file = _fileSystem.GetInputFile(filePath);

            if (file.Exists)
            {
                return(file);
            }

            // ...with extension (if not already)
            if (!filePath.HasExtension || filePath.Extension != ".less")
            {
                NormalizedPath extensionPath = filePath.AppendExtension(".less");
                IFile          extensionFile = _fileSystem.GetInputFile(extensionPath);
                if (extensionFile.Exists)
                {
                    return(extensionFile);
                }

                // ...and with underscore prefix (if not already)
                if (!extensionPath.FileName.FullPath.StartsWith("_"))
                {
                    extensionPath = extensionPath.ChangeFileName("_" + extensionPath.FileName.FullPath);
                    extensionFile = _fileSystem.GetInputFile(extensionPath);
                    if (extensionFile.Exists)
                    {
                        return(extensionFile);
                    }
                }
            }

            // ...with underscore prefix (if not already)
            if (!filePath.FileName.FullPath.StartsWith("_"))
            {
                filePath = filePath.ChangeFileName("_" + filePath.FileName.FullPath);
                IFile underscoreFile = _fileSystem.GetInputFile(filePath);
                if (underscoreFile.Exists)
                {
                    return(underscoreFile);
                }
            }

            // Can't find it, default to the original
            return(file);
        }
Example #5
0
        private static IModule[] GetTitleAndDestinationModules(IDocument archiveDoc) => new IModule[]
        {
            new AddTitle(
                Config.FromDocument(doc =>
            {
                if (doc.ContainsKey(WebKeys.ArchiveTitle))
                {
                    return(doc.GetString(WebKeys.ArchiveTitle));
                }

                // Default title
                string title = doc.GetString(Keys.Title);
                if (doc.ContainsKey(Keys.GroupKey))
                {
                    title += " - " + doc.GetString(Keys.GroupKey);
                }
                int index = doc.GetInt(Keys.Index);
                return(index <= 1 ? title : (title + $" (Page {index})"));
            })).KeepExisting(false),
            new SetDestination(
                Config.FromDocument(doc =>
            {
                if (doc.ContainsKey(WebKeys.ArchiveDestination))
                {
                    return(doc.GetPath(WebKeys.ArchiveDestination));
                }

                // Default destination
                NormalizedPath destination = archiveDoc.Destination.ChangeExtension(null);
                if (doc.ContainsKey(Keys.GroupKey))
                {
                    destination.Combine(NormalizedPath.ReplaceInvalidFileNameChars(doc.GetString(Keys.GroupKey)));
                }
                int index = doc.GetInt(Keys.Index);
                if (index > 1)
                {
                    destination = destination.Combine(index.ToString());
                }
                return(destination.AppendExtension(".html"));
            }),
                true)
        };
Example #6
0
        /// <inheritdoc />
        protected override async Task <IEnumerable <IDocument> > ExecuteContextAsync(IExecutionContext context)
        {
            // Iterate redirects and generate all of the per-redirect documents (I.e., meta refresh pages)
            ConcurrentDictionary <NormalizedPath, string> redirects = new ConcurrentDictionary <NormalizedPath, string>();

            // Need to materialize the parallel operation before creating the additional outputs
            List <IDocument> outputs = new List <IDocument>();

            foreach (IDocument input in context.Inputs)
            {
                await foreach (IDocument output in GetOutputsAsync(input))
                {
                    outputs.Add(output);
                }
            }

            // Generate other output documents if requested
            if (redirects.Count > 0)
            {
                foreach (KeyValuePair <NormalizedPath, Func <IDictionary <NormalizedPath, string>, string> > additionalOutput in _additionalOutputs)
                {
                    string content = additionalOutput.Value(redirects);
                    if (!string.IsNullOrEmpty(content))
                    {
                        outputs.Add(
                            context.CreateDocument(
                                additionalOutput.Key,
                                await context.GetContentProviderAsync(content)));
                    }
                }
            }

            return(outputs);

            async IAsyncEnumerable <IDocument> GetOutputsAsync(IDocument input)
            {
                IReadOnlyList <NormalizedPath> paths = await _paths.GetValueAsync(input, context);

                if (paths != null)
                {
                    foreach (NormalizedPath fromPath in paths.Where(x => !x.IsNull))
                    {
                        // Make sure it's a relative path
                        if (!fromPath.IsRelative)
                        {
                            context.LogWarning($"The redirect path must be relative for document {input.Source.ToSafeDisplayString()}: {fromPath}");
                            continue;
                        }

                        // Record the redirect for additional processing
                        string url = context.GetLink(input, _includeHost);
                        redirects.TryAdd(fromPath, url);

                        // Meta refresh documents
                        NormalizedPath outputPath = fromPath;
                        if (!string.Equals(outputPath.Extension, ".html", StringComparison.OrdinalIgnoreCase))
                        {
                            outputPath = outputPath.AppendExtension(".html");
                        }

                        if (_metaRefreshPages)
                        {
                            yield return(context.CreateDocument(
                                             outputPath,
                                             await context.GetContentProviderAsync(
                                                 $@"
<!doctype html>
<html>    
  <head>      
    <title>Redirected</title>      
    <meta http-equiv=""refresh"" content=""0;url='{url}'"" />    
  </head>    
  <body> 
    <p>This page has moved to a <a href=""{url}"">{url}</a></p> 
  </body>  
</html>",
                                                 MediaTypes.Html)));
                        }
                    }
                }
            }
        }