motorino/internal/mdx/emdash.go
tgiachi 4ab8f8d122
All checks were successful
check / check (pull_request) Successful in 30s
fix: find code blocks with the Markdown parser and make the escape work everywhere
The hand written fence scanner took a line that starts with a code span for a
fence and missed fences in quotes and lists. The code blocks are now the ones
goldmark finds. A backslash before a placeholder is turned into a marker that
survives the Markdown and the list expansion, so it works in a code span and
next to a block name, and two backslashes are a literal one. The em dash rule
reports the line of the body, refuses padded entities and checks the header keys
by name, the feed leaves protocol-relative links alone, base_url needs a host
and a language file cannot set it.
2026-11-15 10:38:56 +01:00

69 lines
2.4 KiB
Go

// SPDX-License-Identifier: GPL-3.0-or-later
package mdx
import (
"fmt"
"regexp"
"strings"
)
// emDash is the character the published site never shows.
const emDash = "—"
// Rewrites of the manifesto headings, compiled once. The manifesto sources use
// em dashes: "NN — Title" becomes "NN. Title", "Word — Title" becomes
// "Word: Title" and an attribution line loses its dash.
var (
numberedHeading = regexp.MustCompile(`(?m)^(#+ )(\d\d) ` + emDash + ` `)
wordHeading = regexp.MustCompile(`(?m)^(#+ [^\d\n][^` + emDash + `\n]*?) ` + emDash + ` `)
attribution = regexp.MustCompile(`(?m)^> ` + emDash + ` `)
)
// parentheticals are the sentences in which a pair of dashes becomes commas.
var parentheticals = []struct{ from, to string }{
{" " + emDash + " where technically feasible " + emDash + " ", ", where technically feasible, "},
{" " + emDash + " dove tecnicamente possibile " + emDash + " ", ", dove tecnicamente possibile, "},
{" " + emDash + " and continues", ", and continues"},
{" " + emDash + " e continua", ", e continua"},
{"kmalloc.org " + emDash + " ", "kmalloc.org: "},
}
// NoEmDash rewrites the em dashes of a manifesto source with the rules above
// and returns an error if any em dash is left, so a new one is noticed.
func NoEmDash(md string) (string, error) {
md = numberedHeading.ReplaceAllString(md, "${1}${2}. ")
md = wordHeading.ReplaceAllString(md, "${1}: ")
md = attribution.ReplaceAllString(md, "> ")
for _, p := range parentheticals {
md = strings.ReplaceAll(md, p.from, p.to)
}
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)—|&#0*8212;|&#x0*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 HasEmDash(line) {
return fmt.Errorf("%s:%d: no em dashes, please", name, i+1)
}
}
return nil
}