<commit_before>﻿using EdityMcEditface.HtmlRenderer;
using EdityMcEditface.HtmlRenderer.FileInfo;
using EdityMcEditface.HtmlRenderer.Transforms;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace EdityMcEditface.HtmlRenderer.Compiler
{
    public class HtmlCompiler : IContentCompiler
    {
        private IFileFinder fileFinder;
        private String outDir;
        private String layout;
        private ITargetFileInfoProvider fileInfoProvider;

        public HtmlCompiler(IFileFinder fileFinder, String outDir, String layout, ITargetFileInfoProvider fileInfoProvider)
        {
            this.fileFinder = fileFinder;
            this.outDir = outDir;
            this.layout = layout;
            this.fileInfoProvider = fileInfoProvider;
        }

        public void buildPage(String relativeFile)
        {
            relativeFile = relativeFile.TrimStartingPathChars();

            var outFile = Path.Combine(this.outDir, relativeFile);

            if(OutputExtension != null)
            {
                outFile = Path.ChangeExtension(outFile, OutputExtension);
            }

            ITargetFileInfo fileInfo = fileInfoProvider.GetFileInfo(relativeFile, null);
            TemplateEnvironment environment = new TemplateEnvironment(fileInfo.FileNoExtension, fileFinder);
            PageStack pageStack = new PageStack(environment, fileFinder);
            pageStack.ContentFile = fileInfo.HtmlFile;
            if (pageStack.Visible)
            {
                pageStack.pushLayout(layout);

                var pathBase = environment.PathBase;

                HtmlDocumentRenderer dr = new HtmlDocumentRenderer(environment);
                dr.addTransform(new HashTreeMenus(fileFinder));
                dr.addTransform(new ExpandRootedPaths(pathBase));
                if (!String.IsNullOrEmpty(pathBase))
                {
                    //Safe to fix relative urls last since it wont replace any that already start with the path base.
                    dr.addTransform(new FixRelativeUrls(pathBase));
                }
                var document = dr.getDocument(pageStack.Pages);
                var outDir = Path.GetDirectoryName(outFile);
                if (!Directory.Exists(outDir))
                {
                    Directory.CreateDirectory(outDir);
                }
                using (var writer = new StreamWriter(File.Open(outFile, FileMode.Create, FileAccess.Write, FileShare.None)))
                {
                    writer.Write(document.DocumentNode.OuterHtml);
                }
                fileFinder.CopyLinkedFiles(this.outDir, pageStack);
            }
        }

        public void copyProjectContent()<commit_msg>Added version string when compiling html pages.<commit_after>﻿using EdityMcEditface.HtmlRenderer;
using EdityMcEditface.HtmlRenderer.FileInfo;
using EdityMcEditface.HtmlRenderer.Transforms;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace EdityMcEditface.HtmlRenderer.Compiler
{
    public class HtmlCompiler : IContentCompiler
    {
        private IFileFinder fileFinder;
        private String outDir;
        private String layout;
        private ITargetFileInfoProvider fileInfoProvider;
        private String version;

        public HtmlCompiler(IFileFinder fileFinder, String outDir, String layout, ITargetFileInfoProvider fileInfoProvider)
        {
            this.fileFinder = fileFinder;
            this.outDir = outDir;
            this.layout = layout;
            this.fileInfoProvider = fileInfoProvider;
            this.version = DateTime.Now.ToString("yyyyMMddhhmmss");
        }

        public void buildPage(String relativeFile)
        {
            relativeFile = relativeFile.TrimStartingPathChars();

            var outFile = Path.Combine(this.outDir, relativeFile);

            if(OutputExtension != null)
            {
                outFile = Path.ChangeExtension(outFile, OutputExtension);
            }

            ITargetFileInfo fileInfo = fileInfoProvider.GetFileInfo(relativeFile, null);
            TemplateEnvironment environment = new TemplateEnvironment(fileInfo.FileNoExtension, fileFinder, version);
            PageStack pageStack = new PageStack(environment, fileFinder);
            pageStack.ContentFile = fileInfo.HtmlFile;
            if (pageStack.Visible)
            {
                pageStack.pushLayout(layout);

                var pathBase = environment.PathBase;

                HtmlDocumentRenderer dr = new HtmlDocumentRenderer(environment);
                dr.addTransform(new HashTreeMenus(fileFinder));
                dr.addTransform(new ExpandRootedPaths(pathBase));
                if (!String.IsNullOrEmpty(pathBase))
                {
                    //Safe to fix relative urls last since it wont replace any that already start with the path base.
                    dr.addTransform(new FixRelativeUrls(pathBase));
                }
                var document = dr.getDocument(pageStack.Pages);
                var outDir = Path.GetDirectoryName(outFile);
                if (!Directory.Exists(outDir))
                {
                    Directory.CreateDirectory(outDir);
                }
                using (var writer = new StreamWriter(File.Open(outFile, FileMode.Create, FileAccess.Write, FileShare.None)))
                {
                    writer.Write(document.DocumentNode.OuterHtml);
                }
                fileFinder.CopyLinkedFiles(this.outDir, pageStack);
            }
        }

        public void copyProjectContent()
{'fraction_non_alphanumeric': 0.07176781002638523, 'fraction_numerical': 0.0, 'mean_word_length': 2.1345093715545755, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3829207', 'n_tokens_mistral': 1402, 'n_tokens_neox': 1353, 'n_words': 293}