Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,14 @@
"prerequisites": [],
"difficulty": 3
},
{
"slug": "markdown",
"name": "Markdown",
"uuid": "5949b3ab-37c3-4340-840a-a0bf3ecf406e",
"practices": [],
"prerequisites": [],
"difficulty": 3
},
{
"slug": "matrix",
"name": "Matrix",
Expand Down
5 changes: 5 additions & 0 deletions exercises/practice/markdown/.busted
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
return {
default = {
ROOT = { '.' }
}
}
6 changes: 6 additions & 0 deletions exercises/practice/markdown/.docs/instructions.append.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# dummy

## MoonScript-specific Instructions

Contrary to the above, this is not a refactoring exercise.
Your task is to translate working Lua code into MoonScript.
13 changes: 13 additions & 0 deletions exercises/practice/markdown/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Instructions

Refactor a Markdown parser.

The markdown exercise is a refactoring exercise.
There is code that parses a given string with [Markdown syntax][markdown] and returns the associated HTML for that string.
Even though this code is confusingly written and hard to follow, somehow it works and all the tests are passing!
Your challenge is to re-write this code to make it easier to read and maintain while still making sure that all the tests keep passing.

It would be helpful if you made notes of what you did in your refactoring in comments so reviewers can see that, but it isn't strictly necessary.
The most important thing is to make the code better!

[markdown]: https://docs.github.com/en/get-started/writing-on-github/getting-started-with-writing-and-formatting-on-github/basic-writing-and-formatting-syntax
20 changes: 20 additions & 0 deletions exercises/practice/markdown/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"authors": [
"glennj"
],
"files": {
"solution": [
"markdown.moon"
],
"test": [
"markdown_spec.moon"
],
"example": [
".meta/example.moon"
],
"editor": [
"markdown.lua"
]
},
"blurb": "Refactor a Markdown parser."
}
44 changes: 44 additions & 0 deletions exercises/practice/markdown/.meta/example.moon
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
parse = (input) ->
markup = ""
in_list = false

open_list = ->
if not in_list
in_list = true
markup ..= '<ul>'

close_list = ->
if in_list
in_list = false
markup ..= '</ul>'

for line in input\gmatch "[^\n]+"
-- handle the in-line markup
line = line\gsub("__(.-)__", "<strong>%1</strong>")
line = line\gsub("_(.-)_", "<em>%1</em>")

-- is it a list item?
text = line\match "^[*]%s+(.+)"
if text
open_list!
markup ..= "<li>#{text}</li>"

else
close_list!

-- is it a heading?
header, text = line\match "^(#+)%s+(.+)"
if header and #header <= 6
tag = "h#{#header}"
markup ..= "<#{tag}>#{text}</#{tag}>"

-- it's a plain paragraph
else
markup ..= "<p>#{line}</p>"

close_list!
markup


{ :parse }

14 changes: 14 additions & 0 deletions exercises/practice/markdown/.meta/spec_generator.moon
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
json = require 'dkjson'
json_string = (s) -> json.encode s

{
module_name: 'Markdown',

generate_test: (case, level) ->
lines = {
"result = Markdown.#{case.property} #{json_string case.input.markdown}",
"expected = #{quote case.expected}",
"assert.are.equal expected, result"
}
table.concat [indent line, level for line in *lines], '\n'
}
66 changes: 66 additions & 0 deletions exercises/practice/markdown/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

[e75c8103-a6b8-45d9-84ad-e68520545f6e]
description = "parses normal text as a paragraph"

[69a4165d-9bf8-4dd7-bfdc-536eaca80a6a]
description = "parsing italics"

[ec345a1d-db20-4569-a81a-172fe0cad8a1]
description = "parsing bold text"

[51164ed4-5641-4909-8fab-fbaa9d37d5a8]
description = "mixed normal, italics and bold text"

[ad85f60d-0edd-4c6a-a9b1-73e1c4790d15]
description = "with h1 header level"

[d0f7a31f-6935-44ac-8a9a-1e8ab16af77f]
description = "with h2 header level"

[9df3f500-0622-4696-81a7-d5babd9b5f49]
description = "with h3 header level"

[50862777-a5e8-42e9-a3b8-4ba6fcd0ed03]
description = "with h4 header level"

[ee1c23ac-4c86-4f2a-8b9c-403548d4ab82]
description = "with h5 header level"

[13b5f410-33f5-44f0-a6a7-cfd4ab74b5d5]
description = "with h6 header level"

[6dca5d10-5c22-4e2a-ac2b-bd6f21e61939]
description = "with h7 header level"
include = false

[81c0c4db-435e-4d77-860d-45afacdad810]
description = "h7 header level is a paragraph"
reimplements = "6dca5d10-5c22-4e2a-ac2b-bd6f21e61939"

[25288a2b-8edc-45db-84cf-0b6c6ee034d6]
description = "unordered lists"

[7bf92413-df8f-4de8-9184-b724f363c3da]
description = "With a little bit of everything"

[0b3ed1ec-3991-4b8b-8518-5cb73d4a64fe]
description = "with markdown symbols in the header text that should not be interpreted"

[113a2e58-78de-4efa-90e9-20972224d759]
description = "with markdown symbols in the list item text that should not be interpreted"

[e65e46e2-17b7-4216-b3ac-f44a1b9bcdb4]
description = "with markdown symbols in the paragraph text that should not be interpreted"

[f0bbbbde-0f52-4c0c-99ec-be4c60126dd4]
description = "unordered lists close properly with preceding and following lines"
53 changes: 53 additions & 0 deletions exercises/practice/markdown/markdown.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
local function parse(input)
local markup = ''
local in_list = false

local open_list = function()
if not in_list then
in_list = true
markup = markup .. '<ul>'
end
end

local close_list = function()
if in_list then
in_list = false
markup = markup .. '</ul>'
end
end

for line in input:gmatch('[^\n]+') do
-- handle the in-line markup
line = string.gsub(line, '__(.-)__', '<strong>%1</strong>')
line = string.gsub(line, '_(.-)_', '<em>%1</em>')

-- is it a list item?
if line:sub(1,1) == '*' then
open_list()
markup = markup .. '<li>' .. line:sub(3) .. '</li>'

else
close_list()

-- is it a heading?
local text, header
header, text = line:match('^(#+)%s+(.+)')
if header and #header <= 6 then
local tag = 'h' .. #header
markup = markup .. '<' .. tag .. '>' .. text .. '</' .. tag .. '>'

-- it's a plain paragraph
else
markup = markup .. '<p>' .. line .. '</p>'
end
end
end

close_list()

return markup
end

return {
parse = parse
}
5 changes: 5 additions & 0 deletions exercises/practice/markdown/markdown.moon
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- Your task is to translate the Lua code in markdown.lua into MoonScript
{
parse: (input) ->
error 'Implement me'
}
87 changes: 87 additions & 0 deletions exercises/practice/markdown/markdown_spec.moon
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
Markdown = require 'markdown'

describe 'markdown', ->
it 'parses normal text as a paragraph', ->
result = Markdown.parse "This will be a paragraph"
expected = '<p>This will be a paragraph</p>'
assert.are.equal expected, result

pending 'parsing italics', ->
result = Markdown.parse "_This will be italic_"
expected = '<p><em>This will be italic</em></p>'
assert.are.equal expected, result

pending 'parsing bold text', ->
result = Markdown.parse "__This will be bold__"
expected = '<p><strong>This will be bold</strong></p>'
assert.are.equal expected, result

pending 'mixed normal, italics and bold text', ->
result = Markdown.parse "This will _be_ __mixed__"
expected = '<p>This will <em>be</em> <strong>mixed</strong></p>'
assert.are.equal expected, result

pending 'with h1 header level', ->
result = Markdown.parse "# This will be an h1"
expected = '<h1>This will be an h1</h1>'
assert.are.equal expected, result

pending 'with h2 header level', ->
result = Markdown.parse "## This will be an h2"
expected = '<h2>This will be an h2</h2>'
assert.are.equal expected, result

pending 'with h3 header level', ->
result = Markdown.parse "### This will be an h3"
expected = '<h3>This will be an h3</h3>'
assert.are.equal expected, result

pending 'with h4 header level', ->
result = Markdown.parse "#### This will be an h4"
expected = '<h4>This will be an h4</h4>'
assert.are.equal expected, result

pending 'with h5 header level', ->
result = Markdown.parse "##### This will be an h5"
expected = '<h5>This will be an h5</h5>'
assert.are.equal expected, result

pending 'with h6 header level', ->
result = Markdown.parse "###### This will be an h6"
expected = '<h6>This will be an h6</h6>'
assert.are.equal expected, result

pending 'h7 header level is a paragraph', ->
result = Markdown.parse "####### This will not be an h7"
expected = '<p>####### This will not be an h7</p>'
assert.are.equal expected, result

pending 'unordered lists', ->
result = Markdown.parse "* Item 1\n* Item 2"
expected = '<ul><li>Item 1</li><li>Item 2</li></ul>'
assert.are.equal expected, result

pending 'With a little bit of everything', ->
result = Markdown.parse "# Header!\n* __Bold Item__\n* _Italic Item_"
expected = '<h1>Header!</h1><ul><li><strong>Bold Item</strong></li><li><em>Italic Item</em></li></ul>'
assert.are.equal expected, result

pending 'with markdown symbols in the header text that should not be interpreted', ->
result = Markdown.parse "# This is a header with # and * in the text"
expected = '<h1>This is a header with # and * in the text</h1>'
assert.are.equal expected, result

pending 'with markdown symbols in the list item text that should not be interpreted', ->
result = Markdown.parse "* Item 1 with a # in the text\n* Item 2 with * in the text"
expected = '<ul><li>Item 1 with a # in the text</li><li>Item 2 with * in the text</li></ul>'
assert.are.equal expected, result

pending 'with markdown symbols in the paragraph text that should not be interpreted', ->
result = Markdown.parse "This is a paragraph with # and * in the text"
expected = '<p>This is a paragraph with # and * in the text</p>'
assert.are.equal expected, result

pending 'unordered lists close properly with preceding and following lines', ->
result = Markdown.parse "# Start a list\n* Item 1\n* Item 2\nEnd a list"
expected = '<h1>Start a list</h1><ul><li>Item 1</li><li>Item 2</li></ul><p>End a list</p>'
assert.are.equal expected, result
Loading