diff --git a/README.md b/README.md
index 9ad28fa..06b5921 100644
--- a/README.md
+++ b/README.md
@@ -48,7 +48,7 @@ Pages: `Title`, `ID` (the same in every language, it links the translations), `D
### Placeholders
-`{{name}}`, with lowercase letters, digits and underscores, is replaced by a string from `site.json` or the language file. An unknown placeholder stops the build and names the file. Fenced code blocks are left as written, so an article can show a placeholder there; elsewhere a backslash before the placeholder (`\{{name}}`) keeps it as written, and Markdown drops the backslash. Code spans are substituted, which is how a page shows a value in monospace. `{{news_list}}`, `{{latest_news}}` and `{{categories_list}}` stand alone in a paragraph and expand to generated lists.
+`{{name}}`, with lowercase letters, digits and underscores, is replaced by a string from `site.json` or the language file. An unknown placeholder stops the build and names the file. Fenced code blocks (found the way CommonMark finds them, in lists and quotes too) are left as written, so an article can show a placeholder there. Anywhere else a backslash before the placeholder (`\{{name}}`) keeps it as written, in a code span too; two backslashes are one literal backslash followed by the value. Code spans are otherwise substituted, which is how a page shows a value in monospace. The id of a heading is not checked against ids that a template writes itself, such as the `news` of the latest news block. `{{news_list}}`, `{{latest_news}}` and `{{categories_list}}` stand alone in a paragraph and expand to generated lists.
## Development
diff --git a/internal/build/build_test.go b/internal/build/build_test.go
index 9d88ad0..1d6623d 100644
--- a/internal/build/build_test.go
+++ b/internal/build/build_test.go
@@ -407,6 +407,23 @@ func TestBuildPlaceholdersAndCode(t *testing.T) {
mustContain(t, "people", got, "en", "escaped {{lang}}.", "{{lang}} {{nope}}", "And en.")
}
+// TestBuildEscapedPlaceholders checks the escape with block placeholders: an
+// escaped one inside a sentence or alone in a paragraph is shown, not expanded
+// and not an error, and a code span can show a placeholder as written.
+func TestBuildEscapedPlaceholders(t *testing.T) {
+ files := baseFiles()
+ files["content/en/people.md"] = page("people", "People", "", "See \\{{news_list}} here.\n\n\\{{latest_news}}\n\nAnd `\\{{lang}}`.\n")
+ out, err := runBuild(t, files)
+ if err != nil {
+ t.Fatalf("Build() error = %v", err)
+ }
+ got := readOut(t, out, "people.html")
+ mustContain(t, "people", got, "See {{news_list}} here.", "
{{latest_news}}
", "{{lang}}")
+ if strings.Contains(got, "
p`}}
+ got := feed("https://kmalloc.org", feedLang(), "/news/", items, 5)
+ mustContain(t, "feed", got, `src="//cdn.example/x.png"`, `href="https://kmalloc.org/p.html"`)
+ if strings.Contains(got, "https://kmalloc.org//cdn") {
+ t.Error("a protocol-relative link got the site address")
+ }
+}
+
// TestFeedWithoutPosts checks that an empty feed is still a valid channel.
func TestFeedWithoutPosts(t *testing.T) {
got := feed("https://kmalloc.org", feedLang(), "/news/", nil, 20)
diff --git a/internal/build/pages.go b/internal/build/pages.go
index c873d09..31849d3 100644
--- a/internal/build/pages.go
+++ b/internal/build/pages.go
@@ -81,7 +81,7 @@ func (b *builder) renderPage(p content.Page) error {
// markdown replaces the placeholders of a source, converts it to HTML and
// expands the generated lists. The name is used in error messages.
func (b *builder) markdown(name, source, lang string, ctx map[string]string) (string, []mdx.Heading, error) {
- text, err := render.SubstituteMarkdown(name, source, ctx)
+ text, err := mdx.SubstituteMarkdown(name, source, ctx)
if err != nil {
return "", nil, err
}
@@ -89,8 +89,13 @@ func (b *builder) markdown(name, source, lang string, ctx map[string]string) (st
if err != nil {
return "", nil, fmt.Errorf("%s: %w", name, err)
}
- html, err = b.expandBlocks(name, html, lang, ctx)
- return html, headings, err
+ if html, err = b.expandBlocks(name, html, lang, ctx); err != nil {
+ return "", nil, err
+ }
+ for i := range headings {
+ headings[i].Text = render.Unescape(headings[i].Text)
+ }
+ return render.Unescape(html), headings, nil
}
// codeElement matches a code element, where a placeholder is shown as written.
diff --git a/internal/content/pages.go b/internal/content/pages.go
index 696dfe3..5931766 100644
--- a/internal/content/pages.go
+++ b/internal/content/pages.go
@@ -146,8 +146,8 @@ func loadPage(file, source, defaultLang, lang, rel string) (Page, error) {
// The body of the manifesto is the owner's text and is rewritten when the
// page is built; the keys of the header are not.
for _, key := range []string{"Title", "Description", "Note"} {
- if err := mdx.RejectEmDash(source+" (key "+key+")", h.Get(key)); err != nil {
- return Page{}, err
+ if mdx.HasEmDash(h.Get(key)) {
+ return Page{}, fmt.Errorf("%s: the key %s has an em dash, please remove it", source, key)
}
}
}
diff --git a/internal/mdx/emdash.go b/internal/mdx/emdash.go
index 932f980..2f6b888 100644
--- a/internal/mdx/emdash.go
+++ b/internal/mdx/emdash.go
@@ -38,22 +38,30 @@ func NoEmDash(md string) (string, error) {
for _, p := range parentheticals {
md = strings.ReplaceAll(md, p.from, p.to)
}
- if err := RejectEmDash("the body", md); err != nil {
- return "", err
+ for i, line := range strings.Split(md, "\n") {
+ if HasEmDash(line) {
+ return "", fmt.Errorf("line %d of the body: no em dashes, please", i+1)
+ }
}
return md, nil
}
// emDashEntity matches the HTML entities of the em dash, which would put it
// on the page as well.
-var emDashEntity = regexp.MustCompile(`(?i)—|—|—`)
+var emDashEntity = regexp.MustCompile(`(?i)—|*8212;|*2014;`)
+
+// HasEmDash reports whether text has an em dash, written as the character or as
+// an HTML entity.
+func HasEmDash(text string) bool {
+ return strings.Contains(text, emDash) || emDashEntity.MatchString(text)
+}
// RejectEmDash returns an error naming the file and the line of the first em
// dash in text, written as the character or as an HTML entity, or nil when
// there is none.
func RejectEmDash(name, text string) error {
for i, line := range strings.Split(text, "\n") {
- if strings.Contains(line, emDash) || emDashEntity.MatchString(line) {
+ if HasEmDash(line) {
return fmt.Errorf("%s:%d: no em dashes, please", name, i+1)
}
}
diff --git a/internal/mdx/emdash_test.go b/internal/mdx/emdash_test.go
index 8e13178..dcd1267 100644
--- a/internal/mdx/emdash_test.go
+++ b/internal/mdx/emdash_test.go
@@ -47,7 +47,7 @@ func TestNoEmDashLeftover(t *testing.T) {
// TestRejectEmDashEntities checks that the entities of the em dash are refused too.
func TestRejectEmDashEntities(t *testing.T) {
- for _, in := range []string{"a — b", "a — b", "a — b", "a — b", "a — b"} {
+ for _, in := range []string{"a — b", "a — b", "a — b", "a — b", "a — b", "a — b", "a — b"} {
if err := RejectEmDash("f.md", in); err == nil {
t.Errorf("RejectEmDash(%q) = nil, want an error", in)
}
@@ -61,8 +61,17 @@ func TestRejectEmDashEntities(t *testing.T) {
// body, which is how the caller counts it.
func TestNoEmDashErrorSaysBody(t *testing.T) {
_, err := NoEmDash("fine\nsome words \u2014 and then more\n")
- if err == nil || !strings.Contains(err.Error(), "the body:2") {
- t.Errorf("NoEmDash() error = %v, want it to say the body:2", err)
+ if err == nil || !strings.Contains(err.Error(), "line 2 of the body") {
+ t.Errorf("NoEmDash() error = %v, want it to say line 2 of the body", err)
+ }
+}
+
+// TestHasEmDash checks the test that the header keys use.
+func TestHasEmDash(t *testing.T) {
+ for in, want := range map[string]bool{"plain": false, "a \u2014 b": true, "a — b": true, "a – b": false, "": false} {
+ if got := HasEmDash(in); got != want {
+ t.Errorf("HasEmDash(%q) = %v, want %v", in, got, want)
+ }
}
}
diff --git a/internal/mdx/mdx_test.go b/internal/mdx/mdx_test.go
index c576ca4..9d47e40 100644
--- a/internal/mdx/mdx_test.go
+++ b/internal/mdx/mdx_test.go
@@ -5,6 +5,8 @@ package mdx
import (
"strings"
"testing"
+
+ "git.kmalloc.org/squid/motorino/internal/render"
)
// TestRenderParagraphAndList checks the basic blocks and inline markup.
@@ -177,6 +179,68 @@ func TestRenderBadExplicitIDCannotInject(t *testing.T) {
}
}
+// TestSubstituteMarkdownFences checks that placeholders in fenced code blocks
+// stay as written, wherever CommonMark finds the block, and that an unknown one
+// there is no error.
+func TestSubstituteMarkdownFences(t *testing.T) {
+ ctx := map[string]string{"lang": "en"}
+ tests := []struct{ name, src, want string }{
+ {"backtick fence", "A {{lang}}\n\n```\n{{lang}} {{nope}}\n```\n\nB {{lang}}\n", "A en\n\n```\n{{lang}} {{nope}}\n```\n\nB en\n"},
+ {"tilde fence with info", "~~~go\n{{lang}}\n~~~\n{{lang}}\n", "~~~go\n{{lang}}\n~~~\nen\n"},
+ {"code span is substituted", "Value `{{lang}}`.\n", "Value `en`.\n"},
+ {"fence in a quote", "> ```\n> {{nope}}\n> ```\n\n{{lang}}\n", "> ```\n> {{nope}}\n> ```\n\nen\n"},
+ {"fence in a list item", "1. item\n\n ```\n {{nope}}\n ```\n\n{{lang}}\n", "1. item\n\n ```\n {{nope}}\n ```\n\nen\n"},
+ {"code span at the start of a line is not a fence", "```x``` and {{lang}}\n\nthen {{lang}}\n", "```x``` and en\n\nthen en\n"},
+ {"unclosed fence runs to the end", "{{lang}}\n\n```\n{{nope}}\n{{lang}}\n", "en\n\n```\n{{nope}}\n{{lang}}\n"},
+ {"a closer indented four spaces does not close", "```\n ```\n{{nope}}\n```\n{{lang}}\n", "```\n ```\n{{nope}}\n```\nen\n"},
+ {"a closer may be longer", "```\n{{nope}}\n`````\n{{lang}}\n", "```\n{{nope}}\n`````\nen\n"},
+ {"CRLF line endings", "{{lang}}\r\n\r\n```\r\n{{nope}}\r\n```\r\n{{lang}}\r\n", "en\r\n\r\n```\r\n{{nope}}\r\n```\r\nen\r\n"},
+ {"empty fence", "```\n```\n{{lang}}\n", "```\n```\nen\n"},
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ got, err := SubstituteMarkdown("page.md", tt.src, ctx)
+ if err != nil {
+ t.Fatalf("SubstituteMarkdown() error = %v", err)
+ }
+ if got != tt.want {
+ t.Errorf("SubstituteMarkdown() = %q, want %q", got, tt.want)
+ }
+ })
+ }
+}
+
+// TestSubstituteMarkdownUnknownOutsideFence checks that an unknown placeholder
+// outside a fence still stops the build and names the file.
+func TestSubstituteMarkdownUnknownOutsideFence(t *testing.T) {
+ _, err := SubstituteMarkdown("news/x.md", "```\n{{a}}\n```\n\nbad {{nope}}\n", map[string]string{})
+ if err == nil || !strings.Contains(err.Error(), "news/x.md") || !strings.Contains(err.Error(), "nope") {
+ t.Errorf("SubstituteMarkdown() error = %v, want it to name news/x.md and nope", err)
+ }
+}
+
+// TestSubstituteMarkdownEscapeEverywhere checks that a backslash keeps a
+// placeholder as written in text, in a code span and next to a block name, and
+// that the written form comes back through render.Unescape.
+func TestSubstituteMarkdownEscapeEverywhere(t *testing.T) {
+ src := "Text \\{{lang}} and `\\{{lang}}` and \\{{news_list}}.\n"
+ got, err := SubstituteMarkdown("page.md", src, map[string]string{"lang": "en"})
+ if err != nil {
+ t.Fatal(err)
+ }
+ html, _, err := Render(got)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if strings.Contains(html, "{{") {
+ t.Errorf("html %q still has braces before Unescape", html)
+ }
+ want := "Text {{lang}} and {{lang}} and {{news_list}}.