Skip to content

Commit

Permalink
Merge pull request #44 from gfx/inspect
Browse files Browse the repository at this point in the history
support p and pp for CommonMarker::Node
  • Loading branch information
gjtorikian authored Apr 22, 2017
2 parents 8de8518 + b29e92e commit f44edb4
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/commonmarker/node.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
require 'commonmarker/node/inspect'

module CommonMarker
class Node
include Enumerable
include Inspect

# Public: An iterator that "walks the tree," descending into children recursively.
#
Expand Down
59 changes: 59 additions & 0 deletions lib/commonmarker/node/inspect.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# frozen_string_literal: true

require 'pp'

module CommonMarker
class Node
module Inspect
PP_INDENT_SIZE = 2

def inspect
PP.pp(self, String.new, Float::INFINITY)
end

# @param [PrettyPrint] pp
def pretty_print(pp)
pp.group(PP_INDENT_SIZE, "#<#{self.class}(#{type}):", '>') do
pp.breakable

attrs = %i[
sourcepos
string_content
url
title
header_level
list_type
list_start
list_tight
fence_info
].map do |name|
begin
[name, __send__(name)]
rescue NodeError
nil
end
end.compact

pp.seplist(attrs) do |name, value|
pp.text "#{name}="
pp.pp value
end

if first_child
pp.breakable
pp.group(PP_INDENT_SIZE) do
children = []
node = first_child
while node
children << node
node = node.next
end
pp.text 'children='
pp.pp children
end
end
end
end
end
end
end
8 changes: 8 additions & 0 deletions test/test_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,12 @@ def test_walk_and_delete_node
end
assert_equal "<p>Hi there, I am mostly text!</p>\n", @doc.to_html
end

def test_inspect
assert_match /#<CommonMarker::Node\(document\):/, @doc.inspect
end

def test_pretty_print
assert_match /#<CommonMarker::Node\(document\):/, PP.pp(@doc, '')
end
end

0 comments on commit f44edb4

Please sign in to comment.