<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using CSF.IO;

namespace CSF.Zpt.BatchRendering
{
  /// <summary>
  /// Default implementation of <see cref="IRenderingJobFactory"/>.
  /// </summary>
  public class RenderingJobFactory : IRenderingJobFactory
  {
    #region fields

    private IZptDocumentFactory _documentFactory;

    #endregion

    #region methods

    public IEnumerable<IRenderingJob> GetRenderingJobs(IBatchRenderingOptions inputOutputInfo,
                                                       RenderingMode? mode)
    {
      IEnumerable<IRenderingJob> output;

      if(inputOutputInfo.InputStream != null)
      {
        output = ReadFromStandardInput(mode);
      }
      else
      {
        output = ReadFromInputPaths(inputOutputInfo, mode);
      }

      return output;
    }

    private IEnumerable<IRenderingJob> ReadFromStandardInput(RenderingMode? mode)
    {
      Func<IZptDocument> documentCreator;

      using(var stream = Console.OpenStandardInput())
      {
        if(mode == RenderingMode.Xml)
        {
          documentCreator = () => _documentFactory.CreateDocument(stream, RenderingMode.Xml);
        }
        else
        {
          documentCreator = () => _documentFactory.CreateDocument(stream, RenderingMode.Html);
        }
      }

      return new [] { new RenderingJob(documentCreator) };
    }

    private IEnumerable<IRenderingJob> ReadFromInputPaths(IBatchRenderingOptions inputOutputInfo,
                                                          RenderingMode? mode)
    {
      return inputOutputInfo.InputPaths.SelectMany(x => CreateRenderingJobs(x, inputOutputInfo, mode));
    }

    private IEnumerable<IRenderingJob> CreateRenderingJobs(FileSystemInfo inputPath,
                                                           IBatchRenderingOptions inputOutputInfo,
                                                           RenderingMode? mode)
    {
      IEnumerable<IRenderingJob> output;

      if(inputPath != null && (inputPath is FileInfo))
      {
        output = new [] { CreateRenderingJob((FileInfo) inputPath, mode, inputPath.GetParent()) };
      }
      else if(inputPath != null && (inputPath is DirectoryInfo))
      {
        var dir = (DirectoryInfo) inputPath;
<commit_msg>Fix bug in rendering job factory exposed by new tests<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using CSF.IO;

namespace CSF.Zpt.BatchRendering
{
  /// <summary>
  /// Default implementation of <see cref="IRenderingJobFactory"/>.
  /// </summary>
  public class RenderingJobFactory : IRenderingJobFactory
  {
    #region fields

    private IZptDocumentFactory _documentFactory;

    #endregion

    #region methods

    public IEnumerable<IRenderingJob> GetRenderingJobs(IBatchRenderingOptions inputOutputInfo,
                                                       RenderingMode? mode)
    {
      IEnumerable<IRenderingJob> output;

      if(inputOutputInfo.InputStream != null)
      {
        output = ReadFromStream(inputOutputInfo.InputStream, mode);
      }
      else
      {
        output = ReadFromInputPaths(inputOutputInfo, mode);
      }

      return output;
    }

    private IEnumerable<IRenderingJob> ReadFromStream(Stream stream, RenderingMode? mode)
    {
      Func<IZptDocument> documentCreator;

      if(mode == RenderingMode.Xml)
      {
        documentCreator = () => _documentFactory.CreateDocument(stream, RenderingMode.Xml);
      }
      else
      {
        documentCreator = () => _documentFactory.CreateDocument(stream, RenderingMode.Html);
      }

      return new [] { new RenderingJob(documentCreator) };
    }

    private IEnumerable<IRenderingJob> ReadFromInputPaths(IBatchRenderingOptions inputOutputInfo,
                                                          RenderingMode? mode)
    {
      return inputOutputInfo.InputPaths.SelectMany(x => CreateRenderingJobs(x, inputOutputInfo, mode));
    }

    private IEnumerable<IRenderingJob> CreateRenderingJobs(FileSystemInfo inputPath,
                                                           IBatchRenderingOptions inputOutputInfo,
                                                           RenderingMode? mode)
    {
      IEnumerable<IRenderingJob> output;

      if(inputPath != null && (inputPath is FileInfo))
      {
        output = new [] { CreateRenderingJob((FileInfo) inputPath, mode, inputPath.GetParent()) };
      }
      else if(inputPath != null && (inputPath is DirectoryInfo))
      {
        var dir = (DirectoryInfo) inputPath;

{'fraction_non_alphanumeric': 0.08463203463203463, 'fraction_numerical': 0.0, 'mean_word_length': 2.2428070175438597, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1450544', 'n_tokens_mistral': 1198, 'n_tokens_neox': 1153, 'n_words': 235}