Skip to content

Commit

Permalink
make newline optional at the end of codeblocks
Browse files Browse the repository at this point in the history
  • Loading branch information
lilacstella committed Jun 27, 2024
1 parent 12f00f7 commit bc93e34
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/rundoc/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Rundoc
class Parser
DEFAULT_KEYWORD = ":::"
INDENT_BLOCK = '(?<before_indent>(^\s*$\n|\A)(^(?:[ ]{4}|\t))(?<indent_contents>.*)(?<after_indent>[^\s].*$\n?(?:(?:^\s*$\n?)*^(?:[ ]{4}|\t).*[^\s].*$\n?)*))'
GITHUB_BLOCK = '^(?<fence>(?<fence_char>~|`){3,})\s*?(?<lang>\w+)?\s*?\n(?<contents>.*?)^\g<fence>\g<fence_char>*\s*?\n'
GITHUB_BLOCK = '^(?<fence>(?<fence_char>~|`){3,})\s*?(?<lang>\w+)?\s*?\n(?<contents>.*?)^\g<fence>\g<fence_char>*\s*?\n?'
CODEBLOCK_REGEX = /(#{GITHUB_BLOCK})/m
COMMAND_REGEX = ->(keyword) {
/^#{keyword}(?<tag>(\s|=|-|>)?(=|-|>)?)\s*(?<command>(\S)+)\s+(?<statement>.*)$/
Expand Down
11 changes: 11 additions & 0 deletions test/rundoc/regex_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -190,4 +190,15 @@ def test_complex_regex

assert_equal contents.strip, match.to_s.strip
end

def test_codeblock_optional_newline_regex
contents = "hi\n```\n:::>>$ echo \"hello\"\n```\n"
regex = Rundoc::Parser::CODEBLOCK_REGEX
match = contents.match(regex)
assert_equal "```\n:::>>$ echo \"hello\"\n```", match.to_s.strip

contents = "hi\n```\n:::>>$ echo \"hello\"\n```"
match = contents.match(regex)
assert_equal "```\n:::>>$ echo \"hello\"\n```", match.to_s.strip
end
end

0 comments on commit bc93e34

Please sign in to comment.