diff --git a/cmd/transpile/main_test.go b/cmd/transpile/main_test.go index 20351d7..4843d23 100644 --- a/cmd/transpile/main_test.go +++ b/cmd/transpile/main_test.go @@ -15,13 +15,12 @@ import ( var testMothYaml = []byte(`--- answers: - YAML answer -pre: - authors: - - Arthur - - Buster - - DW - attachments: - - filename: moo.txt +authors: + - Arthur + - Buster + - DW +attachments: + - filename: moo.txt --- YAML body `) @@ -189,4 +188,4 @@ func TestFilesystem(t *testing.T) { if !strings.Contains(stdout.String(), "Moo.") { t.Error("Wrong file pulled", stdout.String()) } -} \ No newline at end of file +} diff --git a/cmd/transpile/testdata/cat1/1/mkpuzzle b/cmd/transpile/testdata/cat1/1/mkpuzzle index 161b2e7..997ea22 100755 --- a/cmd/transpile/testdata/cat1/1/mkpuzzle +++ b/cmd/transpile/testdata/cat1/1/mkpuzzle @@ -11,10 +11,8 @@ case $1:$2 in cat <moo." - } + "Authors": ["author1.0"], + "Body": "

moo.

" } EOT ;; diff --git a/example-puzzles/example/1/puzzle.md b/example-puzzles/example/1/puzzle.md index 928e312..f104e2f 100644 --- a/example-puzzles/example/1/puzzle.md +++ b/example-puzzles/example/1/puzzle.md @@ -1,7 +1,6 @@ --- -pre: - authors: - - neale +authors: + - neale debug: summary: static puzzles answers: diff --git a/example-puzzles/example/10/puzzle.md b/example-puzzles/example/10/puzzle.md index ce8e0bb..65cbc30 100644 --- a/example-puzzles/example/10/puzzle.md +++ b/example-puzzles/example/10/puzzle.md @@ -1,7 +1,6 @@ --- -pre: - authors: - - neale +authors: + - neale debug: summary: Making excellent puzzles answers: diff --git a/example-puzzles/example/2/puzzle.md b/example-puzzles/example/2/puzzle.md index 4836b3d..ee61f1a 100644 --- a/example-puzzles/example/2/puzzle.md +++ b/example-puzzles/example/2/puzzle.md @@ -1,11 +1,10 @@ --- -pre: - authors: - - neale - attachments: - - filename: salad.jpg - - filename: s2.jpg - filesystempath: salad2.jpg +authors: + - neale +attachments: + - filename: salad.jpg + - filename: s2.jpg + filesystempath: salad2.jpg debug: summary: Static puzzle resource files answers: diff --git a/example-puzzles/example/3/mkpuzzle b/example-puzzles/example/3/mkpuzzle index bfe1854..5ee5631 100755 --- a/example-puzzles/example/3/mkpuzzle +++ b/example-puzzles/example/3/mkpuzzle @@ -15,15 +15,13 @@ answer = ' '.join(random.sample(words, 4)) def puzzle(): number = random.randint(20, 500) obj = { - "Pre": { - "Authors": ["neale"], - "Body": ( - "

Dynamic puzzles are provided with a JSON-generating mkpuzzles program in the puzzle directory.

" - "

You can write mkpuzzles in any language you like. This puzzle was written in Python 3.

" - "

Here is some salad:

" - ), - "Attachments": ["salad.jpg"], - }, + "Authors": ["neale"], + "Body": ( + "

Dynamic puzzles are provided with a JSON-generating mkpuzzles program in the puzzle directory.

" + "

You can write mkpuzzles in any language you like. This puzzle was written in Python 3.

" + "

Here is some salad:

" + ), + "Attachments": ["salad.jpg"], "Answers": [ answer, ], diff --git a/example-puzzles/example/5/puzzle.md b/example-puzzles/example/5/puzzle.md index 211ef51..217d390 100644 --- a/example-puzzles/example/5/puzzle.md +++ b/example-puzzles/example/5/puzzle.md @@ -1,10 +1,9 @@ --- -pre: - authors: - - neale - scripts: - - filename: helpers.js - - filename: draggable.js +authors: + - neale +scripts: + - filename: helpers.js + - filename: draggable.js answers: - helper debug: diff --git a/example-puzzles/mkcategory/subcat1/1/puzzle.md b/example-puzzles/mkcategory/subcat1/1/puzzle.md index 18d2e72..332145f 100644 --- a/example-puzzles/mkcategory/subcat1/1/puzzle.md +++ b/example-puzzles/mkcategory/subcat1/1/puzzle.md @@ -1,6 +1,5 @@ --- -pre: - authors: ["neale"] +authors: ["neale"] answers: ["6"] --- This category shows off how to use the transpiler from an diff --git a/example-puzzles/mkcategory/subcat1/2/puzzle.md b/example-puzzles/mkcategory/subcat1/2/puzzle.md index 8c4756b..2f789ce 100644 --- a/example-puzzles/mkcategory/subcat1/2/puzzle.md +++ b/example-puzzles/mkcategory/subcat1/2/puzzle.md @@ -1,6 +1,5 @@ --- -pre: - authors: ["neale"] +authors: ["neale"] answers: ["C", "c"] --- 2 4 6 8 A _ diff --git a/example-puzzles/mkcategory/subcat2/3/puzzle.md b/example-puzzles/mkcategory/subcat2/3/puzzle.md index 151347b..17cdd9e 100644 --- a/example-puzzles/mkcategory/subcat2/3/puzzle.md +++ b/example-puzzles/mkcategory/subcat2/3/puzzle.md @@ -1,6 +1,5 @@ --- -pre: - authors: ["neale"] +authors: ["neale"] answers: ["17"] --- 1 2 3 5 7 11 13 _ diff --git a/pkg/transpile/category_test.go b/pkg/transpile/category_test.go index 1407a52..e8fcbed 100644 --- a/pkg/transpile/category_test.go +++ b/pkg/transpile/category_test.go @@ -59,16 +59,16 @@ func TestOsFsCategory(t *testing.T) { if p, err := static.Puzzle(1); err != nil { t.Error(err) - } else if len(p.Pre.Authors) != 1 { - t.Error("Wrong authors list", p.Pre.Authors) - } else if p.Pre.Authors[0] != "neale" { - t.Error("Wrong authors", p.Pre.Authors) + } else if len(p.Authors) != 1 { + t.Error("Wrong authors list", p.Authors) + } else if p.Authors[0] != "neale" { + t.Error("Wrong authors", p.Authors) } if p, err := static.Puzzle(3); err != nil { t.Error(err) - } else if len(p.Pre.Authors) != 1 { - t.Error("Wrong authors", p.Pre.Authors) + } else if len(p.Authors) != 1 { + t.Error("Wrong authors", p.Authors) } generated := NewFsCategory(fs, "generated") diff --git a/pkg/transpile/common_test.go b/pkg/transpile/common_test.go index 4428a0c..f151208 100644 --- a/pkg/transpile/common_test.go +++ b/pkg/transpile/common_test.go @@ -7,13 +7,12 @@ import ( var testMothYaml = []byte(`--- answers: - YAML answer -pre: - authors: - - Arthur - - Buster - - DW - attachments: - - moo.txt +authors: + - Arthur + - Buster + - DW +attachments: + - moo.txt --- YAML body `) @@ -27,9 +26,8 @@ RFC822 body var testMothMarkdown = []byte(`--- answers: - answer -pre: - authors: - - Fred +authors: + - Fred --- one | two diff --git a/pkg/transpile/mothball.go b/pkg/transpile/mothball.go index ab16081..e279ef0 100644 --- a/pkg/transpile/mothball.go +++ b/pkg/transpile/mothball.go @@ -53,7 +53,7 @@ func Mothball(c Category, w io.Writer) error { } // Write out all attachments and scripts - attachments := append(puzzle.Pre.Attachments, puzzle.Pre.Scripts...) + attachments := append(puzzle.Attachments, puzzle.Scripts...) for _, att := range attachments { attPath := fmt.Sprintf("%d/%s", points, att) aw, err := zf.Create(attPath) diff --git a/pkg/transpile/puzzle.go b/pkg/transpile/puzzle.go index 984a295..eacde24 100644 --- a/pkg/transpile/puzzle.go +++ b/pkg/transpile/puzzle.go @@ -32,28 +32,26 @@ type AnswerResponse struct { // Puzzle contains everything about a puzzle that a client would see. type Puzzle struct { - Pre struct { - Authors []string - Attachments []string - Scripts []string - Body string - AnswerPattern string - AnswerHashes []string - } - Post struct { - Objective string - Success struct { - Acceptable string - Mastery string - } - KSAs []string - } Debug struct { Log []string Errors []string Hints []string Summary string } + Authors []string + Attachments []string + Scripts []string + Body string + AnswerPattern string + AnswerHashes []string + Objective string + KSAs []string + Success struct { + Acceptable string + Mastery string + } + + // Answers will be empty in a mothball Answers []string } @@ -61,30 +59,26 @@ func (puzzle *Puzzle) computeAnswerHashes() { if len(puzzle.Answers) == 0 { return } - puzzle.Pre.AnswerHashes = make([]string, len(puzzle.Answers)) + puzzle.AnswerHashes = make([]string, len(puzzle.Answers)) for i, answer := range puzzle.Answers { sum := sha256.Sum256([]byte(answer)) hexsum := fmt.Sprintf("%x", sum) - puzzle.Pre.AnswerHashes[i] = hexsum + puzzle.AnswerHashes[i] = hexsum } } // StaticPuzzle contains everything a static puzzle might tell us. type StaticPuzzle struct { - Pre struct { - Authors []string - Attachments []StaticAttachment - Scripts []StaticAttachment - AnswerPattern string - } - Post struct { - Objective string - Success struct { - Acceptable string - Mastery string - } - KSAs []string + Authors []string + Attachments []StaticAttachment + Scripts []StaticAttachment + AnswerPattern string + Objective string + Success struct { + Acceptable string + Mastery string } + KSAs []string Debug struct { Log []string Errors []string @@ -187,7 +181,7 @@ type FsPuzzle struct { // Puzzle returns a Puzzle struct for the current puzzle. func (fp FsPuzzle) Puzzle() (Puzzle, error) { - var puzzle Puzzle + puzzle := Puzzle{} static, body, err := fp.staticPuzzle() if err != nil { @@ -195,20 +189,21 @@ func (fp FsPuzzle) Puzzle() (Puzzle, error) { } // Convert to an exportable Puzzle - puzzle.Post = static.Post puzzle.Debug = static.Debug puzzle.Answers = static.Answers - puzzle.Pre.Authors = static.Pre.Authors - puzzle.Pre.Body = string(body) - puzzle.Pre.AnswerPattern = static.Pre.AnswerPattern - puzzle.Pre.Attachments = make([]string, len(static.Pre.Attachments)) - for i, attachment := range static.Pre.Attachments { - puzzle.Pre.Attachments[i] = attachment.Filename + puzzle.Authors = static.Authors + puzzle.Body = string(body) + puzzle.AnswerPattern = static.AnswerPattern + puzzle.Attachments = make([]string, len(static.Attachments)) + for i, attachment := range static.Attachments { + puzzle.Attachments[i] = attachment.Filename } - puzzle.Pre.Scripts = make([]string, len(static.Pre.Scripts)) - for i, script := range static.Pre.Scripts { - puzzle.Pre.Scripts[i] = script.Filename + puzzle.Scripts = make([]string, len(static.Scripts)) + for i, script := range static.Scripts { + puzzle.Scripts[i] = script.Filename } + empty := Puzzle{} + puzzle.Debug = empty.Debug puzzle.computeAnswerHashes() return puzzle, nil @@ -223,7 +218,7 @@ func (fp FsPuzzle) Open(name string) (ReadSeekCloser, error) { } var fsPath string - for _, attachment := range append(static.Pre.Attachments, static.Pre.Scripts...) { + for _, attachment := range append(static.Attachments, static.Scripts...) { if attachment.Filename == name { if attachment.FilesystemPath == "" { fsPath = attachment.Filename @@ -337,13 +332,13 @@ func rfc822HeaderParser(r io.Reader) (StaticPuzzle, error) { key = strings.ToLower(key) switch key { case "author": - p.Pre.Authors = val + p.Authors = val case "pattern": - p.Pre.AnswerPattern = val[0] + p.AnswerPattern = val[0] case "script": - p.Pre.Scripts = legacyAttachmentParser(val) + p.Scripts = legacyAttachmentParser(val) case "file": - p.Pre.Attachments = legacyAttachmentParser(val) + p.Attachments = legacyAttachmentParser(val) case "answer": p.Answers = val case "summary": @@ -353,13 +348,13 @@ func rfc822HeaderParser(r io.Reader) (StaticPuzzle, error) { case "solution": p.Debug.Hints = val case "ksa": - p.Post.KSAs = val + p.KSAs = val case "objective": - p.Post.Objective = val[0] + p.Objective = val[0] case "success.acceptable": - p.Post.Success.Acceptable = val[0] + p.Success.Acceptable = val[0] case "success.mastery": - p.Post.Success.Mastery = val[0] + p.Success.Mastery = val[0] default: return p, fmt.Errorf("Unknown header field: %s", key) } diff --git a/pkg/transpile/puzzle_test.go b/pkg/transpile/puzzle_test.go index 0503ae9..4d4d6b1 100644 --- a/pkg/transpile/puzzle_test.go +++ b/pkg/transpile/puzzle_test.go @@ -23,11 +23,11 @@ func TestPuzzle(t *testing.T) { if (len(p.Answers) == 0) || (p.Answers[0] != "YAML answer") { t.Error("Answers are wrong", p.Answers) } - if (len(p.Pre.Authors) != 3) || (p.Pre.Authors[1] != "Buster") { - t.Error("Authors are wrong", p.Pre.Authors) + if (len(p.Authors) != 3) || (p.Authors[1] != "Buster") { + t.Error("Authors are wrong", p.Authors) } - if p.Pre.Body != "

YAML body

\n" { - t.Errorf("Body parsed wrong: %#v", p.Pre.Body) + if p.Body != "

YAML body

\n" { + t.Errorf("Body parsed wrong: %#v", p.Body) } f, err := pd.Open("moo.txt") @@ -52,11 +52,11 @@ func TestPuzzle(t *testing.T) { if (len(p.Answers) == 0) || (p.Answers[0] != "RFC822 answer") { t.Error("Answers are wrong", p.Answers) } - if (len(p.Pre.Authors) != 3) || (p.Pre.Authors[1] != "Arthur") { - t.Error("Authors are wrong", p.Pre.Authors) + if (len(p.Authors) != 3) || (p.Authors[1] != "Arthur") { + t.Error("Authors are wrong", p.Authors) } - if p.Pre.Body != "

RFC822 body

\n" { - t.Errorf("Body parsed wrong: %#v", p.Pre.Body) + if p.Body != "

RFC822 body

\n" { + t.Errorf("Body parsed wrong: %#v", p.Body) } } @@ -66,9 +66,9 @@ func TestPuzzle(t *testing.T) { if puzzle, err := NewFsPuzzlePoints(catFs, 4).Puzzle(); err != nil { t.Error("Markdown test file:", err) - } else if !strings.Contains(puzzle.Pre.Body, "") { + } else if !strings.Contains(puzzle.Body, "
") { t.Error("Markdown table extension isn't making tables") - } else if !strings.Contains(puzzle.Pre.Body, "
") { + } else if !strings.Contains(puzzle.Body, "
") { t.Error("Markdown dictionary extension isn't making tables") } @@ -110,7 +110,7 @@ func TestFsPuzzle(t *testing.T) { if puzzle, err := NewFsPuzzlePoints(catFs, 2).Puzzle(); err != nil { t.Error(err) - } else if !strings.Contains(puzzle.Pre.Body, "class=\"moo\"") { + } else if !strings.Contains(puzzle.Body, "class=\"moo\"") { t.Error("Raw HTML didn't make it through") } @@ -150,11 +150,10 @@ func TestFsPuzzle(t *testing.T) { func TestAttachment(t *testing.T) { buf := bytes.NewBufferString(` -pre: - attachments: - - simple - - filename: complex - filesystempath: backingfile +attachments: + - simple + - filename: complex + filesystempath: backingfile `) p, err := yamlHeaderParser(buf) if err != nil { @@ -162,7 +161,7 @@ pre: return } - att := p.Pre.Attachments + att := p.Attachments if len(att) != 2 { t.Error("Wrong number of attachments", att) } diff --git a/pkg/transpile/testdata/generated/mkcategory b/pkg/transpile/testdata/generated/mkcategory index caae745..1e90e4b 100755 --- a/pkg/transpile/testdata/generated/mkcategory +++ b/pkg/transpile/testdata/generated/mkcategory @@ -18,10 +18,8 @@ EOT cat <moo." - } + "Authors": ["author1.0"], + "Body": "

moo.

" } EOT ;; diff --git a/pkg/transpile/testdata/static/1/puzzle.md b/pkg/transpile/testdata/static/1/puzzle.md index 3fdaeee..0f8e630 100644 --- a/pkg/transpile/testdata/static/1/puzzle.md +++ b/pkg/transpile/testdata/static/1/puzzle.md @@ -1,7 +1,6 @@ --- -pre: - authors: - - neale +authors: + - neale answers: - moo --- diff --git a/pkg/transpile/testdata/static/3/mkpuzzle b/pkg/transpile/testdata/static/3/mkpuzzle index e03fb00..cb0c887 100755 --- a/pkg/transpile/testdata/static/3/mkpuzzle +++ b/pkg/transpile/testdata/static/3/mkpuzzle @@ -10,10 +10,8 @@ case $1:$2 in cat <<'EOT' { "Answers": ["answer"], - "Pre": { - "Authors": ["neale"], - "Body": "I am a generated puzzle." - } + "Authors": ["neale"], + "Body": "I am a generated puzzle." } EOT ;;