Example #1
0
        public static string UpdateFilePath(string path, string documentType, TemplateCollection templateCollection)
        {
            if (templateCollection == null)
            {
                return(path);
            }
            var templates = templateCollection[documentType];

            // Get default template extension
            if (templates == null || templates.Count == 0)
            {
                return(path);
            }

            var defaultTemplate = templates.FirstOrDefault(s => s.IsPrimary) ?? templates[0];

            return(Path.ChangeExtension(path, defaultTemplate.Extension));
        }
Example #2
0
 /// <summary>
 /// TemplateName can be either file or folder
 /// 1. If TemplateName is file, it is considered as the default template
 /// 2. If TemplateName is a folder, files inside the folder is considered as the template, each file is named after {DocumentType}.{extension}
 /// </summary>
 /// <param name="templateName"></param>
 /// <param name="resourceProvider"></param>
 public TemplateProcessor(ResourceCollection resourceProvider)
 {
     _resourceProvider = resourceProvider;
     Templates         = new TemplateCollection(resourceProvider);
 }
Example #3
0
 private IEnumerable <TemplateResourceInfo> ExtractDependentFilePaths(TemplateCollection templates)
 {
     return(templates.Values.SelectMany(s => s).SelectMany(s => s.Resources));
 }
Example #4
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);
        }
Example #5
0
        public static void UpdateFileMap(DocumentBuildContext context, string outputDirectory, TemplateCollection templateCollection)
        {
            //update internal XrefMap
            if (context.XRefSpecMap != null)
            {
                foreach (var pair in context.XRefSpecMap)
                {
                    string targetFilePath;
                    if (context.FileMap.TryGetValue(pair.Value.Href, out targetFilePath))
                    {
                        pair.Value.Href = targetFilePath;
                    }
                    else
                    {
                        Logger.LogWarning($"{pair.Value.Href} is not found in .filemap");
                    }
                }
            }

            context.SetExternalXRefSpec();
        }
Example #6
0
 /// <summary>
 /// TemplateName can be either file or folder
 /// 1. If TemplateName is file, it is considered as the default template
 /// 2. If TemplateName is a folder, files inside the folder is considered as the template, each file is named after {DocumentType}.{extension}
 /// </summary>
 /// <param name="templateName"></param>
 /// <param name="resourceProvider"></param>
 public TemplateProcessor(ResourceCollection resourceProvider)
 {
     _resourceProvider = resourceProvider;
     _global           = LoadGlobalJson(resourceProvider);
     Templates         = new TemplateCollection(resourceProvider);
 }