Skip to content

Commit

Permalink
Refactor method names and property accessors
Browse files Browse the repository at this point in the history
  • Loading branch information
straight-shoota committed Oct 11, 2017
1 parent 00d57f0 commit 191393e
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 79 deletions.
Empty file added spec-out.txt
Empty file.
10 changes: 5 additions & 5 deletions src/markd/lexers/block.cr
Expand Up @@ -106,7 +106,7 @@ module Markd::Lexer
line = line.gsub(Char::ZERO, '\u{FFFD}')
@line = line

while (last_child = container.last_child) && last_child.open?
while (last_child = container.last_child?) && last_child.open?
container = last_child

find_next_nonspace
Expand All @@ -117,7 +117,7 @@ module Markd::Lexer
when Rule::ContinueStatus::Stop
# we've failed to match a block
# back up to last matching block
container = container.parent!
container = container.parent
break
when Rule::ContinueStatus::Return
# we've hit end of line for fenced code close and can return
Expand Down Expand Up @@ -168,15 +168,15 @@ module Markd::Lexer
else
# not a lazy continuation
close_unmatched_blocks
if @blank && (last_child = container.last_child)
if @blank && (last_child = container.last_child?)
last_child.last_line_blank = true
end

container_type = container.type
last_line_blank = @blank &&
!(container_type.block_quote? ||
(container_type.code_block? && container.fenced?) ||
(container_type.item? && !container.first_child && container.source_pos[0][0] == @current_line))
(container_type.item? && !container.first_child? && container.source_pos[0][0] == @current_line))

cont = container
while cont
Expand Down Expand Up @@ -261,7 +261,7 @@ module Markd::Lexer
def close_unmatched_blocks
unless @all_closed
while (oldtip = @oldtip) && oldtip != @last_matched_container
parent = oldtip.parent
parent = oldtip.parent?
token(oldtip, @current_line - 1)
@oldtip = parent
end
Expand Down
12 changes: 6 additions & 6 deletions src/markd/lexers/inline.cr
Expand Up @@ -69,9 +69,9 @@ module Markd::Lexer

private def newline(node : Node)
@pos += 1 # assume we're at a \n
last_child = node.last_child
last_child = node.last_child?
# check previous node for trailing spaces
if last_child && last_child.type == Node::Type::Text &&
if last_child && last_child.type.text? &&
char(last_child.text, -1) == ' '
hard_break = if last_child.text.size == 1
false # Must be space
Expand Down Expand Up @@ -249,9 +249,9 @@ module Markd::Lexer
child.data["destination"] = dest
child.data["title"] = title || ""

tmp = opener.node.next
tmp = opener.node.next?
while tmp
next_node = tmp.next
next_node = tmp.next?
tmp.unlink
child.append_child(tmp)
tmp = next_node
Expand Down Expand Up @@ -339,9 +339,9 @@ module Markd::Lexer
# build contents for new emph element
emph = Node.new((use_delims == 1) ? Node::Type::Emphasis : Node::Type::Strong)

tmp = opener_inl.next
tmp = opener_inl.next?
while tmp && tmp != closer_inl
next_node = tmp.next
next_node = tmp.next?
tmp.unlink
emph.append_child(tmp)
tmp = next_node
Expand Down
87 changes: 39 additions & 48 deletions src/markd/node.cr
Expand Up @@ -33,29 +33,23 @@ module Markd

property data : Hash(String, DataValue)

property parent : Node?
def parent?
@parent
end
def parent!
@parent.not_nil!
end
property first_child : Node?
property last_child : Node?
property prev : Node?
property next : Node?
property! parent : Node?
property! first_child : Node?
property! last_child : Node?
property! prev : Node?
property! next : Node?

property source_pos : Array(Array(Int32))
property? open
property last_line_blank : Bool
property? last_line_blank : Bool

property fenced : Bool
property? fenced : Bool
property fence_language : String
property fence_char : String
property fence_length : Int32
property fence_offset : Int32

def initialize(@type, **options)
def initialize(@type)
@data = {} of String => DataValue
@source_pos = [[1, 1], [0, 0]]
@text = ""
Expand All @@ -74,9 +68,9 @@ module Markd
child.unlink
child.parent = self

if @last_child
@last_child.not_nil!.next = child
child.prev = @last_child.not_nil!
if last = last_child?
last.next = child
child.prev = last
@last_child = child
else
@first_child = child
Expand All @@ -86,41 +80,37 @@ module Markd

def insert_after(sibling : Node)
sibling.unlink
sibling.next = @next
if sibling.next
sibling.next.not_nil!.prev = sibling

if nxt = next?
nxt.prev = sibling
elsif parent = parent?
parent.last_child = sibling
end
sibling.next = nxt

sibling.prev = self
@next = sibling
sibling.parent = @parent
unless sibling.next
sibling.parent.not_nil!.last_child = sibling
end
sibling.parent = parent?
end

def unlink
if @prev
@prev.not_nil!.next = @next
elsif @parent
@parent.not_nil!.first_child = @next
if prev = prev?
prev.next = next?
elsif parent = parent?
parent.first_child = next?
end

if @next
@next.not_nil!.prev = @prev
elsif @parent
@parent.not_nil!.last_child = @prev
if nxt = next?
nxt.prev = prev?
elsif parent = parent?
parent.last_child = prev?
end

@parent = nil
@next = nil
@prev = nil
end

def fenced?
@fenced == true
end

def walker
Walker.new(self)
end
Expand All @@ -141,35 +131,36 @@ module Markd
property root : Node
property entering : Bool

def initialize(@current : Node)
@root = @current.not_nil!
def initialize(current : Node)
@current = current
@root = current
@entering = true
end

def next
return unless @current
current = @current
return unless current

current = @current.not_nil!
entering = @entering

if entering && container?(current.type)
if current.first_child
@current = current.first_child.not_nil!
if first_child = current.first_child?
@current = first_child
@entering = true
else
@entering = false
end
elsif current == @root
@current = nil
elsif !current.next
@current = current.parent
@entering = false
else
@current = current.next
elsif nxt = current.next?
@current = current.next?
@entering = true
else
@current = current.parent?
@entering = false
end

return {
{
entering: entering,
node: current,
}
Expand Down
2 changes: 1 addition & 1 deletion src/markd/renderers/html_renderer.cr
Expand Up @@ -176,7 +176,7 @@ module Markd
end

private def toc(node : Node)
return if node.type != Node::Type::Heading
return unless node.type.heading?

title = URI.escape(node.text)

Expand Down
2 changes: 1 addition & 1 deletion src/markd/rules/block_quote.cr
Expand Up @@ -28,7 +28,7 @@ module Markd::Rule
end

def can_contain?(type : Node::Type) : Bool
type != Node::Type::Item
!type.item?
end

def accepts_lines?
Expand Down
5 changes: 2 additions & 3 deletions src/markd/rules/code_block.cr
Expand Up @@ -23,9 +23,8 @@ module Markd::Rule

MatchValue::Leaf
elsif parser.indented && !parser.blank && (tip = parser.tip) &&
tip.type != Node::Type::Paragraph &&
(container.type != Node::Type::List ||
(container.type == Node::Type::List && container.data["padding"].as(Int32) >= 4))
!tip.type.paragraph? &&
(!container.type.list? || container.data["padding"].as(Int32) >= 4)
# indented
parser.advance_offset(Rule::CODE_INDENT, true)
parser.close_unmatched_blocks
Expand Down
2 changes: 1 addition & 1 deletion src/markd/rules/document.cr
Expand Up @@ -15,7 +15,7 @@ module Markd::Rule
end

def can_contain?(type : Node::Type) : Bool
type != Node::Type::Item
!type.item?
end

def accepts_lines?
Expand Down
2 changes: 1 addition & 1 deletion src/markd/rules/html_block.cr
Expand Up @@ -9,7 +9,7 @@ module Markd::Rule

Rule::HTML_BLOCK_OPEN.each_with_index do |regex, index|
if (text.match(regex) &&
(index < block_type_size || container.type != Node::Type::Paragraph))
(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
4 changes: 2 additions & 2 deletions src/markd/rules/item.cr
Expand Up @@ -11,7 +11,7 @@ module Markd::Rule
indent_offset = container.data["marker_offset"].as(Int32) + container.data["padding"].as(Int32)

if parser.blank
if container.first_child
if container.first_child?
parser.advance_next_nonspace
else
# Blank line after empty list item
Expand All @@ -31,7 +31,7 @@ module Markd::Rule
end

def can_contain?(type : Node::Type)
type != Node::Type::Item
!type.item?
end

def accepts_lines?
Expand Down
22 changes: 11 additions & 11 deletions src/markd/rules/list.cr
Expand Up @@ -6,7 +6,7 @@ module Markd::Rule
ORDERED_LIST_MARKERS = {'.', ')'}

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

Expand All @@ -30,24 +30,24 @@ module Markd::Rule
end

def token(parser : Lexer, container : Node)
item = container.first_child
item = container.first_child?
while item
if ends_with_blankline?(item) && item.next
if ends_with_blankline?(item) && item.next?
container.data["tight"] = false
break
end

subitem = item.first_child
subitem = item.first_child?
while subitem
if ends_with_blankline?(subitem) && (item.next || subitem.next)
if ends_with_blankline?(subitem) && (item.next? || subitem.next?)
container.data["tight"] = false
break
end

subitem = subitem.next
subitem = subitem.next?
end

item = item.next
item = item.next?
end
end

Expand Down Expand Up @@ -88,7 +88,7 @@ module Markd::Rule
end
number = pos >= 1 ? line[0..pos - 1].to_i : -1
if pos >= 1 && pos <= 9 && ORDERED_LIST_MARKERS.includes?(line[pos]?) &&
(container.type != Node::Type::Paragraph || number == 1)
(!container.type.paragraph? || number == 1)
data["type"] = "ordered"
data["start"] = number
data["delimiter"] = line[pos].to_s
Expand All @@ -103,7 +103,7 @@ module Markd::Rule
return empty_data
end

if container.type == Node::Type::Paragraph &&
if container.type.paragraph? &&
slice(parser, parser.next_nonspace + first_match_size).each_char.all? &.ascii_whitespace?
return empty_data
end
Expand Down Expand Up @@ -137,10 +137,10 @@ module Markd::Rule

private def ends_with_blankline?(container : Node) : Bool
while container
return true if container.last_line_blank
return true if container.last_line_blank?

break unless [Node::Type::List, Node::Type::Item].includes?(container.type)
container = container.last_child
container = container.last_child?
end

false
Expand Down

0 comments on commit 191393e

Please sign in to comment.