//---------------------------------------------------------------------------------------------------------------------

        /// <summary>Loads service the root directories for file system and web access.</summary>
        protected void GetRootDirectories()
        {
            if (fileRootDir != null || relativeUrl != null)
            {
                return;
            }
            if (RootDirectory != null)
            {
                if (RootDirectory.StartsWith("$(SERVICEROOT)"))
                {
                    fileRootDir = (RootDirectory.Replace("$(SERVICEROOT)", context.ServiceFileRoot)).Replace('/', System.IO.Path.DirectorySeparatorChar);
                    relativeUrl = RootDirectory.Replace("$(SERVICEROOT)", context.ServiceWebRoot);
                }
                else
                {
                    fileRootDir = (context.SiteRootFolder + "/" + RootDirectory).Replace('/', System.IO.Path.DirectorySeparatorChar);
                    relativeUrl = RootDirectory;
                }
                relativeUrl = Regex.Replace(relativeUrl, "/+$", String.Empty);
            }
            if (IconUrl != null)
            {
                IconUrl = IconUrl.Replace("$(SERVICEROOT)", context.ServiceWebRoot);
            }
            if (ViewUrl != null)
            {
                ViewUrl = ViewUrl.Replace("$(SERVICEROOT)", context.ServiceWebRoot);
            }
        }
Пример #2
0
        /// <summary>
        /// Initialise a new pseudo-pattern - http://patternlab.io/docs/pattern-pseudo-patterns.html
        /// </summary>
        /// <param name="filePath">The template file path</param>
        /// <param name="pseudoName">The pseudo pattern name</param>
        public Pattern(string filePath, string pseudoName)
        {
            if (string.IsNullOrEmpty(filePath))
            {
                return;
            }

            _filePath   = filePath;
            _pseudoName = pseudoName;

            // Parse the file path to replace data file extensions with template extensions (Needed for pseudo patterns)
            var path =
                ViewUrl.Replace(string.Format("~/{0}/", PatternProvider.FolderNamePattern), string.Empty)
                .Replace(PatternProvider.FileExtensionMustache, string.Empty)
                .Replace(PatternProvider.FileExtensionData, string.Empty);

            // Split the file path into fragments
            var pathFragments =
                path.Split(new[] { Path.AltDirectorySeparatorChar }, StringSplitOptions.RemoveEmptyEntries).ToList();

            if (pathFragments.Count <= 0)
            {
                return;
            }

            // Set the name to the last fragment
            _name = pathFragments[pathFragments.Count - 1];

            // If the name ends with a state value (e.g. @inprogress), split and set the state
            var nameFragments =
                _name.Split(new[] { PatternProvider.IdentifierState }, StringSplitOptions.RemoveEmptyEntries)
                .ToList();

            if (nameFragments.Count > 1)
            {
                _name  = nameFragments[0];
                _state = nameFragments[1];
            }

            // Remove the name from the fragments
            pathFragments.RemoveAt(pathFragments.Count - 1);

            // Set type and sub-type from the remaining fragments
            _type    = pathFragments.Count > 0 ? pathFragments[0] : string.Empty;
            _subType = pathFragments.Count > 1 ? pathFragments[1] : string.Empty;

            // Read contents of template
            _html = File.ReadAllText(_filePath);

            // Find references to other patterns with the contents of the template
            _lineages = new List <string>();

            foreach (
                var partial in
                Regex.Matches(_html, "{{>(.*?)}}")
                .Cast <Match>()
                .Select(match => match.Groups[1].Value.StripPatternParameters())
                .Where(partial => !_lineages.Contains(partial)))
            {
                _lineages.Add(partial);
            }

            _pseudoPatterns = new List <string>();
            _data           = new ViewDataDictionary();

            var folder = new DirectoryInfo(Path.GetDirectoryName(_filePath) ?? string.Empty);

            // Find all the data files for the pattern
            var dataFiles = folder.GetFiles(string.Concat("*", PatternProvider.FileExtensionData), SearchOption.AllDirectories)
                            .Where(d => d.Name.StartsWith(_name)).ToList();

            if (!string.IsNullOrEmpty(_pseudoName))
            {
                // If handling a pseudo pattern read in shared data files and the pseudo specific data file
                dataFiles =
                    dataFiles.Where(
                        d =>
                        !d.Name.Contains(PatternProvider.IdentifierPsuedo) ||
                        d.Name.EndsWith(string.Concat(_pseudoName, PatternProvider.FileExtensionData))).ToList();
            }
            else
            {
                // If the pattern isn't a pseudo pattern, create a list of pseudo patterns from data files
                foreach (
                    var pseudoNameFragments in
                    dataFiles.Where(d => d.Name.Contains(PatternProvider.IdentifierPsuedo))
                    .Select(dataFile => dataFile.Name.Replace(PatternProvider.FileExtensionData, string.Empty)
                            .Split(new[] { PatternProvider.IdentifierPsuedo },
                                   StringSplitOptions.RemoveEmptyEntries)
                            .ToList()).Where(pseudoNameFragments => pseudoNameFragments.Count > 0))
                {
                    pseudoName = pseudoNameFragments.Count > 1 ? pseudoNameFragments[1] : string.Empty;
                    if (!_pseudoPatterns.Contains(pseudoName))
                    {
                        _pseudoPatterns.Add(pseudoName);
                    }
                }

                // Ignore pseudo data files
                dataFiles = dataFiles.Where(d => !d.Name.Contains(PatternProvider.IdentifierPsuedo)).ToList();
            }

            // Append contents of data files into provider data collection
            _data = PatternProvider.AppendData(_data, dataFiles);
        }
Пример #3
0
        public Pattern(string filePath, string pseudoName)
        {
            if (string.IsNullOrEmpty(filePath))
            {
                return;
            }

            _filePath   = filePath;
            _pseudoName = pseudoName;

            var path =
                ViewUrl.Replace(string.Format("~/{0}/", PatternProvider.FolderNamePattern), string.Empty)
                .Replace(PatternProvider.FileExtensionMustache, string.Empty)
                .Replace(PatternProvider.FileExtensionData, string.Empty);

            var pathFragments =
                path.Split(new[] { Path.AltDirectorySeparatorChar }, StringSplitOptions.RemoveEmptyEntries).ToList();

            if (pathFragments.Count <= 0)
            {
                return;
            }

            _name = pathFragments[pathFragments.Count - 1];

            var nameFragments =
                _name.Split(new[] { PatternProvider.IdentifierState }, StringSplitOptions.RemoveEmptyEntries)
                .ToList();

            if (nameFragments.Count > 1)
            {
                _name  = nameFragments[0];
                _state = nameFragments[1];
            }

            pathFragments.RemoveAt(pathFragments.Count - 1);

            _type     = pathFragments.Count > 0 ? pathFragments[0] : string.Empty;
            _subType  = pathFragments.Count > 1 ? pathFragments[1] : string.Empty;
            _html     = File.ReadAllText(_filePath);
            _lineages = new List <string>();

            foreach (
                var partial in
                Regex.Matches(_html, "{{>(.*?)}}")
                .Cast <Match>()
                .Select(match => match.Groups[1].Value.StripPatternParameters())
                .Where(partial => !_lineages.Contains(partial)))
            {
                _lineages.Add(partial);
            }

            _pseudoPatterns = new List <string>();
            _data           = new ViewDataDictionary();

            var folder    = new DirectoryInfo(Path.GetDirectoryName(_filePath) ?? string.Empty);
            var dataFiles = folder.GetFiles(string.Concat("*", PatternProvider.FileExtensionData), SearchOption.AllDirectories)
                            .Where(d => d.Name.StartsWith(_name)).ToList();

            if (!string.IsNullOrEmpty(_pseudoName))
            {
                dataFiles =
                    dataFiles.Where(
                        d =>
                        !d.Name.Contains(PatternProvider.IdentifierPsuedo) ||
                        d.Name.EndsWith(string.Concat(_pseudoName, PatternProvider.FileExtensionData))).ToList();
            }
            else
            {
                foreach (
                    var pseudoNameFragments in
                    dataFiles.Where(d => d.Name.Contains(PatternProvider.IdentifierPsuedo))
                    .Select(dataFile => dataFile.Name.Replace(PatternProvider.FileExtensionData, string.Empty)
                            .Split(new[] { PatternProvider.IdentifierPsuedo },
                                   StringSplitOptions.RemoveEmptyEntries)
                            .ToList()).Where(pseudoNameFragments => pseudoNameFragments.Count > 0))
                {
                    pseudoName = pseudoNameFragments.Count > 1 ? pseudoNameFragments[1] : string.Empty;
                    if (!_pseudoPatterns.Contains(pseudoName))
                    {
                        _pseudoPatterns.Add(pseudoName);
                    }
                }

                dataFiles = dataFiles.Where(d => !d.Name.Contains(PatternProvider.IdentifierPsuedo)).ToList();
            }

            _data = PatternProvider.AppendData(_data, dataFiles);
        }