Пример #1
0
            public void SingleCharWithRepeatedDelimiterWithLeadingSpacesDoesNotSplit()
            {
                // Given
                Engine            engine   = new Engine();
                Pipeline          pipeline = new Pipeline("Pipeline", null);
                IExecutionContext context  = new ExecutionContext(engine, pipeline);

                IDocument[] inputs =
                {
                    context.GetDocument(@"FM1
FM2
  !!!!
Content1
Content2")
                };
                bool        executed    = false;
                FrontMatter frontMatter = new FrontMatter('!', new Execute((x, ctx) =>
                {
                    executed = true;
                    return(new[] { x });
                }));

                // When
                IEnumerable <IDocument> documents = frontMatter.Execute(inputs, context);

                // Then
                Assert.AreEqual(1, documents.Count());
                Assert.IsFalse(executed);
                Assert.AreEqual(@"FM1
FM2
  !!!!
Content1
Content2", documents.First().Content);
            }
Пример #2
0
        public void MatchingStringSplitsAtCorrectLocation()
        {
            // Given
            Engine engine = new Engine();

            engine.Trace.AddListener(new TestTraceListener());
            Metadata          metadata = new Metadata(engine);
            Pipeline          pipeline = new Pipeline("Pipeline", engine, null);
            IExecutionContext context  = new ExecutionContext(engine, pipeline);

            IDocument[] inputs             = { new Document(metadata, pipeline).Clone(@"FM1
FM2
ABC
Content1
Content2") };
            string      frontMatterContent = null;
            FrontMatter frontMatter        = new FrontMatter("ABC", new Execute((x, ctx) =>
            {
                frontMatterContent = x.Content;
                return(new[] { x });
            }));

            // When
            IEnumerable <IDocument> documents = frontMatter.Execute(inputs, context);

            // Then
            Assert.AreEqual(1, documents.Count());
            Assert.AreEqual(@"FM1
FM2
", frontMatterContent);
            Assert.AreEqual(@"Content1
Content2", documents.First().Content);
        }
Пример #3
0
        public void DashStringDoesNotSplitAtNonmatchingDashes()
        {
            // Given
            Engine engine = new Engine();

            engine.Trace.AddListener(new TestTraceListener());
            Metadata          metadata = new Metadata(engine);
            Pipeline          pipeline = new Pipeline("Pipeline", engine, null);
            IExecutionContext context  = new ExecutionContext(engine, pipeline);

            IDocument[] inputs      = { new Document(metadata, pipeline).Clone(@"FM1
FM2
---
Content1
Content2") };
            bool        executed    = false;
            FrontMatter frontMatter = new FrontMatter("-", new Execute((x, ctx) =>
            {
                executed = true;
                return(new[] { x });
            }));

            // When
            IEnumerable <IDocument> documents = frontMatter.Execute(inputs, context);

            // Then
            Assert.AreEqual(1, documents.Count());
            Assert.IsFalse(executed);
            Assert.AreEqual(@"FM1
FM2
---
Content1
Content2", documents.First().Content);
        }
Пример #4
0
        public void MultipleInputDocumentsResultsInMultipleOutputs()
        {
            // Given
            Engine engine = new Engine();

            engine.Trace.AddListener(new TestTraceListener());
            Metadata          metadata = new Metadata(engine);
            Pipeline          pipeline = new Pipeline("Pipeline", engine, null);
            IExecutionContext context  = new ExecutionContext(engine, pipeline);

            IDocument[] inputs             = { new Document(metadata, pipeline).Clone(@"AA
-
XX"), new Document(metadata, pipeline).Clone(@"BB
-
YY") };
            string      frontMatterContent = string.Empty;
            FrontMatter frontMatter        = new FrontMatter(new Execute((x, ctx) =>
            {
                frontMatterContent += x.Content;
                return(new[] { x });
            }));

            // When
            IEnumerable <IDocument> documents = frontMatter.Execute(inputs, context);

            // Then
            Assert.AreEqual(2, documents.Count());
            Assert.AreEqual(@"AA
BB
", frontMatterContent);
            Assert.AreEqual(@"XX", documents.First().Content);
            Assert.AreEqual(@"YY", documents.Skip(1).First().Content);
        }
Пример #5
0
            public void SingleCharWithSingleDelimiterSplitsAtCorrectLocation()
            {
                // Given
                Engine            engine   = new Engine();
                Pipeline          pipeline = new Pipeline("Pipeline", null);
                IExecutionContext context  = new ExecutionContext(engine, pipeline);

                IDocument[] inputs =
                {
                    context.GetDocument(@"FM1
FM2
!
Content1
Content2")
                };
                string      frontMatterContent = null;
                FrontMatter frontMatter        = new FrontMatter('!', new Execute((x, ctx) =>
                {
                    frontMatterContent = x.Content;
                    return(new[] { x });
                }));

                // When
                IEnumerable <IDocument> documents = frontMatter.Execute(inputs, context);

                // Then
                Assert.AreEqual(1, documents.Count());
                Assert.AreEqual(@"FM1
FM2
", frontMatterContent);
                Assert.AreEqual(@"Content1
Content2", documents.First().Content);
            }
Пример #6
0
            public void NoIgnoreDelimiterOnFirstLine()
            {
                // Given
                Engine            engine   = new Engine();
                Pipeline          pipeline = new Pipeline("Pipeline", null);
                IExecutionContext context  = new ExecutionContext(engine, pipeline);

                IDocument[] inputs =
                {
                    context.GetDocument(@"---
FM1
FM2
---
Content1
Content2")
                };
                string      frontMatterContent = null;
                FrontMatter frontMatter        = new FrontMatter(new Execute((x, ctx) =>
                {
                    frontMatterContent = x.Content;
                    return(new[] { x });
                })).IgnoreDelimiterOnFirstLine(false);

                // When
                IEnumerable <IDocument> documents = frontMatter.Execute(inputs, context);

                // Then
                Assert.AreEqual(1, documents.Count());
                Assert.AreEqual("\n", frontMatterContent);
                Assert.AreEqual(@"FM1
FM2
---
Content1
Content2", documents.First().Content);
            }
Пример #7
0
        protected override void Init()
        {
            // ge layout name from file name
            Name = System.IO.Path.GetFileNameWithoutExtension(this.Path);

            // extract parent layout from front matter
            if (FrontMatter.ContainsKey("layout"))
            {
                ParentLayout = (string)FrontMatter["layout"];
            }
        }
Пример #8
0
        protected void gridFrontMatter_RowDeleting(object sender, DevExpress.Web.Data.ASPxDataDeletingEventArgs e)
        {
            FrontMatter DeleteFrontMatter = new FrontMatter();

            DeleteFrontMatter.FrontMatterID = e.Keys["FrontMatterID"].ToString();
            rtm_services.DeleteFrontMatter(DeleteFrontMatter);
            e.Cancel = true;
            var projectId = Session["projectID"].ToString();

            gridFrontMatter.DataSource = rtm_services.GetFrontMatter(projectId);
            gridFrontMatter.DataBind();
        }
        public void ParsesDateTimesAndIsDraftNull(string mockPost, string publishedDate, string updatedDate)
        {
            FrontMatter frontMatter       = FrontMatterManager.GetFrontMatter(mockPost);
            DateTime    pbDate            = frontMatter.Published.Value;
            DateTime    upDate            = frontMatter.Updated.Value;
            DateTime    publishedDateTime = FrontMatterManager.FromISODateString(publishedDate);
            DateTime    updatedDateTime   = FrontMatterManager.FromISODateString(updatedDate);

            Assert.Equal(publishedDateTime.Ticks, pbDate.Ticks);
            Assert.Equal(updatedDateTime.Ticks, upDate.Ticks);
            Assert.False(frontMatter.IsDraft);
        }
        /// <summary>
        /// Blows up in case required fields are not available
        /// </summary>
        /// <param name="mapping">Variable dictionary from Post</param>
        /// <exception cref="FrontMatterMissingFieldsException">Thrown when required fields are missing</exception>
        private static void FrontMatterContentSanityCheck(FrontMatter frontMatter)
        {
            List <string> REQUIRED_FIELDS = new List <string> {
                "Title", "Slug", "Author", "Created"
            };

            foreach (string field in REQUIRED_FIELDS)
            {
                if (frontMatter[field] == null)
                {
                    string msg = string.Format("'{0}' is mandatory", field);
                    throw new FrontMatterMissingFieldsException(msg);
                }
            }
        }
        public void ParsesTitleSlugAndDraftFlag()
        {
            string      mockPost    = @"---
title: Docker Installation Notes
slug: docker-installation-notes
author: oreo
draft: true
date_created: 2018-10-13T16:08:55+00:00
---
<my-component>Some-data</my-component>
";
            FrontMatter frontMatter = FrontMatterManager.GetFrontMatter(mockPost);

            Assert.Equal("Docker Installation Notes", frontMatter.Title);
            Assert.Equal("docker-installation-notes", frontMatter.Slug);
            Assert.Null(frontMatter.Published);
            Assert.Null(frontMatter.Updated);
            Assert.True(frontMatter.IsDraft);
        }
Пример #12
0
        protected void gridFrontMatter_RowInserting(object sender, DevExpress.Web.Data.ASPxDataInsertingEventArgs e)
        {
            ASPxHtmlEditor txtDescription = (ASPxHtmlEditor)gridFrontMatter.FindEditFormLayoutItemTemplateControl("txtDescription");
            FrontMatter    NewFrontMatter = new FrontMatter();

            NewFrontMatter.ProjectID   = Guid.Parse(Session["projectID"].ToString());
            NewFrontMatter.Name        = e.NewValues["Name"].ToString();
            NewFrontMatter.Description = txtDescription.Html;



            rtm_services.NewFrontMatter(NewFrontMatter);
            e.Cancel = true;
            var projectId = Session["projectID"].ToString();

            gridFrontMatter.DataSource = rtm_services.GetFrontMatter(projectId);
            gridFrontMatter.DataBind();
            gridFrontMatter.CancelEdit();
        }
        public void ParsesThumbnail()
        {
            string      mockPost    = @"---
title: Docker Installation Notes
slug: docker-installation-notes
author: oreo
draft: true
date_created: 2018-10-13T16:08:55+00:00
thumbnail:
    id: my own image
    file: some/relative/path
    type: svg
---
<my-component>Some-data</my-component>
";
            FrontMatter frontMatter = FrontMatterManager.GetFrontMatter(mockPost);

            Assert.Equal("my own image", frontMatter.Thumbnail.Id);
            Assert.Equal("some/relative/path", frontMatter.Thumbnail.File);
            Assert.Equal("svg", frontMatter.Thumbnail.Type);
        }
        public static FrontMatter DeserializeContent(string contents)
        {
            try
            {
                string frontMatterYaml = GetFrontMatterFromText(contents);

                YamlStream   yaml  = new YamlStream();
                StringReader input = new StringReader(frontMatterYaml);

                Deserializer deserializer = new DeserializerBuilder()
                                            .WithNamingConvention(new CamelCaseNamingConvention())
                                            .Build();

                FrontMatter frontMatter = deserializer.Deserialize <FrontMatter>(input);
                return(frontMatter);
            }
            catch (Exception e)
            {
                throw new FrontMatterFormatException(e.Message);
            }
        }
Пример #15
0
        public static string Blog(string file)
        {
            string           contents = File.ReadAllText(file);
            MarkdownPipeline pipeline = new MarkdownPipelineBuilder()
                                        .UseAdvancedExtensions()
                                        .UseYamlFrontMatter()
                                        .Build();
            string      result      = Markdown.ToHtml(contents, pipeline);
            FrontMatter frontMatter = FrontMatterManager.GetFrontMatter(contents);
            BlogPost    model       = new BlogPost
            {
                Headline      = frontMatter.Title,
                Id            = frontMatter.Slug,
                Summary       = "",
                Tags          = frontMatter.Tags,
                DateCreated   = FrontMatterManager.FromDateTimeToISOString(frontMatter.Created.Value),
                DateUpdated   = frontMatter.Updated.HasValue ? FrontMatterManager.FromDateTimeToISOString(frontMatter.Updated.Value) : "",
                DatePublished = frontMatter.Updated.HasValue ? FrontMatterManager.FromDateTimeToISOString(frontMatter.Published.Value) : ""
            };
            string o = JsonConvert.SerializeObject(model);

            return(o);
        }
            public void EmptyFirstLineWithDelimiterTreatsAsFrontMatter()
            {
                // Given
                IExecutionContext context = new TestExecutionContext();

                IDocument[] inputs =
                {
                    new TestDocument(@"
---
FM1
FM2
---
Content1
Content2")
                };
                string      frontMatterContent = null;
                FrontMatter frontMatter        = new FrontMatter(new Execute((x, ctx) =>
                {
                    frontMatterContent = x.Content;
                    return(new[] { x });
                }));

                // When
                IEnumerable <IDocument> documents = frontMatter.Execute(inputs, context);

                // Then
                Assert.AreEqual(1, documents.Count());
                Assert.AreEqual(
                    @"
", frontMatterContent);
                Assert.AreEqual(
                    @"FM1
FM2
---
Content1
Content2", documents.First().Content);
            }
            public void SingleCharWithRepeatedDelimiterWithExtraLinesSplitsAtCorrectLocation()
            {
                // Given
                IExecutionContext context = new TestExecutionContext();

                IDocument[] inputs =
                {
                    new TestDocument(@"FM1
FM2

!!!!

Content1
Content2")
                };
                string      frontMatterContent = null;
                FrontMatter frontMatter        = new FrontMatter('!', new Execute((x, ctx) =>
                {
                    frontMatterContent = x.Content;
                    return(new[] { x });
                }));

                // When
                IEnumerable <IDocument> documents = frontMatter.Execute(inputs, context);

                // Then
                Assert.AreEqual(1, documents.Count());
                Assert.AreEqual(
                    @"FM1
FM2

", frontMatterContent);
                Assert.AreEqual(
                    @"
Content1
Content2", documents.First().Content);
            }
Пример #18
0
        public void SingleCharWithRepeatedDelimiterWithExtraLinesSplitsAtCorrectLocation()
        {
            // Given
            Engine engine = new Engine();

            engine.Trace.AddListener(new TestTraceListener());
            Pipeline          pipeline = new Pipeline("Pipeline", engine, null);
            IExecutionContext context  = new ExecutionContext(engine, pipeline);

            IDocument[] inputs             = { new Document(engine, pipeline).Clone(@"FM1
FM2

!!!!

Content1
Content2") };
            string      frontMatterContent = null;
            FrontMatter frontMatter        = new FrontMatter('!', new Execute((x, ctx) =>
            {
                frontMatterContent = x.Content;
                return(new[] { x });
            }));

            // When
            IEnumerable <IDocument> documents = frontMatter.Execute(inputs, context);

            // Then
            Assert.AreEqual(1, documents.Count());
            Assert.AreEqual(@"FM1
FM2

", frontMatterContent);
            Assert.AreEqual(@"
Content1
Content2", documents.First().Content);
        }
Пример #19
0
 public static string GetThumbnailFromFrontMatterOrContent(FrontMatter frontMatter, string content)
 {
     return("");
 }
Пример #20
0
        protected override void Init()
        {
            Permalink  = FrontMatter.ContainsKey("permalink") ? (string)FrontMatter["permalink"] : null;
            Layout     = FrontMatter.ContainsKey("layout") ? (string)FrontMatter["layout"] : null;
            Slug       = FrontMatter.ContainsKey("slug") ? (string)FrontMatter["slug"] : null;
            Published  = FrontMatter.ContainsKey("published") ? (bool)FrontMatter["published"] : true;
            NoCache    = FrontMatter.ContainsKey("no_cache") ? (bool)FrontMatter["no_cache"] : false;
            Categories = new List <string>();
            Tags       = new List <string>();

            // categories
            if (FrontMatter.ContainsKey("categories"))
            {
                var categories = FrontMatter["categories"] as List <object>;
                if (categories != null)
                {
                    Categories = categories.Select(item => item.ToString()).ToList();
                }
                else
                {
                    var commaSeparatedString = FrontMatter["categories"] as string;
                    if (commaSeparatedString != null)
                    {
                        Categories = commaSeparatedString.Split(new char[] { ',', ';', ' ' }, StringSplitOptions.RemoveEmptyEntries).ToList();
                    }
                }
            }

            // tags
            if (FrontMatter.ContainsKey("tags"))
            {
                var tags = FrontMatter["tags"] as List <object>;
                if (tags != null)
                {
                    Tags = tags.Select(item => item.ToString()).ToList();
                }
                else
                {
                    var commaSeparatedString = FrontMatter["tags"] as string;
                    if (commaSeparatedString != null)
                    {
                        Tags = commaSeparatedString.Split(new char[] { ',', ';', ' ' }, StringSplitOptions.RemoveEmptyEntries).ToList();
                    }
                }
            }

            // calculate Permalink
            if (String.IsNullOrWhiteSpace(Permalink))
            {
                Permalink = "/" + VirtualPath.Replace('\\', '/').Trim('/');

                // now we have /some_folder/file.html or /some_file.md
                // remove extension
                int idx = Permalink.LastIndexOf(".");
                if (idx != -1)
                {
                    Permalink = Permalink.Substring(0, idx);
                }

                // check if it ends with /index
                if (Permalink.EndsWith("/index", StringComparison.OrdinalIgnoreCase))
                {
                    Permalink = Permalink.Substring(0, Permalink.Length - 6);
                }

                if (String.IsNullOrEmpty(Permalink))
                {
                    Permalink = "/";
                }
            }

            // is it collection document?
            string[] permalinkParts = Permalink.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);

            if (permalinkParts.Length > 1 && permalinkParts[0].StartsWith("_"))
            {
                this.Collection = permalinkParts[0].Substring(1);
                var collectionMetadata = Site.GetCollectionMetadata(this.Collection);

                // calculate date and permalink from post URL
                string pageName = permalinkParts.Last();
                var    match    = Regex.Match(pageName, @"^(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})(-(?<title>.+))?");
                if (match.Success)
                {
                    string year  = match.Groups["year"].Value;
                    string month = match.Groups["month"].Value;
                    string day   = match.Groups["day"].Value;

                    // replace permalink
                    Permalink = collectionMetadata.Permalink
                                .Replace(":year", year)
                                .Replace(":month", month)
                                .Replace(":day", day)
                                .Replace(":title", match.Groups["title"].Value);

                    // replace date
                    Date = new DateTime(Int32.Parse(year), Int32.Parse(month), Int32.Parse(day));
                }
                else
                {
                    // replace '/_' in the beginning with '/'
                    Permalink = "/" + Permalink.Substring(2);
                }

                // published state
                this.Published = collectionMetadata.Output;

                // add page to a collection
                Site.AddCollectionPage(this);
            }

            // override date from front matter
            if (FrontMatter.ContainsKey("date"))
            {
                Date = (DateTime)FrontMatter["date"];
            }
        }
Пример #21
0
 public string GetFrontMatter(string key)
 {
     return(FrontMatter.ContainsKey(key)
         ? FrontMatter[key] : null);
 }