56 lines
1.9 KiB
Go
56 lines
1.9 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)
|
|
}
|
|
if err := RejectEmDash("manifesto source", md); err != nil {
|
|
return "", err
|
|
}
|
|
return md, nil
|
|
}
|
|
|
|
// RejectEmDash returns an error naming the file and the line of the first em
|
|
// dash in text, 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) {
|
|
return fmt.Errorf("%s:%d: no em dashes, please", name, i+1)
|
|
}
|
|
}
|
|
return nil
|
|
}
|