Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce repetitive method calls via local variables #576

Closed
Closed
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
4 changes: 2 additions & 2 deletions lib/kramdown/parser/kramdown/blank_line.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ class Kramdown
# Parse the blank line at the current postition.
def parse_blank_line
@src.pos += @src.matched_size
if @tree.children.last && @tree.children.last.type == :blank
@tree.children.last.value << @src.matched
if (last_child = @tree.children.last) && last_child.type == :blank
last_child.value << @src.matched
else
@tree.children << new_block_el(:blank, @src.matched)
end
Expand Down
5 changes: 3 additions & 2 deletions lib/kramdown/parser/kramdown/block_boundary.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ class Kramdown

# Return +true+ if we are after a block boundary.
def after_block_boundary?
!@tree.children.last || @tree.children.last.type == :blank ||
(@tree.children.last.type == :eob && @tree.children.last.value.nil?) || @block_ial
last_child = @tree.children.last
!last_child || last_child.type == :blank ||
(last_child.type == :eob && last_child.value.nil?) || @block_ial
end

# Return +true+ if we are before a block boundary.
Expand Down
14 changes: 7 additions & 7 deletions lib/kramdown/parser/kramdown/extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,10 @@ def parse_block_extensions
elsif @src.check(EXT_BLOCK_START)
parse_extension_start_tag(:block)
elsif @src.scan(IAL_BLOCK_START)
if @tree.children.last && @tree.children.last.type != :blank &&
(@tree.children.last.type != :eob ||
[:link_def, :abbrev_def, :footnote_def].include?(@tree.children.last.value))
parse_attribute_list(@src[1], @tree.children.last.options[:ial] ||= {})
if (last_child = @tree.children.last) && last_child.type != :blank &&
(last_child.type != :eob ||
[:link_def, :abbrev_def, :footnote_def].include?(last_child.value))
parse_attribute_list(@src[1], last_child.options[:ial] ||= {})
@tree.children << new_block_el(:eob, :ial) unless @src.check(IAL_BLOCK_START)
else
parse_attribute_list(@src[1], @block_ial ||= {})
Expand All @@ -187,12 +187,12 @@ def parse_span_extensions
if @src.check(EXT_SPAN_START)
parse_extension_start_tag(:span)
elsif @src.check(IAL_SPAN_START)
if @tree.children.last && @tree.children.last.type != :text
if (last_child = @tree.children.last) && last_child.type != :text
@src.pos += @src.matched_size
attr = {}
parse_attribute_list(@src[1], attr)
update_ial_with_ial(@tree.children.last.options[:ial] ||= {}, attr)
update_attr_with_ial(@tree.children.last.attr, attr)
update_ial_with_ial(last_child.options[:ial] ||= {}, attr)
update_attr_with_ial(last_child.attr, attr)
else
warning("Found span IAL after text - ignoring it")
add_text(@src.getch)
Expand Down
5 changes: 3 additions & 2 deletions lib/kramdown/parser/kramdown/link.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,9 @@ def parse_link
emit_warning = !@src[1]
link_id = normalize_link_id(@src[1] || alt_text)
if @link_defs.key?(link_id)
add_link(el, @link_defs[link_id][0], @link_defs[link_id][1], alt_text,
@link_defs[link_id][2] && @link_defs[link_id][2].options[:ial])
link_def = @link_defs[link_id]
add_link(el, link_def[0], link_def[1], alt_text,
link_def[2] && link_def[2].options[:ial])
else
if emit_warning
warning("No link definition for link ID '#{link_id}' found on line #{start_line_number}")
Expand Down
52 changes: 28 additions & 24 deletions lib/kramdown/parser/kramdown/list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def parse_list
parse_first_list_line(@src[1].length, @src[2])
list.children << item

item.value.sub!(self.class::LIST_ITEM_IAL) do |_match|
item.value.sub!(self.class::LIST_ITEM_IAL) do
parse_attribute_list($1, item.options[:ial] ||= {})
''
end
Expand Down Expand Up @@ -122,22 +122,24 @@ def parse_list

it.children = temp.children
it.value = nil
next if it.children.empty?

it_children = it.children
next if it_children.empty?

# Handle the case where an EOB marker is inserted by a block IAL for the first paragraph
it.children.delete_at(1) if it.children.first.type == :p &&
it.children.length >= 2 && it.children[1].type == :eob && it.children.first.options[:ial]
it_children.delete_at(1) if it_children.first.type == :p &&
it_children.length >= 2 && it_children[1].type == :eob && it_children.first.options[:ial]

if it.children.first.type == :p &&
(it.children.length < 2 || it.children[1].type != :blank ||
(it == list.children.last && it.children.length == 2 && !eob_found)) &&
if it_children.first.type == :p &&
(it_children.length < 2 || it_children[1].type != :blank ||
(it == list.children.last && it_children.length == 2 && !eob_found)) &&
(list.children.last != it || list.children.size == 1 ||
list.children[0..-2].any? {|cit| !cit.children.first || cit.children.first.type != :p || cit.children.first.options[:transparent] })
it.children.first.children.first.value << "\n" if it.children.size > 1 && it.children[1].type != :blank
it.children.first.options[:transparent] = true
list.children[0..-2].any? {|cit| !cit.children.first || cit.children.first.type != :p || cit.children.first.options[:transparent] })
it_children.first.children.first.value << "\n" if it_children.size > 1 && it_children[1].type != :blank
it_children.first.options[:transparent] = true
end

last = (it.children.last.type == :blank ? it.children.pop : nil)
last = (it_children.last.type == :blank ? it_children.pop : nil)
end

@tree.children << last if !last.nil? && !eob_found
Expand Down Expand Up @@ -222,27 +224,29 @@ def parse_definition_list

parse_blocks(it, it.value)
it.value = nil
next if it.children.empty?
it_children = it.children
next if it_children.empty?

last = (it.children.last.type == :blank ? it.children.pop : nil)
last = (it_children.last.type == :blank ? it_children.pop : nil)

if it.children.first && it.children.first.type == :p && !it.options.delete(:first_as_para)
it.children.first.children.first.value << "\n" if it.children.size > 1
it.children.first.options[:transparent] = true
if it_children.first && it_children.first.type == :p && !it.options.delete(:first_as_para)
it_children.first.children.first.value << "\n" if it_children.size > 1
it_children.first.options[:transparent] = true
end
end

if @tree.children.length >= 1 && @tree.children.last.type == :dl
@tree.children[-1].children.concat(deflist.children)
elsif @tree.children.length >= 2 && @tree.children[-1].type == :blank &&
@tree.children[-2].type == :dl
@tree.children.pop
@tree.children[-1].children.concat(deflist.children)
children = @tree.children
if children.length >= 1 && children.last.type == :dl
children[-1].children.concat(deflist.children)
elsif children.length >= 2 && children[-1].type == :blank &&
children[-2].type == :dl
children.pop
children[-1].children.concat(deflist.children)
else
@tree.children << deflist
children << deflist
end

@tree.children << last if last
children << last if last

true
end
Expand Down
6 changes: 3 additions & 3 deletions lib/kramdown/parser/kramdown/paragraph.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ def parse_paragraph
result << @src.scan(PARAGRAPH_MATCH)
end
result.rstrip!
if @tree.children.last && @tree.children.last.type == :p
last_item_in_para = @tree.children.last.children.last
if (last_child = @tree.children.last) && last_child.type == :p
last_item_in_para = last_child.children.last
if last_item_in_para && last_item_in_para.type == @text_type
joiner = (extract_string((pos - 3)...pos, @src) == " \n" ? " \n" : "\n")
last_item_in_para.value << joiner << result
else
add_text(result, @tree.children.last)
add_text(result, last_child)
end
else
@tree.children << new_block_el(:p, nil, nil, location: start_line_number)
Expand Down