示例#1
0
 protected override async Task <IEnumerable <IDocument> > ExecuteAsync(IDocument input, IExecutionContext context, object value)
 {
     if (value != null)
     {
         try
         {
             JsonSerializerSettings settings = new JsonSerializerSettings()
             {
                 Formatting = _indenting ? Formatting.Indented : Formatting.None
             };
             if (_camelCase)
             {
                 settings.ContractResolver = new CamelCasePropertyNamesContractResolver();
             }
             _settings?.Invoke(settings);
             string result = JsonConvert.SerializeObject(value, settings);
             if (string.IsNullOrEmpty(_destinationKey))
             {
                 return(input.Clone(await context.GetContentProviderAsync(result)).Yield());
             }
             return(input
                    .Clone(new MetadataItems
             {
                 { _destinationKey, result }
             })
                    .Yield());
         }
         catch (Exception ex)
         {
             // Return original input on exception
             context.LogError($"Error serializing JSON for {input.ToSafeDisplayString()}, returning original input document: {ex}");
         }
     }
     return(input.Yield());
 }
示例#2
0
        protected override async Task <IEnumerable <IDocument> > ExecuteInputAsync(IDocument input, IExecutionContext context)
        {
            string fileName = _fileName == null
                ? input.Destination.FileName.FullPath
                : await _fileName.GetValueAsync(input, context);

            if (!string.IsNullOrWhiteSpace(fileName))
            {
                fileName = GetFileName(fileName);
                if (!string.IsNullOrWhiteSpace(fileName))
                {
                    if (_fileName == null || string.IsNullOrWhiteSpace(_outputKey))
                    {
                        // No output key so set the destination
                        FilePath path = input.Destination.ChangeFileName(fileName);
                        return(input.Clone(path).Yield());
                    }
                    else
                    {
                        // Set the specified output key
                        return(input.Clone(
                                   new MetadataItems
                        {
                            { _outputKey, fileName }
                        })
                               .Yield());
                    }
                }
            }
            return(input.Yield());
        }
示例#3
0
        /// <inheritdoc />
        protected override async Task <IEnumerable <IDocument> > ExecuteConfigAsync(IDocument input, IExecutionContext context, string value)
        {
            if (value == null)
            {
                value = string.Empty;
            }
            if (string.IsNullOrEmpty(_search))
            {
                return(input.Yield());
            }
            string currentDocumentContent = await input.GetContentStringAsync();

            if (_contentFinder != null)
            {
                string newDocumentContent = Regex.Replace(
                    currentDocumentContent,
                    _search,
                    match => _contentFinder(match, input)?.ToString() ?? string.Empty,
                    _regexOptions);
                return((currentDocumentContent == newDocumentContent
                    ? input
                    : input.Clone(await context.GetContentProviderAsync(newDocumentContent)))
                       .Yield());
            }
            string replaced = _isRegex
                ? Regex.Replace(currentDocumentContent, _search, value, _regexOptions)
                : currentDocumentContent.Replace(_search, value);

            return(input.Clone(await context.GetContentProviderAsync(replaced)).Yield());
        }
示例#4
0
        protected override async Task <IEnumerable <IDocument> > ExecuteAsync(IDocument input, IExecutionContext context)
        {
            context.LogDebug($"Processing Sass for {input.ToSafeDisplayString()}");

            FilePath inputPath = await _inputPath.GetValueAsync(input, context);

            if (inputPath?.IsAbsolute != true)
            {
                inputPath = context.FileSystem.GetInputFile(new FilePath(Path.GetRandomFileName())).Path;
                context.LogWarning($"No input path found for document {input.ToSafeDisplayString()}, using {inputPath.FileName.FullPath}");
            }

            string content = await input.GetStringAsync();

            // Sass conversion
            FileImporter importer = new FileImporter(context.FileSystem, _importPathFunc);
            ScssOptions  options  = new ScssOptions
            {
                OutputStyle       = _outputStyle,
                GenerateSourceMap = _generateSourceMap,
                SourceComments    = _includeSourceComments,
                InputFile         = inputPath.FullPath,
                TryImport         = importer.TryImport
            };
            IEnumerable <string> includePaths = _includePaths
                                                .Where(x => x != null)
                                                .Select(x => x.IsAbsolute ? x.FullPath : context.FileSystem.GetContainingInputPath(x)?.Combine(x)?.FullPath)
                                                .Where(x => x != null);

            options.IncludePaths.AddRange(includePaths);
            ScssResult result = Scss.ConvertToCss(content, options);

            // Process the result
            DirectoryPath relativeDirectory = context.FileSystem.GetContainingInputPath(inputPath);
            FilePath      relativePath      = relativeDirectory?.GetRelativePath(inputPath) ?? inputPath.FileName;

            FilePath  cssPath     = relativePath.ChangeExtension("css");
            IDocument cssDocument = input.Clone(
                cssPath,
                await context.GetContentProviderAsync(result.Css ?? string.Empty));

            // Generate a source map if requested
            if (_generateSourceMap && result.SourceMap != null)
            {
                FilePath  sourceMapPath     = relativePath.ChangeExtension("map");
                IDocument sourceMapDocument = input.Clone(
                    sourceMapPath,
                    await context.GetContentProviderAsync(result.SourceMap));
                return(new[] { cssDocument, sourceMapDocument });
            }

            return(cssDocument.Yield());
        }
示例#5
0
 protected override IEnumerable <IDocument> Execute(object content, IDocument input, IExecutionContext context)
 {
     if (content == null)
     {
         content = string.Empty;
     }
     if (string.IsNullOrEmpty(_search))
     {
         return(new[] { input.Clone(content.ToString()) });
     }
     return(new[] { input.Clone(content.ToString().Replace(_search, input.Content)) });
 }
示例#6
0
文件: ReplaceIn.cs 项目: Rohansi/Wyam
 protected override IEnumerable<IDocument> Execute(object content, IDocument input, IExecutionContext context)
 {
     if (content == null)
     {
         content = string.Empty;
     }
     if (string.IsNullOrEmpty(_search))
     {
         return new[] { input.Clone(content.ToString()) };
     }
     return new[] { input.Clone(content.ToString().Replace(_search, input.Content)) };
 }
示例#7
0
        protected override async Task <IEnumerable <IDocument> > ExecuteInputAsync(IDocument input, IExecutionContext context)
        {
            MetadataDictionary metadata;

            using (Stream contentStream = input.GetContentStream())
            {
                metadata = await JsonSerializer.DeserializeAsync <MetadataDictionary>(contentStream, _options, context.CancellationToken);
            }
            if (!string.IsNullOrEmpty(_key))
            {
                input = input.Clone(new[] { new KeyValuePair <string, object>(_key, metadata) });
            }
            return(_populateDocument ? input.Clone(metadata).Yield() : input.Yield());
        }
示例#8
0
 protected override Task <IEnumerable <IDocument> > ExecuteConfigAsync(IDocument input, IExecutionContext context, object value) =>
 Task.FromResult(
     (_onlyIfNonExisting && input.ContainsKey(_key)) || (_ignoreNull && value == null)
             ? input.Yield()
             : input.Clone(new MetadataItems {
     { _key, value }
 }).Yield());
 public static IDocument CloneOrCreateDocument(
     this IDocumentFactory documentFactory,
     IDocument document,
     NormalizedPath destination,
     IContentProvider contentProvider = null) =>
 document?.Clone(destination, contentProvider)
 ?? documentFactory.CreateDocument(destination, contentProvider);
示例#10
0
        /// <inheritdoc />
        protected override async Task <IEnumerable <IDocument> > ExecuteAsync(IDocument input, IExecutionContext context)
        {
            FilePath sidecarPath = await _sidecarPath.GetValueAsync(input, context);

            if (sidecarPath != null)
            {
                IFile sidecarFile = context.FileSystem.GetInputFile(sidecarPath);
                if (sidecarFile.Exists)
                {
                    string sidecarContent = await sidecarFile.ReadAllTextAsync();

                    foreach (IDocument result in await context.ExecuteModulesAsync(Children, input.Clone(await context.GetContentProviderAsync(sidecarContent)).Yield()))
                    {
                        return(input.Clone(result).Yield());
                    }
                }
                else
                {
                    return(input.Yield());
                }
            }
            else
            {
                return(input.Yield());
            }
            return(null);
        }
示例#11
0
        protected override async Task <IEnumerable <IDocument> > ExecuteInputAsync(IDocument input, IExecutionContext context)
        {
            byte[]       assembly = ScriptHelper.Compile(await input.GetContentStringAsync(), input, context);
            MemoryStream stream   = context.MemoryStreamFactory.GetStream(assembly);

            return(input.Clone(context.GetContentProvider(stream, ScriptMediaType)).Yield());
        }
        private async Task <InsertingStreamLocation> ExecuteShortcodesAsync(
            IDocument input,
            IExecutionContext context,
            ShortcodeLocation location,
            ImmutableDictionary <string, IShortcode> shortcodes)
        {
            // Execute the shortcode
            IDocument shortcodeResult = await shortcodes[location.Name].ExecuteAsync(location.Arguments, location.Content, input, context);
            IDocument mergedResult    = null;

            if (shortcodeResult != null)
            {
                // Merge output metadata with the current input document
                // Creating a new document is the easiest way to ensure all the metadata from shortcodes gets accumulated correctly
                mergedResult = input.Clone(shortcodeResult, shortcodeResult.ContentProvider);

                // Don't process nested shortcodes if it's the raw shortcode
                if (!location.Name.Equals(RawShortcode.RawShortcodeName, StringComparison.OrdinalIgnoreCase))
                {
                    // Recursively parse shortcodes
                    IDocument nestedResult = await ProcessShortcodesAsync(mergedResult, context);

                    if (nestedResult != null)
                    {
                        mergedResult = nestedResult;
                    }
                }
                return(new InsertingStreamLocation(location.FirstIndex, location.LastIndex, mergedResult));
            }

            return(new InsertingStreamLocation(location.FirstIndex, location.LastIndex, mergedResult));
        }
 public static IDocument CloneOrCreateDocument(
     this IDocumentFactory documentFactory,
     IDocument document,
     IEnumerable <KeyValuePair <string, object> > items,
     IContentProvider contentProvider = null) =>
 document?.Clone(items, contentProvider)
 ?? documentFactory.CreateDocument(items, contentProvider);
示例#14
0
        protected override IEnumerable <IDocument> Execute(IDocument input, IExecutionContext context)
        {
            List <Dictionary <string, object> > documentMetadata = new List <Dictionary <string, object> >();

            using (TextReader contentReader = new StreamReader(input.GetStream()))
            {
                YamlStream yamlStream = new YamlStream();
                yamlStream.Load(contentReader);
                foreach (YamlDocument document in yamlStream.Documents)
                {
                    // If this is a sequence, get a document for each item
                    if (document.RootNode is YamlSequenceNode rootSequence)
                    {
                        documentMetadata.AddRange(rootSequence.Children.Select(x => GetDocumentMetadata(x, context)));
                    }
                    else
                    {
                        // Otherwise, just get a single set of metadata
                        documentMetadata.Add(GetDocumentMetadata(document.RootNode, context));
                    }
                }
            }
            if (documentMetadata.Count == 0 && _flatten)
            {
                return(input.Yield());
            }
            return(documentMetadata.Select(metadata => input.Clone(metadata)));
        }
示例#15
0
        // Stream

        /// <summary>
        /// Clones this document.
        /// </summary>
        /// <param name="document">The document.</param>
        /// <param name="destination">The new destination or <c>null</c> to keep the existing destination.</param>
        /// <param name="items">New metadata items.</param>
        /// <param name="stream">A stream that contains the new content.</param>
        /// <param name="mediaType">The media type of the content.</param>
        /// <returns>A new document of the same type as this document.</returns>
        public static IDocument Clone(
            this IDocument document,
            NormalizedPath destination,
            IEnumerable <KeyValuePair <string, object> > items,
            Stream stream,
            string mediaType = null) =>
        document.Clone(null, destination, items, IExecutionContext.Current.GetContentProvider(stream, mediaType));
示例#16
0
 /// <summary>
 /// Clones this document.
 /// </summary>
 /// <param name="document">The document.</param>
 /// <param name="source">The new source. If this document already contains a source, then it's used and this is ignored.</param>
 /// <param name="destination">The new destination or <c>null</c> to keep the existing destination.</param>
 /// <param name="content">The new content.</param>
 /// <param name="mediaType">The media type of the content.</param>
 /// <returns>A new document of the same type as this document.</returns>
 public static async Task <IDocument> CloneAsync(
     this IDocument document,
     NormalizedPath source,
     NormalizedPath destination,
     string content,
     string mediaType = null) =>
 document.Clone(source, destination, null, await IExecutionContext.Current.GetContentProviderAsync(content, mediaType));
示例#17
0
        // String

        /// <summary>
        /// Clones this document.
        /// </summary>
        /// <param name="document">The document.</param>
        /// <param name="destination">The new destination or <c>null</c> to keep the existing destination.</param>
        /// <param name="items">New metadata items.</param>
        /// <param name="content">The new content.</param>
        /// <param name="mediaType">The media type of the content.</param>
        /// <returns>A new document of the same type as this document.</returns>
        public static async Task <IDocument> CloneAsync(
            this IDocument document,
            NormalizedPath destination,
            IEnumerable <KeyValuePair <string, object> > items,
            string content,
            string mediaType = null) =>
        document.Clone(null, destination, items, await IExecutionContext.Current.GetContentProviderAsync(content, mediaType));
示例#18
0
 /// <summary>
 /// Clones this document.
 /// </summary>
 /// <param name="document">The document.</param>
 /// <param name="source">The new source. If this document already contains a source, then it's used and this is ignored.</param>
 /// <param name="destination">The new destination or <c>null</c> to keep the existing destination.</param>
 /// <param name="stream">A stream that contains the new content.</param>
 /// <param name="mediaType">The media type of the content.</param>
 /// <returns>A new document of the same type as this document.</returns>
 public static IDocument Clone(
     this IDocument document,
     NormalizedPath source,
     NormalizedPath destination,
     Stream stream,
     string mediaType = null) =>
 document.Clone(source, destination, null, IExecutionContext.Current.GetContentProvider(stream, mediaType));
示例#19
0
 public static IDocument CloneOrCreateDocument(
     this IExecutionContext context,
     IDocument document,
     Stream stream,
     string mediaType = null) =>
 document?.Clone(context.GetContentProvider(stream, mediaType))
 ?? context.CreateDocument(context.GetContentProvider(stream, mediaType));
示例#20
0
        /// <inheritdoc />
        protected override async Task <IEnumerable <IDocument> > ExecuteInputAsync(IDocument input, IExecutionContext context)
        {
            NormalizedPath sidecarPath = await _sidecarPath.GetValueAsync(input, context);

            if (!sidecarPath.IsNull)
            {
                IFile sidecarFile = context.FileSystem.GetInputFile(sidecarPath);
                if (sidecarFile.Exists)
                {
                    IContentProvider sidecarContent = sidecarFile.GetContentProvider();
                    foreach (IDocument result in await context.ExecuteModulesAsync(Children, input.Clone(sidecarContent).Yield()))
                    {
                        return(input.Clone(result).Yield());
                    }
                }
                else
                {
                    return(input.Yield());
                }
            }
            else
            {
                return(input.Yield());
            }
            return(null);
        }
 public static async Task <IDocument> CloneOrCreateDocumentAsync(
     this IExecutionContext context,
     IDocument document,
     string content,
     string mediaType = null) =>
 document?.Clone(await context.GetContentProviderAsync(content, mediaType))
 ?? context.CreateDocument(await context.GetContentProviderAsync(content, mediaType));
示例#22
0
        protected override async Task <IEnumerable <IDocument> > ExecuteAsync(IDocument input, IExecutionContext context)
        {
            IEnumerable <IEnumerable <string> > records;

            using (Stream stream = input.GetStream())
            {
                records = CsvFile.GetAllRecords(stream);
            }

            StringBuilder builder   = new StringBuilder();
            bool          firstLine = true;

            builder.AppendLine("<table>");
            foreach (IEnumerable <string> row in records)
            {
                builder.AppendLine("<tr>");
                foreach (string cell in row)
                {
                    if (_firstLineHeader && firstLine)
                    {
                        builder.AppendLine($"<th>{cell}</th>");
                    }
                    else
                    {
                        builder.AppendLine($"<td>{cell}</td>");
                    }
                }
                builder.AppendLine("</tr>");
                firstLine = false;
            }
            builder.Append("</table>");
            return(input.Clone(await context.GetContentProviderAsync(builder.ToString())).Yield());
        }
示例#23
0
        protected override Task <IEnumerable <IDocument> > ExecuteInputAsync(IDocument input, IExecutionContext context)
        {
            if (input.TryGetValue(_fromKey, out object existingValue))
            {
                if (_format != null)
                {
                    if (existingValue is DateTime)
                    {
                        existingValue = ((DateTime)existingValue).ToString(_format);
                    }
                    else
                    {
                        existingValue = string.Format(_format, existingValue);
                    }
                }

                if (_formatFunc != null)
                {
                    existingValue = _formatFunc.Invoke(existingValue.ToString());
                }

                return(input.Clone(new[] { new KeyValuePair <string, object>(_toKey, existingValue) }).YieldAsync());
            }
            else
            {
                return(input.YieldAsync());
            }
        }
示例#24
0
        protected override async Task <IEnumerable <IDocument> > ExecuteInputAsync(IDocument input, IExecutionContext context)
        {
            var html = await HtmlHelper.ParseHtmlAsync(input);

            if (html is null)
            {
                return(input.Yield());
            }

            // Assumes there is at least on he heading in the document.
            // All h2 headings become entries in the table-of-contents, all h3 headings between two h2 elements
            // are output as children of the preceding h2

            // if the document starts with a h3 heading, return unchanged input document
            var elements = html.QuerySelectorAll("h2, h3");

            if ((elements.FirstOrDefault() as IHtmlHeadingElement)?.NodeName?.Equals("h2", StringComparison.OrdinalIgnoreCase) != true)
            {
                return(input.Yield());
            }

            var toc          = new List <TocEntry>();
            var currentEntry = default(TocEntry);

            foreach (var element in elements)
            {
                if (element is IHtmlHeadingElement heading)
                {
                    var level = Int32.Parse(element.NodeName.Substring(1));

                    if (level == 2)
                    {
                        currentEntry = GetTocEntry(heading);
                        toc.Add(currentEntry);
                    }
                    else if (level == 3)
                    {
                        if (currentEntry == null)
                        {
                            throw new InvalidOperationException();
                        }

                        var childEntry = GetTocEntry(heading);
                        currentEntry.Items.Add(childEntry);
                    }
                    else
                    {
                        throw new InvalidOperationException();
                    }
                }
            }

            var newMetadata = new Dictionary <string, object>()
            {
                { DocsTemplateKeys.ToC, toc.Select(x => x.ToDocument(context)).ToArray() }
            };

            return(input.Clone(newMetadata).Yield());
        }
示例#25
0
        protected override async Task <IEnumerable <IDocument> > ExecuteConfigAsync(IDocument input, IExecutionContext context, IMetadata values)
        {
            string content = values.GetString(Content);

            return(content == null
                ? input.Yield()
                : input.Clone(await context.GetContentProviderAsync(content, values.GetString(MediaType))).Yield());
        }
示例#26
0
        protected override async Task <IEnumerable <IDocument> > ExecuteInputAsync(IDocument input, IExecutionContext context)
        {
            var metadata = await _getMetadata(input, context);

            return(metadata == null
                ? input.Yield()
                : input.Clone(metadata).Yield());
        }
 public static async Task <IDocument> CloneOrCreateDocumentAsync(
     this IExecutionContext context,
     IDocument document,
     IEnumerable <KeyValuePair <string, object> > items,
     string content,
     string mediaType = null) =>
 document?.Clone(items, await context.GetContentProviderAsync(content, mediaType))
 ?? context.CreateDocument(items, await context.GetContentProviderAsync(content, mediaType));
示例#28
0
 public static IDocument CloneOrCreateDocument(
     this IExecutionContext context,
     IDocument document,
     IEnumerable <KeyValuePair <string, object> > items,
     Stream stream,
     string mediaType = null) =>
 document?.Clone(items, context.GetContentProvider(stream, mediaType))
 ?? context.CreateDocument(items, context.GetContentProvider(stream, mediaType));
示例#29
0
 private static IModule[] GetTopLevelIndexModules(IDocument archiveDoc) => new IModule[]
 {
     new ReplaceDocuments(Config.FromContext(ctx => archiveDoc.Clone(new MetadataItems {
         { Keys.Children, ctx.Inputs }
     }).Yield())),
     new AddTitle(),
     new SetDestination(Config.FromValue(archiveDoc.Destination.ChangeExtension(".html")), true)
 };
示例#30
0
        /// <inheritdoc />
        protected override async Task <IEnumerable <IDocument> > ExecuteConfigAsync(IDocument input, IExecutionContext context, string value)
        {
            if (value == null)
            {
                value = string.Empty;
            }
            if (string.IsNullOrEmpty(_search))
            {
                return(input.Clone(await context.GetContentProviderAsync(value)).Yield());
            }
            string inputContent = await input.GetContentStringAsync();

            string replaced = _isRegex
                ? Regex.Replace(inputContent, _search, value, _regexOptions)
                : value.Replace(_search, inputContent);

            return(input.Clone(await context.GetContentProviderAsync(replaced)).Yield());
        }
示例#31
0
 public static IDocument CloneOrCreateDocument(
     this IExecutionContext context,
     IDocument document,
     NormalizedPath source,
     NormalizedPath destination,
     Stream stream,
     string mediaType = null) =>
 document?.Clone(source, destination, context.GetContentProvider(stream, mediaType))
 ?? context.CreateDocument(source, destination, context.GetContentProvider(stream, mediaType));
示例#32
0
文件: Append.cs 项目: st1pps/Wyam
 protected override IEnumerable<IDocument> Execute(object content, IDocument input, IExecutionContext context)
 {
     return new [] { content == null ? input : input.Clone(input.Content + content) };
 }
示例#33
0
 protected override IDocument Execute(string content, IDocument input)
 {
     return input.Clone(content);
 }
示例#34
0
 protected override IDocument Execute(string content, IDocument input)
 {
     return input.Clone(new[] { new KeyValuePair<string, object>(_key, content) });
 }