Skip to content

Commit

Permalink
added corner types check
Browse files Browse the repository at this point in the history
  • Loading branch information
Jens Nazarenus committed Dec 24, 2013
1 parent 98735a3 commit f2c10a3
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 7 deletions.
26 changes: 22 additions & 4 deletions lib/sketchdown/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,34 @@ class Parser

def initialize(grid)
@grid = grid
@root = @grid.root
end

def parse

# 1. remove rectangles
# 2. get lines, arrows, etc.
# 3. render that
# 4. loop through rectangles and build a grid
# duplicate layer
# find and remove rectangles
# render lines, arrows and stuff
# build layer for rectangles and parse them
#
#
# ---+
# | +-------+
# +---> | |
# +-------+
#
# +--> +------+
# | | |
# | | |
# +------+

for e in @root

end

end

def

end
end
39 changes: 38 additions & 1 deletion lib/sketchdown/textgrid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,49 @@ def is_figure(cell, mark)
marks.each do |k,v|
if v.include?(ch)
includes = true
type = k

if mark == :corner
type = get_corner_type(cell)
else
type = k
end
end
end
return [includes, type]
end

def is_hline?(cell)
is_figure(cell, :line)[1] == :horizontal
end

def is_vline?(cell)
is_figure(cell, :line)[1] == :vertical
end

# Determines the corner type of the current cell.
#
# cell - the cell which is used for determination
#
# Returns :north_east, :north_west, :south_east,
# :south_west
def get_corner_type(cell)
neighbors = get_neighbors(cell)
north = neighbors[:north]
south = neighbors[:south]
west = neighbors[:west]
east = neighbors[:east]

if is_hline?(east) && is_vline?(south)
:north_west
elsif is_hline?(west) && is_vline?(south)
:north_east
elsif is_hline?(west) && is_vline?(north)
:south_east
elsif is_hline?(east) && is_vline?(north)
:south_west
end
end

# Calculates the width. The width is the length of the line with
# the most caharacters.
#
Expand Down
24 changes: 22 additions & 2 deletions test/test_textgrid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,10 @@ class TestTextgrid < Test::Unit::TestCase
@cell_vline = Textcell.new(0,1)
@cell_arrow_w = Textcell.new(16,1)
@cell_blank = Textcell.new(1,1)
end
end

should "recognize a corner" do
assert_equal [true, :all], @grid.is_figure(@cell_corner, :corner)
assert_equal [true, :north_west], @grid.is_figure(@cell_corner, :corner)
end

should "recognize a horizontal line" do
Expand All @@ -125,5 +125,25 @@ class TestTextgrid < Test::Unit::TestCase
assert_equal [true, :all], @grid.is_figure(@cell_blank, :blank)
end
end

context "corner types" do
setup do
@grid = Textgrid.new(@input)
@nw = Textcell.new(0,0)
@ne = Textcell.new(14,0)
@se = Textcell.new(14,3)
@sw = Textcell.new(0,3)
@no = Textcell.new(99,99)
end

should "recognize all corners" do
assert_equal [true, :north_west], @grid.is_figure(@nw, :corner)
assert_equal [true, :north_east], @grid.is_figure(@ne, :corner)
assert_equal [true, :south_east], @grid.is_figure(@se, :corner)
assert_equal [true, :south_west], @grid.is_figure(@sw, :corner)
assert_equal [false, nil], @grid.is_figure(@no, :corner)
end

end
end
end

0 comments on commit f2c10a3

Please sign in to comment.