mirror of
https://github.com/go-sylixos/elvish.git
synced 2024-12-14 19:27:58 +08:00
a4589bcdf0
- Try to avoid escaping & and < when they are not problematic. - Escape non-breaking space. - Format lists as loose lists. - Only indent inline raw HTML when it can actually be parsed as a paragraph-interrupting HTML block.
58 lines
1.1 KiB
Go
58 lines
1.1 KiB
Go
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"html"
|
|
"io"
|
|
"os"
|
|
|
|
"src.elv.sh/pkg/diff"
|
|
"src.elv.sh/pkg/md"
|
|
)
|
|
|
|
var (
|
|
overwrite = flag.Bool("w", false, "write result to source file (requires -fmt)")
|
|
showDiff = flag.Bool("d", false, "show diff")
|
|
)
|
|
|
|
func main() {
|
|
md.UnescapeHTML = html.UnescapeString
|
|
flag.Parse()
|
|
|
|
files := flag.Args()
|
|
if len(files) == 0 {
|
|
text, err := io.ReadAll(os.Stdin)
|
|
if err != nil {
|
|
fmt.Fprintln(os.Stderr, "read stdin:", err)
|
|
os.Exit(2)
|
|
}
|
|
fmt.Print(format(string(text)))
|
|
return
|
|
}
|
|
for _, file := range files {
|
|
text, err := os.ReadFile(file)
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "read %s: %v\n", file, err)
|
|
os.Exit(2)
|
|
}
|
|
result := format(string(text))
|
|
if *overwrite {
|
|
err := os.WriteFile(file, []byte(result), 0644)
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "write %s: %v\n", file, err)
|
|
os.Exit(2)
|
|
}
|
|
} else if !*showDiff {
|
|
fmt.Print(result)
|
|
}
|
|
if *showDiff {
|
|
os.Stdout.Write(diff.Diff(file+".orig", text, file, []byte(result)))
|
|
}
|
|
}
|
|
}
|
|
|
|
func format(original string) string {
|
|
return md.RenderString(original, &md.FmtCodec{})
|
|
}
|