Skip to content

Commit

Permalink
Replace Rule#char_at with idiomatic Crystal
Browse files Browse the repository at this point in the history
  • Loading branch information
straight-shoota committed Oct 11, 2017
1 parent cc888f7 commit 54444d1
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 12 deletions.
4 changes: 0 additions & 4 deletions src/markd/rule.cr
Expand Up @@ -105,10 +105,6 @@ module Markd
# accepts_line
abstract def accepts_lines? : Bool

private def char_at(parser : Parser, index = parser.next_nonspace) : Char?
parser.line[index]?
end

private def space_or_tab?(char : Char?) : Bool
[' ', '\t'].includes?(char)
end
Expand Down
4 changes: 2 additions & 2 deletions src/markd/rules/block_quote.cr
Expand Up @@ -36,14 +36,14 @@ module Markd::Rule
end

private def match?(parser)
!parser.indented && char_at(parser) == '>'
!parser.indented && parser.line[parser.next_nonspace]? == '>'
end

private def seek(parser : Parser)
parser.advance_next_nonspace
parser.advance_offset(1, false)

if space_or_tab?(char_at(parser, parser.offset))
if space_or_tab?(parser.line[parser.offset]?)
parser.advance_offset(1, true)
end
end
Expand Down
2 changes: 1 addition & 1 deletion src/markd/rules/code_block.cr
Expand Up @@ -52,7 +52,7 @@ module Markd::Rule
else
# skip optional spaces of fence offset
index = container.fence_offset
while index > 0 && space_or_tab?(char_at(parser, parser.offset))
while index > 0 && space_or_tab?(parser.line[parser.offset]?)
parser.advance_offset(1, true)
index -= 1
end
Expand Down
2 changes: 1 addition & 1 deletion src/markd/rules/html_block.cr
Expand Up @@ -3,7 +3,7 @@ module Markd::Rule
include Rule

def match(parser : Parser, container : Node)
if !parser.indented && char_at(parser) == '<'
if !parser.indented && parser.line[parser.next_nonspace]? == '<'
text = parser.line[parser.next_nonspace..-1]
block_type_size = Rule::HTML_BLOCK_OPEN.size - 1

Expand Down
8 changes: 4 additions & 4 deletions src/markd/rules/list.cr
Expand Up @@ -98,7 +98,7 @@ module Markd::Rule
end
end

next_char = char_at(parser, parser.next_nonspace + first_match_size)
next_char = parser.line[parser.next_nonspace + first_match_size]?
unless next_char.nil? || space_or_tab?(next_char)
return empty_data
end
Expand All @@ -115,19 +115,19 @@ module Markd::Rule

loop do
parser.advance_offset(1, true)
next_char = char_at(parser, parser.offset)
next_char = parser.line[parser.offset]?

break unless parser.column - spaces_start_column < 5 && space_or_tab?(next_char)
end

blank_item = char_at(parser, parser.offset).nil?
blank_item = parser.line[parser.offset]?.nil?
spaces_after_marker = parser.column - spaces_start_column
if spaces_after_marker >= 5 || spaces_after_marker < 1 || blank_item
data["padding"] = first_match_size + 1
parser.column = spaces_start_column
parser.offset = spaces_start_offset

parser.advance_offset(1, true) if space_or_tab?(char_at(parser, parser.offset))
parser.advance_offset(1, true) if space_or_tab?(parser.line[parser.offset]?)
else
data["padding"] = first_match_size + spaces_after_marker
end
Expand Down

0 comments on commit 54444d1

Please sign in to comment.