Beispiel #1
0
        public void CanParseTags()
        {
            var path = Path.GetFullPath(@"data\documents\hastags.txt");

            var command = new ParseDocumentCommand();

            command.DocumentPath = path;
            command.ExecuteAsync().Wait();

            Assert.Equal(new[] { "foo", "bar", "baz" }, command.Metadata.Get <string[]>("tags"));
        }
Beispiel #2
0
        public void Load()
        {
            var parser = new ParseDocumentCommand();

            parser.DocumentPath = this.SourcePath;
            parser.Execute();

            this.Content = parser.Content;

            this.Metadata = parser.Metadata;
        }
Beispiel #3
0
        public void CanParseNoMetadata()
        {
            var    path     = Path.GetFullPath(@"data\documents\nometa.txt");
            string expected = "This is text.\r\n   It has no metadata.";

            var command = new ParseDocumentCommand();

            command.DocumentPath = path;
            command.ExecuteAsync().Wait();

            Assert.Null(command.Date);
            Assert.False(command.Draft);
            Assert.Empty(command.Metadata);
            Assert.Equal(expected, command.Content);
        }
Beispiel #4
0
        public void CanParseMetadata()
        {
            var    path     = Path.GetFullPath(@"data\documents\hasmeta.txt");
            string expected = "This is text.\r\n   It has title metadata.";

            var command = new ParseDocumentCommand();

            command.DocumentPath = path;
            command.ExecuteAsync().Wait();

            Assert.Null(command.Date);
            Assert.NotEmpty(command.Metadata);
            Assert.Equal("Title from the metadata.", command.Metadata.Get <string>("title"));
            Assert.Equal(expected, command.Content);
        }
Beispiel #5
0
        public void Load(LoadDocumentFlags flags, IEnumerable <string> knownExtensions)
        {
            // Parse the document and update our document metadata.
            //
            var parser = new ParseDocumentCommand();

            parser.DocumentPath  = this.SourcePath;
            parser.SummaryMarker = "\r\n\r\n===";
            parser.Execute();

            this.Content = parser.Content;

            if (parser.Date.HasValue)
            {
                this.Date = parser.Date.Value;
            }

            this.Draft = (parser.Draft || this.Date > DateTime.Now);

            this.Paginate = parser.Metadata.Get <int>("paginate", 0);
            parser.Metadata.Remove("paginate");

            string output;

            if (parser.Metadata.TryGet <string>("output", out output))
            {
                this.SetOutputPaths(output);
                parser.Metadata.Remove("output");
            }

            this.Metadata = parser.Metadata;

            // The rest of this function is about calculating the correct
            // name for the file.
            //
            var fileName = Path.GetFileName(this.RelativePath);

            string updateFileName = null;

            string updateInPath = String.Empty;

            // See if this file should be processed by any of the
            // rendering engines.
            //
            this.ExtensionsForRendering = new List <string>();

            for (; ;)
            {
                var extension = Path.GetExtension(fileName).TrimStart('.');
                if (knownExtensions.Contains(extension))
                {
                    this.ExtensionsForRendering.Add(extension);
                    fileName = Path.GetFileNameWithoutExtension(fileName);

                    updateFileName = fileName;
                }
                else
                {
                    break;
                }
            }

            if (LoadDocumentFlags.DateFromFileName == (flags & LoadDocumentFlags.DateFromFileName))
            {
                var match = DateFromFileName.Match(fileName);

                if (match.Success)
                {
                    var year   = Convert.ToInt32(match.Groups[1].Value, 10);
                    var month  = Convert.ToInt32(match.Groups[2].Value, 10);
                    var day    = Convert.ToInt32(match.Groups[3].Value, 10);
                    var hour   = match.Groups[4].Success ? Convert.ToInt32(match.Groups[4].Value, 10) : 0;
                    var minute = match.Groups[5].Success ? Convert.ToInt32(match.Groups[5].Value, 10) : 0;
                    var second = match.Groups[6].Success ? Convert.ToInt32(match.Groups[6].Value, 10) : 0;

                    // If the parser didn't override the date, use the date from the filename.
                    //
                    if (!parser.Date.HasValue)
                    {
                        this.Date = new DateTime(year, month, day, hour, minute, second);
                    }

                    fileName = fileName.Substring(match.Length);

                    updateFileName = fileName;
                }
            }

            if (LoadDocumentFlags.DateInPath == (flags & LoadDocumentFlags.DateInPath) && this.Date != DateTime.MinValue)
            {
                updateInPath = String.Join("\\", this.Date.Year, this.Date.Month, this.Date.Day);
            }

            if (!this.Metadata.Contains("title"))
            {
                this.Metadata.Add("title", Path.GetFileNameWithoutExtension(fileName));
            }

            // Sanitize the filename into a good URL.
            //
            var sanitized = SanitizeEntryId(fileName);

            if (!fileName.Equals(sanitized))
            {
                fileName = sanitized;

                updateFileName = fileName;
            }

            if (LoadDocumentFlags.CleanUrls == (flags & LoadDocumentFlags.CleanUrls) && !"index.html".Equals(fileName, StringComparison.OrdinalIgnoreCase) && ".html".Equals(Path.GetExtension(fileName), StringComparison.OrdinalIgnoreCase))
            {
                updateInPath = Path.Combine(updateInPath, Path.GetFileNameWithoutExtension(fileName));

                fileName = "index.html";

                updateFileName = fileName;
            }

            // If the name or path was updated, update the appropriately parts of the document.
            //
            if (!String.IsNullOrEmpty(updateFileName) || !String.IsNullOrEmpty(updateInPath))
            {
                if (String.IsNullOrEmpty(updateFileName))
                {
                    updateFileName = fileName;
                }

                var updateInUrl = String.IsNullOrEmpty(updateInPath) ? String.Empty : updateInPath.Replace('\\', '/') + '/';

                this.RelativePath = Path.Combine(Path.GetDirectoryName(this.RelativePath), updateInPath, updateFileName);

                this.OutputPath = Path.Combine(Path.GetDirectoryName(this.OutputPath), updateInPath, updateFileName);

                var lastSlash = this.Url.LastIndexOf('/');

                this.Url = String.Concat(this.Url.Substring(0, lastSlash + 1), updateInUrl, updateFileName.Equals("index.html", StringComparison.OrdinalIgnoreCase) ? String.Empty : updateFileName);
            }
        }