~adnano/kiln-devel

kiln: frontmatter: add shorthand notation # v2 PROPOSED

oliverpool: 1
 frontmatter: add shorthand notation #

 3 files changed, 56 insertions(+), 4 deletions(-)
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~adnano/kiln-devel/patches/46572/mbox | git am -3
Learn more about email & git

[PATCH kiln v2] frontmatter: add shorthand notation # Export this patch

if the first content line is like
 # some title

it will be interpreted as
 ---
 title: "some title"
 ---

---

Taking into account our conversation regarding my
previous patch, I have moved the "title shortcut"
to be a frontmatter shorthand instead.

I also updated the doc.

Let me know what you think.

Olivier
---
 docs/kiln.1.scd     | 11 +++++++++++
 frontmatter.go      | 25 +++++++++++++++++++++++--
 frontmatter_test.go | 24 ++++++++++++++++++++++--
 3 files changed, 56 insertions(+), 4 deletions(-)

diff --git a/docs/kiln.1.scd b/docs/kiln.1.scd
index f69fab2..3274902 100644
--- a/docs/kiln.1.scd
+++ b/docs/kiln.1.scd
@@ -79,6 +79,17 @@ Example:
	Page content
	```

In the absence of a frontmatter, the first line will be interpreted as a title if
it starts with "# ".

Example

	```
	# Page title

	Page content
	```

The following keys are supported:

*title*
diff --git a/frontmatter.go b/frontmatter.go
index e89433f..7042d47 100644
--- a/frontmatter.go
+++ b/frontmatter.go
@@ -1,17 +1,38 @@
package main

import "bytes"
import (
	"bytes"
	"strconv"
)

var (
	frontmatterOpen  = []byte("---")
	frontmatterClose = []byte("\n---")
	lf               = []byte("\n")
	crlf             = []byte("\r\n")
	frontmatterShort = []byte("# ")
)

func extractFrontmatter(b []byte) (frontmatter, content []byte) {
	if !bytes.HasPrefix(b, frontmatterOpen) {
		return nil, b

		// check for short frontmatter: # some title
		var found bool
		b, found = bytes.CutPrefix(b, frontmatterShort)
		if !found {
			return nil, b
		}

		// extract title and format it as yaml
		title := b
		i := bytes.IndexByte(b, '\n')
		if i != -1 {
			title = bytes.TrimSuffix(title[:i], []byte("\r"))
		} else {
			i = len(b) - 1
		}

		return []byte("title: " + strconv.Quote(string(title))), b[i+1:]
	}
	next := b[len(frontmatterOpen):]
	if len(next) > 0 && !bytes.HasPrefix(next, lf) && !bytes.HasPrefix(next, crlf) {
diff --git a/frontmatter_test.go b/frontmatter_test.go
index db20ee8..9f17e8a 100644
--- a/frontmatter_test.go
+++ b/frontmatter_test.go
@@ -26,9 +26,9 @@ func TestExtractFrontmatter(t *testing.T) {
			Content:     "\nHello, world!",
		},
		{
			Raw:         "# Hello, world!",
			Raw:         "## Hello, world!",
			Frontmatter: "",
			Content:     "# Hello, world!",
			Content:     "## Hello, world!",
		},
		{
			Raw:         "---\ne: f\ng: h",
@@ -60,6 +60,26 @@ func TestExtractFrontmatter(t *testing.T) {
			Frontmatter: "",
			Content:     "----",
		},
		{
			Raw:         "---\n---\n# Hello, world!",
			Frontmatter: "",
			Content:     "\n# Hello, world!",
		},
		{
			Raw:         "# some title",
			Frontmatter: "title: \"some title\"",
			Content:     "",
		},
		{
			Raw:         "# some title\nwith content",
			Frontmatter: "title: \"some title\"",
			Content:     "with content",
		},
		{
			Raw:         "# some title\r\nwith content",
			Frontmatter: "title: \"some title\"",
			Content:     "with content",
		},
	}

	for _, test := range tests {
-- 
2.42.1