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
9 changes: 2 additions & 7 deletions .ameba.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,9 @@ Naming/QueryBoolMethods:
Naming/BlockParameterName:
Enabled: false

Style/UnlessElse:
Enabled: false

Style/ParenthesesAroundCondition:
Enabled: false

Style/NegatedConditionsInUnless:
Enabled: false
Enabled: true
AllowSafeAssignment: true

Lint/NotNil:
Excluded:
Expand Down
4 changes: 2 additions & 2 deletions spec/spec_helper.cr
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,11 @@ def extract_spec_tests(file)
result_start = false

begin
File.open(file) do |f|
File.open(file) do |input|
line_number = 0
test_tags = ""

while line = f.read_line
while (line = input.read_line)
line_number += 1
line = line.gsub(/\r\n?/, "\n")
break if line.includes?("<!-- END TESTS -->")
Expand Down
4 changes: 2 additions & 2 deletions src/markd/html_entities.cr
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ module Markd::HTMLEntities
end
else
entities_key = chars[0..-1]
if resolved_entity = Markd::HTMLEntities::ENTITIES_MAPPINGS[entities_key]?
if (resolved_entity = Markd::HTMLEntities::ENTITIES_MAPPINGS[entities_key]?)
return resolved_entity
end
end
Expand All @@ -48,7 +48,7 @@ module Markd::HTMLEntities
def self.decode_codepoint(codepoint)
return "\uFFFD" if codepoint >= 0xD800 && codepoint <= 0xDFFF || codepoint > 0x10FFF

if decoded = Markd::HTMLEntities::DECODE_MAPPINGS[codepoint]?
if (decoded = Markd::HTMLEntities::DECODE_MAPPINGS[codepoint]?)
codepoint = decoded
end

Expand Down
16 changes: 8 additions & 8 deletions src/markd/node.cr
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ module Markd
child.unlink
child.parent = self

if last = last_child?
if (last = last_child?)
last.next = child
child.prev = last
@last_child = child
Expand All @@ -97,9 +97,9 @@ module Markd
def insert_after(sibling : Node)
sibling.unlink

if nxt = next?
if (nxt = next?)
nxt.prev = sibling
elsif parent = parent?
elsif (parent = parent?)
parent.last_child = sibling
end
sibling.next = nxt
Expand All @@ -110,15 +110,15 @@ module Markd
end

def unlink
if prev = prev?
if (prev = prev?)
prev.next = next?
elsif parent = parent?
elsif (parent = parent?)
parent.first_child = next?
end

if nxt = next?
if (nxt = next?)
nxt.prev = prev?
elsif parent = parent?
elsif (parent = parent?)
parent.last_child = prev?
end

Expand Down Expand Up @@ -158,7 +158,7 @@ module Markd
entering = @entering

if entering && current.type.container?
if first_child = current.first_child?
if (first_child = current.first_child?)
@current = first_child
@entering = true
else
Expand Down
8 changes: 4 additions & 4 deletions src/markd/parsers/block.cr
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ module Markd::Parser
# ignore last blank line created by final newline
lines_size -= 1 if source.ends_with?('\n')

while tip = tip?
while (tip = tip?)
token(tip, lines_size)
end
end
Expand Down Expand Up @@ -177,7 +177,7 @@ module Markd::Parser
add_line

# if HtmlBlock, check for end condition
if (container_type.html_block? && match_html_block?(container))
if container_type.html_block? && match_html_block?(container)
token(container, @current_line)
end
elsif @offset < line.size && !@blank
Expand Down Expand Up @@ -269,7 +269,7 @@ module Markd::Parser
if @line.empty?
@blank = true
else
while char = @line[offset]?
while (char = @line[offset]?)
case char
when ' '
offset += 1
Expand Down Expand Up @@ -330,7 +330,7 @@ module Markd::Parser
end

private def match_html_block?(container : Node)
if block_type = container.data["html_block_type"]
if (block_type = container.data["html_block_type"])
block_type = block_type.as(Int32)
block_type >= 0 && block_type <= 4 && Rule::HTML_BLOCK_CLOSE[block_type].match(@line[@offset..-1])
else
Expand Down
40 changes: 20 additions & 20 deletions src/markd/parsers/inline.cr
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ module Markd::Parser

num_ticks = @pos - start_pos
after_open_ticks = @pos
while text = match(Rule::TICKS)
while (text = match(Rule::TICKS))
if text.bytesize == num_ticks
child = Node.new(Node::Type::Code)
child_text = @text.byte_slice(after_open_ticks, (@pos - num_ticks) - after_open_ticks).gsub(Rule::LINE_ENDING, " ")
Expand Down Expand Up @@ -387,9 +387,7 @@ module Markd::Parser
case closer_char
when '*', '_', '~'
if closer_char != '~' || (closer_char == '~' && @options.gfm)
unless opener
closer = closer.next?
else
if opener
# calculate actual number of delimiters used from closer
use_delims = (closer.num_delims >= 2 && opener.num_delims >= 2) ? 2 : 1

Expand Down Expand Up @@ -444,6 +442,8 @@ module Markd::Parser
remove_delimiter(closer)
closer = tmp_stack
end
else
closer = closer.next?
end
end
when '\''
Expand Down Expand Up @@ -476,17 +476,17 @@ module Markd::Parser
end

private def auto_link(node : Node)
if matched_text = match(Rule::EMAIL_AUTO_LINK)
if (matched_text = match(Rule::EMAIL_AUTO_LINK))
node.append_child(link(matched_text, true))
return true
elsif matched_text = match(Rule::AUTO_LINK)
elsif (matched_text = match(Rule::AUTO_LINK))
node.append_child(link(matched_text, false))
return true
elsif @options.gfm && @options.autolink
# These are all the extended autolinks from the
# autolink extension

if matched_text = match(Rule::WWW_AUTO_LINK)
if (matched_text = match(Rule::WWW_AUTO_LINK))
clean_text = autolink_cleanup(matched_text)
if clean_text.empty?
node.append_child(text(matched_text))
Expand All @@ -496,11 +496,11 @@ module Markd::Parser
node.append_child(text(post)) if post.size > 0 && matched_text != clean_text
end
return true
elsif matched_text = (
elsif (matched_text = (
match(Rule::PROTOCOL_AUTO_LINK) ||
match(Rule::XMPP_AUTO_LINK) ||
match(Rule::MAILTO_AUTO_LINK)
)
))
clean_text = autolink_cleanup(matched_text)
if clean_text.empty?
node.append_child(text(matched_text))
Expand All @@ -510,7 +510,7 @@ module Markd::Parser
node.append_child(text(post)) if post.size > 0 && matched_text != clean_text
end
return true
elsif matched_text = match(Rule::EXTENDED_EMAIL_AUTO_LINK)
elsif (matched_text = match(Rule::EXTENDED_EMAIL_AUTO_LINK))
# Emails that end in - or _ are declared not to be links by the spec:
#
# `.`, `-`, and `_` can occur on both sides of the `@`, but only `.` may occur at
Expand All @@ -532,7 +532,7 @@ module Markd::Parser
end

private def html_tag(node : Node)
if text = match(Rule::HTML_TAG)
if (text = match(Rule::HTML_TAG))
child = Node.new(Node::Type::HTMLInline)

if @options.gfm && @options.tagfilter
Expand Down Expand Up @@ -614,7 +614,7 @@ module Markd::Parser
end

private def string(node : Node)
if text = match_main
if (text = match_main)
if @options.smart?
text = text.gsub(Rule::ELLIPSIS, '\u{2026}')
.gsub(Rule::DASH) do |chars|
Expand Down Expand Up @@ -674,12 +674,12 @@ module Markd::Parser
end

private def link_destination
dest = if text = match(Rule::LINK_DESTINATION_BRACES)
dest = if (text = match(Rule::LINK_DESTINATION_BRACES))
text[1..-2]
elsif char_at?(@pos) != '<'
save_pos = @pos
open_parens = 0
while char = char_at?(@pos)
while (char = char_at?(@pos))
case char
when '\\'
@pos += 1
Expand Down Expand Up @@ -726,7 +726,7 @@ module Markd::Parser

delimiter = Delimiter.new(char, num_delims, num_delims, child, @delimiters, nil, res[:can_open], res[:can_close])

if prev = delimiter.previous?
if (prev = delimiter.previous?)
prev.next = delimiter
end

Expand All @@ -736,11 +736,11 @@ module Markd::Parser
end

private def remove_delimiter(delimiter : Delimiter)
if prev = delimiter.previous?
if (prev = delimiter.previous?)
prev.next = delimiter.next?
end

if nxt = delimiter.next?
if (nxt = delimiter.next?)
nxt.previous = delimiter.previous?
else
# top of stack
Expand Down Expand Up @@ -897,7 +897,7 @@ module Markd::Parser
# Parse zero or more space characters, including at most one newline
private def spnl
seen_newline = false
while c = char_at?(@pos)
while (c = char_at?(@pos))
if !seen_newline && c == '\n'
seen_newline = true
elsif c != ' '
Expand All @@ -912,7 +912,7 @@ module Markd::Parser

private def match(regex : Regex) : String?
text = @text.byte_slice(@pos)
if match = text.match(regex)
if (match = text.match(regex))
@pos += match.byte_end.not_nil!
return match[0]
end
Expand All @@ -926,7 +926,7 @@ module Markd::Parser

private def match_main : String?
start_pos = @pos
while char = char_at?(@pos)
while (char = char_at?(@pos))
# If we detected a special string (like a URL), and it's
# not the beggining of the string, we need to break right away.
#
Expand Down
2 changes: 1 addition & 1 deletion src/markd/renderer.cr
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ module Markd
def render(document : Node, formatter : T?) forall T
Utils.timer("rendering", @options.time) do
walker = document.walker
while event = walker.next
while (event = walker.next)
node, entering = event

case node.type
Expand Down
2 changes: 1 addition & 1 deletion src/markd/renderers/html_renderer.cr
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ module Markd
def strong(node : Node, entering : Bool) : Nil
@strong_stack -= 1 if @options.gfm && !entering

tag("strong", end_tag: !entering) if (@strong_stack == 0)
tag("strong", end_tag: !entering) if @strong_stack == 0

@strong_stack += 1 if @options.gfm && entering
end
Expand Down
2 changes: 1 addition & 1 deletion src/markd/rules/heading.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module Markd::Rule
SETEXT_HEADING_MARKER = /^(?:=+|-+)[ \t]*$/

def match(parser : Parser, container : Node) : MatchValue
if match = match?(parser, ATX_HEADING_MARKER)
if (match = match?(parser, ATX_HEADING_MARKER))
# ATX Heading matched
parser.advance_next_nonspace
parser.advance_offset(match[0].size, false)
Expand Down
10 changes: 5 additions & 5 deletions src/markd/rules/html_block.cr
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ module Markd::Rule
block_type_size = Rule::HTML_BLOCK_OPEN.size - 1

Rule::HTML_BLOCK_OPEN.each_with_index do |regex, index|
if (text.match(regex) &&
(index < block_type_size || !container.type.paragraph?))
if text.match(regex) &&
(index < block_type_size || !container.type.paragraph?)
parser.close_unmatched_blocks
# We don't adjust parser.offset;
# spaces are part of the HTML block:
Expand Down Expand Up @@ -47,18 +47,18 @@ module Markd::Rule
end

def self.escape_disallowed_html(text : String) : String
String.build do |s|
String.build do |string|
pos = 0

text.scan(/<\/?\s*(#{GFM_DISALLOWED_HTML_TAGS.join('|')})\b/i) do |match|
start = text.index(match[0], pos)
next if start.nil?

s << text[pos...start] << "&lt;#{match[0][1..]}"
string << text[pos...start] << "&lt;#{match[0][1..]}"
pos = start + match[0].size
end

s << text[pos..-1]
string << text[pos..-1]
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions src/markd/rules/list.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ module Markd::Rule
ORDERED_LIST_MARKERS = {'.', ')'}

def match(parser : Parser, container : Node) : MatchValue
if (!parser.indented || container.type.list?)
if !parser.indented || container.type.list?
data = parse_list_marker(parser, container)
return MatchValue::None unless data && !data.empty?
return MatchValue::None if !data || data.empty?

parser.close_unmatched_blocks
if !parser.tip.type.list? || !list_match?(container.data, data)
Expand Down Expand Up @@ -167,7 +167,7 @@ module Markd::Rule
while container
return true if container.last_line_blank?

break unless !container.last_line_checked? && container.type.in?(Node::Type::List, Node::Type::Item)
break if container.last_line_checked? || !container.type.in?(Node::Type::List, Node::Type::Item)
container.last_line_checked = true
container = container.last_child?
end
Expand Down
4 changes: 2 additions & 2 deletions src/markd/rules/table.cr
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ module Markd::Rule
def token(parser : Parser, container : Node) : Nil
lines = container.text.strip.split("\n")

row_sizes = lines[...2].map do |l|
strip_pipe(l.strip).split(TABLE_CELL_SEPARATOR).size
row_sizes = lines[...2].map do |line|
strip_pipe(line.strip).split(TABLE_CELL_SEPARATOR).size
end.uniq!

# Do we have a real table?
Expand Down