예제 #1
0
        private static Manifest ParseManifest(XElement element)
        {
            Manifest result = null;

            if (element != null && element.Name.LocalName == ELEMENT_MANIFEST)
            {
                result = new Manifest
                {
                    Id = element.Attribute(ATTRIBUTE_ID)?.Value
                };
                foreach (var childElement in element.Elements())
                {
                    if (childElement.Name.LocalName == ELEMENT_ITEM)
                    {
                        var manifestItem = new ManifestItem
                        {
                            Id           = childElement.Attribute(ATTRIBUTE_ID)?.Value,
                            Href         = childElement.Attribute(ATTRIBUTE_HREF)?.Value,
                            MediaType    = childElement.Attribute(ATTRIBUTE_MEDIATYPE)?.Value,
                            Fallback     = childElement.Attribute(ATTRIBUTE_FALLBACK)?.Value,
                            Properties   = childElement.Attribute(ATTRIBUTE_PROPERTIES)?.Value,
                            MediaOverlay = childElement.Attribute(ATTRIBUTE_MEDIAOVERLAY)?.Value,
                        };
                        result.Items.Add(manifestItem);
                    }
                }
            }
            return(result);
        }
        public static SearchIndexItem ExtractItem(HtmlDocument html, string href, ManifestItem item)
        {
            var contentBuilder = new StringBuilder();

            if (html.DocumentNode.SelectNodes("/html/head/meta[@name='searchOption' and @content='noindex']") != null)
            {
                return(null);
            }

            var nodes = html.DocumentNode.SelectNodes("//*[contains(@class,'data-searchable')]") ?? Enumerable.Empty <HtmlNode>();

            nodes = nodes.Union(html.DocumentNode.SelectNodes("//article") ?? Enumerable.Empty <HtmlNode>());
            foreach (var node in nodes)
            {
                ExtractTextFromNode(node, contentBuilder);
            }

            var content = NormalizeContent(contentBuilder.ToString());
            var title   = ExtractTitleFromHtml(html);
            var langs   = ExtractLanguagesFromHtml(item);
            var type    = item.DocumentType == "Conceptual" ? "article" : "api";

            return(new SearchIndexItem {
                Type = type, Href = href, Title = title, Keywords = content, Languages = langs
            });
        }
예제 #3
0
        public void TestLiquidTemplateProcessSingleTemplateWithNoScriptShouldWork()
        {
            // 1. Prepare template
            var    templateName = "NoScript.liquid";
            string template     = @"
{% for item in model -%}
{{ item.name }}
{% endfor -%}
";
            var    model        = new
            {
                model = new[]
                {
                    new { name = "test1" },
                    new { name = "test2" },
                }
            };
            var modelFileName = Path.Combine(_inputFolder, "TestLiquidTemplateProcessor_NoScript.yml");
            var item          = new ManifestItem {
                DocumentType = string.Empty, Key = modelFileName, ModelFile = modelFileName, ResourceFile = modelFileName
            };

            ProcessTemplate(templateName, null, new[] { item }, model, _outputFolder, Tuple.Create("default.liquid", template));

            var outputFile = Path.Combine(_outputFolder, Path.ChangeExtension(modelFileName, string.Empty));

            Assert.True(File.Exists(outputFile));
            Assert.Equal(@"
test1
test2
", File.ReadAllText(outputFile));
        }
        private void OutputSearchIndex(string relativePath, string indexFile, Manifest manifest, string json)
        {
            string directory = Directory.GetParent(indexFile).FullName;

            Directory.CreateDirectory(directory); // If not created, create

            // Delete existing index
            foreach (string file in Directory.GetFiles(directory, "index.*json"))
            {
                File.Delete(file);
            }

            // Create new index
            File.WriteAllText(indexFile, json);

            var manifestItem = new ManifestItem
            {
                DocumentType = "Resource",
                Metadata     = new Dictionary <string, object>(),
            };

            manifestItem.OutputFiles.Add("resource", new OutputFileInfo
            {
                RelativePath = PathUtility.MakeRelativePath(relativePath, indexFile),
            });

            manifest.Files?.Add(manifestItem);
        }
예제 #5
0
        public override void Handle(HtmlDocument document, ManifestItem manifestItem, string inputFile, string outputFile)
        {
            _fileMapping[outputFile] = inputFile;

            // RFC 3986: relative-ref = relative-part [ "?" query ] [ "#" fragment ]
            _linksWithBookmark[outputFile] =
                (from node in GetNodesWithAttribute(document, "href")
                 let link = node.GetAttributeValue("href", null)
                            let bookmarkIndex = link.IndexOf("#")
                                                where bookmarkIndex != -1
                                                let bookmark = link.Substring(bookmarkIndex + 1)
                                                               let index = link.IndexOfAny(new[] { '?', '#' })
                                                                           let decodedLink = HttpUtility.UrlDecode(link.Remove(index))
                                                                                             where !WhiteList.Contains(bookmark) && TypeForwardedToPathUtility.IsRelativePath(decodedLink)
                                                                                             select new LinkItem
            {
                Title = node.InnerText,
                Href = TransformPath(outputFile, decodedLink),
                Bookmark = bookmark,
                SourceFragment = WebUtility.HtmlDecode(node.GetAttributeValue("data-raw-source", null)),
                SourceFile = WebUtility.HtmlDecode(node.GetAttributeValue("sourceFile", null)),
                SourceLineNumber = node.GetAttributeValue("sourceStartLineNumber", 0),
                TargetLineNumber = node.Line
            }).ToList();
            var anchors = GetNodeAttribute(document, "id").Concat(GetNodeAttribute(document, "name"));

            _registeredBookmarks[outputFile] = new HashSet <string>(anchors);
        }
예제 #6
0
        public void TestMustacheTemplateProcessSingleTemplateWithNoScriptShouldWork()
        {
            // 1. Prepare template
            var    templateName = "NoScript";
            string template     = @"
{{#model}}
name1={{name1}},
name2={{name2}};
{{/model}}
";
            var    model        = new
            {
                model = new object[]
                {
                    new { name1 = "test1" },
                    new { name2 = "test2" },
                }
            };
            var modelFileName = Path.Combine(_inputFolder, "TestTemplateProcessor_NoScript.yml");
            var item          = new ManifestItem {
                DocumentType = string.Empty, Key = modelFileName, ModelFile = modelFileName, ResourceFile = modelFileName
            };

            ProcessTemplate(templateName, null, new[] { item }, model, _outputFolder, Tuple.Create("default.tmpl", template));

            var outputFile = Path.Combine(_outputFolder, Path.ChangeExtension(modelFileName, string.Empty));

            Assert.True(File.Exists(outputFile));
            Assert.Equal(@"
name1=test1,
name2=;
name1=,
name2=test2;
", File.ReadAllText(outputFile));
        }
예제 #7
0
        public void TestMustacheTemplateProcessTemplateFolderWithDifferentTypeShouldWork()
        {
            var    templateName       = "TemplateFolder.html";
            string defaultTemplate    = @"
default:
{{#model}}
{{name}}
{{/model}}
";
            string conceptualTemplate = @"
conceptual:
{{#model}}
{{name}}
{{/model}}
";
            string script             = @"
function transform(model){
    model.model.push({name:'test2'});
    return model;
}";

            var model = new
            {
                model = new List <object>
                {
                    new { name = "test1" },
                }
            };

            string inputFolder = null;
            var    item1       = new ManifestItem {
                ModelFile = "TestTemplateProcessor_TemplateFolderWithDifferentType1.yml", Key = "x.yml", DocumentType = "Conceptual"
            };
            var item2 = new ManifestItem {
                DocumentType = string.Empty, ModelFile = "TestTemplateProcessor_TemplateFolderWithDifferentType2.yml", Key = "y.yml"
            };

            ProcessTemplate(templateName, inputFolder, new[] { item1, item2 }, model, _outputFolder,
                            Tuple.Create("default.html.tmpl", defaultTemplate),
                            Tuple.Create($"{templateName}/conceptual.md.tmpl", conceptualTemplate),
                            Tuple.Create("default.html.js", script),
                            Tuple.Create("conceptual.md.js", script)
                            );
            var outputFilePath1 = Path.Combine(_outputFolder, "TestTemplateProcessor_TemplateFolderWithDifferentType1.md");

            Assert.True(File.Exists(outputFilePath1));
            Assert.Equal(@"
conceptual:
test1
test2
", File.ReadAllText(outputFilePath1));
            var outputFilePath2 = Path.Combine(_outputFolder, "TestTemplateProcessor_TemplateFolderWithDifferentType2.html");

            Assert.True(File.Exists(outputFilePath2));
            Assert.Equal(@"
default:
test1
test2
", File.ReadAllText(outputFilePath2));
        }
예제 #8
0
        /// <summary>
        /// Root toc should always from working folder
        /// Parent toc is the first nearest toc
        /// </summary>
        /// <param name="context">The document build context</param>
        /// <param name="item">The manifest item</param>
        /// <returns>A class containing root toc path and parent toc path</returns>
        private static TocInfo GetTocInfo(IDocumentBuildContext context, ManifestItem item)
        {
            string       key           = GetFileKey(item.Key);
            RelativePath rootTocPath   = null;
            RelativePath parentTocPath = null;
            var          rootToc       = context.GetTocFileKeySet(RelativePath.WorkingFolder)?.FirstOrDefault();
            var          parentToc     = context.GetTocFileKeySet(key)?.FirstOrDefault();

            if (parentToc == null)
            {
                // fall back to get the toc file from the same directory
                var directory = ((RelativePath)key).GetDirectoryPath();
                parentToc = context.GetTocFileKeySet(directory)?.FirstOrDefault();
            }

            if (rootToc != null)
            {
                rootTocPath = GetFinalFilePath(rootToc, context);
            }

            if (parentToc != null)
            {
                parentTocPath = GetFinalFilePath(parentToc, context);
            }

            return(new TocInfo(rootTocPath, parentTocPath));
        }
예제 #9
0
 private static void AddFileCore(ManifestItem item, string extension, string targetRelativePath)
 {
     item.OutputFiles[extension] = new OutputFileInfo
     {
         RelativePath = targetRelativePath,
     };
 }
예제 #10
0
        /// <summary>
        /// Root toc should always from working folder
        /// Parent toc is the first nearest toc
        /// </summary>
        /// <param name="context">The document build context</param>
        /// <param name="item">The manifest item</param>
        /// <returns>A class containing root toc path and parent toc path</returns>
        private static TocInfo GetTocInfo(IDocumentBuildContext context, ManifestItem item)
        {
            string relativePath = item.OriginalFile;
            string key = GetFileKey(relativePath);
            RelativePath rootTocPath = null;
            RelativePath parentTocPath = null;
            var rootToc = context.GetTocFileKeySet(RelativePath.WorkingFolder)?.FirstOrDefault();
            var parentToc = context.GetTocFileKeySet(key)?.FirstOrDefault();
            if (parentToc == null)
            {
                // fall back to get the toc file from the same directory
                var directory = ((RelativePath)key).GetDirectoryPath();
                parentToc = context.GetTocFileKeySet(directory)?.FirstOrDefault();
            }

            if (rootToc != null)
            {
                rootTocPath = GetFinalFilePath(rootToc, context);
            }

            if (parentToc != null)
            {
                parentTocPath = GetFinalFilePath(parentToc, context);
            }

            return new TocInfo(rootTocPath, parentTocPath);
        }
예제 #11
0
 private bool AreManifestItemsEqual(ManifestItem first, ManifestItem second)
 {
     return(first.Id == second.Id &&
            first.Href == second.Href &&
            first.Properties == second.Properties &&
            first.ContentType == second.ContentType);
 }
예제 #12
0
        public void TestLiquidTemplateProcessNoTemplateShouldFail()
        {
            var modelFileName = "TestLiquidTemplateProcessor_NoTemplate.yml";
            var modelFile     = Path.Combine(_outputFolder, modelFileName);
            var model         = new
            {
                model = new List <dynamic>
                {
                    new { name = "test1" },
                    new { name = "test2" },
                }
            };
            var templateName = "NoTemplate.liquid";
            var item         = new ManifestItem {
                ModelFile = modelFileName, OriginalFile = modelFileName, DocumentType = string.Empty
            };

            ProcessTemplate(templateName, null, new[] { item }, model, _outputFolder);
            Assert.True(!File.Exists(modelFile));
            item = new ManifestItem {
                ModelFile = modelFileName, OriginalFile = modelFileName, ResourceFile = modelFileName, DocumentType = string.Empty
            };
            ProcessTemplate(templateName, null, new[] { item }, model, _outputFolder);
            Assert.True(File.Exists(modelFile));
        }
예제 #13
0
 public override void Handle(HtmlDocument document, ManifestItem manifestItem, string inputFile, string outputFile)
 {
     foreach (var node in document.DocumentNode.Descendants())
     {
         if (!node.HasAttributes)
         {
             continue;
         }
         foreach (var attr in node.ChildAttributes("sourceFile"))
         {
             attr.Remove();
         }
         foreach (var attr in node.ChildAttributes("sourceStartLineNumber"))
         {
             attr.Remove();
         }
         foreach (var attr in node.ChildAttributes("sourceEndLineNumber"))
         {
             attr.Remove();
         }
         foreach (var attr in node.ChildAttributes("data-raw-source"))
         {
             attr.Remove();
         }
     }
 }
예제 #14
0
        public static void AddFile(this Manifest manifest, ManifestItem item, string extension, string targetRelativePath)
        {
            if (manifest == null)
            {
                throw new ArgumentNullException(nameof(manifest));
            }
            if (item == null)
            {
                throw new ArgumentNullException(nameof(item));
            }
            if (extension == null)
            {
                throw new ArgumentNullException(nameof(extension));
            }
            if (targetRelativePath == null)
            {
                throw new ArgumentNullException(nameof(targetRelativePath));
            }
            if (targetRelativePath.Length == 0)
            {
                throw new ArgumentException("Value cannot be empty.", nameof(extension));
            }

            lock (manifest)
            {
                AddFileCore(item, extension, targetRelativePath);
            }
        }
예제 #15
0
 public SystemAttributes(DocumentBuildContext context, ManifestItem item, string lang)
 {
     Language = lang;
     GetTocInfo(context, item);
     TocRelativePath     = TocPath == null ? null : ((RelativePath)TocPath).MakeRelativeTo((RelativePath)item.ModelFile);
     RootTocRelativePath = RootTocPath == null ? null : ((RelativePath)RootTocPath).MakeRelativeTo((RelativePath)item.ModelFile);
     RelativePathToRoot  = (RelativePath.Empty).MakeRelativeTo((RelativePath)item.ModelFile);
 }
예제 #16
0
 public static SourceFileInfo FromManifestItem(ManifestItem manifestItem)
 {
     return(new SourceFileInfo
     {
         DocumentType = manifestItem.DocumentType,
         SourceRelativePath = manifestItem.SourceRelativePath
     });
 }
예제 #17
0
        public void HandleWithScopeWrapper(HtmlDocument document, ManifestItem manifestItem, string inputFile, string outputFile)
        {
            string phase = this.GetType().Name;

            using (new LoggerPhaseScope(phase, false))
            {
                Handle(document, manifestItem, inputFile, outputFile);
            }
        }
예제 #18
0
        public async Task LoadNcx_PathSpecifiedInManifestItem_LoadsXHtmlToc()
        {
            var testee       = new NcxLoader();
            var manifestItem = new ManifestItem(null, "toc.ncx", string.Empty, ContentType.Ncx);
            var file         = TestItemFactory.CreateFileFromString("content/toc.ncx", "<toc />");

            var result = await testee.LoadNcx(new[] { manifestItem }, "content/book.opf", new[] { file });

            Assert.AreEqual("content/toc.ncx", result.Path);
            Assert.AreEqual("<toc />", result.Doc.ToString());
        }
예제 #19
0
 public SystemAttributes(IDocumentBuildContext context, ManifestItem item, string lang)
 {
     Language = lang;
     var tuple = GetTocInfo(context, item);
     TocPath = tuple.ParentToc;
     RootTocPath = tuple.RootToc;
     var file = (RelativePath)item.ModelFile;
     TocRelativePath = tuple.ParentToc == null ? null : tuple.ParentToc.MakeRelativeTo(file);
     RootTocRelativePath = tuple.RootToc == null ? null : tuple.RootToc.MakeRelativeTo(file);
     RelativePathToRoot = (RelativePath.Empty).MakeRelativeTo(file);
     PathFromRoot = file.RemoveWorkingFolder();
 }
        public static IValidator GetValidator(ManifestItem version)
        {
#if NETFRAMEWORK
            if (version.Value.Contains("2"))
#else
            if (version.Value.Contains('2'))
#endif
            {
                return(new ManifestV2Validator());
            }
            return(null);
        }
예제 #21
0
        /// <summary>
        /// Prepare list of resources for f2d files
        /// </summary>
        /// <param name="item"></param>
        /// <param name="accessToken"></param>
        /// <returns></returns>
        private static async Task <List <string> > F2DDerivates(ManifestItem item, string accessToken)
        {
            JObject manifest = await GetDerivativeAsync(item.Path.BasePath + "manifest.json.gz", accessToken);

            List <string> files = new List <string>();

            files.Add("manifest.json.gz");

            files.AddRange(GetAssets(manifest));

            return(files);
        }
예제 #22
0
        /// <summary>
        /// Prepare list of resources for SVF files
        /// </summary>
        /// <param name="item"></param>
        /// <param name="accessToken"></param>
        /// <returns></returns>
        private static async Task <List <string> > SVFDerivates(ManifestItem item, string accessToken)
        {
            JObject manifest = await GetDerivativeAsync(item.Path.URN, accessToken);

            List <string> files = new List <string>();

            files.Add(item.Path.URN.Substring(item.Path.BasePath.Length));

            files.AddRange(GetAssets(manifest));

            return(files);
        }
예제 #23
0
        public void TestMustacheTemplateProcessSingleTemplateWithNoScriptWithPartialShouldWork()
        {
            // 1. Prepare template
            var    templateName = "NoScriptWithPartial";
            string template     = @"
{{#model}}
{{name}}
{{/model}}
{{>partial1}}
";
            string partial1     = @"partial 1:
{{>partial2}}";
            string partial2     = @"partial 2:
{{#model}}
{{name}}
{{/model}}
";
            var    model        = new
            {
                model = new[]
                {
                    new { name = "test1" },
                    new { name = "test2" },
                }
            };
            var modelFileName = Path.Combine(_inputFolder, "TestTemplateProcessor_NoScriptWithPartial.yml");
            var item          = new ManifestItem {
                DocumentType = string.Empty, Key = modelFileName, ModelFile = modelFileName, ResourceFile = modelFileName
            };

            ProcessTemplate(
                templateName,
                null,
                new[] { item },
                model,
                _outputFolder,
                Tuple.Create("default.tmpl", template),
                Tuple.Create("partial1.tmpl.partial", partial1),
                Tuple.Create("partial2.tmpl.partial", partial2));

            var outputFile = Path.Combine(_outputFolder, Path.ChangeExtension(modelFileName, string.Empty));

            Assert.True(File.Exists(outputFile));
            Assert.Equal(@"
test1
test2
partial 1:
partial 2:
test1
test2
", File.ReadAllText(outputFile));
        }
예제 #24
0
        public void TestLiquidTemplateProcessSingleTemplateWithNoScriptWithIncludeShouldWork()
        {
            // 1. Prepare template
            var    templateName = "NoScriptWithInclude.liquid";
            string template     = @"
{% for item in model -%}
{{ item.name }}
{% endfor -%}
{% include partial1 -%}
";
            string partial1     = @"partial 1:
{% include partial2 -%}";
            string partial2     = @"partial 2:
{% for item in model -%}
{{ item.name }}
{% endfor -%}
";
            var    model        = new
            {
                model = new[]
                {
                    new { name = "test1" },
                    new { name = "test2" },
                }
            };
            var modelFileName = "TestLiquidTemplateProcessor_NoScriptWithPartial.yml";
            var item          = new ManifestItem {
                DocumentType = string.Empty, OriginalFile = modelFileName, ModelFile = modelFileName, ResourceFile = modelFileName
            };

            ProcessTemplate(
                templateName,
                null,
                new[] { item },
                model,
                _outputFolder,
                Tuple.Create("default.liquid", template),
                Tuple.Create("_partial1.liquid", partial1),
                Tuple.Create("_partial2.liquid", partial2));

            var outputFile = Path.Combine(_outputFolder, Path.GetFileNameWithoutExtension(modelFileName));

            Assert.True(File.Exists(outputFile));
            Assert.Equal(@"
test1
test2
partial 1:
partial 2:
test1
test2
", File.ReadAllText(outputFile));
        }
예제 #25
0
        public async Task AddAsync(ManifestItem manifestItem)
        {
            await _semaphoreSlim.WaitAsync();

            try
            {
                _manifestCollection.Add(manifestItem);
            }
            finally
            {
                _semaphoreSlim.Release();
            }
        }
예제 #26
0
        public SystemAttributes(IDocumentBuildContext context, ManifestItem item, string lang)
        {
            Language = lang;
            var tuple = GetTocInfo(context, item);

            TocPath     = tuple.ParentToc;
            RootTocPath = tuple.RootToc;
            var file = (RelativePath)item.ModelFile;

            TocRelativePath     = tuple.ParentToc == null ? null : tuple.ParentToc.MakeRelativeTo(file);
            RootTocRelativePath = tuple.RootToc == null ? null : tuple.RootToc.MakeRelativeTo(file);
            RelativePathToRoot  = (RelativePath.Empty).MakeRelativeTo(file);
            PathFromRoot        = file.RemoveWorkingFolder();
        }
예제 #27
0
        public static HtmlNode GetHtmlOutputArticleNode(this ManifestItem manifestItem, string outputFolder)
        {
            HtmlNode articleNode = manifestItem.
                                   GetHtmlOutputDoc(outputFolder).
                                   DocumentNode.
                                   SelectSingleNode($"//article[@class='{UtilsConstants.ArticleClass}']");

            if (articleNode == null)
            {
                throw new InvalidDataException($"{nameof(ManifestItemExtensions)}: Html output {manifestItem.GetHtmlOutputRelPath()} has no article node");
            }

            return(articleNode);
        }
        public GenerationFormViewModel()
        {
            this.TemplateList = new TemplateSelectionListViewModel();
            TemplateList.LoadTemplatesFromFolder(@"e:\SkyDrive\Code\Projects\TalTek\CodeGEN\Templates\");

            //  this.SolutionManifest = ProjectManifest.LoadProperties("test.cgsav");

            this.SolutionManifest = new ProjectManifest();
            this.SolutionManifest.ProjectManifestAlias    = "Test manifest";
            this.SolutionManifest.ProjectManifestFileName = "_testManifes.cg";


            ManifestGroup grp = new ManifestGroup();

            grp.GroupAlias             = "Test 1234";
            grp.UIProviderAssemblyName = "";
            grp.UIProviderTypeName     = "?";

            ManifestItem mi1 = new ManifestItem();

            mi1.TemplateFileName = "c:\template1";
            mi1.TemplateAlias    = "Template 1 Alias";
            grp.Templates.Add(mi1);

            this.SolutionManifest.ProjectTemplateGroups.Add(grp);


            ManifestGroup grp2 = new ManifestGroup();

            grp2.GroupAlias             = "Test 32";
            grp2.UIProviderAssemblyName = "";
            grp2.UIProviderTypeName     = "?";

            ManifestItem mi2 = new ManifestItem();

            mi2.TemplateFileName = "c:\template1";
            mi2.TemplateAlias    = "Template 1 Alias";
            grp2.Templates.Add(mi2);
            this.SolutionManifest.ProjectTemplateGroups.Add(grp2);


            //this.SolutionManifest.SaveSettings("test.cgsav");


            //
            this.SolutionFile = SolutionManifest.ProjectManifestFileName;

            //
            this.TemplateProvider = new SQLTableSelection();
        }
예제 #29
0
        public void TestLiquidTemplateProcessSingleTemplateWithDependenciesShouldWork()
        {
            var templateName = "WithIncludes.liquid";

            string template = @"
{% ref reference1.html -%}
{% ref reference2.html -%}
{% for item in model -%}
{{ item.name }}
{% endfor -%}
";
            string script   = @"
function transform(text){
    var model = JSON.parse(text);
    model.model.push({name:'test2'});
    return model;
}";

            var model = new
            {
                model = new List <object>
                {
                    new { name = "test1" },
                }
            };

            var    modelFileName = "TestLiquidTemplateProcessor_WithIncludes.yml";
            string inputFolder   = null;
            var    item          = new ManifestItem {
                ModelFile = modelFileName, DocumentType = string.Empty, OriginalFile = modelFileName
            };

            ProcessTemplate(templateName, inputFolder, new[] { item }, model, _outputFolder,
                            Tuple.Create("default.html.liquid", template),
                            Tuple.Create("default.html.js", script),
                            Tuple.Create("reference1.html", string.Empty),
                            Tuple.Create("reference2.html", string.Empty)
                            );
            var outputFilePath = Path.Combine(_outputFolder, Path.ChangeExtension(modelFileName, "html"));

            Assert.True(File.Exists(outputFilePath));
            Assert.True(File.Exists(Path.Combine(_outputFolder, "reference1.html")));
            Assert.True(File.Exists(Path.Combine(_outputFolder, "reference2.html")));
            Assert.Equal(@"
test1
test2
", File.ReadAllText(outputFilePath));
        }
예제 #30
0
        public void TestMustacheTemplateProcessInvalidTemplateShouldFail()
        {
            var    templateName  = "InvalidTemplate.html";
            string inputFolder   = null;
            var    modelFileName = Path.Combine(_inputFolder, "TestTemplateProcessor_InvalidTemplate.yml");
            var    item          = new ManifestItem {
                ModelFile = modelFileName, DocumentType = string.Empty
            };

            ProcessTemplate(templateName, inputFolder, new[] { item }, new object(), _outputFolder,
                            Tuple.Create("default.invalidtmpl", string.Empty),
                            Tuple.Create("default.js", string.Empty),
                            Tuple.Create("reference1.html", string.Empty),
                            Tuple.Create("reference2.html", string.Empty)
                            );
        }
예제 #31
0
        public SyncHoleWorker(IStorageClient client,
                              IConfigManager configManager,
                              ManifestItem workerState)
        {
            FilePath       = workerState.FilePath;
            State          = workerState;
            _client        = client;
            _configManager = configManager;
            _fileInfo      = new FileInfo(FilePath);

            //use creation time for container name
            var format        = _configManager.VaultNameFormat;
            var containerName = _fileInfo.CreationTimeUtc.ToString(format);

            State.ContainerName = containerName;
        }
예제 #32
0
        public void TestMustacheTemplateProcessSingleTemplateWithIncludesShouldWork()
        {
            var templateName = "WithIncludes.html";

            string template = @"
{{ !include('reference1.html') }}
{{ !include('reference2.html') }}
{{#model}}
{{name}}
{{/model}}
";
            string script   = @"
function transform(model){
    model.model.push({name:'test2'});
    return model;
}";

            var model = new
            {
                model = new List <object>
                {
                    new { name = "test1" },
                }
            };

            var    modelFileName = Path.Combine(_inputFolder, "TestTemplateProcessor_WithIncludes.yml");
            string inputFolder   = null;
            var    item          = new ManifestItem {
                ModelFile = modelFileName, DocumentType = string.Empty, Key = modelFileName
            };

            ProcessTemplate(templateName, inputFolder, new[] { item }, model, _outputFolder,
                            Tuple.Create("default.html.tmpl", template),
                            Tuple.Create("default.html.js", script),
                            Tuple.Create("reference1.html", string.Empty),
                            Tuple.Create("reference2.html", string.Empty)
                            );
            var outputFilePath = Path.Combine(_outputFolder, Path.ChangeExtension(modelFileName, "html"));

            Assert.True(File.Exists(outputFilePath));
            Assert.True(File.Exists(Path.Combine(_outputFolder, "reference1.html")));
            Assert.True(File.Exists(Path.Combine(_outputFolder, "reference2.html")));
            Assert.Equal(@"
test1
test2
", File.ReadAllText(outputFilePath));
        }
        public static void Apply(PackageContext packageContext, ManifestItem manifestItem, Regex matchRegex, string replacement)
        {
            string manifestItemPath = null;

            if (manifestItem == null)
                throw new ArgumentNullException("manifestItem");

            if (manifestItem.Type != ManifestItemType.File)
                throw new ApplicationException("Can not replace text in a manifest item which is not a file.");

            manifestItemPath = packageContext.GetAbsoluteOutputPath(manifestItem.RelativePath);

            if (!File.Exists(manifestItemPath))
                throw new ApplicationException("Manifest item not found at path \"{0}\".".FormatString(manifestItemPath));

            File.WriteAllText(manifestItemPath, Apply(File.ReadAllText(manifestItemPath), matchRegex, replacement));
        }
예제 #34
0
        // TODO: change to use IDocumentBuildContext
        public static TemplateManifestItem Transform(DocumentBuildContext context, ManifestItem item, TemplateCollection templateCollection, string outputDirectory, bool exportMetadata, Func<string, string> metadataFilePathProvider)
        {
            if (item.Model == null || item.Model.Content == null) throw new ArgumentNullException("Content for item.Model should not be null!");
            var baseDirectory = context.BuildOutputFolder ?? string.Empty;
            var manifestItem = new TemplateManifestItem
            {
                DocumentType = item.DocumentType,
                OriginalFile = item.LocalPathFromRepoRoot,
                OutputFiles = new Dictionary<string, string>()
            };
            HashSet<string> missingUids = new HashSet<string>();
            if (templateCollection == null || templateCollection.Count == 0)
            {
                return manifestItem;
            }
            try
            {
                var model = item.Model.Content;
                var templates = templateCollection[item.DocumentType];
                // 1. process model
                if (templates == null)
                {
                    // Logger.LogWarning($"There is no template processing {item.DocumentType} document \"{item.LocalPathFromRepoRoot}\"");
                }
                else
                {
                    var systemAttrs = new SystemAttributes(context, item, TemplateProcessor.Language);
                    foreach (var template in templates)
                    {
                        var extension = template.Extension;
                        string outputFile = Path.ChangeExtension(item.ModelFile, extension);
                        string outputPath = Path.Combine(outputDirectory ?? string.Empty, outputFile);
                        var dir = Path.GetDirectoryName(outputPath);
                        if (!string.IsNullOrEmpty(dir)) Directory.CreateDirectory(dir);
                        string transformed;
                        var result = template.TransformModel(model, systemAttrs);

                        if (exportMetadata)
                        {
                            if (metadataFilePathProvider == null)
                            {
                                throw new ArgumentNullException(nameof(metadataFilePathProvider));
                            }

                            JsonUtility.Serialize(metadataFilePathProvider(outputPath), result.Model);
                        }

                        transformed = result.Result;


                        if (!string.IsNullOrWhiteSpace(transformed))
                        {
                            if (extension.Equals(".html", StringComparison.OrdinalIgnoreCase))
                            {
                                try
                                {
                                    TranformHtml(context, transformed, item.ModelFile, outputPath);
                                }
                                catch (AggregateException e)
                                {
                                    e.Handle(s =>
                                    {
                                        var xrefExcetpion = s as CrossReferenceNotResolvedException;
                                        if (xrefExcetpion != null)
                                        {
                                            missingUids.Add(xrefExcetpion.UidRawText);
                                            return true;
                                        }
                                        else
                                        {
                                            return false;
                                        }
                                    });
                                }
                            }
                            else
                            {
                                File.WriteAllText(outputPath, transformed, Encoding.UTF8);
                            }

                            Logger.Log(LogLevel.Verbose, $"Transformed model \"{item.ModelFile}\" to \"{outputPath}\".");
                        }
                        else
                        {
                            // TODO: WHAT to do if is transformed to empty string? STILL creat empty file?
                            Logger.LogWarning($"Model \"{item.ModelFile}\" is transformed to empty string with template \"{template.Name}\"");
                            File.WriteAllText(outputPath, string.Empty);
                        }
                        manifestItem.OutputFiles.Add(extension, outputFile);
                    }
                }

                // 2. process resource
                if (item.ResourceFile != null)
                {
                    PathUtility.CopyFile(Path.Combine(baseDirectory, item.ResourceFile), Path.Combine(outputDirectory, item.ResourceFile), true);
                    manifestItem.OutputFiles.Add("resource", item.ResourceFile);
                }
            }
            catch (Exception e)
            {
                Logger.LogError($"Unable to transform {item.ModelFile}: {e.Message}. Ignored.");
                throw;
            }

            if (missingUids.Count > 0)
            {
                var uids = string.Join(", ", missingUids.Select(s => $"\"{s}\""));
                Logger.LogWarning($"Unable to resolve cross-reference {uids} for \"{manifestItem.OriginalFile.ToDisplayPath()}\"");
            }

            return manifestItem;
        }
        public static void Apply(PackageContext packageContext, ManifestItem manifestItem, string match, string replacement)
        {
            Regex matchRegex = new Regex(match);

            Apply(packageContext, manifestItem, matchRegex, replacement);
        }
예제 #36
0
        public static TemplateManifestItem Transform(DocumentBuildContext context, ManifestItem item, TemplateCollection templateCollection, string outputDirectory, bool exportMetadata, Func<string, string> metadataFilePathProvider)
        {
            var baseDirectory = context.BuildOutputFolder ?? string.Empty;
            var manifestItem = new TemplateManifestItem
            {
                DocumentType = item.DocumentType,
                OriginalFile = item.LocalPathFromRepoRoot,
                OutputFiles = new Dictionary<string, string>()
            };
            if (templateCollection == null || templateCollection.Count == 0)
            {
                return manifestItem;
            }
            try
            {
                var model = item.Model?.Content;
                var templates = templateCollection[item.DocumentType];
                // 1. process model
                if (templates == null)
                {
                    // Logger.LogWarning($"There is no template processing {item.DocumentType} document \"{item.LocalPathFromRepoRoot}\"");
                }
                else
                {
                    var modelFile = Path.Combine(baseDirectory, item.ModelFile);
                    var systemAttrs = new SystemAttributes(context, item, TemplateProcessor.Language);
                    foreach (var template in templates)
                    {
                        var extension = template.Extension;
                        string outputFile = Path.ChangeExtension(item.ModelFile, extension);
                        string outputPath = Path.Combine(outputDirectory ?? string.Empty, outputFile);
                        var dir = Path.GetDirectoryName(outputPath);
                        if (!string.IsNullOrEmpty(dir)) Directory.CreateDirectory(dir);
                        string transformed;
                        if (model == null)
                        {
                            // TODO: remove
                            // currently keep to pass UT
                            transformed = template.Transform(item.ModelFile, systemAttrs);
                        }
                        else
                        {
                            var result = template.TransformModel(model, systemAttrs);

                            if (exportMetadata)
                            {
                                if (metadataFilePathProvider == null)
                                {
                                    throw new ArgumentNullException(nameof(metadataFilePathProvider));
                                }

                                JsonUtility.Serialize(metadataFilePathProvider(outputPath), result.Model);
                            }

                            transformed = result.Result;
                        }

                        if (!string.IsNullOrWhiteSpace(transformed))
                        {
                            if (extension.Equals(".html", StringComparison.OrdinalIgnoreCase))
                            {
                                TranformHtml(context, transformed, item.ModelFile, outputPath);
                            }
                            else
                            {
                                File.WriteAllText(outputPath, transformed, Encoding.UTF8);
                            }

                            Logger.Log(LogLevel.Verbose, $"Transformed model \"{item.ModelFile}\" to \"{outputPath}\".");
                        }
                        else
                        {
                            // TODO: WHAT to do if is transformed to empty string? STILL creat empty file?
                            Logger.LogWarning($"Model \"{item.ModelFile}\" is transformed to empty string with template \"{template.Name}\"");
                            File.WriteAllText(outputPath, string.Empty);
                        }
                        manifestItem.OutputFiles.Add(extension, outputFile);
                    }
                }

                // 2. process resource
                if (item.ResourceFile != null)
                {
                    PathUtility.CopyFile(Path.Combine(baseDirectory, item.ResourceFile), Path.Combine(outputDirectory, item.ResourceFile), true);
                    manifestItem.OutputFiles.Add("resource", item.ResourceFile);
                }
            }
            catch (Exception e)
            {
                Logger.LogWarning($"Unable to transform {item.ModelFile}: {e.Message}. Ignored.");
            }

            return manifestItem;
        }