Add first pass

This commit is contained in:
Jeremy Walker
2023-09-20 15:04:24 +02:00
commit d08fcf62b9
7 changed files with 365 additions and 0 deletions

25
README.md Normal file
View File

@@ -0,0 +1,25 @@
# Admonition Block Formatter
This is the code that formats admonition blocks for Exercism.
It ensures that blocks always use four tildes.
For example: it converts
````
```exercism/note
something
```
````
to
```
~~~~exercism/note
something
~~~~
```
## Tests
The code requires Ruby, but no other dependencies.
Run the tests by running `rake`.

11
Rakefile Normal file
View File

@@ -0,0 +1,11 @@
require "rake/testtask"
Rake::TestTask.new(:test) do |t|
t.libs << "test"
t.libs << "lib"
t.warning = false
t.test_files = FileList["test/**/*_test.rb"]
end
task default: :test

View File

@@ -0,0 +1,29 @@
class AdmonitionBlockFormatter
def self.format(code)
[:format_four, :format_three].each do |meth|
while code != (new_code = send(meth, code))
code = new_code
end
end
code
end
def self.format_three(code)
matches = THREE_TILDE_REGEX.match(code)
return code unless matches
matches['before'] + "~~~~" + matches['type'] + matches['admonition'] + "~~~~" + matches['after']
end
def self.format_four(code)
matches = FOUR_TILDE_REGEX.match(code)
return code unless matches
matches['before'] + "~~~~" + matches['type'] + matches['admonition'] + "~~~~" + matches['after']
end
THREE_TILDE_REGEX = /(?<before>[\s\S]*)(?<start>```)(?<type>exercism\/[a-z]+)(?<admonition>[\s\S]*?)(?<end>```)(?<after>[\s\S]*)/.freeze
FOUR_TILDE_REGEX = /(?<before>[\s\S]*)(?<start>````)(?<type>exercism\/[a-z]+)(?<admonition>[\s\S]*?)(?<end>````)(?<after>[\s\S]*)/.freeze
end

124
test/four_tildes_test.rb Normal file
View File

@@ -0,0 +1,124 @@
require 'test_helper'
class FourTildesTest < Minitest::Test
def test_basic
input = <<~INPUT
````exercism/note
something
````
INPUT
output = <<~OUTPUT
~~~~exercism/note
something
~~~~
OUTPUT
assert_equal output, AdmonitionBlockFormatter.format(input)
end
def test_fuller
input = <<~INPUT
something
````exercism/note
some code
with a `variable` here
some other code
````
endstuff
INPUT
output = <<~OUTPUT
something
~~~~exercism/note
some code
with a `variable` here
some other code
~~~~
endstuff
OUTPUT
assert_equal output, AdmonitionBlockFormatter.format(input)
end
def test_multiple
input = <<~INPUT
something
````exercism/note
some code
````
else
````exercism/note
other code
````
endstuff
INPUT
output = <<~OUTPUT
something
~~~~exercism/note
some code
~~~~
else
~~~~exercism/note
other code
~~~~
endstuff
OUTPUT
assert_equal output, AdmonitionBlockFormatter.format(input)
end
def test_nested
input = <<~INPUT
````exercism/note
before
```ruby
code1
```
middle
```
code2
```
after
````
```
code3
```
INPUT
output = <<~OUTPUT
~~~~exercism/note
before
```ruby
code1
```
middle
```
code2
```
after
~~~~
```
code3
```
OUTPUT
assert_equal output, AdmonitionBlockFormatter.format(input)
end
end

85
test/mixed_test.rb Normal file
View File

@@ -0,0 +1,85 @@
require 'test_helper'
class MixedTest < Minitest::Test
def test_three_then_four_then_three
input = <<~INPUT
something
```exercism/note
code 1
```
````exercism/note
code 2
````
```exercism/note
code 3
```
end bit
INPUT
output = <<~OUTPUT
something
~~~~exercism/note
code 1
~~~~
~~~~exercism/note
code 2
~~~~
~~~~exercism/note
code 3
~~~~
end bit
OUTPUT
assert_equal output, AdmonitionBlockFormatter.format(input)
end
def test_four_then_three_then_four
input = <<~INPUT
something
````exercism/note
code 1
````
```exercism/note
code 2
```
````exercism/note
code 3
````
end bit
INPUT
output = <<~OUTPUT
something
~~~~exercism/note
code 1
~~~~
~~~~exercism/note
code 2
~~~~
~~~~exercism/note
code 3
~~~~
end bit
OUTPUT
assert_equal output, AdmonitionBlockFormatter.format(input)
end
end

8
test/test_helper.rb Normal file
View File

@@ -0,0 +1,8 @@
# gem "minitest"
require "minitest/autorun"
# require "minitest/pride"
# require "minitest/mock"
# require "mocha/minitest"
$LOAD_PATH.unshift File.expand_path('lib', __dir__)
require "admonition_block_formatter"

83
test/three_tildes_test.rb Normal file
View File

@@ -0,0 +1,83 @@
require 'test_helper'
class ThreeTildesTest < Minitest::Test
def test_basic
input = <<~INPUT
```exercism/note
something
```
INPUT
output = <<~OUTPUT
~~~~exercism/note
something
~~~~
OUTPUT
assert_equal output, AdmonitionBlockFormatter.format(input)
end
def test_fuller
input = <<~INPUT
something
```exercism/note
some code
with a `variable` here
some other code
```
endstuff
INPUT
output = <<~OUTPUT
something
~~~~exercism/note
some code
with a `variable` here
some other code
~~~~
endstuff
OUTPUT
assert_equal output, AdmonitionBlockFormatter.format(input)
end
def test_multiple
input = <<~INPUT
something
```exercism/note
some code
```
else
```exercism/note
other code
```
endstuff
INPUT
output = <<~OUTPUT
something
~~~~exercism/note
some code
~~~~
else
~~~~exercism/note
other code
~~~~
endstuff
OUTPUT
assert_equal output, AdmonitionBlockFormatter.format(input)
end
end