mirror of https://github.com/dirtbags/moth.git
Add markdown command to transpiler
This commit is contained in:
parent
12fae78207
commit
e2eb6646e7
|
@ -16,6 +16,7 @@ import (
|
||||||
|
|
||||||
// T represents the state of things
|
// T represents the state of things
|
||||||
type T struct {
|
type T struct {
|
||||||
|
Stdin io.Reader
|
||||||
Stdout io.Writer
|
Stdout io.Writer
|
||||||
Stderr io.Writer
|
Stderr io.Writer
|
||||||
Args []string
|
Args []string
|
||||||
|
@ -42,6 +43,7 @@ func usage(w io.Writer) {
|
||||||
fmt.Fprintln(w, " puzzle: Print puzzle JSON")
|
fmt.Fprintln(w, " puzzle: Print puzzle JSON")
|
||||||
fmt.Fprintln(w, " file: Open a file for a puzzle")
|
fmt.Fprintln(w, " file: Open a file for a puzzle")
|
||||||
fmt.Fprintln(w, " answer: Check correctness of an answer")
|
fmt.Fprintln(w, " answer: Check correctness of an answer")
|
||||||
|
fmt.Fprintln(w, " markdown: Format stdin with markdown")
|
||||||
}
|
}
|
||||||
|
|
||||||
// ParseArgs parses arguments and runs the appropriate action.
|
// ParseArgs parses arguments and runs the appropriate action.
|
||||||
|
@ -71,6 +73,8 @@ func (t *T) ParseArgs() (Command, error) {
|
||||||
case "answer":
|
case "answer":
|
||||||
cmd = t.CheckAnswer
|
cmd = t.CheckAnswer
|
||||||
flags.StringVar(&t.answer, "answer", "", "Answer to check")
|
flags.StringVar(&t.answer, "answer", "", "Answer to check")
|
||||||
|
case "markdown":
|
||||||
|
cmd = t.Markdown
|
||||||
case "help":
|
case "help":
|
||||||
usage(t.Stderr)
|
usage(t.Stderr)
|
||||||
return nothing, nil
|
return nothing, nil
|
||||||
|
@ -185,6 +189,11 @@ func (t *T) CheckAnswer() error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Markdown runs stdin through a Markdown engine
|
||||||
|
func (t *T) Markdown() error {
|
||||||
|
return transpile.Markdown(t.Stdin, t.Stdout)
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
t := &T{
|
t := &T{
|
||||||
Stdout: os.Stdout,
|
Stdout: os.Stdout,
|
||||||
|
|
|
@ -51,9 +51,11 @@ func (tp T) Run(args ...string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestTranspilerEverything(t *testing.T) {
|
func TestTranspilerEverything(t *testing.T) {
|
||||||
|
stdin := new(bytes.Buffer)
|
||||||
stdout := new(bytes.Buffer)
|
stdout := new(bytes.Buffer)
|
||||||
stderr := new(bytes.Buffer)
|
stderr := new(bytes.Buffer)
|
||||||
tp := T{
|
tp := T{
|
||||||
|
Stdin: stdin,
|
||||||
Stdout: stdout,
|
Stdout: stdout,
|
||||||
Stderr: stderr,
|
Stderr: stderr,
|
||||||
BaseFs: newTestFs(),
|
BaseFs: newTestFs(),
|
||||||
|
@ -94,6 +96,15 @@ func TestTranspilerEverything(t *testing.T) {
|
||||||
t.Error("Answer validation failed", stdout.String())
|
t.Error("Answer validation failed", stdout.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
stdout.Reset()
|
||||||
|
stdin.Reset()
|
||||||
|
stdin.WriteString("text *emphasized* text")
|
||||||
|
if err := tp.Run("markdown"); err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
if stdout.String() != "<p>text <em>emphasized</em> text</p>\n" {
|
||||||
|
t.Error("Markdown conversion failed", stdout.String())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMothballs(t *testing.T) {
|
func TestMothballs(t *testing.T) {
|
||||||
|
@ -165,9 +176,11 @@ func TestMothballs(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFilesystem(t *testing.T) {
|
func TestFilesystem(t *testing.T) {
|
||||||
|
stdin := new(bytes.Buffer)
|
||||||
stdout := new(bytes.Buffer)
|
stdout := new(bytes.Buffer)
|
||||||
stderr := new(bytes.Buffer)
|
stderr := new(bytes.Buffer)
|
||||||
tp := T{
|
tp := T{
|
||||||
|
Stdin: stdin,
|
||||||
Stdout: stdout,
|
Stdout: stdout,
|
||||||
Stderr: stderr,
|
Stderr: stderr,
|
||||||
BaseFs: afero.NewOsFs(),
|
BaseFs: afero.NewOsFs(),
|
||||||
|
|
|
@ -0,0 +1,29 @@
|
||||||
|
package transpile
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
|
|
||||||
|
"github.com/yuin/goldmark"
|
||||||
|
"github.com/yuin/goldmark/extension"
|
||||||
|
"github.com/yuin/goldmark/renderer/html"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Markdown formats the provided bytes using whatever Markdown engine we're currently using.
|
||||||
|
func Markdown(input io.Reader, output io.Writer) error {
|
||||||
|
md := goldmark.New(
|
||||||
|
goldmark.WithExtensions(
|
||||||
|
extension.Table,
|
||||||
|
extension.DefinitionList,
|
||||||
|
),
|
||||||
|
goldmark.WithRendererOptions(
|
||||||
|
html.WithUnsafe(),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
buf, err := ioutil.ReadAll(input)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
md.Convert(buf, output)
|
||||||
|
return nil
|
||||||
|
}
|
|
@ -19,9 +19,6 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/spf13/afero"
|
"github.com/spf13/afero"
|
||||||
"github.com/yuin/goldmark"
|
|
||||||
"github.com/yuin/goldmark/extension"
|
|
||||||
"github.com/yuin/goldmark/renderer/html"
|
|
||||||
"gopkg.in/yaml.v2"
|
"gopkg.in/yaml.v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -281,20 +278,9 @@ func (fp FsPuzzle) staticPuzzle() (StaticPuzzle, []byte, error) {
|
||||||
return static, nil, err
|
return static, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
body := new(bytes.Buffer)
|
html := new(bytes.Buffer)
|
||||||
|
err = Markdown(bodyBuf, html)
|
||||||
md := goldmark.New(
|
return static, html.Bytes(), err
|
||||||
goldmark.WithExtensions(
|
|
||||||
extension.Table,
|
|
||||||
extension.DefinitionList,
|
|
||||||
),
|
|
||||||
goldmark.WithRendererOptions(
|
|
||||||
html.WithUnsafe(),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
md.Convert(bodyBuf.Bytes(), body)
|
|
||||||
|
|
||||||
return static, body.Bytes(), err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func legacyAttachmentParser(val []string) []StaticAttachment {
|
func legacyAttachmentParser(val []string) []StaticAttachment {
|
||||||
|
|
Loading…
Reference in New Issue