-
-
Save hannesfostie/7964e07b55477c101e39 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
regex_string = "\n|" \ | |
"<b>|</b>|" \ | |
"<b>|<b>|" \ | |
"*|*|" \ | |
"[^<\n]+" | |
[hannes] ~/dev/test % ruby parser.rb | |
parser.rb:12:in `initialize': target of repeat operator is not specified: / (RegexpError) | |
|<b>|<\/b>|<b>|<b>|*|*|[^< | |
]+/m | |
from parser.rb:12:in `new' | |
from parser.rb:12:in `<class:MarkdownParser>' | |
from parser.rb:3:in `<main>' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Execute: `ruby parser.rb` | |
# Context: building a textformatter for Prawn that takes markdown | |
# instead of the usual html elements and formats them inline. | |
# The regex is part of the original with the HTML replacements | |
# still in place. I added the second <b> matching (without the /) | |
# as a first step towards a solution, and this still works. | |
# All of my attempts to replace the <b> with * in that case | |
# have failed so far. Some simply don't work as expected, | |
# others throw an error. | |
require 'minitest/autorun' | |
class MarkdownParser | |
# @group Extension API | |
PARSER_REGEX = begin | |
regex_string = "\n|" \ | |
"<b>|</b>|" \ | |
"<b>|<b>|" \ | |
"[^<\n]+" | |
Regexp.new(regex_string, Regexp::MULTILINE) | |
end | |
def self.parse input, regex=PARSER_REGEX | |
input.scan(regex) | |
end | |
end | |
class TestMarkdownParser < Minitest::Test | |
def test_parser_1 | |
string = "foo <b>bold</b> bar" | |
assert_equal ["foo ", "<b>", "bold", "</b>", " bar"], MarkdownParser.parse(string) | |
end | |
def test_parser_2 | |
string = "foo <b>bold<b> bar" | |
assert_equal ["foo ", "<b>", "bold", "<b>", " bar"], MarkdownParser.parse(string) | |
end | |
def test_parser_3 | |
string = "foo *bold* bar" | |
assert_equal ["foo ", "*", "bold", "*", " bar"], MarkdownParser.parse(string) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment